intel_pmc_ipc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /*
  2. * intel_pmc_ipc.c: Driver for the Intel PMC IPC mechanism
  3. *
  4. * (C) Copyright 2014-2015 Intel Corporation
  5. *
  6. * This driver is based on Intel SCU IPC driver(intel_scu_opc.c) by
  7. * Sreedhara DS <sreedhara.ds@intel.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; version 2
  12. * of the License.
  13. *
  14. * PMC running in ARC processor communicates with other entity running in IA
  15. * core through IPC mechanism which in turn messaging between IA core ad PMC.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/delay.h>
  19. #include <linux/errno.h>
  20. #include <linux/init.h>
  21. #include <linux/device.h>
  22. #include <linux/pm.h>
  23. #include <linux/pci.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/pm_qos.h>
  27. #include <linux/kernel.h>
  28. #include <linux/bitops.h>
  29. #include <linux/sched.h>
  30. #include <linux/atomic.h>
  31. #include <linux/notifier.h>
  32. #include <linux/suspend.h>
  33. #include <linux/acpi.h>
  34. #include <linux/io-64-nonatomic-lo-hi.h>
  35. #include <asm/intel_pmc_ipc.h>
  36. #include <linux/platform_data/itco_wdt.h>
  37. /*
  38. * IPC registers
  39. * The IA write to IPC_CMD command register triggers an interrupt to the ARC,
  40. * The ARC handles the interrupt and services it, writing optional data to
  41. * the IPC1 registers, updates the IPC_STS response register with the status.
  42. */
  43. #define IPC_CMD 0x0
  44. #define IPC_CMD_MSI 0x100
  45. #define IPC_CMD_SIZE 16
  46. #define IPC_CMD_SUBCMD 12
  47. #define IPC_STATUS 0x04
  48. #define IPC_STATUS_IRQ 0x4
  49. #define IPC_STATUS_ERR 0x2
  50. #define IPC_STATUS_BUSY 0x1
  51. #define IPC_SPTR 0x08
  52. #define IPC_DPTR 0x0C
  53. #define IPC_WRITE_BUFFER 0x80
  54. #define IPC_READ_BUFFER 0x90
  55. /* Residency with clock rate at 19.2MHz to usecs */
  56. #define S0IX_RESIDENCY_IN_USECS(d, s) \
  57. ({ \
  58. u64 result = 10ull * ((d) + (s)); \
  59. do_div(result, 192); \
  60. result; \
  61. })
  62. /*
  63. * 16-byte buffer for sending data associated with IPC command.
  64. */
  65. #define IPC_DATA_BUFFER_SIZE 16
  66. #define IPC_LOOP_CNT 3000000
  67. #define IPC_MAX_SEC 3
  68. #define IPC_TRIGGER_MODE_IRQ true
  69. /* exported resources from IFWI */
  70. #define PLAT_RESOURCE_IPC_INDEX 0
  71. #define PLAT_RESOURCE_IPC_SIZE 0x1000
  72. #define PLAT_RESOURCE_GCR_OFFSET 0x1000
  73. #define PLAT_RESOURCE_GCR_SIZE 0x1000
  74. #define PLAT_RESOURCE_BIOS_DATA_INDEX 1
  75. #define PLAT_RESOURCE_BIOS_IFACE_INDEX 2
  76. #define PLAT_RESOURCE_TELEM_SSRAM_INDEX 3
  77. #define PLAT_RESOURCE_ISP_DATA_INDEX 4
  78. #define PLAT_RESOURCE_ISP_IFACE_INDEX 5
  79. #define PLAT_RESOURCE_GTD_DATA_INDEX 6
  80. #define PLAT_RESOURCE_GTD_IFACE_INDEX 7
  81. #define PLAT_RESOURCE_ACPI_IO_INDEX 0
  82. /*
  83. * BIOS does not create an ACPI device for each PMC function,
  84. * but exports multiple resources from one ACPI device(IPC) for
  85. * multiple functions. This driver is responsible to create a
  86. * platform device and to export resources for those functions.
  87. */
  88. #define TCO_DEVICE_NAME "iTCO_wdt"
  89. #define SMI_EN_OFFSET 0x40
  90. #define SMI_EN_SIZE 4
  91. #define TCO_BASE_OFFSET 0x60
  92. #define TCO_REGS_SIZE 16
  93. #define PUNIT_DEVICE_NAME "intel_punit_ipc"
  94. #define TELEMETRY_DEVICE_NAME "intel_telemetry"
  95. #define TELEM_SSRAM_SIZE 240
  96. #define TELEM_PMC_SSRAM_OFFSET 0x1B00
  97. #define TELEM_PUNIT_SSRAM_OFFSET 0x1A00
  98. #define TCO_PMC_OFFSET 0x8
  99. #define TCO_PMC_SIZE 0x4
  100. /* PMC register bit definitions */
  101. /* PMC_CFG_REG bit masks */
  102. #define PMC_CFG_NO_REBOOT_MASK (1 << 4)
  103. #define PMC_CFG_NO_REBOOT_EN (1 << 4)
  104. #define PMC_CFG_NO_REBOOT_DIS (0 << 4)
  105. static struct intel_pmc_ipc_dev {
  106. struct device *dev;
  107. void __iomem *ipc_base;
  108. bool irq_mode;
  109. int irq;
  110. int cmd;
  111. struct completion cmd_complete;
  112. /* The following PMC BARs share the same ACPI device with the IPC */
  113. resource_size_t acpi_io_base;
  114. int acpi_io_size;
  115. struct platform_device *tco_dev;
  116. /* gcr */
  117. void __iomem *gcr_mem_base;
  118. bool has_gcr_regs;
  119. /* punit */
  120. struct platform_device *punit_dev;
  121. /* Telemetry */
  122. resource_size_t telem_pmc_ssram_base;
  123. resource_size_t telem_punit_ssram_base;
  124. int telem_pmc_ssram_size;
  125. int telem_punit_ssram_size;
  126. u8 telem_res_inval;
  127. struct platform_device *telemetry_dev;
  128. } ipcdev;
  129. static char *ipc_err_sources[] = {
  130. [IPC_ERR_NONE] =
  131. "no error",
  132. [IPC_ERR_CMD_NOT_SUPPORTED] =
  133. "command not supported",
  134. [IPC_ERR_CMD_NOT_SERVICED] =
  135. "command not serviced",
  136. [IPC_ERR_UNABLE_TO_SERVICE] =
  137. "unable to service",
  138. [IPC_ERR_CMD_INVALID] =
  139. "command invalid",
  140. [IPC_ERR_CMD_FAILED] =
  141. "command failed",
  142. [IPC_ERR_EMSECURITY] =
  143. "Invalid Battery",
  144. [IPC_ERR_UNSIGNEDKERNEL] =
  145. "Unsigned kernel",
  146. };
  147. /* Prevent concurrent calls to the PMC */
  148. static DEFINE_MUTEX(ipclock);
  149. static inline void ipc_send_command(u32 cmd)
  150. {
  151. ipcdev.cmd = cmd;
  152. if (ipcdev.irq_mode) {
  153. reinit_completion(&ipcdev.cmd_complete);
  154. cmd |= IPC_CMD_MSI;
  155. }
  156. writel(cmd, ipcdev.ipc_base + IPC_CMD);
  157. }
  158. static inline u32 ipc_read_status(void)
  159. {
  160. return readl(ipcdev.ipc_base + IPC_STATUS);
  161. }
  162. static inline void ipc_data_writel(u32 data, u32 offset)
  163. {
  164. writel(data, ipcdev.ipc_base + IPC_WRITE_BUFFER + offset);
  165. }
  166. static inline u8 __maybe_unused ipc_data_readb(u32 offset)
  167. {
  168. return readb(ipcdev.ipc_base + IPC_READ_BUFFER + offset);
  169. }
  170. static inline u32 ipc_data_readl(u32 offset)
  171. {
  172. return readl(ipcdev.ipc_base + IPC_READ_BUFFER + offset);
  173. }
  174. static inline u64 gcr_data_readq(u32 offset)
  175. {
  176. return readq(ipcdev.gcr_mem_base + offset);
  177. }
  178. static inline int is_gcr_valid(u32 offset)
  179. {
  180. if (!ipcdev.has_gcr_regs)
  181. return -EACCES;
  182. if (offset > PLAT_RESOURCE_GCR_SIZE)
  183. return -EINVAL;
  184. return 0;
  185. }
  186. /**
  187. * intel_pmc_gcr_read() - Read PMC GCR register
  188. * @offset: offset of GCR register from GCR address base
  189. * @data: data pointer for storing the register output
  190. *
  191. * Reads the PMC GCR register of given offset.
  192. *
  193. * Return: negative value on error or 0 on success.
  194. */
  195. int intel_pmc_gcr_read(u32 offset, u32 *data)
  196. {
  197. int ret;
  198. mutex_lock(&ipclock);
  199. ret = is_gcr_valid(offset);
  200. if (ret < 0) {
  201. mutex_unlock(&ipclock);
  202. return ret;
  203. }
  204. *data = readl(ipcdev.gcr_mem_base + offset);
  205. mutex_unlock(&ipclock);
  206. return 0;
  207. }
  208. EXPORT_SYMBOL_GPL(intel_pmc_gcr_read);
  209. /**
  210. * intel_pmc_gcr_write() - Write PMC GCR register
  211. * @offset: offset of GCR register from GCR address base
  212. * @data: register update value
  213. *
  214. * Writes the PMC GCR register of given offset with given
  215. * value.
  216. *
  217. * Return: negative value on error or 0 on success.
  218. */
  219. int intel_pmc_gcr_write(u32 offset, u32 data)
  220. {
  221. int ret;
  222. mutex_lock(&ipclock);
  223. ret = is_gcr_valid(offset);
  224. if (ret < 0) {
  225. mutex_unlock(&ipclock);
  226. return ret;
  227. }
  228. writel(data, ipcdev.gcr_mem_base + offset);
  229. mutex_unlock(&ipclock);
  230. return 0;
  231. }
  232. EXPORT_SYMBOL_GPL(intel_pmc_gcr_write);
  233. /**
  234. * intel_pmc_gcr_update() - Update PMC GCR register bits
  235. * @offset: offset of GCR register from GCR address base
  236. * @mask: bit mask for update operation
  237. * @val: update value
  238. *
  239. * Updates the bits of given GCR register as specified by
  240. * @mask and @val.
  241. *
  242. * Return: negative value on error or 0 on success.
  243. */
  244. int intel_pmc_gcr_update(u32 offset, u32 mask, u32 val)
  245. {
  246. u32 new_val;
  247. int ret = 0;
  248. mutex_lock(&ipclock);
  249. ret = is_gcr_valid(offset);
  250. if (ret < 0)
  251. goto gcr_ipc_unlock;
  252. new_val = readl(ipcdev.gcr_mem_base + offset);
  253. new_val &= ~mask;
  254. new_val |= val & mask;
  255. writel(new_val, ipcdev.gcr_mem_base + offset);
  256. new_val = readl(ipcdev.gcr_mem_base + offset);
  257. /* check whether the bit update is successful */
  258. if ((new_val & mask) != (val & mask)) {
  259. ret = -EIO;
  260. goto gcr_ipc_unlock;
  261. }
  262. gcr_ipc_unlock:
  263. mutex_unlock(&ipclock);
  264. return ret;
  265. }
  266. EXPORT_SYMBOL_GPL(intel_pmc_gcr_update);
  267. static int update_no_reboot_bit(void *priv, bool set)
  268. {
  269. u32 value = set ? PMC_CFG_NO_REBOOT_EN : PMC_CFG_NO_REBOOT_DIS;
  270. return intel_pmc_gcr_update(PMC_GCR_PMC_CFG_REG,
  271. PMC_CFG_NO_REBOOT_MASK, value);
  272. }
  273. static int intel_pmc_ipc_check_status(void)
  274. {
  275. int status;
  276. int ret = 0;
  277. if (ipcdev.irq_mode) {
  278. if (0 == wait_for_completion_timeout(
  279. &ipcdev.cmd_complete, IPC_MAX_SEC * HZ))
  280. ret = -ETIMEDOUT;
  281. } else {
  282. int loop_count = IPC_LOOP_CNT;
  283. while ((ipc_read_status() & IPC_STATUS_BUSY) && --loop_count)
  284. udelay(1);
  285. if (loop_count == 0)
  286. ret = -ETIMEDOUT;
  287. }
  288. status = ipc_read_status();
  289. if (ret == -ETIMEDOUT) {
  290. dev_err(ipcdev.dev,
  291. "IPC timed out, TS=0x%x, CMD=0x%x\n",
  292. status, ipcdev.cmd);
  293. return ret;
  294. }
  295. if (status & IPC_STATUS_ERR) {
  296. int i;
  297. ret = -EIO;
  298. i = (status >> IPC_CMD_SIZE) & 0xFF;
  299. if (i < ARRAY_SIZE(ipc_err_sources))
  300. dev_err(ipcdev.dev,
  301. "IPC failed: %s, STS=0x%x, CMD=0x%x\n",
  302. ipc_err_sources[i], status, ipcdev.cmd);
  303. else
  304. dev_err(ipcdev.dev,
  305. "IPC failed: unknown, STS=0x%x, CMD=0x%x\n",
  306. status, ipcdev.cmd);
  307. if ((i == IPC_ERR_UNSIGNEDKERNEL) || (i == IPC_ERR_EMSECURITY))
  308. ret = -EACCES;
  309. }
  310. return ret;
  311. }
  312. /**
  313. * intel_pmc_ipc_simple_command() - Simple IPC command
  314. * @cmd: IPC command code.
  315. * @sub: IPC command sub type.
  316. *
  317. * Send a simple IPC command to PMC when don't need to specify
  318. * input/output data and source/dest pointers.
  319. *
  320. * Return: an IPC error code or 0 on success.
  321. */
  322. int intel_pmc_ipc_simple_command(int cmd, int sub)
  323. {
  324. int ret;
  325. mutex_lock(&ipclock);
  326. if (ipcdev.dev == NULL) {
  327. mutex_unlock(&ipclock);
  328. return -ENODEV;
  329. }
  330. ipc_send_command(sub << IPC_CMD_SUBCMD | cmd);
  331. ret = intel_pmc_ipc_check_status();
  332. mutex_unlock(&ipclock);
  333. return ret;
  334. }
  335. EXPORT_SYMBOL_GPL(intel_pmc_ipc_simple_command);
  336. /**
  337. * intel_pmc_ipc_raw_cmd() - IPC command with data and pointers
  338. * @cmd: IPC command code.
  339. * @sub: IPC command sub type.
  340. * @in: input data of this IPC command.
  341. * @inlen: input data length in bytes.
  342. * @out: output data of this IPC command.
  343. * @outlen: output data length in dwords.
  344. * @sptr: data writing to SPTR register.
  345. * @dptr: data writing to DPTR register.
  346. *
  347. * Send an IPC command to PMC with input/output data and source/dest pointers.
  348. *
  349. * Return: an IPC error code or 0 on success.
  350. */
  351. int intel_pmc_ipc_raw_cmd(u32 cmd, u32 sub, u8 *in, u32 inlen, u32 *out,
  352. u32 outlen, u32 dptr, u32 sptr)
  353. {
  354. u32 wbuf[4] = { 0 };
  355. int ret;
  356. int i;
  357. if (inlen > IPC_DATA_BUFFER_SIZE || outlen > IPC_DATA_BUFFER_SIZE / 4)
  358. return -EINVAL;
  359. mutex_lock(&ipclock);
  360. if (ipcdev.dev == NULL) {
  361. mutex_unlock(&ipclock);
  362. return -ENODEV;
  363. }
  364. memcpy(wbuf, in, inlen);
  365. writel(dptr, ipcdev.ipc_base + IPC_DPTR);
  366. writel(sptr, ipcdev.ipc_base + IPC_SPTR);
  367. /* The input data register is 32bit register and inlen is in Byte */
  368. for (i = 0; i < ((inlen + 3) / 4); i++)
  369. ipc_data_writel(wbuf[i], 4 * i);
  370. ipc_send_command((inlen << IPC_CMD_SIZE) |
  371. (sub << IPC_CMD_SUBCMD) | cmd);
  372. ret = intel_pmc_ipc_check_status();
  373. if (!ret) {
  374. /* out is read from 32bit register and outlen is in 32bit */
  375. for (i = 0; i < outlen; i++)
  376. *out++ = ipc_data_readl(4 * i);
  377. }
  378. mutex_unlock(&ipclock);
  379. return ret;
  380. }
  381. EXPORT_SYMBOL_GPL(intel_pmc_ipc_raw_cmd);
  382. /**
  383. * intel_pmc_ipc_command() - IPC command with input/output data
  384. * @cmd: IPC command code.
  385. * @sub: IPC command sub type.
  386. * @in: input data of this IPC command.
  387. * @inlen: input data length in bytes.
  388. * @out: output data of this IPC command.
  389. * @outlen: output data length in dwords.
  390. *
  391. * Send an IPC command to PMC with input/output data.
  392. *
  393. * Return: an IPC error code or 0 on success.
  394. */
  395. int intel_pmc_ipc_command(u32 cmd, u32 sub, u8 *in, u32 inlen,
  396. u32 *out, u32 outlen)
  397. {
  398. return intel_pmc_ipc_raw_cmd(cmd, sub, in, inlen, out, outlen, 0, 0);
  399. }
  400. EXPORT_SYMBOL_GPL(intel_pmc_ipc_command);
  401. static irqreturn_t ioc(int irq, void *dev_id)
  402. {
  403. int status;
  404. if (ipcdev.irq_mode) {
  405. status = ipc_read_status();
  406. writel(status | IPC_STATUS_IRQ, ipcdev.ipc_base + IPC_STATUS);
  407. }
  408. complete(&ipcdev.cmd_complete);
  409. return IRQ_HANDLED;
  410. }
  411. static int ipc_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  412. {
  413. struct intel_pmc_ipc_dev *pmc = &ipcdev;
  414. int ret;
  415. /* Only one PMC is supported */
  416. if (pmc->dev)
  417. return -EBUSY;
  418. pmc->irq_mode = IPC_TRIGGER_MODE_IRQ;
  419. ret = pcim_enable_device(pdev);
  420. if (ret)
  421. return ret;
  422. ret = pcim_iomap_regions(pdev, 1 << 0, pci_name(pdev));
  423. if (ret)
  424. return ret;
  425. init_completion(&pmc->cmd_complete);
  426. pmc->ipc_base = pcim_iomap_table(pdev)[0];
  427. ret = devm_request_irq(&pdev->dev, pdev->irq, ioc, 0, "intel_pmc_ipc",
  428. pmc);
  429. if (ret) {
  430. dev_err(&pdev->dev, "Failed to request irq\n");
  431. return ret;
  432. }
  433. pmc->dev = &pdev->dev;
  434. pci_set_drvdata(pdev, pmc);
  435. return 0;
  436. }
  437. static const struct pci_device_id ipc_pci_ids[] = {
  438. {PCI_VDEVICE(INTEL, 0x0a94), 0},
  439. {PCI_VDEVICE(INTEL, 0x1a94), 0},
  440. {PCI_VDEVICE(INTEL, 0x5a94), 0},
  441. { 0,}
  442. };
  443. MODULE_DEVICE_TABLE(pci, ipc_pci_ids);
  444. static struct pci_driver ipc_pci_driver = {
  445. .name = "intel_pmc_ipc",
  446. .id_table = ipc_pci_ids,
  447. .probe = ipc_pci_probe,
  448. };
  449. static ssize_t intel_pmc_ipc_simple_cmd_store(struct device *dev,
  450. struct device_attribute *attr,
  451. const char *buf, size_t count)
  452. {
  453. int subcmd;
  454. int cmd;
  455. int ret;
  456. ret = sscanf(buf, "%d %d", &cmd, &subcmd);
  457. if (ret != 2) {
  458. dev_err(dev, "Error args\n");
  459. return -EINVAL;
  460. }
  461. ret = intel_pmc_ipc_simple_command(cmd, subcmd);
  462. if (ret) {
  463. dev_err(dev, "command %d error with %d\n", cmd, ret);
  464. return ret;
  465. }
  466. return (ssize_t)count;
  467. }
  468. static ssize_t intel_pmc_ipc_northpeak_store(struct device *dev,
  469. struct device_attribute *attr,
  470. const char *buf, size_t count)
  471. {
  472. unsigned long val;
  473. int subcmd;
  474. int ret;
  475. if (kstrtoul(buf, 0, &val))
  476. return -EINVAL;
  477. if (val)
  478. subcmd = 1;
  479. else
  480. subcmd = 0;
  481. ret = intel_pmc_ipc_simple_command(PMC_IPC_NORTHPEAK_CTRL, subcmd);
  482. if (ret) {
  483. dev_err(dev, "command north %d error with %d\n", subcmd, ret);
  484. return ret;
  485. }
  486. return (ssize_t)count;
  487. }
  488. static DEVICE_ATTR(simplecmd, S_IWUSR,
  489. NULL, intel_pmc_ipc_simple_cmd_store);
  490. static DEVICE_ATTR(northpeak, S_IWUSR,
  491. NULL, intel_pmc_ipc_northpeak_store);
  492. static struct attribute *intel_ipc_attrs[] = {
  493. &dev_attr_northpeak.attr,
  494. &dev_attr_simplecmd.attr,
  495. NULL
  496. };
  497. static const struct attribute_group intel_ipc_group = {
  498. .attrs = intel_ipc_attrs,
  499. };
  500. static struct resource punit_res_array[] = {
  501. /* Punit BIOS */
  502. {
  503. .flags = IORESOURCE_MEM,
  504. },
  505. {
  506. .flags = IORESOURCE_MEM,
  507. },
  508. /* Punit ISP */
  509. {
  510. .flags = IORESOURCE_MEM,
  511. },
  512. {
  513. .flags = IORESOURCE_MEM,
  514. },
  515. /* Punit GTD */
  516. {
  517. .flags = IORESOURCE_MEM,
  518. },
  519. {
  520. .flags = IORESOURCE_MEM,
  521. },
  522. };
  523. #define TCO_RESOURCE_ACPI_IO 0
  524. #define TCO_RESOURCE_SMI_EN_IO 1
  525. #define TCO_RESOURCE_GCR_MEM 2
  526. static struct resource tco_res[] = {
  527. /* ACPI - TCO */
  528. {
  529. .flags = IORESOURCE_IO,
  530. },
  531. /* ACPI - SMI */
  532. {
  533. .flags = IORESOURCE_IO,
  534. },
  535. };
  536. static struct itco_wdt_platform_data tco_info = {
  537. .name = "Apollo Lake SoC",
  538. .version = 5,
  539. .no_reboot_priv = &ipcdev,
  540. .update_no_reboot_bit = update_no_reboot_bit,
  541. };
  542. #define TELEMETRY_RESOURCE_PUNIT_SSRAM 0
  543. #define TELEMETRY_RESOURCE_PMC_SSRAM 1
  544. static struct resource telemetry_res[] = {
  545. /*Telemetry*/
  546. {
  547. .flags = IORESOURCE_MEM,
  548. },
  549. {
  550. .flags = IORESOURCE_MEM,
  551. },
  552. };
  553. static int ipc_create_punit_device(void)
  554. {
  555. struct platform_device *pdev;
  556. const struct platform_device_info pdevinfo = {
  557. .parent = ipcdev.dev,
  558. .name = PUNIT_DEVICE_NAME,
  559. .id = -1,
  560. .res = punit_res_array,
  561. .num_res = ARRAY_SIZE(punit_res_array),
  562. };
  563. pdev = platform_device_register_full(&pdevinfo);
  564. if (IS_ERR(pdev))
  565. return PTR_ERR(pdev);
  566. ipcdev.punit_dev = pdev;
  567. return 0;
  568. }
  569. static int ipc_create_tco_device(void)
  570. {
  571. struct platform_device *pdev;
  572. struct resource *res;
  573. const struct platform_device_info pdevinfo = {
  574. .parent = ipcdev.dev,
  575. .name = TCO_DEVICE_NAME,
  576. .id = -1,
  577. .res = tco_res,
  578. .num_res = ARRAY_SIZE(tco_res),
  579. .data = &tco_info,
  580. .size_data = sizeof(tco_info),
  581. };
  582. res = tco_res + TCO_RESOURCE_ACPI_IO;
  583. res->start = ipcdev.acpi_io_base + TCO_BASE_OFFSET;
  584. res->end = res->start + TCO_REGS_SIZE - 1;
  585. res = tco_res + TCO_RESOURCE_SMI_EN_IO;
  586. res->start = ipcdev.acpi_io_base + SMI_EN_OFFSET;
  587. res->end = res->start + SMI_EN_SIZE - 1;
  588. pdev = platform_device_register_full(&pdevinfo);
  589. if (IS_ERR(pdev))
  590. return PTR_ERR(pdev);
  591. ipcdev.tco_dev = pdev;
  592. return 0;
  593. }
  594. static int ipc_create_telemetry_device(void)
  595. {
  596. struct platform_device *pdev;
  597. struct resource *res;
  598. const struct platform_device_info pdevinfo = {
  599. .parent = ipcdev.dev,
  600. .name = TELEMETRY_DEVICE_NAME,
  601. .id = -1,
  602. .res = telemetry_res,
  603. .num_res = ARRAY_SIZE(telemetry_res),
  604. };
  605. res = telemetry_res + TELEMETRY_RESOURCE_PUNIT_SSRAM;
  606. res->start = ipcdev.telem_punit_ssram_base;
  607. res->end = res->start + ipcdev.telem_punit_ssram_size - 1;
  608. res = telemetry_res + TELEMETRY_RESOURCE_PMC_SSRAM;
  609. res->start = ipcdev.telem_pmc_ssram_base;
  610. res->end = res->start + ipcdev.telem_pmc_ssram_size - 1;
  611. pdev = platform_device_register_full(&pdevinfo);
  612. if (IS_ERR(pdev))
  613. return PTR_ERR(pdev);
  614. ipcdev.telemetry_dev = pdev;
  615. return 0;
  616. }
  617. static int ipc_create_pmc_devices(void)
  618. {
  619. int ret;
  620. /* If we have ACPI based watchdog use that instead */
  621. if (!acpi_has_watchdog()) {
  622. ret = ipc_create_tco_device();
  623. if (ret) {
  624. dev_err(ipcdev.dev, "Failed to add tco platform device\n");
  625. return ret;
  626. }
  627. }
  628. ret = ipc_create_punit_device();
  629. if (ret) {
  630. dev_err(ipcdev.dev, "Failed to add punit platform device\n");
  631. platform_device_unregister(ipcdev.tco_dev);
  632. }
  633. if (!ipcdev.telem_res_inval) {
  634. ret = ipc_create_telemetry_device();
  635. if (ret)
  636. dev_warn(ipcdev.dev,
  637. "Failed to add telemetry platform device\n");
  638. }
  639. return ret;
  640. }
  641. static int ipc_plat_get_res(struct platform_device *pdev)
  642. {
  643. struct resource *res, *punit_res;
  644. void __iomem *addr;
  645. int size;
  646. res = platform_get_resource(pdev, IORESOURCE_IO,
  647. PLAT_RESOURCE_ACPI_IO_INDEX);
  648. if (!res) {
  649. dev_err(&pdev->dev, "Failed to get io resource\n");
  650. return -ENXIO;
  651. }
  652. size = resource_size(res);
  653. ipcdev.acpi_io_base = res->start;
  654. ipcdev.acpi_io_size = size;
  655. dev_info(&pdev->dev, "io res: %pR\n", res);
  656. punit_res = punit_res_array;
  657. /* This is index 0 to cover BIOS data register */
  658. res = platform_get_resource(pdev, IORESOURCE_MEM,
  659. PLAT_RESOURCE_BIOS_DATA_INDEX);
  660. if (!res) {
  661. dev_err(&pdev->dev, "Failed to get res of punit BIOS data\n");
  662. return -ENXIO;
  663. }
  664. *punit_res = *res;
  665. dev_info(&pdev->dev, "punit BIOS data res: %pR\n", res);
  666. /* This is index 1 to cover BIOS interface register */
  667. res = platform_get_resource(pdev, IORESOURCE_MEM,
  668. PLAT_RESOURCE_BIOS_IFACE_INDEX);
  669. if (!res) {
  670. dev_err(&pdev->dev, "Failed to get res of punit BIOS iface\n");
  671. return -ENXIO;
  672. }
  673. *++punit_res = *res;
  674. dev_info(&pdev->dev, "punit BIOS interface res: %pR\n", res);
  675. /* This is index 2 to cover ISP data register, optional */
  676. res = platform_get_resource(pdev, IORESOURCE_MEM,
  677. PLAT_RESOURCE_ISP_DATA_INDEX);
  678. ++punit_res;
  679. if (res) {
  680. *punit_res = *res;
  681. dev_info(&pdev->dev, "punit ISP data res: %pR\n", res);
  682. }
  683. /* This is index 3 to cover ISP interface register, optional */
  684. res = platform_get_resource(pdev, IORESOURCE_MEM,
  685. PLAT_RESOURCE_ISP_IFACE_INDEX);
  686. ++punit_res;
  687. if (res) {
  688. *punit_res = *res;
  689. dev_info(&pdev->dev, "punit ISP interface res: %pR\n", res);
  690. }
  691. /* This is index 4 to cover GTD data register, optional */
  692. res = platform_get_resource(pdev, IORESOURCE_MEM,
  693. PLAT_RESOURCE_GTD_DATA_INDEX);
  694. ++punit_res;
  695. if (res) {
  696. *punit_res = *res;
  697. dev_info(&pdev->dev, "punit GTD data res: %pR\n", res);
  698. }
  699. /* This is index 5 to cover GTD interface register, optional */
  700. res = platform_get_resource(pdev, IORESOURCE_MEM,
  701. PLAT_RESOURCE_GTD_IFACE_INDEX);
  702. ++punit_res;
  703. if (res) {
  704. *punit_res = *res;
  705. dev_info(&pdev->dev, "punit GTD interface res: %pR\n", res);
  706. }
  707. res = platform_get_resource(pdev, IORESOURCE_MEM,
  708. PLAT_RESOURCE_IPC_INDEX);
  709. if (!res) {
  710. dev_err(&pdev->dev, "Failed to get ipc resource\n");
  711. return -ENXIO;
  712. }
  713. size = PLAT_RESOURCE_IPC_SIZE + PLAT_RESOURCE_GCR_SIZE;
  714. res->end = res->start + size - 1;
  715. addr = devm_ioremap_resource(&pdev->dev, res);
  716. if (IS_ERR(addr))
  717. return PTR_ERR(addr);
  718. ipcdev.ipc_base = addr;
  719. ipcdev.gcr_mem_base = addr + PLAT_RESOURCE_GCR_OFFSET;
  720. dev_info(&pdev->dev, "ipc res: %pR\n", res);
  721. ipcdev.telem_res_inval = 0;
  722. res = platform_get_resource(pdev, IORESOURCE_MEM,
  723. PLAT_RESOURCE_TELEM_SSRAM_INDEX);
  724. if (!res) {
  725. dev_err(&pdev->dev, "Failed to get telemetry ssram resource\n");
  726. ipcdev.telem_res_inval = 1;
  727. } else {
  728. ipcdev.telem_punit_ssram_base = res->start +
  729. TELEM_PUNIT_SSRAM_OFFSET;
  730. ipcdev.telem_punit_ssram_size = TELEM_SSRAM_SIZE;
  731. ipcdev.telem_pmc_ssram_base = res->start +
  732. TELEM_PMC_SSRAM_OFFSET;
  733. ipcdev.telem_pmc_ssram_size = TELEM_SSRAM_SIZE;
  734. dev_info(&pdev->dev, "telemetry ssram res: %pR\n", res);
  735. }
  736. return 0;
  737. }
  738. /**
  739. * intel_pmc_s0ix_counter_read() - Read S0ix residency.
  740. * @data: Out param that contains current S0ix residency count.
  741. *
  742. * Return: an error code or 0 on success.
  743. */
  744. int intel_pmc_s0ix_counter_read(u64 *data)
  745. {
  746. u64 deep, shlw;
  747. if (!ipcdev.has_gcr_regs)
  748. return -EACCES;
  749. deep = gcr_data_readq(PMC_GCR_TELEM_DEEP_S0IX_REG);
  750. shlw = gcr_data_readq(PMC_GCR_TELEM_SHLW_S0IX_REG);
  751. *data = S0IX_RESIDENCY_IN_USECS(deep, shlw);
  752. return 0;
  753. }
  754. EXPORT_SYMBOL_GPL(intel_pmc_s0ix_counter_read);
  755. #ifdef CONFIG_ACPI
  756. static const struct acpi_device_id ipc_acpi_ids[] = {
  757. { "INT34D2", 0},
  758. { }
  759. };
  760. MODULE_DEVICE_TABLE(acpi, ipc_acpi_ids);
  761. #endif
  762. static int ipc_plat_probe(struct platform_device *pdev)
  763. {
  764. int ret;
  765. ipcdev.dev = &pdev->dev;
  766. ipcdev.irq_mode = IPC_TRIGGER_MODE_IRQ;
  767. init_completion(&ipcdev.cmd_complete);
  768. ipcdev.irq = platform_get_irq(pdev, 0);
  769. if (ipcdev.irq < 0) {
  770. dev_err(&pdev->dev, "Failed to get irq\n");
  771. return -EINVAL;
  772. }
  773. ret = ipc_plat_get_res(pdev);
  774. if (ret) {
  775. dev_err(&pdev->dev, "Failed to request resource\n");
  776. return ret;
  777. }
  778. ret = ipc_create_pmc_devices();
  779. if (ret) {
  780. dev_err(&pdev->dev, "Failed to create pmc devices\n");
  781. return ret;
  782. }
  783. if (devm_request_irq(&pdev->dev, ipcdev.irq, ioc, IRQF_NO_SUSPEND,
  784. "intel_pmc_ipc", &ipcdev)) {
  785. dev_err(&pdev->dev, "Failed to request irq\n");
  786. ret = -EBUSY;
  787. goto err_irq;
  788. }
  789. ret = sysfs_create_group(&pdev->dev.kobj, &intel_ipc_group);
  790. if (ret) {
  791. dev_err(&pdev->dev, "Failed to create sysfs group %d\n",
  792. ret);
  793. goto err_sys;
  794. }
  795. ipcdev.has_gcr_regs = true;
  796. return 0;
  797. err_sys:
  798. devm_free_irq(&pdev->dev, ipcdev.irq, &ipcdev);
  799. err_irq:
  800. platform_device_unregister(ipcdev.tco_dev);
  801. platform_device_unregister(ipcdev.punit_dev);
  802. platform_device_unregister(ipcdev.telemetry_dev);
  803. return ret;
  804. }
  805. static int ipc_plat_remove(struct platform_device *pdev)
  806. {
  807. sysfs_remove_group(&pdev->dev.kobj, &intel_ipc_group);
  808. devm_free_irq(&pdev->dev, ipcdev.irq, &ipcdev);
  809. platform_device_unregister(ipcdev.tco_dev);
  810. platform_device_unregister(ipcdev.punit_dev);
  811. platform_device_unregister(ipcdev.telemetry_dev);
  812. ipcdev.dev = NULL;
  813. return 0;
  814. }
  815. static struct platform_driver ipc_plat_driver = {
  816. .remove = ipc_plat_remove,
  817. .probe = ipc_plat_probe,
  818. .driver = {
  819. .name = "pmc-ipc-plat",
  820. .acpi_match_table = ACPI_PTR(ipc_acpi_ids),
  821. },
  822. };
  823. static int __init intel_pmc_ipc_init(void)
  824. {
  825. int ret;
  826. ret = platform_driver_register(&ipc_plat_driver);
  827. if (ret) {
  828. pr_err("Failed to register PMC ipc platform driver\n");
  829. return ret;
  830. }
  831. ret = pci_register_driver(&ipc_pci_driver);
  832. if (ret) {
  833. pr_err("Failed to register PMC ipc pci driver\n");
  834. platform_driver_unregister(&ipc_plat_driver);
  835. return ret;
  836. }
  837. return ret;
  838. }
  839. static void __exit intel_pmc_ipc_exit(void)
  840. {
  841. pci_unregister_driver(&ipc_pci_driver);
  842. platform_driver_unregister(&ipc_plat_driver);
  843. }
  844. MODULE_AUTHOR("Zha Qipeng <qipeng.zha@intel.com>");
  845. MODULE_DESCRIPTION("Intel PMC IPC driver");
  846. MODULE_LICENSE("GPL");
  847. /* Some modules are dependent on this, so init earlier */
  848. fs_initcall(intel_pmc_ipc_init);
  849. module_exit(intel_pmc_ipc_exit);