diff mbox series

[1/3] can: rcar_canfd: Simplify clock handling

Message ID 2cf38c10b83c8e5c04d68b17a930b6d9dbf66f40.1716973640.git.geert+renesas@glider.be (mailing list archive)
State Accepted
Delegated to: Geert Uytterhoeven
Headers show
Series can: rcar_canfd: Small improvements and cleanups | expand

Commit Message

Geert Uytterhoeven May 29, 2024, 9:12 a.m. UTC
The main CAN clock is either the internal CANFD clock, or the external
CAN clock.  Hence replace the two-valued enum by a simple boolean flag.
Consolidate all CANFD clock handling inside a single branch.

Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
---
 drivers/net/can/rcar/rcar_canfd.c | 24 +++++++-----------------
 1 file changed, 7 insertions(+), 17 deletions(-)

Comments

Vincent MAILHOL June 2, 2024, 8:03 a.m. UTC | #1
On Wed. 29 May 2024 at 18:12, Geert Uytterhoeven
<geert+renesas@glider.be> wrote:
> The main CAN clock is either the internal CANFD clock, or the external
> CAN clock.  Hence replace the two-valued enum by a simple boolean flag.
> Consolidate all CANFD clock handling inside a single branch.

For what it is worth, your patch also saves up to 8 bytes in struct
rcar_canfd_global (depends on the architecture).

Before:

  $ pahole drivers/net/can/rcar/rcar_canfd.o -C rcar_canfd_global
  struct rcar_canfd_global {
      struct rcar_canfd_channel * ch[8];               /*     0    64 */
      /* --- cacheline 1 boundary (64 bytes) --- */
      void *                     base;                 /*    64     8 */
      struct platform_device *   pdev;                 /*    72     8 */
      struct clk *               clkp;                 /*    80     8 */
      struct clk *               can_clk;              /*    88     8 */
      enum rcar_canfd_fcanclk    fcan;                 /*    96     4 */

      /* XXX 4 bytes hole, try to pack */

      long unsigned int          channels_mask;        /*   104     8 */
      bool                       fdmode;               /*   112     1 */

      /* XXX 7 bytes hole, try to pack */

      struct reset_control *     rstc1;                /*   120     8 */
      /* --- cacheline 2 boundary (128 bytes) --- */
      struct reset_control *     rstc2;                /*   128     8 */
      const struct rcar_canfd_hw_info  * info;         /*   136     8 */

      /* size: 144, cachelines: 3, members: 11 */
      /* sum members: 133, holes: 2, sum holes: 11 */
      /* last cacheline: 16 bytes */
  };

After:

  $ pahole drivers/net/can/rcar/rcar_canfd.o -C rcar_canfd_global
  struct rcar_canfd_global {
      struct rcar_canfd_channel * ch[8];               /*     0    64 */
      /* --- cacheline 1 boundary (64 bytes) --- */
      void *                     base;                 /*    64     8 */
      struct platform_device *   pdev;                 /*    72     8 */
      struct clk *               clkp;                 /*    80     8 */
      struct clk *               can_clk;              /*    88     8 */
      long unsigned int          channels_mask;        /*    96     8 */
      bool                       extclk;               /*   104     1 */
      bool                       fdmode;               /*   105     1 */

      /* XXX 6 bytes hole, try to pack */

      struct reset_control *     rstc1;                /*   112     8 */
      struct reset_control *     rstc2;                /*   120     8 */
      /* --- cacheline 2 boundary (128 bytes) --- */
      const struct rcar_canfd_hw_info  * info;         /*   128     8 */

      /* size: 136, cachelines: 3, members: 11 */
      /* sum members: 130, holes: 1, sum holes: 6 */
      /* last cacheline: 8 bytes */
  };

> Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
> ---
>  drivers/net/can/rcar/rcar_canfd.c | 24 +++++++-----------------
>  1 file changed, 7 insertions(+), 17 deletions(-)
>
> diff --git a/drivers/net/can/rcar/rcar_canfd.c b/drivers/net/can/rcar/rcar_canfd.c
> index b828427187353d6f..474840b58e8f13f1 100644
> --- a/drivers/net/can/rcar/rcar_canfd.c
> +++ b/drivers/net/can/rcar/rcar_canfd.c
> @@ -508,12 +508,6 @@
>   */
>  #define RCANFD_CFFIFO_IDX              0
>
> -/* fCAN clock select register settings */
> -enum rcar_canfd_fcanclk {
> -       RCANFD_CANFDCLK = 0,            /* CANFD clock */
> -       RCANFD_EXTCLK,                  /* Externally input clock */
> -};
> -
>  struct rcar_canfd_global;
>
>  struct rcar_canfd_hw_info {
> @@ -545,8 +539,8 @@ struct rcar_canfd_global {
>         struct platform_device *pdev;   /* Respective platform device */
>         struct clk *clkp;               /* Peripheral clock */
>         struct clk *can_clk;            /* fCAN clock */
> -       enum rcar_canfd_fcanclk fcan;   /* CANFD or Ext clock */
>         unsigned long channels_mask;    /* Enabled channels mask */
> +       bool extclk;                    /* CANFD or Ext clock */
>         bool fdmode;                    /* CAN FD or Classical CAN only mode */

Notwithstanding comment: you may consider to replace those two booleans by a:

          unsigned int flags;

This way, no more fields would be needed in the future if more quirks are added.

>         struct reset_control *rstc1;
>         struct reset_control *rstc2;
> @@ -777,7 +771,7 @@ static void rcar_canfd_configure_controller(struct rcar_canfd_global *gpriv)
>                 cfg |= RCANFD_GCFG_CMPOC;
>
>         /* Set External Clock if selected */
> -       if (gpriv->fcan != RCANFD_CANFDCLK)
> +       if (gpriv->extclk)
>                 cfg |= RCANFD_GCFG_DCS;
>
>         rcar_canfd_set_bit(gpriv->base, RCANFD_GCFG, cfg);
> @@ -1941,16 +1935,12 @@ static int rcar_canfd_probe(struct platform_device *pdev)
>                         return dev_err_probe(dev, PTR_ERR(gpriv->can_clk),
>                                              "cannot get canfd clock\n");
>
> -               gpriv->fcan = RCANFD_CANFDCLK;
> -
> +               /* CANFD clock may be further divided within the IP */
> +               fcan_freq = clk_get_rate(gpriv->can_clk) / info->postdiv;
>         } else {
> -               gpriv->fcan = RCANFD_EXTCLK;
> +               fcan_freq = clk_get_rate(gpriv->can_clk);
> +               gpriv->extclk = true;
>         }
> -       fcan_freq = clk_get_rate(gpriv->can_clk);
> -
> -       if (gpriv->fcan == RCANFD_CANFDCLK)
> -               /* CANFD clock is further divided by (1/2) within the IP */
> -               fcan_freq /= info->postdiv;
>
>         addr = devm_platform_ioremap_resource(pdev, 0);
>         if (IS_ERR(addr)) {
> @@ -2060,7 +2050,7 @@ static int rcar_canfd_probe(struct platform_device *pdev)
>
>         platform_set_drvdata(pdev, gpriv);
>         dev_info(dev, "global operational state (clk %d, fdmode %d)\n",
> -                gpriv->fcan, gpriv->fdmode);
> +                gpriv->extclk, gpriv->fdmode);
>         return 0;
>
>  fail_channel:
> --
> 2.34.1
>
>
Wolfram Sang June 3, 2024, 10:32 a.m. UTC | #2
On Wed, May 29, 2024 at 11:12:13AM +0200, Geert Uytterhoeven wrote:
> The main CAN clock is either the internal CANFD clock, or the external
> CAN clock.  Hence replace the two-valued enum by a simple boolean flag.
> Consolidate all CANFD clock handling inside a single branch.
> 
> Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>

Nice simplification!

Reviewed-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
Geert Uytterhoeven June 6, 2024, 10:15 a.m. UTC | #3
Hi Vincent,

On Sun, Jun 2, 2024 at 10:03 AM Vincent MAILHOL
<mailhol.vincent@wanadoo.fr> wrote:
> On Wed. 29 May 2024 at 18:12, Geert Uytterhoeven
> <geert+renesas@glider.be> wrote:
> > The main CAN clock is either the internal CANFD clock, or the external
> > CAN clock.  Hence replace the two-valued enum by a simple boolean flag.
> > Consolidate all CANFD clock handling inside a single branch.
>
> For what it is worth, your patch also saves up to 8 bytes in struct
> rcar_canfd_global (depends on the architecture).

True.

> > @@ -545,8 +539,8 @@ struct rcar_canfd_global {
> >         struct platform_device *pdev;   /* Respective platform device */
> >         struct clk *clkp;               /* Peripheral clock */
> >         struct clk *can_clk;            /* fCAN clock */
> > -       enum rcar_canfd_fcanclk fcan;   /* CANFD or Ext clock */
> >         unsigned long channels_mask;    /* Enabled channels mask */
> > +       bool extclk;                    /* CANFD or Ext clock */
> >         bool fdmode;                    /* CAN FD or Classical CAN only mode */
>
> Notwithstanding comment: you may consider to replace those two booleans by a:
>
>           unsigned int flags;
>
> This way, no more fields would be needed in the future if more quirks are added.

Using "unsigned int flags" and BIT(x) flags would increase code size
by 8 bytes (arm/arm64).
Using "unsigned int foo:1" bitfields would increase code size by 16
(arm) or 12 (arm64) bytes.
So as long as we can fit more bools inside the hole, it is more
efficient to do so...

Gr{oetje,eeting}s,

                        Geert
Vincent MAILHOL June 6, 2024, 11:04 a.m. UTC | #4
On Thu. 6 June 2024 at 19:15, Geert Uytterhoeven <geert@linux-m68k.org> wrote:
> Hi Vincent,
>
> On Sun, Jun 2, 2024 at 10:03 AM Vincent MAILHOL
> <mailhol.vincent@wanadoo.fr> wrote:
> > On Wed. 29 May 2024 at 18:12, Geert Uytterhoeven
> > <geert+renesas@glider.be> wrote:
> > > The main CAN clock is either the internal CANFD clock, or the external
> > > CAN clock.  Hence replace the two-valued enum by a simple boolean flag.
> > > Consolidate all CANFD clock handling inside a single branch.
> >
> > For what it is worth, your patch also saves up to 8 bytes in struct
> > rcar_canfd_global (depends on the architecture).
>
> True.
>
> > > @@ -545,8 +539,8 @@ struct rcar_canfd_global {
> > >         struct platform_device *pdev;   /* Respective platform device */
> > >         struct clk *clkp;               /* Peripheral clock */
> > >         struct clk *can_clk;            /* fCAN clock */
> > > -       enum rcar_canfd_fcanclk fcan;   /* CANFD or Ext clock */
> > >         unsigned long channels_mask;    /* Enabled channels mask */
> > > +       bool extclk;                    /* CANFD or Ext clock */
> > >         bool fdmode;                    /* CAN FD or Classical CAN only mode */
> >
> > Notwithstanding comment: you may consider to replace those two booleans by a:
> >
> >           unsigned int flags;
> >
> > This way, no more fields would be needed in the future if more quirks are added.
>
> Using "unsigned int flags" and BIT(x) flags would increase code size
> by 8 bytes (arm/arm64).

I am not sure where you derive your figure from, but looking at the pahole:

  $ pahole drivers/net/can/rcar/rcar_canfd.o -C rcar_canfd_global
  struct rcar_canfd_global {
      struct rcar_canfd_channel * ch[8];               /*     0    64 */
      /* --- cacheline 1 boundary (64 bytes) --- */
      void *                     base;                 /*    64     8 */
      struct platform_device *   pdev;                 /*    72     8 */
      struct clk *               clkp;                 /*    80     8 */
      struct clk *               can_clk;              /*    88     8 */
      long unsigned int          channels_mask;        /*    96     8 */
      bool                       extclk;               /*   104     1 */
      bool                       fdmode;               /*   105     1 */

      /* XXX 6 bytes hole, try to pack */

      struct reset_control *     rstc1;                /*   112     8 */
      struct reset_control *     rstc2;                /*   120     8 */
      /* --- cacheline 2 boundary (128 bytes) --- */
      const struct rcar_canfd_hw_info  * info;         /*   128     8 */

      /* size: 136, cachelines: 3, members: 11 */
      /* sum members: 130, holes: 1, sum holes: 6 */
      /* last cacheline: 8 bytes */
  };

on a 64 bits architecture, you are currently using two booleans (two
bytes), followed by a hole of six bytes, which is a total of eight
bytes. This should be way enough to fit an unsigned int. Same would go
on 32 bits architecture in which you would use two bytes for the
booleans and have a hole of two bytes, which is four bytes: one more
time enough for an unsigned int.

In both scenarios, you are not consuming more bytes, nor are you
saving bytes. It is a neutral change.

Of course, the pahole above was done on x86_64, but as far as I know,
arm and arm64 paddings behave similarly.

> Using "unsigned int foo:1" bitfields would increase code size by 16
> (arm) or 12 (arm64) bytes.
> So as long as we can fit more bools inside the hole, it is more
> efficient to do so...

I do not get this either. Where did you get your 16 bytes from? If I do:

  struct foo {
          unsigned int foo1:1;
          unsigned int foo2:1;
  };

and

  struct bar {
          unsigned int flags;
  };

then I am pretty sure that sizeof(struct foo) is the same as
sizeof(struct bar). That's the point of the bitfields: as long as the
total of the bitfields fit in the type, the total size consumed by the
bitfield is the same as the type size.

But just to reiter my previous message, these are notwithstanding
comments. I am fine if you keep the patch as-is ;)

> Gr{oetje,eeting}s,
>
>                         Geert
>
> --
> Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org
>
> In personal conversations with technical people, I call myself a hacker. But
> when I'm talking to journalists I just say "programmer" or something like that.
>                                 -- Linus Torvalds
>
Geert Uytterhoeven June 6, 2024, 11:38 a.m. UTC | #5
Hi Vincent,

On Thu, Jun 6, 2024 at 1:05 PM Vincent MAILHOL
<mailhol.vincent@wanadoo.fr> wrote:
> On Thu. 6 June 2024 at 19:15, Geert Uytterhoeven <geert@linux-m68k.org> wrote:
> > On Sun, Jun 2, 2024 at 10:03 AM Vincent MAILHOL
> > <mailhol.vincent@wanadoo.fr> wrote:
> > > On Wed. 29 May 2024 at 18:12, Geert Uytterhoeven
> > > <geert+renesas@glider.be> wrote:
> > > > The main CAN clock is either the internal CANFD clock, or the external
> > > > CAN clock.  Hence replace the two-valued enum by a simple boolean flag.
> > > > Consolidate all CANFD clock handling inside a single branch.
> > >
> > > For what it is worth, your patch also saves up to 8 bytes in struct
> > > rcar_canfd_global (depends on the architecture).
> >
> > True.
> >
> > > > @@ -545,8 +539,8 @@ struct rcar_canfd_global {
> > > >         struct platform_device *pdev;   /* Respective platform device */
> > > >         struct clk *clkp;               /* Peripheral clock */
> > > >         struct clk *can_clk;            /* fCAN clock */
> > > > -       enum rcar_canfd_fcanclk fcan;   /* CANFD or Ext clock */
> > > >         unsigned long channels_mask;    /* Enabled channels mask */
> > > > +       bool extclk;                    /* CANFD or Ext clock */
> > > >         bool fdmode;                    /* CAN FD or Classical CAN only mode */
> > >
> > > Notwithstanding comment: you may consider to replace those two booleans by a:
> > >
> > >           unsigned int flags;
> > >
> > > This way, no more fields would be needed in the future if more quirks are added.
> >
> > Using "unsigned int flags" and BIT(x) flags would increase code size
> > by 8 bytes (arm/arm64).
>
> I am not sure where you derive your figure from, but looking at the pahole:

pahole shows the size of data structures.

> > Using "unsigned int foo:1" bitfields would increase code size by 16
> > (arm) or 12 (arm64) bytes.
> > So as long as we can fit more bools inside the hole, it is more
> > efficient to do so...
>
> I do not get this either. Where did you get your 16 bytes from? If I do:

I also looked at code size[*]: while storing bits takes less space than
storing bytes, processing bits may require more instructions than
processing bytes (depending on the architecture).

[*] size drivers/net/can/rcar/rcar_canfd.o

> But just to reiter my previous message, these are notwithstanding
> comments. I am fine if you keep the patch as-is ;)

So I'd like to keep the patch as-is.

Gr{oetje,eeting}s,

                        Geert
Marc Kleine-Budde June 21, 2024, 8:36 a.m. UTC | #6
On 06.06.2024 13:38:24, Geert Uytterhoeven wrote:
> > > > > @@ -545,8 +539,8 @@ struct rcar_canfd_global {
> > > > >         struct platform_device *pdev;   /* Respective platform device */
> > > > >         struct clk *clkp;               /* Peripheral clock */
> > > > >         struct clk *can_clk;            /* fCAN clock */
> > > > > -       enum rcar_canfd_fcanclk fcan;   /* CANFD or Ext clock */
> > > > >         unsigned long channels_mask;    /* Enabled channels mask */
> > > > > +       bool extclk;                    /* CANFD or Ext clock */
> > > > >         bool fdmode;                    /* CAN FD or Classical CAN only mode */
> > > >
> > > > Notwithstanding comment: you may consider to replace those two booleans by a:
> > > >
> > > >           unsigned int flags;
> > > >
> > > > This way, no more fields would be needed in the future if more quirks are added.
> > >
> > > Using "unsigned int flags" and BIT(x) flags would increase code size
> > > by 8 bytes (arm/arm64).
> >
> > I am not sure where you derive your figure from, but looking at the pahole:
> 
> pahole shows the size of data structures.
> 
> > > Using "unsigned int foo:1" bitfields would increase code size by 16
> > > (arm) or 12 (arm64) bytes.
> > > So as long as we can fit more bools inside the hole, it is more
> > > efficient to do so...
> >
> > I do not get this either. Where did you get your 16 bytes from? If I do:
> 
> I also looked at code size[*]: while storing bits takes less space than
> storing bytes, processing bits may require more instructions than
> processing bytes (depending on the architecture).
> 
> [*] size drivers/net/can/rcar/rcar_canfd.o

You have probably used "scripts/bloat-o-meter" from the kernel source
for this, right?

Marc
Geert Uytterhoeven June 21, 2024, 11:27 a.m. UTC | #7
Hi Marc,

On Fri, Jun 21, 2024 at 10:36 AM Marc Kleine-Budde <mkl@pengutronix.de> wrote:
> On 06.06.2024 13:38:24, Geert Uytterhoeven wrote:
> > > > > > @@ -545,8 +539,8 @@ struct rcar_canfd_global {
> > > > > >         struct platform_device *pdev;   /* Respective platform device */
> > > > > >         struct clk *clkp;               /* Peripheral clock */
> > > > > >         struct clk *can_clk;            /* fCAN clock */
> > > > > > -       enum rcar_canfd_fcanclk fcan;   /* CANFD or Ext clock */
> > > > > >         unsigned long channels_mask;    /* Enabled channels mask */
> > > > > > +       bool extclk;                    /* CANFD or Ext clock */
> > > > > >         bool fdmode;                    /* CAN FD or Classical CAN only mode */
> > > > >
> > > > > Notwithstanding comment: you may consider to replace those two booleans by a:
> > > > >
> > > > >           unsigned int flags;
> > > > >
> > > > > This way, no more fields would be needed in the future if more quirks are added.
> > > >
> > > > Using "unsigned int flags" and BIT(x) flags would increase code size
> > > > by 8 bytes (arm/arm64).
> > >
> > > I am not sure where you derive your figure from, but looking at the pahole:
> >
> > pahole shows the size of data structures.
> >
> > > > Using "unsigned int foo:1" bitfields would increase code size by 16
> > > > (arm) or 12 (arm64) bytes.
> > > > So as long as we can fit more bools inside the hole, it is more
> > > > efficient to do so...
> > >
> > > I do not get this either. Where did you get your 16 bytes from? If I do:
> >
> > I also looked at code size[*]: while storing bits takes less space than
> > storing bytes, processing bits may require more instructions than
> > processing bytes (depending on the architecture).
> >
> > [*] size drivers/net/can/rcar/rcar_canfd.o
>
> You have probably used "scripts/bloat-o-meter" from the kernel source
> for this, right?

Not this time; I used "size" from the binutils package instead.

Gr{oetje,eeting}s,

                        Geert
diff mbox series

Patch

diff --git a/drivers/net/can/rcar/rcar_canfd.c b/drivers/net/can/rcar/rcar_canfd.c
index b828427187353d6f..474840b58e8f13f1 100644
--- a/drivers/net/can/rcar/rcar_canfd.c
+++ b/drivers/net/can/rcar/rcar_canfd.c
@@ -508,12 +508,6 @@ 
  */
 #define RCANFD_CFFIFO_IDX		0
 
-/* fCAN clock select register settings */
-enum rcar_canfd_fcanclk {
-	RCANFD_CANFDCLK = 0,		/* CANFD clock */
-	RCANFD_EXTCLK,			/* Externally input clock */
-};
-
 struct rcar_canfd_global;
 
 struct rcar_canfd_hw_info {
@@ -545,8 +539,8 @@  struct rcar_canfd_global {
 	struct platform_device *pdev;	/* Respective platform device */
 	struct clk *clkp;		/* Peripheral clock */
 	struct clk *can_clk;		/* fCAN clock */
-	enum rcar_canfd_fcanclk fcan;	/* CANFD or Ext clock */
 	unsigned long channels_mask;	/* Enabled channels mask */
+	bool extclk;			/* CANFD or Ext clock */
 	bool fdmode;			/* CAN FD or Classical CAN only mode */
 	struct reset_control *rstc1;
 	struct reset_control *rstc2;
@@ -777,7 +771,7 @@  static void rcar_canfd_configure_controller(struct rcar_canfd_global *gpriv)
 		cfg |= RCANFD_GCFG_CMPOC;
 
 	/* Set External Clock if selected */
-	if (gpriv->fcan != RCANFD_CANFDCLK)
+	if (gpriv->extclk)
 		cfg |= RCANFD_GCFG_DCS;
 
 	rcar_canfd_set_bit(gpriv->base, RCANFD_GCFG, cfg);
@@ -1941,16 +1935,12 @@  static int rcar_canfd_probe(struct platform_device *pdev)
 			return dev_err_probe(dev, PTR_ERR(gpriv->can_clk),
 					     "cannot get canfd clock\n");
 
-		gpriv->fcan = RCANFD_CANFDCLK;
-
+		/* CANFD clock may be further divided within the IP */
+		fcan_freq = clk_get_rate(gpriv->can_clk) / info->postdiv;
 	} else {
-		gpriv->fcan = RCANFD_EXTCLK;
+		fcan_freq = clk_get_rate(gpriv->can_clk);
+		gpriv->extclk = true;
 	}
-	fcan_freq = clk_get_rate(gpriv->can_clk);
-
-	if (gpriv->fcan == RCANFD_CANFDCLK)
-		/* CANFD clock is further divided by (1/2) within the IP */
-		fcan_freq /= info->postdiv;
 
 	addr = devm_platform_ioremap_resource(pdev, 0);
 	if (IS_ERR(addr)) {
@@ -2060,7 +2050,7 @@  static int rcar_canfd_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, gpriv);
 	dev_info(dev, "global operational state (clk %d, fdmode %d)\n",
-		 gpriv->fcan, gpriv->fdmode);
+		 gpriv->extclk, gpriv->fdmode);
 	return 0;
 
 fail_channel: