1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for the Intel SCU IPC mechanism
5 * (C) Copyright 2008-2010,2015 Intel Corporation
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.
16 #include <linux/delay.h>
17 #include <linux/device.h>
18 #include <linux/errno.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
25 #include <asm/intel_scu_ipc.h>
27 /* IPC defines the following message types */
28 #define IPCMSG_PCNTRL 0xff /* Power controller unit read/write */
30 /* Command id associated with message IPCMSG_PCNTRL */
31 #define IPC_CMD_PCNTRL_W 0 /* Register write */
32 #define IPC_CMD_PCNTRL_R 1 /* Register read */
33 #define IPC_CMD_PCNTRL_M 2 /* Register read-modify-write */
36 * IPC register summary
38 * IPC register blocks are memory mapped at fixed address of PCI BAR 0.
39 * To read or write information to the SCU, driver writes to IPC-1 memory
40 * mapped registers. The following is the IPC mechanism
42 * 1. IA core cDMI interface claims this transaction and converts it to a
43 * Transaction Layer Packet (TLP) message which is sent across the cDMI.
45 * 2. South Complex cDMI block receives this message and writes it to
46 * the IPC-1 register block, causing an interrupt to the SCU
48 * 3. SCU firmware decodes this interrupt and IPC message and the appropriate
49 * message handler is called within firmware.
52 #define IPC_WWBUF_SIZE 20 /* IPC Write buffer Size */
53 #define IPC_RWBUF_SIZE 20 /* IPC Read buffer Size */
54 #define IPC_IOC 0x100 /* IPC command register IOC bit */
56 struct intel_scu_ipc_dev {
61 void __iomem *ipc_base;
62 struct completion cmd_complete;
65 #define IPC_STATUS 0x04
66 #define IPC_STATUS_IRQ BIT(2)
67 #define IPC_STATUS_ERR BIT(1)
68 #define IPC_STATUS_BUSY BIT(0)
71 * IPC Write/Read Buffers:
72 * 16 byte buffer for sending and receiving data to and from SCU.
74 #define IPC_WRITE_BUFFER 0x80
75 #define IPC_READ_BUFFER 0x90
77 /* Timeout in jiffies */
78 #define IPC_TIMEOUT (10 * HZ)
80 static struct intel_scu_ipc_dev *ipcdev; /* Only one for now */
81 static DEFINE_MUTEX(ipclock); /* lock used to prevent multiple call to SCU */
83 static struct class intel_scu_ipc_class = {
84 .name = "intel_scu_ipc",
88 * intel_scu_ipc_dev_get() - Get SCU IPC instance
90 * The recommended new API takes SCU IPC instance as parameter and this
91 * function can be called by driver to get the instance. This also makes
92 * sure the driver providing the IPC functionality cannot be unloaded
93 * while the caller has the instance.
95 * Call intel_scu_ipc_dev_put() to release the instance.
97 * Returns %NULL if SCU IPC is not currently available.
99 struct intel_scu_ipc_dev *intel_scu_ipc_dev_get(void)
101 struct intel_scu_ipc_dev *scu = NULL;
103 mutex_lock(&ipclock);
105 get_device(&ipcdev->dev);
107 * Prevent the IPC provider from being unloaded while it
110 if (!try_module_get(ipcdev->owner))
111 put_device(&ipcdev->dev);
116 mutex_unlock(&ipclock);
119 EXPORT_SYMBOL_GPL(intel_scu_ipc_dev_get);
122 * intel_scu_ipc_dev_put() - Put SCU IPC instance
123 * @scu: SCU IPC instance
125 * This function releases the SCU IPC instance retrieved from
126 * intel_scu_ipc_dev_get() and allows the driver providing IPC to be
129 void intel_scu_ipc_dev_put(struct intel_scu_ipc_dev *scu)
132 module_put(scu->owner);
133 put_device(&scu->dev);
136 EXPORT_SYMBOL_GPL(intel_scu_ipc_dev_put);
138 struct intel_scu_ipc_devres {
139 struct intel_scu_ipc_dev *scu;
142 static void devm_intel_scu_ipc_dev_release(struct device *dev, void *res)
144 struct intel_scu_ipc_devres *dr = res;
145 struct intel_scu_ipc_dev *scu = dr->scu;
147 intel_scu_ipc_dev_put(scu);
151 * devm_intel_scu_ipc_dev_get() - Allocate managed SCU IPC device
152 * @dev: Device requesting the SCU IPC device
154 * The recommended new API takes SCU IPC instance as parameter and this
155 * function can be called by driver to get the instance. This also makes
156 * sure the driver providing the IPC functionality cannot be unloaded
157 * while the caller has the instance.
159 * Returns %NULL if SCU IPC is not currently available.
161 struct intel_scu_ipc_dev *devm_intel_scu_ipc_dev_get(struct device *dev)
163 struct intel_scu_ipc_devres *dr;
164 struct intel_scu_ipc_dev *scu;
166 dr = devres_alloc(devm_intel_scu_ipc_dev_release, sizeof(*dr), GFP_KERNEL);
170 scu = intel_scu_ipc_dev_get();
181 EXPORT_SYMBOL_GPL(devm_intel_scu_ipc_dev_get);
185 * Command Register (Write Only):
186 * A write to this register results in an interrupt to the SCU core processor
188 * |rfu2(8) | size(8) | command id(4) | rfu1(3) | ioc(1) | command(8)|
190 static inline void ipc_command(struct intel_scu_ipc_dev *scu, u32 cmd)
192 reinit_completion(&scu->cmd_complete);
193 writel(cmd | IPC_IOC, scu->ipc_base);
198 * IPC Write Buffer (Write Only):
199 * 16-byte buffer for sending data associated with IPC command to
200 * SCU. Size of the data is specified in the IPC_COMMAND_REG register
202 static inline void ipc_data_writel(struct intel_scu_ipc_dev *scu, u32 data, u32 offset)
204 writel(data, scu->ipc_base + IPC_WRITE_BUFFER + offset);
208 * Status Register (Read Only):
209 * Driver will read this register to get the ready/busy status of the IPC
210 * block and error status of the IPC command that was just processed by SCU
212 * |rfu3(8)|error code(8)|initiator id(8)|cmd id(4)|rfu1(2)|error(1)|busy(1)|
214 static inline u8 ipc_read_status(struct intel_scu_ipc_dev *scu)
216 return __raw_readl(scu->ipc_base + IPC_STATUS);
219 /* Read ipc byte data */
220 static inline u8 ipc_data_readb(struct intel_scu_ipc_dev *scu, u32 offset)
222 return readb(scu->ipc_base + IPC_READ_BUFFER + offset);
225 /* Read ipc u32 data */
226 static inline u32 ipc_data_readl(struct intel_scu_ipc_dev *scu, u32 offset)
228 return readl(scu->ipc_base + IPC_READ_BUFFER + offset);
231 /* Wait till scu status is busy */
232 static inline int busy_loop(struct intel_scu_ipc_dev *scu)
234 unsigned long end = jiffies + IPC_TIMEOUT;
239 status = ipc_read_status(scu);
240 if (!(status & IPC_STATUS_BUSY))
241 return (status & IPC_STATUS_ERR) ? -EIO : 0;
243 usleep_range(50, 100);
244 } while (time_before(jiffies, end));
249 /* Wait till ipc ioc interrupt is received or timeout in 10 HZ */
250 static inline int ipc_wait_for_interrupt(struct intel_scu_ipc_dev *scu)
254 if (!wait_for_completion_timeout(&scu->cmd_complete, IPC_TIMEOUT))
257 status = ipc_read_status(scu);
258 if (status & IPC_STATUS_ERR)
264 static int intel_scu_ipc_check_status(struct intel_scu_ipc_dev *scu)
266 return scu->irq > 0 ? ipc_wait_for_interrupt(scu) : busy_loop(scu);
269 /* Read/Write power control(PMIC in Langwell, MSIC in PenWell) registers */
270 static int pwr_reg_rdwr(struct intel_scu_ipc_dev *scu, u16 *addr, u8 *data,
271 u32 count, u32 op, u32 id)
276 u8 cbuf[IPC_WWBUF_SIZE];
277 u32 *wbuf = (u32 *)&cbuf;
279 memset(cbuf, 0, sizeof(cbuf));
281 mutex_lock(&ipclock);
285 mutex_unlock(&ipclock);
289 for (nc = 0; nc < count; nc++, offset += 2) {
290 cbuf[offset] = addr[nc];
291 cbuf[offset + 1] = addr[nc] >> 8;
294 if (id == IPC_CMD_PCNTRL_R) {
295 for (nc = 0, offset = 0; nc < count; nc++, offset += 4)
296 ipc_data_writel(scu, wbuf[nc], offset);
297 ipc_command(scu, (count * 2) << 16 | id << 12 | 0 << 8 | op);
298 } else if (id == IPC_CMD_PCNTRL_W) {
299 for (nc = 0; nc < count; nc++, offset += 1)
300 cbuf[offset] = data[nc];
301 for (nc = 0, offset = 0; nc < count; nc++, offset += 4)
302 ipc_data_writel(scu, wbuf[nc], offset);
303 ipc_command(scu, (count * 3) << 16 | id << 12 | 0 << 8 | op);
304 } else if (id == IPC_CMD_PCNTRL_M) {
305 cbuf[offset] = data[0];
306 cbuf[offset + 1] = data[1];
307 ipc_data_writel(scu, wbuf[0], 0); /* Write wbuff */
308 ipc_command(scu, 4 << 16 | id << 12 | 0 << 8 | op);
311 err = intel_scu_ipc_check_status(scu);
312 if (!err && id == IPC_CMD_PCNTRL_R) { /* Read rbuf */
313 /* Workaround: values are read as 0 without memcpy_fromio */
314 memcpy_fromio(cbuf, scu->ipc_base + 0x90, 16);
315 for (nc = 0; nc < count; nc++)
316 data[nc] = ipc_data_readb(scu, nc);
318 mutex_unlock(&ipclock);
323 * intel_scu_ipc_dev_ioread8() - Read a byte via the SCU
324 * @scu: Optional SCU IPC instance
325 * @addr: Register on SCU
326 * @data: Return pointer for read byte
328 * Read a single register. Returns %0 on success or an error code. All
329 * locking between SCU accesses is handled for the caller.
331 * This function may sleep.
333 int intel_scu_ipc_dev_ioread8(struct intel_scu_ipc_dev *scu, u16 addr, u8 *data)
335 return pwr_reg_rdwr(scu, &addr, data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
337 EXPORT_SYMBOL(intel_scu_ipc_dev_ioread8);
340 * intel_scu_ipc_dev_iowrite8() - Write a byte via the SCU
341 * @scu: Optional SCU IPC instance
342 * @addr: Register on SCU
343 * @data: Byte to write
345 * Write a single register. Returns %0 on success or an error code. All
346 * locking between SCU accesses is handled for the caller.
348 * This function may sleep.
350 int intel_scu_ipc_dev_iowrite8(struct intel_scu_ipc_dev *scu, u16 addr, u8 data)
352 return pwr_reg_rdwr(scu, &addr, &data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
354 EXPORT_SYMBOL(intel_scu_ipc_dev_iowrite8);
357 * intel_scu_ipc_dev_readv() - Read a set of registers
358 * @scu: Optional SCU IPC instance
359 * @addr: Register list
360 * @data: Bytes to return
361 * @len: Length of array
363 * Read registers. Returns %0 on success or an error code. All locking
364 * between SCU accesses is handled for the caller.
366 * The largest array length permitted by the hardware is 5 items.
368 * This function may sleep.
370 int intel_scu_ipc_dev_readv(struct intel_scu_ipc_dev *scu, u16 *addr, u8 *data,
373 return pwr_reg_rdwr(scu, addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
375 EXPORT_SYMBOL(intel_scu_ipc_dev_readv);
378 * intel_scu_ipc_dev_writev() - Write a set of registers
379 * @scu: Optional SCU IPC instance
380 * @addr: Register list
381 * @data: Bytes to write
382 * @len: Length of array
384 * Write registers. Returns %0 on success or an error code. All locking
385 * between SCU accesses is handled for the caller.
387 * The largest array length permitted by the hardware is 5 items.
389 * This function may sleep.
391 int intel_scu_ipc_dev_writev(struct intel_scu_ipc_dev *scu, u16 *addr, u8 *data,
394 return pwr_reg_rdwr(scu, addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
396 EXPORT_SYMBOL(intel_scu_ipc_dev_writev);
399 * intel_scu_ipc_dev_update() - Update a register
400 * @scu: Optional SCU IPC instance
401 * @addr: Register address
402 * @data: Bits to update
403 * @mask: Mask of bits to update
405 * Read-modify-write power control unit register. The first data argument
406 * must be register value and second is mask value mask is a bitmap that
407 * indicates which bits to update. %0 = masked. Don't modify this bit, %1 =
408 * modify this bit. returns %0 on success or an error code.
410 * This function may sleep. Locking between SCU accesses is handled
413 int intel_scu_ipc_dev_update(struct intel_scu_ipc_dev *scu, u16 addr, u8 data,
416 u8 tmp[2] = { data, mask };
417 return pwr_reg_rdwr(scu, &addr, tmp, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_M);
419 EXPORT_SYMBOL(intel_scu_ipc_dev_update);
422 * intel_scu_ipc_dev_simple_command() - Send a simple command
423 * @scu: Optional SCU IPC instance
427 * Issue a simple command to the SCU. Do not use this interface if you must
428 * then access data as any data values may be overwritten by another SCU
429 * access by the time this function returns.
431 * This function may sleep. Locking for SCU accesses is handled for the
434 int intel_scu_ipc_dev_simple_command(struct intel_scu_ipc_dev *scu, int cmd,
440 mutex_lock(&ipclock);
444 mutex_unlock(&ipclock);
448 cmdval = sub << 12 | cmd;
449 ipc_command(scu, cmdval);
450 err = intel_scu_ipc_check_status(scu);
451 mutex_unlock(&ipclock);
453 dev_err(&scu->dev, "IPC command %#x failed with %d\n", cmdval, err);
456 EXPORT_SYMBOL(intel_scu_ipc_dev_simple_command);
459 * intel_scu_ipc_dev_command_with_size() - Command with data
460 * @scu: Optional SCU IPC instance
464 * @inlen: Input length in bytes
465 * @size: Input size written to the IPC command register in whatever
466 * units (dword, byte) the particular firmware requires. Normally
467 * should be the same as @inlen.
469 * @outlen: Output length in bytes
471 * Issue a command to the SCU which involves data transfers. Do the
472 * data copies under the lock but leave it for the caller to interpret.
474 int intel_scu_ipc_dev_command_with_size(struct intel_scu_ipc_dev *scu, int cmd,
475 int sub, const void *in, size_t inlen,
476 size_t size, void *out, size_t outlen)
478 size_t outbuflen = DIV_ROUND_UP(outlen, sizeof(u32));
479 size_t inbuflen = DIV_ROUND_UP(inlen, sizeof(u32));
480 u32 cmdval, inbuf[4] = {};
483 if (inbuflen > 4 || outbuflen > 4)
486 mutex_lock(&ipclock);
490 mutex_unlock(&ipclock);
494 memcpy(inbuf, in, inlen);
495 for (i = 0; i < inbuflen; i++)
496 ipc_data_writel(scu, inbuf[i], 4 * i);
498 cmdval = (size << 16) | (sub << 12) | cmd;
499 ipc_command(scu, cmdval);
500 err = intel_scu_ipc_check_status(scu);
505 for (i = 0; i < outbuflen; i++)
506 outbuf[i] = ipc_data_readl(scu, 4 * i);
508 memcpy(out, outbuf, outlen);
511 mutex_unlock(&ipclock);
513 dev_err(&scu->dev, "IPC command %#x failed with %d\n", cmdval, err);
516 EXPORT_SYMBOL(intel_scu_ipc_dev_command_with_size);
519 * Interrupt handler gets called when ioc bit of IPC_COMMAND_REG set to 1
520 * When ioc bit is set to 1, caller api must wait for interrupt handler called
521 * which in turn unlocks the caller api. Currently this is not used
523 * This is edge triggered so we need take no action to clear anything
525 static irqreturn_t ioc(int irq, void *dev_id)
527 struct intel_scu_ipc_dev *scu = dev_id;
528 int status = ipc_read_status(scu);
530 writel(status | IPC_STATUS_IRQ, scu->ipc_base + IPC_STATUS);
531 complete(&scu->cmd_complete);
536 static void intel_scu_ipc_release(struct device *dev)
538 struct intel_scu_ipc_dev *scu;
540 scu = container_of(dev, struct intel_scu_ipc_dev, dev);
542 free_irq(scu->irq, scu);
543 iounmap(scu->ipc_base);
544 release_mem_region(scu->mem.start, resource_size(&scu->mem));
549 * __intel_scu_ipc_register() - Register SCU IPC device
550 * @parent: Parent device
551 * @scu_data: Data used to configure SCU IPC
552 * @owner: Module registering the SCU IPC device
554 * Call this function to register SCU IPC mechanism under @parent.
555 * Returns pointer to the new SCU IPC device or ERR_PTR() in case of
556 * failure. The caller may use the returned instance if it needs to do
557 * SCU IPC calls itself.
559 struct intel_scu_ipc_dev *
560 __intel_scu_ipc_register(struct device *parent,
561 const struct intel_scu_ipc_data *scu_data,
562 struct module *owner)
565 struct intel_scu_ipc_dev *scu;
566 void __iomem *ipc_base;
568 mutex_lock(&ipclock);
569 /* We support only one IPC */
575 scu = kzalloc(sizeof(*scu), GFP_KERNEL);
582 scu->dev.parent = parent;
583 scu->dev.class = &intel_scu_ipc_class;
584 scu->dev.release = intel_scu_ipc_release;
586 if (!request_mem_region(scu_data->mem.start, resource_size(&scu_data->mem),
592 ipc_base = ioremap(scu_data->mem.start, resource_size(&scu_data->mem));
598 scu->ipc_base = ipc_base;
599 scu->mem = scu_data->mem;
600 scu->irq = scu_data->irq;
601 init_completion(&scu->cmd_complete);
604 err = request_irq(scu->irq, ioc, 0, "intel_scu_ipc", scu);
610 * After this point intel_scu_ipc_release() takes care of
611 * releasing the SCU IPC resources once refcount drops to zero.
613 dev_set_name(&scu->dev, "intel_scu_ipc");
614 err = device_register(&scu->dev);
616 put_device(&scu->dev);
620 /* Assign device at last */
622 mutex_unlock(&ipclock);
629 release_mem_region(scu_data->mem.start, resource_size(&scu_data->mem));
633 mutex_unlock(&ipclock);
637 EXPORT_SYMBOL_GPL(__intel_scu_ipc_register);
640 * intel_scu_ipc_unregister() - Unregister SCU IPC
641 * @scu: SCU IPC handle
643 * This unregisters the SCU IPC device and releases the acquired
644 * resources once the refcount goes to zero.
646 void intel_scu_ipc_unregister(struct intel_scu_ipc_dev *scu)
648 mutex_lock(&ipclock);
649 if (!WARN_ON(!ipcdev)) {
651 device_unregister(&scu->dev);
653 mutex_unlock(&ipclock);
655 EXPORT_SYMBOL_GPL(intel_scu_ipc_unregister);
657 static void devm_intel_scu_ipc_unregister(struct device *dev, void *res)
659 struct intel_scu_ipc_devres *dr = res;
660 struct intel_scu_ipc_dev *scu = dr->scu;
662 intel_scu_ipc_unregister(scu);
666 * __devm_intel_scu_ipc_register() - Register managed SCU IPC device
667 * @parent: Parent device
668 * @scu_data: Data used to configure SCU IPC
669 * @owner: Module registering the SCU IPC device
671 * Call this function to register managed SCU IPC mechanism under
672 * @parent. Returns pointer to the new SCU IPC device or ERR_PTR() in
673 * case of failure. The caller may use the returned instance if it needs
674 * to do SCU IPC calls itself.
676 struct intel_scu_ipc_dev *
677 __devm_intel_scu_ipc_register(struct device *parent,
678 const struct intel_scu_ipc_data *scu_data,
679 struct module *owner)
681 struct intel_scu_ipc_devres *dr;
682 struct intel_scu_ipc_dev *scu;
684 dr = devres_alloc(devm_intel_scu_ipc_unregister, sizeof(*dr), GFP_KERNEL);
688 scu = __intel_scu_ipc_register(parent, scu_data, owner);
695 devres_add(parent, dr);
699 EXPORT_SYMBOL_GPL(__devm_intel_scu_ipc_register);
701 static int __init intel_scu_ipc_init(void)
703 return class_register(&intel_scu_ipc_class);
705 subsys_initcall(intel_scu_ipc_init);
707 static void __exit intel_scu_ipc_exit(void)
709 class_unregister(&intel_scu_ipc_class);
711 module_exit(intel_scu_ipc_exit);