diff mbox

[5/5] intel_scu_ipc: move ipclock to struct intel_scu_ipc_dev

Message ID 1444399896-78599-5-git-send-email-andriy.shevchenko@linux.intel.com (mailing list archive)
State Changes Requested, archived
Headers show

Commit Message

Andy Shevchenko Oct. 9, 2015, 2:11 p.m. UTC
No need to use global variable for a mutex. Move it to be a member of SCU IPC
device structure.

While here, protect dev member assignment in ->remove() since user may
potentially call unbind from sysfs even if driver is built-in.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/platform/x86/intel_scu_ipc.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)

Comments

Andy Shevchenko Oct. 9, 2015, 3:21 p.m. UTC | #1
On Fri, 2015-10-09 at 17:11 +0300, Andy Shevchenko wrote:
> No need to use global variable for a mutex. Move it to be a member of 
> SCU IPC
> device structure.

This one is wrong, sorry.

> 
> While here, protect dev member assignment in ->remove() since user 
> may
> potentially call unbind from sysfs even if driver is built-in.

This part is still right.

> 
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
>  drivers/platform/x86/intel_scu_ipc.c | 34 +++++++++++++++++++-------
> --------
>  1 file changed, 19 insertions(+), 15 deletions(-)
> 
> diff --git a/drivers/platform/x86/intel_scu_ipc.c 
> b/drivers/platform/x86/intel_scu_ipc.c
> index 9de2029..16f1621 100644
> --- a/drivers/platform/x86/intel_scu_ipc.c
> +++ b/drivers/platform/x86/intel_scu_ipc.c
> @@ -92,11 +92,13 @@ static struct intel_scu_ipc_pdata_t 
> intel_scu_ipc_tangier_pdata = {
>  	.irq_mode = 0,
>  };
>  
> +/* @lock: lock used to prevent multiple call to SCU */
>  struct intel_scu_ipc_dev {
>  	struct device *dev;
>  	void __iomem *ipc_base;
>  	void __iomem *i2c_base;
>  	struct completion cmd_complete;
> +	struct mutex lock;
>  	u8 irq_mode;
>  };
>  
> @@ -112,8 +114,6 @@ static struct intel_scu_ipc_dev  ipcdev; /* Only 
> one for now */
>  #define IPC_I2C_CNTRL_ADDR	0
>  #define I2C_DATA_ADDR		0x04
>  
> -static DEFINE_MUTEX(ipclock); /* lock used to prevent multiple call 
> to SCU */
> -
>  /*
>   * Send ipc command
>   * Command Register (Write Only):
> @@ -222,10 +222,10 @@ static int pwr_reg_rdwr(u16 *addr, u8 *data, 
> u32 count, u32 op, u32 id)
>  
>  	memset(cbuf, 0, sizeof(cbuf));
>  
> -	mutex_lock(&ipclock);
> +	mutex_lock(&scu->lock);
>  
>  	if (scu->dev == NULL) {
> -		mutex_unlock(&ipclock);
> +		mutex_unlock(&scu->lock);
>  		return -ENODEV;
>  	}
>  
> @@ -258,7 +258,7 @@ static int pwr_reg_rdwr(u16 *addr, u8 *data, u32 
> count, u32 op, u32 id)
>  		for (nc = 0; nc < count; nc++)
>  			data[nc] = ipc_data_readb(scu, nc);
>  	}
> -	mutex_unlock(&ipclock);
> +	mutex_unlock(&scu->lock);
>  	return err;
>  }
>  
> @@ -440,14 +440,14 @@ int intel_scu_ipc_simple_command(int cmd, int 
> sub)
>  	struct intel_scu_ipc_dev *scu = &ipcdev;
>  	int err;
>  
> -	mutex_lock(&ipclock);
> +	mutex_lock(&scu->lock);
>  	if (scu->dev == NULL) {
> -		mutex_unlock(&ipclock);
> +		mutex_unlock(&scu->lock);
>  		return -ENODEV;
>  	}
>  	ipc_command(scu, sub << 12 | cmd);
>  	err = intel_scu_ipc_check_status(scu);
> -	mutex_unlock(&ipclock);
> +	mutex_unlock(&scu->lock);
>  	return err;
>  }
>  EXPORT_SYMBOL(intel_scu_ipc_simple_command);
> @@ -470,9 +470,9 @@ int intel_scu_ipc_command(int cmd, int sub, u32 
> *in, int inlen,
>  	struct intel_scu_ipc_dev *scu = &ipcdev;
>  	int i, err;
>  
> -	mutex_lock(&ipclock);
> +	mutex_lock(&scu->lock);
>  	if (scu->dev == NULL) {
> -		mutex_unlock(&ipclock);
> +		mutex_unlock(&scu->lock);
>  		return -ENODEV;
>  	}
>  
> @@ -487,7 +487,7 @@ int intel_scu_ipc_command(int cmd, int sub, u32 
> *in, int inlen,
>  			*out++ = ipc_data_readl(scu, 4 * i);
>  	}
>  
> -	mutex_unlock(&ipclock);
> +	mutex_unlock(&scu->lock);
>  	return err;
>  }
>  EXPORT_SYMBOL(intel_scu_ipc_command);
> @@ -513,9 +513,9 @@ int intel_scu_ipc_i2c_cntrl(u32 addr, u32 *data)
>  	struct intel_scu_ipc_dev *scu = &ipcdev;
>  	u32 cmd = 0;
>  
> -	mutex_lock(&ipclock);
> +	mutex_lock(&scu->lock);
>  	if (scu->dev == NULL) {
> -		mutex_unlock(&ipclock);
> +		mutex_unlock(&scu->lock);
>  		return -ENODEV;
>  	}
>  	cmd = (addr >> 24) & 0xFF;
> @@ -532,10 +532,10 @@ int intel_scu_ipc_i2c_cntrl(u32 addr, u32 
> *data)
>  		dev_err(scu->dev,
>  			"intel_scu_ipc: I2C INVALID_CMD = 0x%x\n", 
> cmd);
>  
> -		mutex_unlock(&ipclock);
> +		mutex_unlock(&scu->lock);
>  		return -EIO;
>  	}
> -	mutex_unlock(&ipclock);
> +	mutex_unlock(&scu->lock);
>  	return 0;
>  }
>  EXPORT_SYMBOL(intel_scu_ipc_i2c_cntrl);
> @@ -593,6 +593,7 @@ static int ipc_probe(struct pci_dev *pdev, const 
> struct pci_device_id *id)
>  		return err;
>  
>  	init_completion(&scu->cmd_complete);
> +	mutex_init(&scu->lock);
>  
>  	err = devm_request_irq(&pdev->dev, pdev->irq, ioc, 0, 
> "intel_scu_ipc",
>  			       scu);
> @@ -625,7 +626,10 @@ static void ipc_remove(struct pci_dev *pdev)
>  {
>  	struct intel_scu_ipc_dev *scu = pci_get_drvdata(pdev);
>  
> +	mutex_lock(&scu->lock);
>  	scu->dev = NULL;
> +	mutex_unlock(&scu->lock);
> +
>  	iounmap(scu->i2c_base);
>  	intel_scu_devices_destroy();
>  }
Darren Hart Oct. 11, 2015, 4:43 a.m. UTC | #2
On Fri, Oct 09, 2015 at 06:21:37PM +0300, Andy Shevchenko wrote:
> On Fri, 2015-10-09 at 17:11 +0300, Andy Shevchenko wrote:
> > No need to use global variable for a mutex. Move it to be a member of 
> > SCU IPC
> > device structure.
> 
> This one is wrong, sorry.
> 
> > 
> > While here, protect dev member assignment in ->remove() since user 
> > may
> > potentially call unbind from sysfs even if driver is built-in.
> 
> This part is still right.

OK.

With my minor comment to 1/5 and this change, care to send a v2?
Andy Shevchenko Oct. 12, 2015, 8:02 a.m. UTC | #3
On Sat, 2015-10-10 at 21:43 -0700, Darren Hart wrote:
> On Fri, Oct 09, 2015 at 06:21:37PM +0300, Andy Shevchenko wrote:
> > On Fri, 2015-10-09 at 17:11 +0300, Andy Shevchenko wrote:
> > > No need to use global variable for a mutex. Move it to be a 
> > > member of 
> > > SCU IPC
> > > device structure.
> > 
> > This one is wrong, sorry.
> > 
> > > 
> > > While here, protect dev member assignment in ->remove() since 
> > > user 
> > > may
> > > potentially call unbind from sysfs even if driver is built-in.
> > 
> > This part is still right.
> 
> OK.
> 
> With my minor comment to 1/5 and this change, care to send a v2?

Will do later this week.
Also I will update message of the patch 4 as you suggested.
diff mbox

Patch

diff --git a/drivers/platform/x86/intel_scu_ipc.c b/drivers/platform/x86/intel_scu_ipc.c
index 9de2029..16f1621 100644
--- a/drivers/platform/x86/intel_scu_ipc.c
+++ b/drivers/platform/x86/intel_scu_ipc.c
@@ -92,11 +92,13 @@  static struct intel_scu_ipc_pdata_t intel_scu_ipc_tangier_pdata = {
 	.irq_mode = 0,
 };
 
+/* @lock: lock used to prevent multiple call to SCU */
 struct intel_scu_ipc_dev {
 	struct device *dev;
 	void __iomem *ipc_base;
 	void __iomem *i2c_base;
 	struct completion cmd_complete;
+	struct mutex lock;
 	u8 irq_mode;
 };
 
@@ -112,8 +114,6 @@  static struct intel_scu_ipc_dev  ipcdev; /* Only one for now */
 #define IPC_I2C_CNTRL_ADDR	0
 #define I2C_DATA_ADDR		0x04
 
-static DEFINE_MUTEX(ipclock); /* lock used to prevent multiple call to SCU */
-
 /*
  * Send ipc command
  * Command Register (Write Only):
@@ -222,10 +222,10 @@  static int pwr_reg_rdwr(u16 *addr, u8 *data, u32 count, u32 op, u32 id)
 
 	memset(cbuf, 0, sizeof(cbuf));
 
-	mutex_lock(&ipclock);
+	mutex_lock(&scu->lock);
 
 	if (scu->dev == NULL) {
-		mutex_unlock(&ipclock);
+		mutex_unlock(&scu->lock);
 		return -ENODEV;
 	}
 
@@ -258,7 +258,7 @@  static int pwr_reg_rdwr(u16 *addr, u8 *data, u32 count, u32 op, u32 id)
 		for (nc = 0; nc < count; nc++)
 			data[nc] = ipc_data_readb(scu, nc);
 	}
-	mutex_unlock(&ipclock);
+	mutex_unlock(&scu->lock);
 	return err;
 }
 
@@ -440,14 +440,14 @@  int intel_scu_ipc_simple_command(int cmd, int sub)
 	struct intel_scu_ipc_dev *scu = &ipcdev;
 	int err;
 
-	mutex_lock(&ipclock);
+	mutex_lock(&scu->lock);
 	if (scu->dev == NULL) {
-		mutex_unlock(&ipclock);
+		mutex_unlock(&scu->lock);
 		return -ENODEV;
 	}
 	ipc_command(scu, sub << 12 | cmd);
 	err = intel_scu_ipc_check_status(scu);
-	mutex_unlock(&ipclock);
+	mutex_unlock(&scu->lock);
 	return err;
 }
 EXPORT_SYMBOL(intel_scu_ipc_simple_command);
@@ -470,9 +470,9 @@  int intel_scu_ipc_command(int cmd, int sub, u32 *in, int inlen,
 	struct intel_scu_ipc_dev *scu = &ipcdev;
 	int i, err;
 
-	mutex_lock(&ipclock);
+	mutex_lock(&scu->lock);
 	if (scu->dev == NULL) {
-		mutex_unlock(&ipclock);
+		mutex_unlock(&scu->lock);
 		return -ENODEV;
 	}
 
@@ -487,7 +487,7 @@  int intel_scu_ipc_command(int cmd, int sub, u32 *in, int inlen,
 			*out++ = ipc_data_readl(scu, 4 * i);
 	}
 
-	mutex_unlock(&ipclock);
+	mutex_unlock(&scu->lock);
 	return err;
 }
 EXPORT_SYMBOL(intel_scu_ipc_command);
@@ -513,9 +513,9 @@  int intel_scu_ipc_i2c_cntrl(u32 addr, u32 *data)
 	struct intel_scu_ipc_dev *scu = &ipcdev;
 	u32 cmd = 0;
 
-	mutex_lock(&ipclock);
+	mutex_lock(&scu->lock);
 	if (scu->dev == NULL) {
-		mutex_unlock(&ipclock);
+		mutex_unlock(&scu->lock);
 		return -ENODEV;
 	}
 	cmd = (addr >> 24) & 0xFF;
@@ -532,10 +532,10 @@  int intel_scu_ipc_i2c_cntrl(u32 addr, u32 *data)
 		dev_err(scu->dev,
 			"intel_scu_ipc: I2C INVALID_CMD = 0x%x\n", cmd);
 
-		mutex_unlock(&ipclock);
+		mutex_unlock(&scu->lock);
 		return -EIO;
 	}
-	mutex_unlock(&ipclock);
+	mutex_unlock(&scu->lock);
 	return 0;
 }
 EXPORT_SYMBOL(intel_scu_ipc_i2c_cntrl);
@@ -593,6 +593,7 @@  static int ipc_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		return err;
 
 	init_completion(&scu->cmd_complete);
+	mutex_init(&scu->lock);
 
 	err = devm_request_irq(&pdev->dev, pdev->irq, ioc, 0, "intel_scu_ipc",
 			       scu);
@@ -625,7 +626,10 @@  static void ipc_remove(struct pci_dev *pdev)
 {
 	struct intel_scu_ipc_dev *scu = pci_get_drvdata(pdev);
 
+	mutex_lock(&scu->lock);
 	scu->dev = NULL;
+	mutex_unlock(&scu->lock);
+
 	iounmap(scu->i2c_base);
 	intel_scu_devices_destroy();
 }