diff mbox series

mmc: mtk-sd: Improve data type in msdc_timeout_cal()

Message ID 20240815234602.59684-1-thorsten.blum@toblux.com (mailing list archive)
State New
Headers show
Series mmc: mtk-sd: Improve data type in msdc_timeout_cal() | expand

Commit Message

Thorsten Blum Aug. 15, 2024, 11:46 p.m. UTC
The local variable clk_ns uses at most 32 bits and can be a u32.

Since do_div() casts the divisor to u32 anyway, changing the data type
of clk_ns to u32 also removes the following Coccinelle/coccicheck
warning reported by do_div.cocci:

  WARNING: do_div() does a 64-by-32 division, please consider using div64_u64 instead

Use max_t(u32,,) to simplify the code and improve its readability.

Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
---
 drivers/mmc/host/mtk-sd.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

Comments

kernel test robot Aug. 17, 2024, 11:36 a.m. UTC | #1
Hi Thorsten,

kernel test robot noticed the following build errors:

[auto build test ERROR on linus/master]
[also build test ERROR on v6.11-rc3 next-20240816]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Thorsten-Blum/mmc-mtk-sd-Improve-data-type-in-msdc_timeout_cal/20240816-102400
base:   linus/master
patch link:    https://lore.kernel.org/r/20240815234602.59684-1-thorsten.blum%40toblux.com
patch subject: [PATCH] mmc: mtk-sd: Improve data type in msdc_timeout_cal()
config: hexagon-allmodconfig (https://download.01.org/0day-ci/archive/20240817/202408171942.A1TKwW7h-lkp@intel.com/config)
compiler: clang version 20.0.0git (https://github.com/llvm/llvm-project 26670e7fa4f032a019d23d56c6a02926e854e8af)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240817/202408171942.A1TKwW7h-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202408171942.A1TKwW7h-lkp@intel.com/

All error/warnings (new ones prefixed by >>):

   In file included from drivers/mmc/host/mtk-sd.c:11:
   In file included from include/linux/dma-mapping.h:11:
   In file included from include/linux/scatterlist.h:8:
   In file included from include/linux/mm.h:2228:
   include/linux/vmstat.h:514:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
     514 |         return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
         |                               ~~~~~~~~~~~ ^ ~~~
   In file included from drivers/mmc/host/mtk-sd.c:11:
   In file included from include/linux/dma-mapping.h:11:
   In file included from include/linux/scatterlist.h:9:
   In file included from arch/hexagon/include/asm/io.h:328:
   include/asm-generic/io.h:548:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     548 |         val = __raw_readb(PCI_IOBASE + addr);
         |                           ~~~~~~~~~~ ^
   include/asm-generic/io.h:561:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     561 |         val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
         |                                                         ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/little_endian.h:37:51: note: expanded from macro '__le16_to_cpu'
      37 | #define __le16_to_cpu(x) ((__force __u16)(__le16)(x))
         |                                                   ^
   In file included from drivers/mmc/host/mtk-sd.c:11:
   In file included from include/linux/dma-mapping.h:11:
   In file included from include/linux/scatterlist.h:9:
   In file included from arch/hexagon/include/asm/io.h:328:
   include/asm-generic/io.h:574:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     574 |         val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
         |                                                         ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/little_endian.h:35:51: note: expanded from macro '__le32_to_cpu'
      35 | #define __le32_to_cpu(x) ((__force __u32)(__le32)(x))
         |                                                   ^
   In file included from drivers/mmc/host/mtk-sd.c:11:
   In file included from include/linux/dma-mapping.h:11:
   In file included from include/linux/scatterlist.h:9:
   In file included from arch/hexagon/include/asm/io.h:328:
   include/asm-generic/io.h:585:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     585 |         __raw_writeb(value, PCI_IOBASE + addr);
         |                             ~~~~~~~~~~ ^
   include/asm-generic/io.h:595:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     595 |         __raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
         |                                                       ~~~~~~~~~~ ^
   include/asm-generic/io.h:605:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     605 |         __raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
         |                                                       ~~~~~~~~~~ ^
>> drivers/mmc/host/mtk-sd.c:805:3: warning: comparison of distinct pointer types ('typeof ((clk_ns)) *' (aka 'unsigned int *') and 'uint64_t *' (aka 'unsigned long long *')) [-Wcompare-distinct-pointer-types]
     805 |                 do_div(clk_ns, mmc->actual_clock);
         |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/asm-generic/div64.h:222:28: note: expanded from macro 'do_div'
     222 |         (void)(((typeof((n)) *)0) == ((uint64_t *)0));  \
         |                ~~~~~~~~~~~~~~~~~~ ^  ~~~~~~~~~~~~~~~
>> drivers/mmc/host/mtk-sd.c:805:3: error: incompatible pointer types passing 'u32 *' (aka 'unsigned int *') to parameter of type 'uint64_t *' (aka 'unsigned long long *') [-Werror,-Wincompatible-pointer-types]
     805 |                 do_div(clk_ns, mmc->actual_clock);
         |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/asm-generic/div64.h:238:22: note: expanded from macro 'do_div'
     238 |                 __rem = __div64_32(&(n), __base);       \
         |                                    ^~~~
   include/asm-generic/div64.h:213:38: note: passing argument to parameter 'dividend' here
     213 | extern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor);
         |                                      ^
>> drivers/mmc/host/mtk-sd.c:805:3: warning: shift count >= width of type [-Wshift-count-overflow]
     805 |                 do_div(clk_ns, mmc->actual_clock);
         |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/asm-generic/div64.h:234:25: note: expanded from macro 'do_div'
     234 |         } else if (likely(((n) >> 32) == 0)) {          \
         |                                ^  ~~
   include/linux/compiler.h:76:40: note: expanded from macro 'likely'
      76 | # define likely(x)      __builtin_expect(!!(x), 1)
         |                                             ^
   9 warnings and 1 error generated.


vim +805 drivers/mmc/host/mtk-sd.c

208489032bdd8d Chaotian Jing              2015-06-15  794  
557011b6b8c87c Chun-Hung Wu               2020-07-20  795  static u64 msdc_timeout_cal(struct msdc_host *host, u64 ns, u64 clks)
208489032bdd8d Chaotian Jing              2015-06-15  796  {
0caf60c4b110b5 Amey Narkhede              2020-09-18  797  	struct mmc_host *mmc = mmc_from_priv(host);
d5c9ac0e2eb203 Thorsten Blum              2024-08-16  798  	u64 timeout;
d5c9ac0e2eb203 Thorsten Blum              2024-08-16  799  	u32 clk_ns, mode = 0;
208489032bdd8d Chaotian Jing              2015-06-15  800  
0caf60c4b110b5 Amey Narkhede              2020-09-18  801  	if (mmc->actual_clock == 0) {
208489032bdd8d Chaotian Jing              2015-06-15  802  		timeout = 0;
208489032bdd8d Chaotian Jing              2015-06-15  803  	} else {
d5c9ac0e2eb203 Thorsten Blum              2024-08-16  804  		clk_ns  = 1000000000U;
0caf60c4b110b5 Amey Narkhede              2020-09-18 @805  		do_div(clk_ns, mmc->actual_clock);
557011b6b8c87c Chun-Hung Wu               2020-07-20  806  		timeout = ns + clk_ns - 1;
557011b6b8c87c Chun-Hung Wu               2020-07-20  807  		do_div(timeout, clk_ns);
557011b6b8c87c Chun-Hung Wu               2020-07-20  808  		timeout += clks;
208489032bdd8d Chaotian Jing              2015-06-15  809  		/* in 1048576 sclk cycle unit */
4fe54318496004 AngeloGioacchino Del Regno 2021-12-16  810  		timeout = DIV_ROUND_UP(timeout, BIT(20));
762d491a8bff7a Chaotian Jing              2017-10-16  811  		if (host->dev_comp->clk_div_bits == 8)
762d491a8bff7a Chaotian Jing              2017-10-16  812  			sdr_get_field(host->base + MSDC_CFG,
762d491a8bff7a Chaotian Jing              2017-10-16  813  				      MSDC_CFG_CKMOD, &mode);
762d491a8bff7a Chaotian Jing              2017-10-16  814  		else
762d491a8bff7a Chaotian Jing              2017-10-16  815  			sdr_get_field(host->base + MSDC_CFG,
762d491a8bff7a Chaotian Jing              2017-10-16  816  				      MSDC_CFG_CKMOD_EXTRA, &mode);
208489032bdd8d Chaotian Jing              2015-06-15  817  		/*DDR mode will double the clk cycles for data timeout */
208489032bdd8d Chaotian Jing              2015-06-15  818  		timeout = mode >= 2 ? timeout * 2 : timeout;
208489032bdd8d Chaotian Jing              2015-06-15  819  		timeout = timeout > 1 ? timeout - 1 : 0;
208489032bdd8d Chaotian Jing              2015-06-15  820  	}
557011b6b8c87c Chun-Hung Wu               2020-07-20  821  	return timeout;
557011b6b8c87c Chun-Hung Wu               2020-07-20  822  }
557011b6b8c87c Chun-Hung Wu               2020-07-20  823
diff mbox series

Patch

diff --git a/drivers/mmc/host/mtk-sd.c b/drivers/mmc/host/mtk-sd.c
index a94835b8ab93..329d7b3616b6 100644
--- a/drivers/mmc/host/mtk-sd.c
+++ b/drivers/mmc/host/mtk-sd.c
@@ -795,13 +795,13 @@  static void msdc_unprepare_data(struct msdc_host *host, struct mmc_data *data)
 static u64 msdc_timeout_cal(struct msdc_host *host, u64 ns, u64 clks)
 {
 	struct mmc_host *mmc = mmc_from_priv(host);
-	u64 timeout, clk_ns;
-	u32 mode = 0;
+	u64 timeout;
+	u32 clk_ns, mode = 0;
 
 	if (mmc->actual_clock == 0) {
 		timeout = 0;
 	} else {
-		clk_ns  = 1000000000ULL;
+		clk_ns  = 1000000000U;
 		do_div(clk_ns, mmc->actual_clock);
 		timeout = ns + clk_ns - 1;
 		do_div(timeout, clk_ns);
@@ -831,7 +831,7 @@  static void msdc_set_timeout(struct msdc_host *host, u64 ns, u64 clks)
 
 	timeout = msdc_timeout_cal(host, ns, clks);
 	sdr_set_field(host->base + SDC_CFG, SDC_CFG_DTOC,
-		      (u32)(timeout > 255 ? 255 : timeout));
+		      max_t(u32, timeout, 255));
 }
 
 static void msdc_set_busy_timeout(struct msdc_host *host, u64 ns, u64 clks)
@@ -840,7 +840,7 @@  static void msdc_set_busy_timeout(struct msdc_host *host, u64 ns, u64 clks)
 
 	timeout = msdc_timeout_cal(host, ns, clks);
 	sdr_set_field(host->base + SDC_CFG, SDC_CFG_WRDTOC,
-		      (u32)(timeout > 8191 ? 8191 : timeout));
+		      max_t(u32, timeout, 8191));
 }
 
 static void msdc_gate_clock(struct msdc_host *host)