intel_pmc_ipc.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. }
  636. if (!ipcdev.telem_res_inval) {
  637. ret = ipc_create_telemetry_device();
  638. if (ret)
  639. dev_warn(ipcdev.dev,
  640. "Failed to add telemetry platform device\n");
  641. }
  642. return ret;
  643. }
  644. static int ipc_plat_get_res(struct platform_device *pdev)
  645. {
  646. struct resource *res, *punit_res;
  647. void __iomem *addr;
  648. int size;
  649. res = platform_get_resource(pdev, IORESOURCE_IO,
  650. PLAT_RESOURCE_ACPI_IO_INDEX);
  651. if (!res) {
  652. dev_err(&pdev->dev, "Failed to get io resource\n");
  653. return -ENXIO;
  654. }
  655. size = resource_size(res);
  656. ipcdev.acpi_io_base = res->start;
  657. ipcdev.acpi_io_size = size;
  658. dev_info(&pdev->dev, "io res: %pR\n", res);
  659. punit_res = punit_res_array;
  660. /* This is index 0 to cover BIOS data register */
  661. res = platform_get_resource(pdev, IORESOURCE_MEM,
  662. PLAT_RESOURCE_BIOS_DATA_INDEX);
  663. if (!res) {
  664. dev_err(&pdev->dev, "Failed to get res of punit BIOS data\n");
  665. return -ENXIO;
  666. }
  667. *punit_res = *res;
  668. dev_info(&pdev->dev, "punit BIOS data res: %pR\n", res);
  669. /* This is index 1 to cover BIOS interface register */
  670. res = platform_get_resource(pdev, IORESOURCE_MEM,
  671. PLAT_RESOURCE_BIOS_IFACE_INDEX);
  672. if (!res) {
  673. dev_err(&pdev->dev, "Failed to get res of punit BIOS iface\n");
  674. return -ENXIO;
  675. }
  676. *++punit_res = *res;
  677. dev_info(&pdev->dev, "punit BIOS interface res: %pR\n", res);
  678. /* This is index 2 to cover ISP data register, optional */
  679. res = platform_get_resource(pdev, IORESOURCE_MEM,
  680. PLAT_RESOURCE_ISP_DATA_INDEX);
  681. ++punit_res;
  682. if (res) {
  683. *punit_res = *res;
  684. dev_info(&pdev->dev, "punit ISP data res: %pR\n", res);
  685. }
  686. /* This is index 3 to cover ISP interface register, optional */
  687. res = platform_get_resource(pdev, IORESOURCE_MEM,
  688. PLAT_RESOURCE_ISP_IFACE_INDEX);
  689. ++punit_res;
  690. if (res) {
  691. *punit_res = *res;
  692. dev_info(&pdev->dev, "punit ISP interface res: %pR\n", res);
  693. }
  694. /* This is index 4 to cover GTD data register, optional */
  695. res = platform_get_resource(pdev, IORESOURCE_MEM,
  696. PLAT_RESOURCE_GTD_DATA_INDEX);
  697. ++punit_res;
  698. if (res) {
  699. *punit_res = *res;
  700. dev_info(&pdev->dev, "punit GTD data res: %pR\n", res);
  701. }
  702. /* This is index 5 to cover GTD interface register, optional */
  703. res = platform_get_resource(pdev, IORESOURCE_MEM,
  704. PLAT_RESOURCE_GTD_IFACE_INDEX);
  705. ++punit_res;
  706. if (res) {
  707. *punit_res = *res;
  708. dev_info(&pdev->dev, "punit GTD interface res: %pR\n", res);
  709. }
  710. res = platform_get_resource(pdev, IORESOURCE_MEM,
  711. PLAT_RESOURCE_IPC_INDEX);
  712. if (!res) {
  713. dev_err(&pdev->dev, "Failed to get ipc resource\n");
  714. return -ENXIO;
  715. }
  716. size = PLAT_RESOURCE_IPC_SIZE + PLAT_RESOURCE_GCR_SIZE;
  717. res->end = res->start + size - 1;
  718. addr = devm_ioremap_resource(&pdev->dev, res);
  719. if (IS_ERR(addr))
  720. return PTR_ERR(addr);
  721. ipcdev.ipc_base = addr;
  722. ipcdev.gcr_mem_base = addr + PLAT_RESOURCE_GCR_OFFSET;
  723. dev_info(&pdev->dev, "ipc res: %pR\n", res);
  724. ipcdev.telem_res_inval = 0;
  725. res = platform_get_resource(pdev, IORESOURCE_MEM,
  726. PLAT_RESOURCE_TELEM_SSRAM_INDEX);
  727. if (!res) {
  728. dev_err(&pdev->dev, "Failed to get telemetry ssram resource\n");
  729. ipcdev.telem_res_inval = 1;
  730. } else {
  731. ipcdev.telem_punit_ssram_base = res->start +
  732. TELEM_PUNIT_SSRAM_OFFSET;
  733. ipcdev.telem_punit_ssram_size = TELEM_SSRAM_SIZE;
  734. ipcdev.telem_pmc_ssram_base = res->start +
  735. TELEM_PMC_SSRAM_OFFSET;
  736. ipcdev.telem_pmc_ssram_size = TELEM_SSRAM_SIZE;
  737. dev_info(&pdev->dev, "telemetry ssram res: %pR\n", res);
  738. }
  739. return 0;
  740. }
  741. /**
  742. * intel_pmc_s0ix_counter_read() - Read S0ix residency.
  743. * @data: Out param that contains current S0ix residency count.
  744. *
  745. * Return: an error code or 0 on success.
  746. */
  747. int intel_pmc_s0ix_counter_read(u64 *data)
  748. {
  749. u64 deep, shlw;
  750. if (!ipcdev.has_gcr_regs)
  751. return -EACCES;
  752. deep = gcr_data_readq(PMC_GCR_TELEM_DEEP_S0IX_REG);
  753. shlw = gcr_data_readq(PMC_GCR_TELEM_SHLW_S0IX_REG);
  754. *data = S0IX_RESIDENCY_IN_USECS(deep, shlw);
  755. return 0;
  756. }
  757. EXPORT_SYMBOL_GPL(intel_pmc_s0ix_counter_read);
  758. #ifdef CONFIG_ACPI
  759. static const struct acpi_device_id ipc_acpi_ids[] = {
  760. { "INT34D2", 0},
  761. { }
  762. };
  763. MODULE_DEVICE_TABLE(acpi, ipc_acpi_ids);
  764. #endif
  765. static int ipc_plat_probe(struct platform_device *pdev)
  766. {
  767. int ret;
  768. ipcdev.dev = &pdev->dev;
  769. ipcdev.irq_mode = IPC_TRIGGER_MODE_IRQ;
  770. init_completion(&ipcdev.cmd_complete);
  771. spin_lock_init(&ipcdev.gcr_lock);
  772. ipcdev.irq = platform_get_irq(pdev, 0);
  773. if (ipcdev.irq < 0) {
  774. dev_err(&pdev->dev, "Failed to get irq\n");
  775. return -EINVAL;
  776. }
  777. ret = ipc_plat_get_res(pdev);
  778. if (ret) {
  779. dev_err(&pdev->dev, "Failed to request resource\n");
  780. return ret;
  781. }
  782. ret = ipc_create_pmc_devices();
  783. if (ret) {
  784. dev_err(&pdev->dev, "Failed to create pmc devices\n");
  785. return ret;
  786. }
  787. if (devm_request_irq(&pdev->dev, ipcdev.irq, ioc, IRQF_NO_SUSPEND,
  788. "intel_pmc_ipc", &ipcdev)) {
  789. dev_err(&pdev->dev, "Failed to request irq\n");
  790. ret = -EBUSY;
  791. goto err_irq;
  792. }
  793. ret = sysfs_create_group(&pdev->dev.kobj, &intel_ipc_group);
  794. if (ret) {
  795. dev_err(&pdev->dev, "Failed to create sysfs group %d\n",
  796. ret);
  797. goto err_sys;
  798. }
  799. ipcdev.has_gcr_regs = true;
  800. return 0;
  801. err_sys:
  802. devm_free_irq(&pdev->dev, ipcdev.irq, &ipcdev);
  803. err_irq:
  804. platform_device_unregister(ipcdev.tco_dev);
  805. platform_device_unregister(ipcdev.punit_dev);
  806. platform_device_unregister(ipcdev.telemetry_dev);
  807. return ret;
  808. }
  809. static int ipc_plat_remove(struct platform_device *pdev)
  810. {
  811. sysfs_remove_group(&pdev->dev.kobj, &intel_ipc_group);
  812. devm_free_irq(&pdev->dev, ipcdev.irq, &ipcdev);
  813. platform_device_unregister(ipcdev.tco_dev);
  814. platform_device_unregister(ipcdev.punit_dev);
  815. platform_device_unregister(ipcdev.telemetry_dev);
  816. ipcdev.dev = NULL;
  817. return 0;
  818. }
  819. static struct platform_driver ipc_plat_driver = {
  820. .remove = ipc_plat_remove,
  821. .probe = ipc_plat_probe,
  822. .driver = {
  823. .name = "pmc-ipc-plat",
  824. .acpi_match_table = ACPI_PTR(ipc_acpi_ids),
  825. },
  826. };
  827. static int __init intel_pmc_ipc_init(void)
  828. {
  829. int ret;
  830. ret = platform_driver_register(&ipc_plat_driver);
  831. if (ret) {
  832. pr_err("Failed to register PMC ipc platform driver\n");
  833. return ret;
  834. }
  835. ret = pci_register_driver(&ipc_pci_driver);
  836. if (ret) {
  837. pr_err("Failed to register PMC ipc pci driver\n");
  838. platform_driver_unregister(&ipc_plat_driver);
  839. return ret;
  840. }
  841. return ret;
  842. }
  843. static void __exit intel_pmc_ipc_exit(void)
  844. {
  845. pci_unregister_driver(&ipc_pci_driver);
  846. platform_driver_unregister(&ipc_plat_driver);
  847. }
  848. MODULE_AUTHOR("Zha Qipeng <qipeng.zha@intel.com>");
  849. MODULE_DESCRIPTION("Intel PMC IPC driver");
  850. MODULE_LICENSE("GPL");
  851. /* Some modules are dependent on this, so init earlier */
  852. fs_initcall(intel_pmc_ipc_init);
  853. module_exit(intel_pmc_ipc_exit);