intel_pmc_ipc.c 24 KB

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