platform/x86: intel_scu_ipc: Avoid working around IO and cleanups
v3 cover platform/x86: intel_scu_ipc: Avoid working around IO and cleanups
The first patch avoids using a workaround for IO (which seems unneeded). The rest is a batch of cleanups. Has been tested on Intel Merrifield (thanks, Ferry).
v3:
- split patches 1 & 2 to two patches each (Ilpo)
- preserve a comment for reading buffer (Ilpo)
- inherited tags for new patches as the result is the same code wise
v2:
- dropped stray change (Mika)
- elaborated conditional changes in the commit message (Mika)
- gathered tags (Mika, Ferry)
Andy Shevchenko (5):
platform/x86: intel_scu_ipc: Unify the flow in pwr_reg_rdwr() platform/x86: intel_scu_ipc: Replace workaround by 32-bit IO platform/x86: intel_scu_ipc: Simplify code with cleanup helpers platform/x86: intel_scu_ipc: Convert to check for errors first platform/x86: intel_scu_ipc: Save a copy of the entire struct
intel_scu_ipc_data
drivers/platform/x86/intel_scu_ipc.c | 142 ++++++++++++--------------- 1 file changed, 61 insertions(+), 81 deletions(-)
Signature · 1 line
2.43.0.rc1.1336.g36b5255a03ac
PATCH v3 1/5 platform/x86: intel_scu_ipc: Unify the flow in pwr_reg_rdwr()
Unify the flow in pwr_reg_rdwr() with intel_scu_ipc_dev_command_with_size(). While read buffer is not currently being used by the callers of pwr_reg_rdwr(), this might change in the future. For the existing code it's a harmless change because the read data for the write operation is simply discarded.
--- drivers/platform/x86/intel_scu_ipc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/platform/x86/intel_scu_ipc.c +1 -1
@@ -325,7 +325,7 @@ static int pwr_reg_rdwr(struct intel_scu_ipc_dev *scu, u16 *addr, u8 *data, } err =intel_scu_ipc_check_status (scu); -if (!err && id ==IPC_CMD_PCNTRL_R ) {/* Read rbuf */ +if (!err) {/* Read rbuf */ /* Workaround: values are read as 0 without memcpy_fromio */ memcpy_fromio (cbuf, scu->ipc_base +0x90 ,16 );for (nc =0 ; nc < count; nc++)
Signature · 1 line
2.43.0.rc1.1336.g36b5255a03ac
PATCH v3 2/5 platform/x86: intel_scu_ipc: Replace workaround by 32-bit IO
In reply to Andy ShevchenkoThe theory is that the so called workaround in pwr_reg_rdwr() is the actual reader of the data in 32-bit chunks. For some reason the 8-bit IO won't fail after that. Replace the workaround by using 32-bit IO explicitly and then memcpy() as much data as was requested by the user. The same approach is already in use in intel_scu_ipc_dev_command_with_size().
--- drivers/platform/x86/intel_scu_ipc.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-)
drivers/platform/x86/intel_scu_ipc.c +3 -10
@@ -217,12 +217,6 @@ static inline u8 ipc_read_status(struct intel_scu_ipc_dev *scu)return __raw_readl (scu->ipc_base +IPC_STATUS ); } -/* Read ipc byte data */ -static inline u8 ipc_data_readb (struct intel_scu_ipc_dev *scu,u32 offset) -{ -return readb (scu->ipc_base +IPC_READ_BUFFER + offset); -} -/* Read ipc u32 data */ static inline u32 ipc_data_readl (struct intel_scu_ipc_dev *scu,u32 offset) {
@@ -326,10 +320,9 @@ static int pwr_reg_rdwr(struct intel_scu_ipc_dev *scu, u16 *addr, u8 *data, err =intel_scu_ipc_check_status (scu);if (!err) {/* Read rbuf */ -/* Workaround: values are read as 0 without memcpy_fromio */ -memcpy_fromio (cbuf, scu->ipc_base +0x90 ,16 ); -for (nc =0 ; nc < count; nc++) - data[nc] =ipc_data_readb (scu, nc); +for (nc =0 , offset =0 ; nc <4 ; nc++, offset +=4 ) + wbuf[nc] =ipc_data_readl (scu, offset); +memcpy (data, wbuf, count); }mutex_unlock (&ipclock);return err;
Signature · 1 line
2.43.0.rc1.1336.g36b5255a03ac
PATCH v3 3/5 platform/x86: intel_scu_ipc: Simplify code with cleanup helpers
In reply to Andy ShevchenkoUse macros defined in linux/cleanup.h to automate resource lifetime control in the driver. The byproduct of this change is that the negative conditionals are swapped by positive ones that are being attached to the respective calls, hence the code uses the regular pattern, i.e. checking for the error first.
--- drivers/platform/x86/intel_scu_ipc.c | 65 +++++++++++----------------- 1 file changed, 25 insertions(+), 40 deletions(-)
drivers/platform/x86/intel_scu_ipc.c +25 -40
@@ -13,6 +13,7 @@* along with other APIs. */ +#include <linux/cleanup.h> #include <linux/delay.h> #include <linux/device.h> #include <linux/errno.h>
@@ -99,23 +100,21 @@ static struct class intel_scu_ipc_class = {*/ struct intel_scu_ipc_dev *intel_scu_ipc_dev_get (void ) { -struct intel_scu_ipc_dev *scu =NULL ; +guard (mutex)(&ipclock); -mutex_lock (&ipclock);if (ipcdev) {get_device (&ipcdev->dev);/* * Prevent the IPC provider from being unloaded while it * is being used. */ -if (!try_module_get (ipcdev->owner)) -put_device (&ipcdev->dev); -else - scu = ipcdev; +if (try_module_get (ipcdev->owner)) +return ipcdev; + +put_device (&ipcdev->dev); } -mutex_unlock (&ipclock); -return scu; +return NULL ; }EXPORT_SYMBOL_GPL (intel_scu_ipc_dev_get);
@@ -289,12 +288,11 @@ static int pwr_reg_rdwr(struct intel_scu_ipc_dev *scu, u16 *addr, u8 *data,memset (cbuf,0 ,sizeof (cbuf)); -mutex_lock (&ipclock); +guard (mutex)(&ipclock); + scu =intel_scu_ipc_get (scu); -if (IS_ERR (scu)) { -mutex_unlock (&ipclock); +if (IS_ERR (scu))return PTR_ERR (scu); - }for (nc =0 ; nc < count; nc++, offset +=2 ) { cbuf[offset] = addr[nc];
@@ -324,7 +322,6 @@ static int pwr_reg_rdwr(struct intel_scu_ipc_dev *scu, u16 *addr, u8 *data, wbuf[nc] =ipc_data_readl (scu, offset);memcpy (data, wbuf, count); } -mutex_unlock (&ipclock);return err; }
@@ -446,17 +443,15 @@ int intel_scu_ipc_dev_simple_command(struct intel_scu_ipc_dev *scu, int cmd,u32 cmdval;int err; -mutex_lock (&ipclock); +guard (mutex)(&ipclock); + scu =intel_scu_ipc_get (scu); -if (IS_ERR (scu)) { -mutex_unlock (&ipclock); +if (IS_ERR (scu))return PTR_ERR (scu); - } cmdval = sub <<12 | cmd;ipc_command (scu, cmdval); err =intel_scu_ipc_check_status (scu); -mutex_unlock (&ipclock);if (err)dev_err (&scu->dev,"IPC command %#x failed with %d\n" , cmdval, err);return err;
@@ -491,12 +486,11 @@ int intel_scu_ipc_dev_command_with_size(struct intel_scu_ipc_dev *scu, int cmd,if (inbuflen >4 || outbuflen >4 )return -EINVAL ; -mutex_lock (&ipclock); +guard (mutex)(&ipclock); + scu =intel_scu_ipc_get (scu); -if (IS_ERR (scu)) { -mutex_unlock (&ipclock); +if (IS_ERR (scu))return PTR_ERR (scu); - }memcpy (inbuf, in, inlen);for (i =0 ; i < inbuflen; i++)
@@ -515,7 +509,6 @@ int intel_scu_ipc_dev_command_with_size(struct intel_scu_ipc_dev *scu, int cmd,memcpy (out, outbuf, outlen); } -mutex_unlock (&ipclock);if (err)dev_err (&scu->dev,"IPC command %#x failed with %d\n" , cmdval, err);return err;
@@ -572,18 +565,15 @@ __intel_scu_ipc_register(struct device *parent,struct intel_scu_ipc_dev *scu;void __iomem *ipc_base; -mutex_lock (&ipclock); +guard (mutex)(&ipclock); +/* We support only one IPC */ -if (ipcdev) { - err = -EBUSY ; - goto err_unlock; - } +if (ipcdev) +return ERR_PTR (-EBUSY ); scu =kzalloc (sizeof (*scu),GFP_KERNEL ); -if (!scu) { - err = -ENOMEM ; - goto err_unlock; - } +if (!scu) +return ERR_PTR (-ENOMEM ); scu->owner = owner; scu->dev.parent = parent;
@@ -621,13 +611,11 @@ __intel_scu_ipc_register(struct device *parent, err =device_register (&scu->dev);if (err) {put_device (&scu->dev); - goto err_unlock; +return ERR_PTR (err); }/* Assign device at last */ ipcdev = scu; -mutex_unlock (&ipclock); -return scu; err_unmap:
@@ -636,9 +624,6 @@ __intel_scu_ipc_register(struct device *parent,release_mem_region (scu_data->mem.start,resource_size (&scu_data->mem)); err_free:kfree (scu); -err_unlock: -mutex_unlock (&ipclock); -return ERR_PTR (err); }EXPORT_SYMBOL_GPL (__intel_scu_ipc_register);
@@ -652,12 +637,12 @@ EXPORT_SYMBOL_GPL(__intel_scu_ipc_register);*/ void intel_scu_ipc_unregister (struct intel_scu_ipc_dev *scu) { -mutex_lock (&ipclock); +guard (mutex)(&ipclock); +if (!WARN_ON (!ipcdev)) { ipcdev =NULL ;device_unregister (&scu->dev); } -mutex_unlock (&ipclock); }EXPORT_SYMBOL_GPL (intel_scu_ipc_unregister);
Signature · 1 line
2.43.0.rc1.1336.g36b5255a03ac
PATCH v3 4/5 platform/x86: intel_scu_ipc: Convert to check for errors first
In reply to Andy ShevchenkoConvert to use usual pattern, i.e. check for errors first. This makes code less indented and easier to read after all.
--- drivers/platform/x86/intel_scu_ipc.c | 37 ++++++++++++++-------------- 1 file changed, 19 insertions(+), 18 deletions(-)
drivers/platform/x86/intel_scu_ipc.c +19 -18
@@ -317,12 +317,15 @@ static int pwr_reg_rdwr(struct intel_scu_ipc_dev *scu, u16 *addr, u8 *data, } err =intel_scu_ipc_check_status (scu); -if (!err) {/* Read rbuf */ -for (nc =0 , offset =0 ; nc <4 ; nc++, offset +=4 ) - wbuf[nc] =ipc_data_readl (scu, offset); -memcpy (data, wbuf, count); - } -return err; +if (err) +return err; + +/* Read rbuf */ +for (nc =0 , offset =0 ; nc <4 ; nc++, offset +=4 ) + wbuf[nc] =ipc_data_readl (scu, offset); +memcpy (data, wbuf, count); + +return 0 ; }/**
@@ -480,7 +483,7 @@ int intel_scu_ipc_dev_command_with_size(struct intel_scu_ipc_dev *scu, int cmd, {size_t outbuflen =DIV_ROUND_UP (outlen,sizeof (u32));size_t inbuflen =DIV_ROUND_UP (inlen,sizeof (u32)); -u32 cmdval, inbuf[4 ] = {}; +u32 cmdval, inbuf[4 ] = {}, outbuf[4 ] = {};int i, err;if (inbuflen >4 || outbuflen >4 )
@@ -499,19 +502,17 @@ int intel_scu_ipc_dev_command_with_size(struct intel_scu_ipc_dev *scu, int cmd, cmdval = (size <<16 ) | (sub <<12 ) | cmd;ipc_command (scu, cmdval); err =intel_scu_ipc_check_status (scu); - -if (!err) { -u32 outbuf[4 ] = {}; - -for (i =0 ; i < outbuflen; i++) - outbuf[i] =ipc_data_readl (scu,4 * i); - -memcpy (out, outbuf, outlen); +if (err) { +dev_err (&scu->dev,"IPC command %#x failed with %d\n" , cmdval, err); +return err; } -if (err) -dev_err (&scu->dev,"IPC command %#x failed with %d\n" , cmdval, err); -return err; +for (i =0 ; i < outbuflen; i++) + outbuf[i] =ipc_data_readl (scu,4 * i); + +memcpy (out, outbuf, outlen); + +return 0 ; }EXPORT_SYMBOL (intel_scu_ipc_dev_command_with_size);
Signature · 1 line
2.43.0.rc1.1336.g36b5255a03ac
PATCH v3 5/5 platform/x86: intel_scu_ipc: Save a copy of the entire struct intel_scu_ipc_data
In reply to Andy ShevchenkoSave a copy of the entire struct intel_scu_ipc_data for easier maintenance in case of expanding (adding new members become simpler).
--- drivers/platform/x86/intel_scu_ipc.c | 33 ++++++++++++++-------------- 1 file changed, 17 insertions(+), 16 deletions(-)
drivers/platform/x86/intel_scu_ipc.c +17 -16
@@ -57,11 +57,11 @@struct intel_scu_ipc_dev {struct device dev; -struct resource mem;struct module *owner; -int irq;void __iomem *ipc_base;struct completion cmd_complete; + +struct intel_scu_ipc_data data; };#define IPC_STATUS 0x04
@@ -255,7 +255,7 @@ static inline int ipc_wait_for_interrupt(struct intel_scu_ipc_dev *scu)static int intel_scu_ipc_check_status (struct intel_scu_ipc_dev *scu) { -return scu->irq >0 ?ipc_wait_for_interrupt (scu) :busy_loop (scu); +return scu->data.irq >0 ?ipc_wait_for_interrupt (scu) :busy_loop (scu); }static struct intel_scu_ipc_dev *intel_scu_ipc_get (struct intel_scu_ipc_dev *scu)
@@ -536,13 +536,13 @@ static irqreturn_t ioc(int irq, void *dev_id)static void intel_scu_ipc_release (struct device *dev) { -struct intel_scu_ipc_dev *scu; + struct intel_scu_ipc_dev *scu = container_of(dev, struct intel_scu_ipc_dev, dev); +struct intel_scu_ipc_data *data = &scu->data; - scu = container_of(dev, struct intel_scu_ipc_dev, dev); -if (scu->irq >0 ) -free_irq (scu->irq, scu); +if (data->irq >0 ) +free_irq (data->irq, scu);iounmap (scu->ipc_base); -release_mem_region (scu->mem.start,resource_size (&scu->mem)); +release_mem_region (data->mem.start,resource_size (&data->mem));kfree (scu); }
@@ -563,6 +563,7 @@ __intel_scu_ipc_register(struct device *parent, struct module *owner) {int err; +struct intel_scu_ipc_data *data;struct intel_scu_ipc_dev *scu;void __iomem *ipc_base;
@@ -581,25 +582,25 @@ __intel_scu_ipc_register(struct device *parent, scu->dev.class = &intel_scu_ipc_class; scu->dev.release = intel_scu_ipc_release; -if (!request_mem_region (scu_data->mem.start,resource_size (&scu_data->mem), -"intel_scu_ipc" )) { +memcpy (&scu->data, scu_data,sizeof (scu->data)); + data = &scu->data; + +if (!request_mem_region (data->mem.start,resource_size (&data->mem),"intel_scu_ipc" )) { err = -EBUSY ; goto err_free; } - ipc_base =ioremap (scu_data->mem.start,resource_size (&scu_data->mem)); + ipc_base =ioremap (data->mem.start,resource_size (&data->mem));if (!ipc_base) { err = -ENOMEM ; goto err_release; } scu->ipc_base = ipc_base; - scu->mem = scu_data->mem; - scu->irq = scu_data->irq;init_completion (&scu->cmd_complete); -if (scu->irq >0 ) { - err =request_irq (scu->irq, ioc,0 ,"intel_scu_ipc" , scu); +if (data->irq >0 ) { + err =request_irq (data->irq, ioc,0 ,"intel_scu_ipc" , scu);if (err) goto err_unmap; }
@@ -622,7 +623,7 @@ __intel_scu_ipc_register(struct device *parent, err_unmap:iounmap (ipc_base); err_release: -release_mem_region (scu_data->mem.start,resource_size (&scu_data->mem)); +release_mem_region (data->mem.start,resource_size (&data->mem)); err_free:kfree (scu);return ERR_PTR (err);
Signature · 1 line
2.43.0.rc1.1336.g36b5255a03ac
Ilpo Järvinen
maintainerQuoted 10 lines · Andy Shevchenko
The first patch avoids using a workaround for IO (which seems unneeded). The rest is a batch of cleanups. Has been tested on Intel Merrifield (thanks, Ferry).
v3:
- split patches 1 & 2 to two patches each (Ilpo)
- preserve a comment for reading buffer (Ilpo)
- inherited tags for new patches as the result is the same code wise[...]
Thank you for your contribution, it has been applied to my local review-ilpo branch. Note it will show up in the public platform-drivers-x86/review-ilpo branch only once I've pushed my local branch there, which might take a while.
The list of commits applied:
[1/5] platform/x86: intel_scu_ipc: Unify the flow in pwr_reg_rdwr()
commit: 6bd35252102e2b79acb54c74527c81c09f690759
[2/5] platform/x86: intel_scu_ipc: Replace workaround by 32-bit IO
commit: 8b142950a5a718d55b09f08900306c75ceed2cf9
[3/5] platform/x86: intel_scu_ipc: Simplify code with cleanup helpers
commit: ce44b96261a6af318a9e28a16f0d9749e89179a6
[4/5] platform/x86: intel_scu_ipc: Convert to check for errors first
commit: acf1b04c6890aef9bb356195221e75ce3862f84e
[5/5] platform/x86: intel_scu_ipc: Save a copy of the entire struct intel_scu_ipc_data
commit: f02fcc7c12a61cfcb65937a570915672fa33ce73--
i.