intel_pmc_ipc.c 24 KB

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