intel_scu_ipc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for the Intel SCU IPC mechanism
  4. *
  5. * (C) Copyright 2008-2010,2015 Intel Corporation
  6. * Author: Sreedhara DS (sreedhara.ds@intel.com)
  7. *
  8. * SCU running in ARC processor communicates with other entity running in IA
  9. * core through IPC mechanism which in turn messaging between IA core ad SCU.
  10. * SCU has two IPC mechanism IPC-1 and IPC-2. IPC-1 is used between IA32 and
  11. * SCU where IPC-2 is used between P-Unit and SCU. This driver delas with
  12. * IPC-1 Driver provides an API for power control unit registers (e.g. MSIC)
  13. * along with other APIs.
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/device.h>
  17. #include <linux/errno.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/iopoll.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/platform_data/x86/intel_scu_ipc.h>
  25. /* IPC defines the following message types */
  26. #define IPCMSG_PCNTRL 0xff /* Power controller unit read/write */
  27. /* Command id associated with message IPCMSG_PCNTRL */
  28. #define IPC_CMD_PCNTRL_W 0 /* Register write */
  29. #define IPC_CMD_PCNTRL_R 1 /* Register read */
  30. #define IPC_CMD_PCNTRL_M 2 /* Register read-modify-write */
  31. /*
  32. * IPC register summary
  33. *
  34. * IPC register blocks are memory mapped at fixed address of PCI BAR 0.
  35. * To read or write information to the SCU, driver writes to IPC-1 memory
  36. * mapped registers. The following is the IPC mechanism
  37. *
  38. * 1. IA core cDMI interface claims this transaction and converts it to a
  39. * Transaction Layer Packet (TLP) message which is sent across the cDMI.
  40. *
  41. * 2. South Complex cDMI block receives this message and writes it to
  42. * the IPC-1 register block, causing an interrupt to the SCU
  43. *
  44. * 3. SCU firmware decodes this interrupt and IPC message and the appropriate
  45. * message handler is called within firmware.
  46. */
  47. #define IPC_WWBUF_SIZE 20 /* IPC Write buffer Size */
  48. #define IPC_RWBUF_SIZE 20 /* IPC Read buffer Size */
  49. #define IPC_IOC 0x100 /* IPC command register IOC bit */
  50. struct intel_scu_ipc_dev {
  51. struct device dev;
  52. struct resource mem;
  53. struct module *owner;
  54. int irq;
  55. void __iomem *ipc_base;
  56. struct completion cmd_complete;
  57. };
  58. #define IPC_STATUS 0x04
  59. #define IPC_STATUS_IRQ BIT(2)
  60. #define IPC_STATUS_ERR BIT(1)
  61. #define IPC_STATUS_BUSY BIT(0)
  62. /*
  63. * IPC Write/Read Buffers:
  64. * 16 byte buffer for sending and receiving data to and from SCU.
  65. */
  66. #define IPC_WRITE_BUFFER 0x80
  67. #define IPC_READ_BUFFER 0x90
  68. /* Timeout in jiffies */
  69. #define IPC_TIMEOUT (10 * HZ)
  70. static struct intel_scu_ipc_dev *ipcdev; /* Only one for now */
  71. static DEFINE_MUTEX(ipclock); /* lock used to prevent multiple call to SCU */
  72. static struct class intel_scu_ipc_class = {
  73. .name = "intel_scu_ipc",
  74. };
  75. /**
  76. * intel_scu_ipc_dev_get() - Get SCU IPC instance
  77. *
  78. * The recommended new API takes SCU IPC instance as parameter and this
  79. * function can be called by driver to get the instance. This also makes
  80. * sure the driver providing the IPC functionality cannot be unloaded
  81. * while the caller has the instance.
  82. *
  83. * Call intel_scu_ipc_dev_put() to release the instance.
  84. *
  85. * Returns %NULL if SCU IPC is not currently available.
  86. */
  87. struct intel_scu_ipc_dev *intel_scu_ipc_dev_get(void)
  88. {
  89. struct intel_scu_ipc_dev *scu = NULL;
  90. mutex_lock(&ipclock);
  91. if (ipcdev) {
  92. get_device(&ipcdev->dev);
  93. /*
  94. * Prevent the IPC provider from being unloaded while it
  95. * is being used.
  96. */
  97. if (!try_module_get(ipcdev->owner))
  98. put_device(&ipcdev->dev);
  99. else
  100. scu = ipcdev;
  101. }
  102. mutex_unlock(&ipclock);
  103. return scu;
  104. }
  105. EXPORT_SYMBOL_GPL(intel_scu_ipc_dev_get);
  106. /**
  107. * intel_scu_ipc_dev_put() - Put SCU IPC instance
  108. * @scu: SCU IPC instance
  109. *
  110. * This function releases the SCU IPC instance retrieved from
  111. * intel_scu_ipc_dev_get() and allows the driver providing IPC to be
  112. * unloaded.
  113. */
  114. void intel_scu_ipc_dev_put(struct intel_scu_ipc_dev *scu)
  115. {
  116. if (scu) {
  117. module_put(scu->owner);
  118. put_device(&scu->dev);
  119. }
  120. }
  121. EXPORT_SYMBOL_GPL(intel_scu_ipc_dev_put);
  122. struct intel_scu_ipc_devres {
  123. struct intel_scu_ipc_dev *scu;
  124. };
  125. static void devm_intel_scu_ipc_dev_release(struct device *dev, void *res)
  126. {
  127. struct intel_scu_ipc_devres *dr = res;
  128. struct intel_scu_ipc_dev *scu = dr->scu;
  129. intel_scu_ipc_dev_put(scu);
  130. }
  131. /**
  132. * devm_intel_scu_ipc_dev_get() - Allocate managed SCU IPC device
  133. * @dev: Device requesting the SCU IPC device
  134. *
  135. * The recommended new API takes SCU IPC instance as parameter and this
  136. * function can be called by driver to get the instance. This also makes
  137. * sure the driver providing the IPC functionality cannot be unloaded
  138. * while the caller has the instance.
  139. *
  140. * Returns %NULL if SCU IPC is not currently available.
  141. */
  142. struct intel_scu_ipc_dev *devm_intel_scu_ipc_dev_get(struct device *dev)
  143. {
  144. struct intel_scu_ipc_devres *dr;
  145. struct intel_scu_ipc_dev *scu;
  146. dr = devres_alloc(devm_intel_scu_ipc_dev_release, sizeof(*dr), GFP_KERNEL);
  147. if (!dr)
  148. return NULL;
  149. scu = intel_scu_ipc_dev_get();
  150. if (!scu) {
  151. devres_free(dr);
  152. return NULL;
  153. }
  154. dr->scu = scu;
  155. devres_add(dev, dr);
  156. return scu;
  157. }
  158. EXPORT_SYMBOL_GPL(devm_intel_scu_ipc_dev_get);
  159. /*
  160. * Send ipc command
  161. * Command Register (Write Only):
  162. * A write to this register results in an interrupt to the SCU core processor
  163. * Format:
  164. * |rfu2(8) | size(8) | command id(4) | rfu1(3) | ioc(1) | command(8)|
  165. */
  166. static inline void ipc_command(struct intel_scu_ipc_dev *scu, u32 cmd)
  167. {
  168. reinit_completion(&scu->cmd_complete);
  169. writel(cmd | IPC_IOC, scu->ipc_base);
  170. }
  171. /*
  172. * Write ipc data
  173. * IPC Write Buffer (Write Only):
  174. * 16-byte buffer for sending data associated with IPC command to
  175. * SCU. Size of the data is specified in the IPC_COMMAND_REG register
  176. */
  177. static inline void ipc_data_writel(struct intel_scu_ipc_dev *scu, u32 data, u32 offset)
  178. {
  179. writel(data, scu->ipc_base + IPC_WRITE_BUFFER + offset);
  180. }
  181. /*
  182. * Status Register (Read Only):
  183. * Driver will read this register to get the ready/busy status of the IPC
  184. * block and error status of the IPC command that was just processed by SCU
  185. * Format:
  186. * |rfu3(8)|error code(8)|initiator id(8)|cmd id(4)|rfu1(2)|error(1)|busy(1)|
  187. */
  188. static inline u8 ipc_read_status(struct intel_scu_ipc_dev *scu)
  189. {
  190. return __raw_readl(scu->ipc_base + IPC_STATUS);
  191. }
  192. /* Read ipc byte data */
  193. static inline u8 ipc_data_readb(struct intel_scu_ipc_dev *scu, u32 offset)
  194. {
  195. return readb(scu->ipc_base + IPC_READ_BUFFER + offset);
  196. }
  197. /* Read ipc u32 data */
  198. static inline u32 ipc_data_readl(struct intel_scu_ipc_dev *scu, u32 offset)
  199. {
  200. return readl(scu->ipc_base + IPC_READ_BUFFER + offset);
  201. }
  202. /* Wait till scu status is busy */
  203. static inline int busy_loop(struct intel_scu_ipc_dev *scu)
  204. {
  205. u8 status;
  206. int err;
  207. err = readx_poll_timeout(ipc_read_status, scu, status, !(status & IPC_STATUS_BUSY),
  208. 100, jiffies_to_usecs(IPC_TIMEOUT));
  209. if (err)
  210. return err;
  211. return (status & IPC_STATUS_ERR) ? -EIO : 0;
  212. }
  213. /* Wait till ipc ioc interrupt is received or timeout in 10 HZ */
  214. static inline int ipc_wait_for_interrupt(struct intel_scu_ipc_dev *scu)
  215. {
  216. int status;
  217. wait_for_completion_timeout(&scu->cmd_complete, IPC_TIMEOUT);
  218. status = ipc_read_status(scu);
  219. if (status & IPC_STATUS_BUSY)
  220. return -ETIMEDOUT;
  221. if (status & IPC_STATUS_ERR)
  222. return -EIO;
  223. return 0;
  224. }
  225. static int intel_scu_ipc_check_status(struct intel_scu_ipc_dev *scu)
  226. {
  227. return scu->irq > 0 ? ipc_wait_for_interrupt(scu) : busy_loop(scu);
  228. }
  229. static struct intel_scu_ipc_dev *intel_scu_ipc_get(struct intel_scu_ipc_dev *scu)
  230. {
  231. u8 status;
  232. if (!scu)
  233. scu = ipcdev;
  234. if (!scu)
  235. return ERR_PTR(-ENODEV);
  236. status = ipc_read_status(scu);
  237. if (status & IPC_STATUS_BUSY) {
  238. dev_dbg(&scu->dev, "device is busy\n");
  239. return ERR_PTR(-EBUSY);
  240. }
  241. return scu;
  242. }
  243. /* Read/Write power control(PMIC in Langwell, MSIC in PenWell) registers */
  244. static int pwr_reg_rdwr(struct intel_scu_ipc_dev *scu, u16 *addr, u8 *data,
  245. u32 count, u32 op, u32 id)
  246. {
  247. int nc;
  248. u32 offset = 0;
  249. int err;
  250. u8 cbuf[IPC_WWBUF_SIZE];
  251. u32 *wbuf = (u32 *)&cbuf;
  252. memset(cbuf, 0, sizeof(cbuf));
  253. mutex_lock(&ipclock);
  254. scu = intel_scu_ipc_get(scu);
  255. if (IS_ERR(scu)) {
  256. mutex_unlock(&ipclock);
  257. return PTR_ERR(scu);
  258. }
  259. for (nc = 0; nc < count; nc++, offset += 2) {
  260. cbuf[offset] = addr[nc];
  261. cbuf[offset + 1] = addr[nc] >> 8;
  262. }
  263. if (id == IPC_CMD_PCNTRL_R) {
  264. for (nc = 0, offset = 0; nc < count; nc++, offset += 4)
  265. ipc_data_writel(scu, wbuf[nc], offset);
  266. ipc_command(scu, (count * 2) << 16 | id << 12 | 0 << 8 | op);
  267. } else if (id == IPC_CMD_PCNTRL_W) {
  268. for (nc = 0; nc < count; nc++, offset += 1)
  269. cbuf[offset] = data[nc];
  270. for (nc = 0, offset = 0; nc < count; nc++, offset += 4)
  271. ipc_data_writel(scu, wbuf[nc], offset);
  272. ipc_command(scu, (count * 3) << 16 | id << 12 | 0 << 8 | op);
  273. } else if (id == IPC_CMD_PCNTRL_M) {
  274. cbuf[offset] = data[0];
  275. cbuf[offset + 1] = data[1];
  276. ipc_data_writel(scu, wbuf[0], 0); /* Write wbuff */
  277. ipc_command(scu, 4 << 16 | id << 12 | 0 << 8 | op);
  278. }
  279. err = intel_scu_ipc_check_status(scu);
  280. if (!err && id == IPC_CMD_PCNTRL_R) { /* Read rbuf */
  281. /* Workaround: values are read as 0 without memcpy_fromio */
  282. memcpy_fromio(cbuf, scu->ipc_base + 0x90, 16);
  283. for (nc = 0; nc < count; nc++)
  284. data[nc] = ipc_data_readb(scu, nc);
  285. }
  286. mutex_unlock(&ipclock);
  287. return err;
  288. }
  289. /**
  290. * intel_scu_ipc_dev_ioread8() - Read a byte via the SCU
  291. * @scu: Optional SCU IPC instance
  292. * @addr: Register on SCU
  293. * @data: Return pointer for read byte
  294. *
  295. * Read a single register. Returns %0 on success or an error code. All
  296. * locking between SCU accesses is handled for the caller.
  297. *
  298. * This function may sleep.
  299. */
  300. int intel_scu_ipc_dev_ioread8(struct intel_scu_ipc_dev *scu, u16 addr, u8 *data)
  301. {
  302. return pwr_reg_rdwr(scu, &addr, data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
  303. }
  304. EXPORT_SYMBOL(intel_scu_ipc_dev_ioread8);
  305. /**
  306. * intel_scu_ipc_dev_iowrite8() - Write a byte via the SCU
  307. * @scu: Optional SCU IPC instance
  308. * @addr: Register on SCU
  309. * @data: Byte to write
  310. *
  311. * Write a single register. Returns %0 on success or an error code. All
  312. * locking between SCU accesses is handled for the caller.
  313. *
  314. * This function may sleep.
  315. */
  316. int intel_scu_ipc_dev_iowrite8(struct intel_scu_ipc_dev *scu, u16 addr, u8 data)
  317. {
  318. return pwr_reg_rdwr(scu, &addr, &data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
  319. }
  320. EXPORT_SYMBOL(intel_scu_ipc_dev_iowrite8);
  321. /**
  322. * intel_scu_ipc_dev_readv() - Read a set of registers
  323. * @scu: Optional SCU IPC instance
  324. * @addr: Register list
  325. * @data: Bytes to return
  326. * @len: Length of array
  327. *
  328. * Read registers. Returns %0 on success or an error code. All locking
  329. * between SCU accesses is handled for the caller.
  330. *
  331. * The largest array length permitted by the hardware is 5 items.
  332. *
  333. * This function may sleep.
  334. */
  335. int intel_scu_ipc_dev_readv(struct intel_scu_ipc_dev *scu, u16 *addr, u8 *data,
  336. size_t len)
  337. {
  338. return pwr_reg_rdwr(scu, addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
  339. }
  340. EXPORT_SYMBOL(intel_scu_ipc_dev_readv);
  341. /**
  342. * intel_scu_ipc_dev_writev() - Write a set of registers
  343. * @scu: Optional SCU IPC instance
  344. * @addr: Register list
  345. * @data: Bytes to write
  346. * @len: Length of array
  347. *
  348. * Write registers. Returns %0 on success or an error code. All locking
  349. * between SCU accesses is handled for the caller.
  350. *
  351. * The largest array length permitted by the hardware is 5 items.
  352. *
  353. * This function may sleep.
  354. */
  355. int intel_scu_ipc_dev_writev(struct intel_scu_ipc_dev *scu, u16 *addr, u8 *data,
  356. size_t len)
  357. {
  358. return pwr_reg_rdwr(scu, addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
  359. }
  360. EXPORT_SYMBOL(intel_scu_ipc_dev_writev);
  361. /**
  362. * intel_scu_ipc_dev_update() - Update a register
  363. * @scu: Optional SCU IPC instance
  364. * @addr: Register address
  365. * @data: Bits to update
  366. * @mask: Mask of bits to update
  367. *
  368. * Read-modify-write power control unit register. The first data argument
  369. * must be register value and second is mask value mask is a bitmap that
  370. * indicates which bits to update. %0 = masked. Don't modify this bit, %1 =
  371. * modify this bit. returns %0 on success or an error code.
  372. *
  373. * This function may sleep. Locking between SCU accesses is handled
  374. * for the caller.
  375. */
  376. int intel_scu_ipc_dev_update(struct intel_scu_ipc_dev *scu, u16 addr, u8 data,
  377. u8 mask)
  378. {
  379. u8 tmp[2] = { data, mask };
  380. return pwr_reg_rdwr(scu, &addr, tmp, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_M);
  381. }
  382. EXPORT_SYMBOL(intel_scu_ipc_dev_update);
  383. /**
  384. * intel_scu_ipc_dev_simple_command() - Send a simple command
  385. * @scu: Optional SCU IPC instance
  386. * @cmd: Command
  387. * @sub: Sub type
  388. *
  389. * Issue a simple command to the SCU. Do not use this interface if you must
  390. * then access data as any data values may be overwritten by another SCU
  391. * access by the time this function returns.
  392. *
  393. * This function may sleep. Locking for SCU accesses is handled for the
  394. * caller.
  395. */
  396. int intel_scu_ipc_dev_simple_command(struct intel_scu_ipc_dev *scu, int cmd,
  397. int sub)
  398. {
  399. u32 cmdval;
  400. int err;
  401. mutex_lock(&ipclock);
  402. scu = intel_scu_ipc_get(scu);
  403. if (IS_ERR(scu)) {
  404. mutex_unlock(&ipclock);
  405. return PTR_ERR(scu);
  406. }
  407. cmdval = sub << 12 | cmd;
  408. ipc_command(scu, cmdval);
  409. err = intel_scu_ipc_check_status(scu);
  410. mutex_unlock(&ipclock);
  411. if (err)
  412. dev_err(&scu->dev, "IPC command %#x failed with %d\n", cmdval, err);
  413. return err;
  414. }
  415. EXPORT_SYMBOL(intel_scu_ipc_dev_simple_command);
  416. /**
  417. * intel_scu_ipc_dev_command_with_size() - Command with data
  418. * @scu: Optional SCU IPC instance
  419. * @cmd: Command
  420. * @sub: Sub type
  421. * @in: Input data
  422. * @inlen: Input length in bytes
  423. * @size: Input size written to the IPC command register in whatever
  424. * units (dword, byte) the particular firmware requires. Normally
  425. * should be the same as @inlen.
  426. * @out: Output data
  427. * @outlen: Output length in bytes
  428. *
  429. * Issue a command to the SCU which involves data transfers. Do the
  430. * data copies under the lock but leave it for the caller to interpret.
  431. */
  432. int intel_scu_ipc_dev_command_with_size(struct intel_scu_ipc_dev *scu, int cmd,
  433. int sub, const void *in, size_t inlen,
  434. size_t size, void *out, size_t outlen)
  435. {
  436. size_t outbuflen = DIV_ROUND_UP(outlen, sizeof(u32));
  437. size_t inbuflen = DIV_ROUND_UP(inlen, sizeof(u32));
  438. u32 cmdval, inbuf[4] = {};
  439. int i, err;
  440. if (inbuflen > 4 || outbuflen > 4)
  441. return -EINVAL;
  442. mutex_lock(&ipclock);
  443. scu = intel_scu_ipc_get(scu);
  444. if (IS_ERR(scu)) {
  445. mutex_unlock(&ipclock);
  446. return PTR_ERR(scu);
  447. }
  448. memcpy(inbuf, in, inlen);
  449. for (i = 0; i < inbuflen; i++)
  450. ipc_data_writel(scu, inbuf[i], 4 * i);
  451. cmdval = (size << 16) | (sub << 12) | cmd;
  452. ipc_command(scu, cmdval);
  453. err = intel_scu_ipc_check_status(scu);
  454. if (!err) {
  455. u32 outbuf[4] = {};
  456. for (i = 0; i < outbuflen; i++)
  457. outbuf[i] = ipc_data_readl(scu, 4 * i);
  458. memcpy(out, outbuf, outlen);
  459. }
  460. mutex_unlock(&ipclock);
  461. if (err)
  462. dev_err(&scu->dev, "IPC command %#x failed with %d\n", cmdval, err);
  463. return err;
  464. }
  465. EXPORT_SYMBOL(intel_scu_ipc_dev_command_with_size);
  466. /*
  467. * Interrupt handler gets called when ioc bit of IPC_COMMAND_REG set to 1
  468. * When ioc bit is set to 1, caller api must wait for interrupt handler called
  469. * which in turn unlocks the caller api. Currently this is not used
  470. *
  471. * This is edge triggered so we need take no action to clear anything
  472. */
  473. static irqreturn_t ioc(int irq, void *dev_id)
  474. {
  475. struct intel_scu_ipc_dev *scu = dev_id;
  476. int status = ipc_read_status(scu);
  477. writel(status | IPC_STATUS_IRQ, scu->ipc_base + IPC_STATUS);
  478. complete(&scu->cmd_complete);
  479. return IRQ_HANDLED;
  480. }
  481. static void intel_scu_ipc_release(struct device *dev)
  482. {
  483. struct intel_scu_ipc_dev *scu;
  484. scu = container_of(dev, struct intel_scu_ipc_dev, dev);
  485. if (scu->irq > 0)
  486. free_irq(scu->irq, scu);
  487. iounmap(scu->ipc_base);
  488. release_mem_region(scu->mem.start, resource_size(&scu->mem));
  489. kfree(scu);
  490. }
  491. /**
  492. * __intel_scu_ipc_register() - Register SCU IPC device
  493. * @parent: Parent device
  494. * @scu_data: Data used to configure SCU IPC
  495. * @owner: Module registering the SCU IPC device
  496. *
  497. * Call this function to register SCU IPC mechanism under @parent.
  498. * Returns pointer to the new SCU IPC device or ERR_PTR() in case of
  499. * failure. The caller may use the returned instance if it needs to do
  500. * SCU IPC calls itself.
  501. */
  502. struct intel_scu_ipc_dev *
  503. __intel_scu_ipc_register(struct device *parent,
  504. const struct intel_scu_ipc_data *scu_data,
  505. struct module *owner)
  506. {
  507. int err;
  508. struct intel_scu_ipc_dev *scu;
  509. void __iomem *ipc_base;
  510. mutex_lock(&ipclock);
  511. /* We support only one IPC */
  512. if (ipcdev) {
  513. err = -EBUSY;
  514. goto err_unlock;
  515. }
  516. scu = kzalloc(sizeof(*scu), GFP_KERNEL);
  517. if (!scu) {
  518. err = -ENOMEM;
  519. goto err_unlock;
  520. }
  521. scu->owner = owner;
  522. scu->dev.parent = parent;
  523. scu->dev.class = &intel_scu_ipc_class;
  524. scu->dev.release = intel_scu_ipc_release;
  525. if (!request_mem_region(scu_data->mem.start, resource_size(&scu_data->mem),
  526. "intel_scu_ipc")) {
  527. err = -EBUSY;
  528. goto err_free;
  529. }
  530. ipc_base = ioremap(scu_data->mem.start, resource_size(&scu_data->mem));
  531. if (!ipc_base) {
  532. err = -ENOMEM;
  533. goto err_release;
  534. }
  535. scu->ipc_base = ipc_base;
  536. scu->mem = scu_data->mem;
  537. scu->irq = scu_data->irq;
  538. init_completion(&scu->cmd_complete);
  539. if (scu->irq > 0) {
  540. err = request_irq(scu->irq, ioc, 0, "intel_scu_ipc", scu);
  541. if (err)
  542. goto err_unmap;
  543. }
  544. /*
  545. * After this point intel_scu_ipc_release() takes care of
  546. * releasing the SCU IPC resources once refcount drops to zero.
  547. */
  548. dev_set_name(&scu->dev, "intel_scu_ipc");
  549. err = device_register(&scu->dev);
  550. if (err) {
  551. put_device(&scu->dev);
  552. goto err_unlock;
  553. }
  554. /* Assign device at last */
  555. ipcdev = scu;
  556. mutex_unlock(&ipclock);
  557. return scu;
  558. err_unmap:
  559. iounmap(ipc_base);
  560. err_release:
  561. release_mem_region(scu_data->mem.start, resource_size(&scu_data->mem));
  562. err_free:
  563. kfree(scu);
  564. err_unlock:
  565. mutex_unlock(&ipclock);
  566. return ERR_PTR(err);
  567. }
  568. EXPORT_SYMBOL_GPL(__intel_scu_ipc_register);
  569. /**
  570. * intel_scu_ipc_unregister() - Unregister SCU IPC
  571. * @scu: SCU IPC handle
  572. *
  573. * This unregisters the SCU IPC device and releases the acquired
  574. * resources once the refcount goes to zero.
  575. */
  576. void intel_scu_ipc_unregister(struct intel_scu_ipc_dev *scu)
  577. {
  578. mutex_lock(&ipclock);
  579. if (!WARN_ON(!ipcdev)) {
  580. ipcdev = NULL;
  581. device_unregister(&scu->dev);
  582. }
  583. mutex_unlock(&ipclock);
  584. }
  585. EXPORT_SYMBOL_GPL(intel_scu_ipc_unregister);
  586. static void devm_intel_scu_ipc_unregister(struct device *dev, void *res)
  587. {
  588. struct intel_scu_ipc_devres *dr = res;
  589. struct intel_scu_ipc_dev *scu = dr->scu;
  590. intel_scu_ipc_unregister(scu);
  591. }
  592. /**
  593. * __devm_intel_scu_ipc_register() - Register managed SCU IPC device
  594. * @parent: Parent device
  595. * @scu_data: Data used to configure SCU IPC
  596. * @owner: Module registering the SCU IPC device
  597. *
  598. * Call this function to register managed SCU IPC mechanism under
  599. * @parent. Returns pointer to the new SCU IPC device or ERR_PTR() in
  600. * case of failure. The caller may use the returned instance if it needs
  601. * to do SCU IPC calls itself.
  602. */
  603. struct intel_scu_ipc_dev *
  604. __devm_intel_scu_ipc_register(struct device *parent,
  605. const struct intel_scu_ipc_data *scu_data,
  606. struct module *owner)
  607. {
  608. struct intel_scu_ipc_devres *dr;
  609. struct intel_scu_ipc_dev *scu;
  610. dr = devres_alloc(devm_intel_scu_ipc_unregister, sizeof(*dr), GFP_KERNEL);
  611. if (!dr)
  612. return NULL;
  613. scu = __intel_scu_ipc_register(parent, scu_data, owner);
  614. if (IS_ERR(scu)) {
  615. devres_free(dr);
  616. return scu;
  617. }
  618. dr->scu = scu;
  619. devres_add(parent, dr);
  620. return scu;
  621. }
  622. EXPORT_SYMBOL_GPL(__devm_intel_scu_ipc_register);
  623. static int __init intel_scu_ipc_init(void)
  624. {
  625. return class_register(&intel_scu_ipc_class);
  626. }
  627. subsys_initcall(intel_scu_ipc_init);
  628. static void __exit intel_scu_ipc_exit(void)
  629. {
  630. class_unregister(&intel_scu_ipc_class);
  631. }
  632. module_exit(intel_scu_ipc_exit);