Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Driver for the Intel SCU IPC mechanism
0004  *
0005  * (C) Copyright 2008-2010,2015 Intel Corporation
0006  * Author: Sreedhara DS (sreedhara.ds@intel.com)
0007  *
0008  * SCU running in ARC processor communicates with other entity running in IA
0009  * core through IPC mechanism which in turn messaging between IA core ad SCU.
0010  * SCU has two IPC mechanism IPC-1 and IPC-2. IPC-1 is used between IA32 and
0011  * SCU where IPC-2 is used between P-Unit and SCU. This driver delas with
0012  * IPC-1 Driver provides an API for power control unit registers (e.g. MSIC)
0013  * along with other APIs.
0014  */
0015 
0016 #include <linux/delay.h>
0017 #include <linux/device.h>
0018 #include <linux/errno.h>
0019 #include <linux/init.h>
0020 #include <linux/interrupt.h>
0021 #include <linux/io.h>
0022 #include <linux/module.h>
0023 #include <linux/slab.h>
0024 
0025 #include <asm/intel_scu_ipc.h>
0026 
0027 /* IPC defines the following message types */
0028 #define IPCMSG_PCNTRL         0xff /* Power controller unit read/write */
0029 
0030 /* Command id associated with message IPCMSG_PCNTRL */
0031 #define IPC_CMD_PCNTRL_W      0 /* Register write */
0032 #define IPC_CMD_PCNTRL_R      1 /* Register read */
0033 #define IPC_CMD_PCNTRL_M      2 /* Register read-modify-write */
0034 
0035 /*
0036  * IPC register summary
0037  *
0038  * IPC register blocks are memory mapped at fixed address of PCI BAR 0.
0039  * To read or write information to the SCU, driver writes to IPC-1 memory
0040  * mapped registers. The following is the IPC mechanism
0041  *
0042  * 1. IA core cDMI interface claims this transaction and converts it to a
0043  *    Transaction Layer Packet (TLP) message which is sent across the cDMI.
0044  *
0045  * 2. South Complex cDMI block receives this message and writes it to
0046  *    the IPC-1 register block, causing an interrupt to the SCU
0047  *
0048  * 3. SCU firmware decodes this interrupt and IPC message and the appropriate
0049  *    message handler is called within firmware.
0050  */
0051 
0052 #define IPC_WWBUF_SIZE    20        /* IPC Write buffer Size */
0053 #define IPC_RWBUF_SIZE    20        /* IPC Read buffer Size */
0054 #define IPC_IOC           0x100     /* IPC command register IOC bit */
0055 
0056 struct intel_scu_ipc_dev {
0057     struct device dev;
0058     struct resource mem;
0059     struct module *owner;
0060     int irq;
0061     void __iomem *ipc_base;
0062     struct completion cmd_complete;
0063 };
0064 
0065 #define IPC_STATUS      0x04
0066 #define IPC_STATUS_IRQ      BIT(2)
0067 #define IPC_STATUS_ERR      BIT(1)
0068 #define IPC_STATUS_BUSY     BIT(0)
0069 
0070 /*
0071  * IPC Write/Read Buffers:
0072  * 16 byte buffer for sending and receiving data to and from SCU.
0073  */
0074 #define IPC_WRITE_BUFFER    0x80
0075 #define IPC_READ_BUFFER     0x90
0076 
0077 /* Timeout in jiffies */
0078 #define IPC_TIMEOUT     (10 * HZ)
0079 
0080 static struct intel_scu_ipc_dev *ipcdev; /* Only one for now */
0081 static DEFINE_MUTEX(ipclock); /* lock used to prevent multiple call to SCU */
0082 
0083 static struct class intel_scu_ipc_class = {
0084     .name = "intel_scu_ipc",
0085     .owner = THIS_MODULE,
0086 };
0087 
0088 /**
0089  * intel_scu_ipc_dev_get() - Get SCU IPC instance
0090  *
0091  * The recommended new API takes SCU IPC instance as parameter and this
0092  * function can be called by driver to get the instance. This also makes
0093  * sure the driver providing the IPC functionality cannot be unloaded
0094  * while the caller has the instance.
0095  *
0096  * Call intel_scu_ipc_dev_put() to release the instance.
0097  *
0098  * Returns %NULL if SCU IPC is not currently available.
0099  */
0100 struct intel_scu_ipc_dev *intel_scu_ipc_dev_get(void)
0101 {
0102     struct intel_scu_ipc_dev *scu = NULL;
0103 
0104     mutex_lock(&ipclock);
0105     if (ipcdev) {
0106         get_device(&ipcdev->dev);
0107         /*
0108          * Prevent the IPC provider from being unloaded while it
0109          * is being used.
0110          */
0111         if (!try_module_get(ipcdev->owner))
0112             put_device(&ipcdev->dev);
0113         else
0114             scu = ipcdev;
0115     }
0116 
0117     mutex_unlock(&ipclock);
0118     return scu;
0119 }
0120 EXPORT_SYMBOL_GPL(intel_scu_ipc_dev_get);
0121 
0122 /**
0123  * intel_scu_ipc_dev_put() - Put SCU IPC instance
0124  * @scu: SCU IPC instance
0125  *
0126  * This function releases the SCU IPC instance retrieved from
0127  * intel_scu_ipc_dev_get() and allows the driver providing IPC to be
0128  * unloaded.
0129  */
0130 void intel_scu_ipc_dev_put(struct intel_scu_ipc_dev *scu)
0131 {
0132     if (scu) {
0133         module_put(scu->owner);
0134         put_device(&scu->dev);
0135     }
0136 }
0137 EXPORT_SYMBOL_GPL(intel_scu_ipc_dev_put);
0138 
0139 struct intel_scu_ipc_devres {
0140     struct intel_scu_ipc_dev *scu;
0141 };
0142 
0143 static void devm_intel_scu_ipc_dev_release(struct device *dev, void *res)
0144 {
0145     struct intel_scu_ipc_devres *dr = res;
0146     struct intel_scu_ipc_dev *scu = dr->scu;
0147 
0148     intel_scu_ipc_dev_put(scu);
0149 }
0150 
0151 /**
0152  * devm_intel_scu_ipc_dev_get() - Allocate managed SCU IPC device
0153  * @dev: Device requesting the SCU IPC device
0154  *
0155  * The recommended new API takes SCU IPC instance as parameter and this
0156  * function can be called by driver to get the instance. This also makes
0157  * sure the driver providing the IPC functionality cannot be unloaded
0158  * while the caller has the instance.
0159  *
0160  * Returns %NULL if SCU IPC is not currently available.
0161  */
0162 struct intel_scu_ipc_dev *devm_intel_scu_ipc_dev_get(struct device *dev)
0163 {
0164     struct intel_scu_ipc_devres *dr;
0165     struct intel_scu_ipc_dev *scu;
0166 
0167     dr = devres_alloc(devm_intel_scu_ipc_dev_release, sizeof(*dr), GFP_KERNEL);
0168     if (!dr)
0169         return NULL;
0170 
0171     scu = intel_scu_ipc_dev_get();
0172     if (!scu) {
0173         devres_free(dr);
0174         return NULL;
0175     }
0176 
0177     dr->scu = scu;
0178     devres_add(dev, dr);
0179 
0180     return scu;
0181 }
0182 EXPORT_SYMBOL_GPL(devm_intel_scu_ipc_dev_get);
0183 
0184 /*
0185  * Send ipc command
0186  * Command Register (Write Only):
0187  * A write to this register results in an interrupt to the SCU core processor
0188  * Format:
0189  * |rfu2(8) | size(8) | command id(4) | rfu1(3) | ioc(1) | command(8)|
0190  */
0191 static inline void ipc_command(struct intel_scu_ipc_dev *scu, u32 cmd)
0192 {
0193     reinit_completion(&scu->cmd_complete);
0194     writel(cmd | IPC_IOC, scu->ipc_base);
0195 }
0196 
0197 /*
0198  * Write ipc data
0199  * IPC Write Buffer (Write Only):
0200  * 16-byte buffer for sending data associated with IPC command to
0201  * SCU. Size of the data is specified in the IPC_COMMAND_REG register
0202  */
0203 static inline void ipc_data_writel(struct intel_scu_ipc_dev *scu, u32 data, u32 offset)
0204 {
0205     writel(data, scu->ipc_base + IPC_WRITE_BUFFER + offset);
0206 }
0207 
0208 /*
0209  * Status Register (Read Only):
0210  * Driver will read this register to get the ready/busy status of the IPC
0211  * block and error status of the IPC command that was just processed by SCU
0212  * Format:
0213  * |rfu3(8)|error code(8)|initiator id(8)|cmd id(4)|rfu1(2)|error(1)|busy(1)|
0214  */
0215 static inline u8 ipc_read_status(struct intel_scu_ipc_dev *scu)
0216 {
0217     return __raw_readl(scu->ipc_base + IPC_STATUS);
0218 }
0219 
0220 /* Read ipc byte data */
0221 static inline u8 ipc_data_readb(struct intel_scu_ipc_dev *scu, u32 offset)
0222 {
0223     return readb(scu->ipc_base + IPC_READ_BUFFER + offset);
0224 }
0225 
0226 /* Read ipc u32 data */
0227 static inline u32 ipc_data_readl(struct intel_scu_ipc_dev *scu, u32 offset)
0228 {
0229     return readl(scu->ipc_base + IPC_READ_BUFFER + offset);
0230 }
0231 
0232 /* Wait till scu status is busy */
0233 static inline int busy_loop(struct intel_scu_ipc_dev *scu)
0234 {
0235     unsigned long end = jiffies + IPC_TIMEOUT;
0236 
0237     do {
0238         u32 status;
0239 
0240         status = ipc_read_status(scu);
0241         if (!(status & IPC_STATUS_BUSY))
0242             return (status & IPC_STATUS_ERR) ? -EIO : 0;
0243 
0244         usleep_range(50, 100);
0245     } while (time_before(jiffies, end));
0246 
0247     return -ETIMEDOUT;
0248 }
0249 
0250 /* Wait till ipc ioc interrupt is received or timeout in 10 HZ */
0251 static inline int ipc_wait_for_interrupt(struct intel_scu_ipc_dev *scu)
0252 {
0253     int status;
0254 
0255     if (!wait_for_completion_timeout(&scu->cmd_complete, IPC_TIMEOUT))
0256         return -ETIMEDOUT;
0257 
0258     status = ipc_read_status(scu);
0259     if (status & IPC_STATUS_ERR)
0260         return -EIO;
0261 
0262     return 0;
0263 }
0264 
0265 static int intel_scu_ipc_check_status(struct intel_scu_ipc_dev *scu)
0266 {
0267     return scu->irq > 0 ? ipc_wait_for_interrupt(scu) : busy_loop(scu);
0268 }
0269 
0270 /* Read/Write power control(PMIC in Langwell, MSIC in PenWell) registers */
0271 static int pwr_reg_rdwr(struct intel_scu_ipc_dev *scu, u16 *addr, u8 *data,
0272             u32 count, u32 op, u32 id)
0273 {
0274     int nc;
0275     u32 offset = 0;
0276     int err;
0277     u8 cbuf[IPC_WWBUF_SIZE];
0278     u32 *wbuf = (u32 *)&cbuf;
0279 
0280     memset(cbuf, 0, sizeof(cbuf));
0281 
0282     mutex_lock(&ipclock);
0283     if (!scu)
0284         scu = ipcdev;
0285     if (!scu) {
0286         mutex_unlock(&ipclock);
0287         return -ENODEV;
0288     }
0289 
0290     for (nc = 0; nc < count; nc++, offset += 2) {
0291         cbuf[offset] = addr[nc];
0292         cbuf[offset + 1] = addr[nc] >> 8;
0293     }
0294 
0295     if (id == IPC_CMD_PCNTRL_R) {
0296         for (nc = 0, offset = 0; nc < count; nc++, offset += 4)
0297             ipc_data_writel(scu, wbuf[nc], offset);
0298         ipc_command(scu, (count * 2) << 16 | id << 12 | 0 << 8 | op);
0299     } else if (id == IPC_CMD_PCNTRL_W) {
0300         for (nc = 0; nc < count; nc++, offset += 1)
0301             cbuf[offset] = data[nc];
0302         for (nc = 0, offset = 0; nc < count; nc++, offset += 4)
0303             ipc_data_writel(scu, wbuf[nc], offset);
0304         ipc_command(scu, (count * 3) << 16 | id << 12 | 0 << 8 | op);
0305     } else if (id == IPC_CMD_PCNTRL_M) {
0306         cbuf[offset] = data[0];
0307         cbuf[offset + 1] = data[1];
0308         ipc_data_writel(scu, wbuf[0], 0); /* Write wbuff */
0309         ipc_command(scu, 4 << 16 | id << 12 | 0 << 8 | op);
0310     }
0311 
0312     err = intel_scu_ipc_check_status(scu);
0313     if (!err && id == IPC_CMD_PCNTRL_R) { /* Read rbuf */
0314         /* Workaround: values are read as 0 without memcpy_fromio */
0315         memcpy_fromio(cbuf, scu->ipc_base + 0x90, 16);
0316         for (nc = 0; nc < count; nc++)
0317             data[nc] = ipc_data_readb(scu, nc);
0318     }
0319     mutex_unlock(&ipclock);
0320     return err;
0321 }
0322 
0323 /**
0324  * intel_scu_ipc_dev_ioread8() - Read a byte via the SCU
0325  * @scu: Optional SCU IPC instance
0326  * @addr: Register on SCU
0327  * @data: Return pointer for read byte
0328  *
0329  * Read a single register. Returns %0 on success or an error code. All
0330  * locking between SCU accesses is handled for the caller.
0331  *
0332  * This function may sleep.
0333  */
0334 int intel_scu_ipc_dev_ioread8(struct intel_scu_ipc_dev *scu, u16 addr, u8 *data)
0335 {
0336     return pwr_reg_rdwr(scu, &addr, data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
0337 }
0338 EXPORT_SYMBOL(intel_scu_ipc_dev_ioread8);
0339 
0340 /**
0341  * intel_scu_ipc_dev_iowrite8() - Write a byte via the SCU
0342  * @scu: Optional SCU IPC instance
0343  * @addr: Register on SCU
0344  * @data: Byte to write
0345  *
0346  * Write a single register. Returns %0 on success or an error code. All
0347  * locking between SCU accesses is handled for the caller.
0348  *
0349  * This function may sleep.
0350  */
0351 int intel_scu_ipc_dev_iowrite8(struct intel_scu_ipc_dev *scu, u16 addr, u8 data)
0352 {
0353     return pwr_reg_rdwr(scu, &addr, &data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
0354 }
0355 EXPORT_SYMBOL(intel_scu_ipc_dev_iowrite8);
0356 
0357 /**
0358  * intel_scu_ipc_dev_readv() - Read a set of registers
0359  * @scu: Optional SCU IPC instance
0360  * @addr: Register list
0361  * @data: Bytes to return
0362  * @len: Length of array
0363  *
0364  * Read registers. Returns %0 on success or an error code. All locking
0365  * between SCU accesses is handled for the caller.
0366  *
0367  * The largest array length permitted by the hardware is 5 items.
0368  *
0369  * This function may sleep.
0370  */
0371 int intel_scu_ipc_dev_readv(struct intel_scu_ipc_dev *scu, u16 *addr, u8 *data,
0372                 size_t len)
0373 {
0374     return pwr_reg_rdwr(scu, addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
0375 }
0376 EXPORT_SYMBOL(intel_scu_ipc_dev_readv);
0377 
0378 /**
0379  * intel_scu_ipc_dev_writev() - Write a set of registers
0380  * @scu: Optional SCU IPC instance
0381  * @addr: Register list
0382  * @data: Bytes to write
0383  * @len: Length of array
0384  *
0385  * Write registers. Returns %0 on success or an error code. All locking
0386  * between SCU accesses is handled for the caller.
0387  *
0388  * The largest array length permitted by the hardware is 5 items.
0389  *
0390  * This function may sleep.
0391  */
0392 int intel_scu_ipc_dev_writev(struct intel_scu_ipc_dev *scu, u16 *addr, u8 *data,
0393                  size_t len)
0394 {
0395     return pwr_reg_rdwr(scu, addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
0396 }
0397 EXPORT_SYMBOL(intel_scu_ipc_dev_writev);
0398 
0399 /**
0400  * intel_scu_ipc_dev_update() - Update a register
0401  * @scu: Optional SCU IPC instance
0402  * @addr: Register address
0403  * @data: Bits to update
0404  * @mask: Mask of bits to update
0405  *
0406  * Read-modify-write power control unit register. The first data argument
0407  * must be register value and second is mask value mask is a bitmap that
0408  * indicates which bits to update. %0 = masked. Don't modify this bit, %1 =
0409  * modify this bit. returns %0 on success or an error code.
0410  *
0411  * This function may sleep. Locking between SCU accesses is handled
0412  * for the caller.
0413  */
0414 int intel_scu_ipc_dev_update(struct intel_scu_ipc_dev *scu, u16 addr, u8 data,
0415                  u8 mask)
0416 {
0417     u8 tmp[2] = { data, mask };
0418     return pwr_reg_rdwr(scu, &addr, tmp, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_M);
0419 }
0420 EXPORT_SYMBOL(intel_scu_ipc_dev_update);
0421 
0422 /**
0423  * intel_scu_ipc_dev_simple_command() - Send a simple command
0424  * @scu: Optional SCU IPC instance
0425  * @cmd: Command
0426  * @sub: Sub type
0427  *
0428  * Issue a simple command to the SCU. Do not use this interface if you must
0429  * then access data as any data values may be overwritten by another SCU
0430  * access by the time this function returns.
0431  *
0432  * This function may sleep. Locking for SCU accesses is handled for the
0433  * caller.
0434  */
0435 int intel_scu_ipc_dev_simple_command(struct intel_scu_ipc_dev *scu, int cmd,
0436                      int sub)
0437 {
0438     u32 cmdval;
0439     int err;
0440 
0441     mutex_lock(&ipclock);
0442     if (!scu)
0443         scu = ipcdev;
0444     if (!scu) {
0445         mutex_unlock(&ipclock);
0446         return -ENODEV;
0447     }
0448     scu = ipcdev;
0449     cmdval = sub << 12 | cmd;
0450     ipc_command(scu, cmdval);
0451     err = intel_scu_ipc_check_status(scu);
0452     mutex_unlock(&ipclock);
0453     if (err)
0454         dev_err(&scu->dev, "IPC command %#x failed with %d\n", cmdval, err);
0455     return err;
0456 }
0457 EXPORT_SYMBOL(intel_scu_ipc_dev_simple_command);
0458 
0459 /**
0460  * intel_scu_ipc_dev_command_with_size() - Command with data
0461  * @scu: Optional SCU IPC instance
0462  * @cmd: Command
0463  * @sub: Sub type
0464  * @in: Input data
0465  * @inlen: Input length in bytes
0466  * @size: Input size written to the IPC command register in whatever
0467  *    units (dword, byte) the particular firmware requires. Normally
0468  *    should be the same as @inlen.
0469  * @out: Output data
0470  * @outlen: Output length in bytes
0471  *
0472  * Issue a command to the SCU which involves data transfers. Do the
0473  * data copies under the lock but leave it for the caller to interpret.
0474  */
0475 int intel_scu_ipc_dev_command_with_size(struct intel_scu_ipc_dev *scu, int cmd,
0476                     int sub, const void *in, size_t inlen,
0477                     size_t size, void *out, size_t outlen)
0478 {
0479     size_t outbuflen = DIV_ROUND_UP(outlen, sizeof(u32));
0480     size_t inbuflen = DIV_ROUND_UP(inlen, sizeof(u32));
0481     u32 cmdval, inbuf[4] = {};
0482     int i, err;
0483 
0484     if (inbuflen > 4 || outbuflen > 4)
0485         return -EINVAL;
0486 
0487     mutex_lock(&ipclock);
0488     if (!scu)
0489         scu = ipcdev;
0490     if (!scu) {
0491         mutex_unlock(&ipclock);
0492         return -ENODEV;
0493     }
0494 
0495     memcpy(inbuf, in, inlen);
0496     for (i = 0; i < inbuflen; i++)
0497         ipc_data_writel(scu, inbuf[i], 4 * i);
0498 
0499     cmdval = (size << 16) | (sub << 12) | cmd;
0500     ipc_command(scu, cmdval);
0501     err = intel_scu_ipc_check_status(scu);
0502 
0503     if (!err) {
0504         u32 outbuf[4] = {};
0505 
0506         for (i = 0; i < outbuflen; i++)
0507             outbuf[i] = ipc_data_readl(scu, 4 * i);
0508 
0509         memcpy(out, outbuf, outlen);
0510     }
0511 
0512     mutex_unlock(&ipclock);
0513     if (err)
0514         dev_err(&scu->dev, "IPC command %#x failed with %d\n", cmdval, err);
0515     return err;
0516 }
0517 EXPORT_SYMBOL(intel_scu_ipc_dev_command_with_size);
0518 
0519 /*
0520  * Interrupt handler gets called when ioc bit of IPC_COMMAND_REG set to 1
0521  * When ioc bit is set to 1, caller api must wait for interrupt handler called
0522  * which in turn unlocks the caller api. Currently this is not used
0523  *
0524  * This is edge triggered so we need take no action to clear anything
0525  */
0526 static irqreturn_t ioc(int irq, void *dev_id)
0527 {
0528     struct intel_scu_ipc_dev *scu = dev_id;
0529     int status = ipc_read_status(scu);
0530 
0531     writel(status | IPC_STATUS_IRQ, scu->ipc_base + IPC_STATUS);
0532     complete(&scu->cmd_complete);
0533 
0534     return IRQ_HANDLED;
0535 }
0536 
0537 static void intel_scu_ipc_release(struct device *dev)
0538 {
0539     struct intel_scu_ipc_dev *scu;
0540 
0541     scu = container_of(dev, struct intel_scu_ipc_dev, dev);
0542     if (scu->irq > 0)
0543         free_irq(scu->irq, scu);
0544     iounmap(scu->ipc_base);
0545     release_mem_region(scu->mem.start, resource_size(&scu->mem));
0546     kfree(scu);
0547 }
0548 
0549 /**
0550  * __intel_scu_ipc_register() - Register SCU IPC device
0551  * @parent: Parent device
0552  * @scu_data: Data used to configure SCU IPC
0553  * @owner: Module registering the SCU IPC device
0554  *
0555  * Call this function to register SCU IPC mechanism under @parent.
0556  * Returns pointer to the new SCU IPC device or ERR_PTR() in case of
0557  * failure. The caller may use the returned instance if it needs to do
0558  * SCU IPC calls itself.
0559  */
0560 struct intel_scu_ipc_dev *
0561 __intel_scu_ipc_register(struct device *parent,
0562              const struct intel_scu_ipc_data *scu_data,
0563              struct module *owner)
0564 {
0565     int err;
0566     struct intel_scu_ipc_dev *scu;
0567     void __iomem *ipc_base;
0568 
0569     mutex_lock(&ipclock);
0570     /* We support only one IPC */
0571     if (ipcdev) {
0572         err = -EBUSY;
0573         goto err_unlock;
0574     }
0575 
0576     scu = kzalloc(sizeof(*scu), GFP_KERNEL);
0577     if (!scu) {
0578         err = -ENOMEM;
0579         goto err_unlock;
0580     }
0581 
0582     scu->owner = owner;
0583     scu->dev.parent = parent;
0584     scu->dev.class = &intel_scu_ipc_class;
0585     scu->dev.release = intel_scu_ipc_release;
0586     dev_set_name(&scu->dev, "intel_scu_ipc");
0587 
0588     if (!request_mem_region(scu_data->mem.start, resource_size(&scu_data->mem),
0589                 "intel_scu_ipc")) {
0590         err = -EBUSY;
0591         goto err_free;
0592     }
0593 
0594     ipc_base = ioremap(scu_data->mem.start, resource_size(&scu_data->mem));
0595     if (!ipc_base) {
0596         err = -ENOMEM;
0597         goto err_release;
0598     }
0599 
0600     scu->ipc_base = ipc_base;
0601     scu->mem = scu_data->mem;
0602     scu->irq = scu_data->irq;
0603     init_completion(&scu->cmd_complete);
0604 
0605     if (scu->irq > 0) {
0606         err = request_irq(scu->irq, ioc, 0, "intel_scu_ipc", scu);
0607         if (err)
0608             goto err_unmap;
0609     }
0610 
0611     /*
0612      * After this point intel_scu_ipc_release() takes care of
0613      * releasing the SCU IPC resources once refcount drops to zero.
0614      */
0615     err = device_register(&scu->dev);
0616     if (err) {
0617         put_device(&scu->dev);
0618         goto err_unlock;
0619     }
0620 
0621     /* Assign device at last */
0622     ipcdev = scu;
0623     mutex_unlock(&ipclock);
0624 
0625     return scu;
0626 
0627 err_unmap:
0628     iounmap(ipc_base);
0629 err_release:
0630     release_mem_region(scu_data->mem.start, resource_size(&scu_data->mem));
0631 err_free:
0632     kfree(scu);
0633 err_unlock:
0634     mutex_unlock(&ipclock);
0635 
0636     return ERR_PTR(err);
0637 }
0638 EXPORT_SYMBOL_GPL(__intel_scu_ipc_register);
0639 
0640 /**
0641  * intel_scu_ipc_unregister() - Unregister SCU IPC
0642  * @scu: SCU IPC handle
0643  *
0644  * This unregisters the SCU IPC device and releases the acquired
0645  * resources once the refcount goes to zero.
0646  */
0647 void intel_scu_ipc_unregister(struct intel_scu_ipc_dev *scu)
0648 {
0649     mutex_lock(&ipclock);
0650     if (!WARN_ON(!ipcdev)) {
0651         ipcdev = NULL;
0652         device_unregister(&scu->dev);
0653     }
0654     mutex_unlock(&ipclock);
0655 }
0656 EXPORT_SYMBOL_GPL(intel_scu_ipc_unregister);
0657 
0658 static void devm_intel_scu_ipc_unregister(struct device *dev, void *res)
0659 {
0660     struct intel_scu_ipc_devres *dr = res;
0661     struct intel_scu_ipc_dev *scu = dr->scu;
0662 
0663     intel_scu_ipc_unregister(scu);
0664 }
0665 
0666 /**
0667  * __devm_intel_scu_ipc_register() - Register managed SCU IPC device
0668  * @parent: Parent device
0669  * @scu_data: Data used to configure SCU IPC
0670  * @owner: Module registering the SCU IPC device
0671  *
0672  * Call this function to register managed SCU IPC mechanism under
0673  * @parent. Returns pointer to the new SCU IPC device or ERR_PTR() in
0674  * case of failure. The caller may use the returned instance if it needs
0675  * to do SCU IPC calls itself.
0676  */
0677 struct intel_scu_ipc_dev *
0678 __devm_intel_scu_ipc_register(struct device *parent,
0679                   const struct intel_scu_ipc_data *scu_data,
0680                   struct module *owner)
0681 {
0682     struct intel_scu_ipc_devres *dr;
0683     struct intel_scu_ipc_dev *scu;
0684 
0685     dr = devres_alloc(devm_intel_scu_ipc_unregister, sizeof(*dr), GFP_KERNEL);
0686     if (!dr)
0687         return NULL;
0688 
0689     scu = __intel_scu_ipc_register(parent, scu_data, owner);
0690     if (IS_ERR(scu)) {
0691         devres_free(dr);
0692         return scu;
0693     }
0694 
0695     dr->scu = scu;
0696     devres_add(parent, dr);
0697 
0698     return scu;
0699 }
0700 EXPORT_SYMBOL_GPL(__devm_intel_scu_ipc_register);
0701 
0702 static int __init intel_scu_ipc_init(void)
0703 {
0704     return class_register(&intel_scu_ipc_class);
0705 }
0706 subsys_initcall(intel_scu_ipc_init);
0707 
0708 static void __exit intel_scu_ipc_exit(void)
0709 {
0710     class_unregister(&intel_scu_ipc_class);
0711 }
0712 module_exit(intel_scu_ipc_exit);