Re: [PATCH 2/5] clocksource/drivers/timer-microchip-pit64b: add Microchip PIT64B support

From: Claudiu.Beznea
Date: Fri Jun 21 2019 - 06:34:21 EST


Hi Daniel,

On 20.06.2019 11:53, Daniel Lezcano wrote:
> Hi Claudiu,
>
> sorry for the late reply.

No problem, I understand.

>
>
> On 13/06/2019 16:12, Claudiu.Beznea@xxxxxxxxxxxxx wrote:
>> Hi Daniel,
>>
>> On 31.05.2019 13:41, Daniel Lezcano wrote:
>>>
>>> Hi Claudiu,
>>>
>>>
>>> On 30/05/2019 09:46, Claudiu.Beznea@xxxxxxxxxxxxx wrote:
>>>> Hi Daniel,
>>>>
>>>> Taking into account the discussion on this tread and the fact that we have
>>>> no answer from Rob on this topic (I'm talking about [1]), what do you think
>>>> it would be best for this driver to be accepted the soonest? Would it be OK
>>>> for you to mimic the approach done by:
>>>>
>>>> drivers/clocksource/timer-integrator-ap.c
>>>>
>>>> with the following bindings in DT:
>>>>
>>>> aliases {
>>>> arm,timer-primary = &timer2;
>>>> arm,timer-secondary = &timer1;
>>>> };
>>>>
>>>> also in PIT64B driver?
>>>>
>>>> Or do you think re-spinning the Alexandre's patches at [2] (which seems to
>>>> me like the generic way to do it) would be better?
>>>
>>> This hardware / OS connection problem is getting really annoying for
>>> everyone and this pattern is repeating itself since several years. It is
>>> time we fix it properly.
>>>
>>> The first solution looks hackish from my POV. The second approach looks
>>> nicer and generic as you say. So I would vote for [2]
>>> flagging approach proposed by Mark [3].
>>
>> With this flagging approach this would mean a kind unification of
>> clocksource and clockevent functionalities under a single one, right? So
>> that the driver would register to the above layers only one device w/ 2
>> functionalities (clocksource and clockevent)? Please correct me if I'm
>> wrong? If so, from my point of view this would require major re-working of
>> clocksource and clockevent subsystems. Correctly if I wrongly understood,
>> please.
>
> Well, actually I was not expecting to change all the framework but just
> pass a flag to the probe function telling if the node is for a
> clocksource, a clockevent or both.
>

Giving so, whit these proposals I'm thinking at having something like this,
using Alexandre's new macros from [2] and passing a bitmask to timer's
probing functions (in the above example adapted only for pit64b driver
introduced in this thread):

diff --git a/drivers/clocksource/timer-microchip-pit64b.c b/drivers/clocksource/timer-microchip-pit64b.c
index 62339d8187ce..b283d51ad4c7 100644
--- a/drivers/clocksource/timer-microchip-pit64b.c
+++ b/drivers/clocksource/timer-microchip-pit64b.c
@@ -377,7 +377,7 @@ static int __init mchp_pit64b_dt_init_clkevt(struct mchp_pit64b_common_data *cd,
return ret;
}

-static int __init mchp_pit64b_dt_init(struct device_node *node)
+static int __init mchp_pit64b_dt_init(struct device_node *node, u32 props)
{
struct mchp_pit64b_common_data *cd;
u32 irq, freq = MCHP_PIT64B_DEF_FREQ;
@@ -426,7 +426,11 @@ static int __init mchp_pit64b_dt_init(struct device_node *node)
goto pclk_unprepare;
}

- if (!data.ced) {
+ if (props & TIMER_OF_PROPERTY_CLOCKSOURCE) {
+ if (data.ced)
+ goto gclk_unprepare;
+
irq = irq_of_parse_and_map(node, 0);
if (!irq) {
pr_debug("%s: Failed to get PIT64B clockevent IRQ!\n",
@@ -437,7 +441,13 @@ static int __init mchp_pit64b_dt_init(struct device_node *node)
ret = mchp_pit64b_dt_init_clkevt(cd, irq);
if (ret)
goto irq_unmap;
- } else {
+ }
+
+ if (props & TIMER_OF_PROPERTY_CLOCKEVENT) {
+ if (data.csd)
+ goto gclk_unprepare;
+
ret = mchp_pit64b_dt_init_clksrc(cd);
if (ret)
goto gclk_unprepare;
diff --git a/drivers/clocksource/timer-of.h b/drivers/clocksource/timer-of.h
index a5478f3e8589..faf95c98b6d2 100644
--- a/drivers/clocksource/timer-of.h
+++ b/drivers/clocksource/timer-of.h
@@ -8,6 +8,10 @@
#define TIMER_OF_CLOCK 0x2
#define TIMER_OF_IRQ 0x4

+#define TIMER_OF_PROPERTY_CLOCKSOURCE BIT(0)
+#define TIMER_OF_PROPERTY_CLOCKEVENT BIT(1)
+
struct of_timer_irq {
int irq;
int index;
diff --git a/drivers/clocksource/timer-probe.c b/drivers/clocksource/timer-probe.c
index 028075720334..69c45f7d198c 100644
--- a/drivers/clocksource/timer-probe.c
+++ b/drivers/clocksource/timer-probe.c
@@ -18,6 +18,8 @@
#include <linux/init.h>
#include <linux/of.h>
#include <linux/clocksource.h>
+#include <linux/interrupt.h>
+#include "timer-of.h"

extern struct of_device_id __timer_of_table[];

@@ -28,8 +30,9 @@ void __init timer_probe(void)
{
struct device_node *np;
const struct of_device_id *match;
- of_init_fn_1_ret init_func_ret;
+ of_init_fn_2_timer_ret init_func_ret;
unsigned timers = 0;
+ u32 props = TIMER_OF_PROPERTY_CLOCKSOURCE | TIMER_OF_PROPERTY_CLOCKEVENT;
int ret;

for_each_matching_node_and_match(np, __timer_of_table, &match) {
@@ -38,7 +41,12 @@ void __init timer_probe(void)

init_func_ret = match->data;

- ret = init_func_ret(np);
+ if (timer_of_is_clocksource(np) &&
+ !timer_of_is_clockevent(np))
+ props = TIMER_OF_PROPERTY_CLOCKSOURCE;
+ if (timer_of_is_clockevent(np) &&
+ !timer_of_is_clocksource(np))
+ props = TIMER_OF_PROPERTY_CLOCKEVENT;
+
+ ret = init_func_ret(np, props);
if (ret) {
pr_err("Failed to initialize '%pOF': %d\n", np, ret);
continue;
diff --git a/include/linux/clocksource.h b/include/linux/clocksource.h
index 308918928767..5c4de4833ed8 100644
--- a/include/linux/clocksource.h
+++ b/include/linux/clocksource.h
@@ -255,7 +255,7 @@ extern int clocksource_mmio_init(void __iomem *, const char *,
extern int clocksource_i8253_init(void);

#define TIMER_OF_DECLARE(name, compat, fn) \
- OF_DECLARE_1_RET(timer, name, compat, fn)
+ OF_DECLARE_2_TIMER_RET(timer, name, compat, fn)

#define CLOCKSOURCE_OF_DECLARE(name, compat, fn) \
TIMER_OF_DECLARE(name, compat, fn)
diff --git a/include/linux/of.h b/include/linux/of.h
index 99b0ebf49632..50a3c27f7717 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -1258,6 +1258,7 @@ static inline int of_get_available_child_count(const struct device_node *np)
#endif

typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
+typedef int (*of_init_fn_2_timer_ret)(struct device_node *, u32);
typedef int (*of_init_fn_1_ret)(struct device_node *);
typedef void (*of_init_fn_1)(struct device_node *);

@@ -1267,6 +1268,8 @@ typedef void (*of_init_fn_1)(struct device_node *);
_OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
#define OF_DECLARE_2(table, name, compat, fn) \
_OF_DECLARE(table, name, compat, fn, of_init_fn_2)
+#define OF_DECLARE_2_TIMER_RET(table, name, compat, fn) \
+ _OF_DECLARE(table, name, compat, fn, of_init_fn_2_timer_ret)

/**
* struct of_changeset_entry - Holds a changeset entry


The only downside of this is that we're parsing these new DT bindings before
calling probe function, then we're checking the result of parsing again, in
probe function, and I'm thinking if it wouldn't be simpler to just parse these
binding in timer's probe function as all the other DT bindings are parsed
(moreover all timers probing functions should be changes to get this new argument).

More than this I see that there is one timer driver which is not probed via
timer_probe() (I'm pointing to drivers/clocksource/numachip.c).

Please let me know what do you think about it.

Thank you,
Claudiu Beznea

>
>
>> At the moment we register different functionalities (clocksource and
>> clockevent) to the above layers for hardware blocks (e.g. with
>> clocksource_register_hz() or clockevents_config_and_register()). If
>> hardware can support clocksource and clockevent we register both these
>> functionalities, if only one is supported we register only one of these.
>> The above layers would choose the best clocksource/clockevent device from
>> the available ones based on rating field for each clocksource/clockevent we
>> register. In all this current behavior I don't see how these flags would
>> interact with clocksource/clockevent subsystem. Could you please let me
>> know how do you see these and the way these new flags would interact with
>> the layers above the drivers?
>>>
>>> I added Arnd in Cc in order to have its opinion.
>>>
>>> [3]
>>> https://lore.kernel.org/lkml/20171215113242.skmh5nzr7wqdmvnw@xxxxxxxxxxxxxxxxxxxxxxxxx/
>>>
>>>> [1]
>>>> https://lore.kernel.org/lkml/20190408151155.20279-1-alexandre.belloni@xxxxxxxxxxx/#t
>>>> [2]
>>>> https://lore.kernel.org/lkml/20171213185313.20017-1-alexandre.belloni@xxxxxxxxxxxxxxxxxx/
>>>>
>>>
>>>
>>>
>>>
>>>
>>>
>
>