intel_pmc_ipc.c 24 KB

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