Common Clock Framework -2- (APIs)

<kernel v5.4>

Common Clock Framework -2- (APIs)

주요 API

  • clk_get()
    • 사용할 클럭을 찾아온다.
    • 클럭 provider의 참조 카운터를 증가시킨다
  • clk_put()
    • 사용이 끝난 클럭을 지정한다.
    • 클럭 provider의 참조 카운터를 감소시킨다
  • clk_prepare()
    • 클럭 소스로부터 클럭이 enable 되게 한다. 슬립 가능한 API이다.
    • 절전 기능이 동작하던 클럭들을 깨운다.
  • clk_unprepare()
    • 클럭 소스로부터 클럭이 disable 되게 한다. 슬립 가능한 API이다.
    • 절전 기능이 동작하는 클럭들을 재운다.
  • clk_enable()
    • 클럭 소스로부터 클럭 gate가 열리도록 하며 슬립하지 않으므로 atomic context에서도 사용할 수 있다.
  • clk_disable()
    • 클럭 소스로부터 클럭 gate가 닫히도록 하며 슬립하지 않으므로 atomic context에서도 사용할 수 있다.
  • clk_get_rate()
    • 현재 클럭의 rate를 알아온다.
  • clk_set_rate()
    • 현재 클럭의 rate를 설정한다.
  • clk_set_parent()
    • mux 클럭의 소스를 선택한다. (부모 클럭을 선택한다)
  • clk_get_parent()
    • mux 클럭의 소스를 알아온다. (부모 클럭을 알아온다.)

관리 카운터

다음은 클럭 코어들의 관리 카운터들이다.

  • ref.refcount
    • 사용되는 클럭 코어들의 참조 카운터가 증/감된다.
  • prepare_count
    • prepare되는 클럭 코어들의 카운터가 증/감된다.
  • enable_count
    • enable되는 클럭 코어들의 카운터가 증/감된다.
  • protect_count
    • protection되는 클럭 코어들의 카운터가 증/감된다.
    • protection된 경우 exclusive 사용자 외에는 구성 값들을 변경할 수 없다.

 

다음 그림은 호출되는 클럭 API와 참조 카운터들을 보여준다.

  • gated 클럭의 경우 clk_enable() 함수를 호출해야 클럭이 출력된다.

 

다음 그림은 디바이스가 클럭을 사용할 때 하이라키로 구성된 클럭들의 각종 참조 카운터들을 보여준다.

  • 클럭 API가 호출되어 최상위 클럭까지 참조 카운터들이 증/감된다.

 


클럭 사용/해제

클럭 사용

clk_get()

drivers/clk/clkdev.c

struct clk *clk_get(struct device *dev, const char *con_id)
{
        const char *dev_id = dev ? dev_name(dev) : NULL;
        struct clk_hw *hw;

        if (dev && dev->of_node) {
                hw = of_clk_get_hw(dev->of_node, 0, con_id);
                if (!IS_ERR(hw) || PTR_ERR(hw) == -EPROBE_DEFER)
                        return clk_hw_create_clk(dev, hw, dev_id, con_id);
        }

        return __clk_get_sys(dev, dev_id, con_id);
}
EXPORT_SYMBOL(clk_get);

디바이스에서 사용할 클럭을 알아온다. @con_id가 주어진 경우 @con_id 명으로 검색하여 해당 클럭을 찾아오고, 지정되지 않은 경우 연결된 부모 클럭을 알아온다. 그리고 클럭 코어의 참조 카운터를 1 증가시킨다.

  • 코드 라인 6~10에서 디바이스 트리에서 @con_id 또는 0번 인덱스의 클럭 hw를 알아온다. 만일 에러가 없거나 유예 상태인 경우 클럭을 할당하고 알아온 클럭 hw의 클럭 코어에 연결하고 클럭을 반환한다.
  • 코드 라인 12에서 클럭 provider 리스트에서 @con_id 명으로 검색하여 해당 클럭을 할당하고 반환한다.

 

__clk_get_sys()

drivers/clk/clkdev.c

static struct clk *__clk_get_sys(struct device *dev, const char *dev_id,
                                 const char *con_id)
{
        struct clk_hw *hw = clk_find_hw(dev_id, con_id);

        return clk_hw_create_clk(dev, hw, dev_id, con_id);
}

클럭 provider 리스트에서 @con_id 명으로 클럭 hw를 검색하여 해당 클럭 hw에 연결할 클럭을 할당하고 반환한다.

 

클럭 찾기

clk_find_hw()

drivers/clk/clkdev.c

struct clk_hw *clk_find_hw(const char *dev_id, const char *con_id)
{
        struct clk_lookup *cl;
        struct clk_hw *hw = ERR_PTR(-ENOENT);

        mutex_lock(&clocks_mutex);
        cl = clk_find(dev_id, con_id);
        if (cl)
                hw = cl->clk_hw;
        mutex_unlock(&clocks_mutex);

        return hw;
}

전역 클럭 리스트에서 @dev_id 및 @con_id 명으로 매치되는 클럭 hw를 반환한다. 검색이 실패한 경우 -ENOENT 에러를 반환한다.

 

clk_find()

drivers/clk/clkdev.c

/*
 * Find the correct struct clk for the device and connection ID.
 * We do slightly fuzzy matching here:
 *  An entry with a NULL ID is assumed to be a wildcard.
 *  If an entry has a device ID, it must match
 *  If an entry has a connection ID, it must match
 * Then we take the most specific entry - with the following
 * order of precedence: dev+con > dev only > con only.
 */
static struct clk_lookup *clk_find(const char *dev_id, const char *con_id)
{
        struct clk_lookup *p, *cl = NULL;
        int match, best_found = 0, best_possible = 0;

        if (dev_id)
                best_possible += 2;
        if (con_id)
                best_possible += 1;

        lockdep_assert_held(&clocks_mutex);

        list_for_each_entry(p, &clocks, node) {
                match = 0;
                if (p->dev_id) {
                        if (!dev_id || strcmp(p->dev_id, dev_id))
                                continue;
                        match += 2;
                }
                if (p->con_id) {
                        if (!con_id || strcmp(p->con_id, con_id))
                                continue;
                        match += 1;
                }

                if (match > best_found) {
                        cl = p;
                        if (match != best_possible)
                                best_found = match;
                        else
                                break;
                }
        }
        return cl;
}

전역 클럭 리스트에서 @dev_id 및 @con_id 명으로 클럭을 찾아 clk_lookup을 반환한다. 검색이 실패한 경우 null을 반환한다.

  • @dev_id 및 @con_id가 주어진 경우 일치되지 않으면 null을 반환한다. 검색된 항목들 중 가장 best 매치를 다음과 같은 조건으로 수행한다.
    • @dev_id 및 @con_id 명이 모두 일치하는 경우 즉각 반환
    • @dev_id 명이 일치하는 마지막 항목
    • @con_id 명이 일치하는 마지막 항목

 

클럭 사용 해제

clk_put()

drivers/clk/clkdev.c

void clk_put(struct clk *clk)
{
        __clk_put(clk);
}
EXPORT_SYMBOL(clk_put);

사용한 클럭을 해제한다.  그리고 사용한 클럭 코어의 참조 카운터를 1 감소시킨다.

 

__clk_put()

drivers/clk/clkdev.c

void __clk_put(struct clk *clk)
{
        struct module *owner;

        if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
                return;

        clk_prepare_lock();

        /*
         * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
         * given user should be balanced with calls to clk_rate_exclusive_put()
         * and by that same consumer
         */
        if (WARN_ON(clk->exclusive_count)) {
                /* We voiced our concern, let's sanitize the situation */
                clk->core->protect_count -= (clk->exclusive_count - 1);
                clk_core_rate_unprotect(clk->core);
                clk->exclusive_count = 0;
        }

        hlist_del(&clk->clks_node);
        if (clk->min_rate > clk->core->req_rate ||
            clk->max_rate < clk->core->req_rate)
                clk_core_set_rate_nolock(clk->core, clk->core->req_rate);

        owner = clk->core->owner;
        kref_put(&clk->core->ref, __clk_release);

        clk_prepare_unlock();

        module_put(owner);

        free_clk(clk);
}

사용한 클럭을 해제한다.  그리고 사용한 클럭 코어의 참조 카운터를 1 감소시킨다.

  • 코드 라인 8에서 클럭 설정을 변경하려면 전역 클럭 락을 획득해야 한다.
  • 코드 라인 15~20에서 클럭 rate 변경에 대한 protection을 위해 0이 아닌 경우 경고 메시지를 출력하고 0으로 변경한다.
  • 코드 라인 22에서 클럭 코어에서 클럭을 제거한다.
  • 코드 라인 23~25에서 클럭 사용자가 요청한 rate 범위가 클럭 코어의 req_rate가 아닌 경우 클럭 코어의 req_rate로 변경한다.
  • 코드 라인 28에서 클럭 코어의 참조 카운터를 감소시킨다.
  • 코드 라인 32에서 모듈의 참조 카운터를 감소시킨다.
  • 코드 라인 34에서 할당된 클럭을 해제한다.

 


클럭 준비/해지

클럭 준비(prepare)

clk_prepare()

drivers/clk/clk.c

/**
 * clk_prepare - prepare a clock source
 * @clk: the clk being prepared
 *
 * clk_prepare may sleep, which differentiates it from clk_enable.  In a simple
 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
 * operation may sleep.  One example is a clk which is accessed over I2c.  In
 * the complex case a clk ungate operation may require a fast and a slow part.
 * It is this reason that clk_prepare and clk_enable are not mutually
 * exclusive.  In fact clk_prepare must be called before clk_enable.
 * Returns 0 on success, -EERROR otherwise.
 */
int clk_prepare(struct clk *clk)
{
        if (!clk)
                return 0;

        return clk_core_prepare_lock(clk->core);
}
EXPORT_SYMBOL_GPL(clk_prepare);

상위 클럭 소스까지 prepare 시킨다.

 

clk_core_prepare_lock()

drivers/clk/clk.c

static int clk_core_prepare_lock(struct clk_core *core)
{
        int ret;

        clk_prepare_lock();
        ret = clk_core_prepare(core);
        clk_prepare_unlock();

        return ret;
}

lock을 획득한 상태에서 상위 클럭 소스까지 prepare 시킨다.

 

clk_core_prepare()

drivers/clk/clk.c

static int clk_core_prepare(struct clk_core *core)
{
        int ret = 0;

        lockdep_assert_held(&prepare_lock);

        if (!core)
                return 0;

        if (core->prepare_count == 0) {
                ret = clk_pm_runtime_get(core);
                if (ret)
                        return ret;

                ret = clk_core_prepare(core->parent);
                if (ret)
                        goto runtime_put;

                trace_clk_prepare(core);

                if (core->ops->prepare)
                        ret = core->ops->prepare(core->hw);

                trace_clk_prepare_complete(core);

                if (ret)
                        goto unprepare;
        }

        core->prepare_count++;

        /*
         * CLK_SET_RATE_GATE is a special case of clock protection
         * Instead of a consumer claiming exclusive rate control, it is
         * actually the provider which prevents any consumer from making any
         * operation which could result in a rate change or rate glitch while
         * the clock is prepared.
         */
        if (core->flags & CLK_SET_RATE_GATE)
                clk_core_rate_protect(core);

        return 0;
unprepare:
        clk_core_unprepare(core->parent);
runtime_put:
        clk_pm_runtime_put(core);
        return ret;
}

상위 클럭 소스까지 prepare 시킨다.

  • 코드 라인 7~8에서 이 함수는 재귀호출에서 사용되므로 클럭이 지정되지 않으면 빠져나간다.
  • 코드 라인 10~28에서 한 번도 prepare 한 적이 없는 경우 다음과 같은 동작을 처리한다.
    • 절전 기능이 있어 슬립된 클럭 코어의 경우 깨운다.
    • 부모 클럭에 대해 재귀 호출로 이 함수를 호출하여 prepare 한다.
    • 이 클럭 디바이스에 구현된 ops->prepare 후크 함수를 호출한다.
  • 코드 라인 30에서 이 클럭 코어의 prepare_count를 1 증가시킨다.
  • 코드 라인 39~40에서 이 클럭 코어가 rate를 변경할 때 반드시 gate가 닫혀있어야 하는 경우 rate를 바꿀 수 없도록 protect를 설정한다.
  • 코드 라인 42에서 정상 값 0을 반환한다.

 

클럭 해제(unprepare)

clk_unprepare()

drivers/clk/clk.c

/**
 * clk_unprepare - undo preparation of a clock source
 * @clk: the clk being unprepared
 *
 * clk_unprepare may sleep, which differentiates it from clk_disable.  In a
 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
 * if the operation may sleep.  One example is a clk which is accessed over
 * I2c.  In the complex case a clk gate operation may require a fast and a slow
 * part.  It is this reason that clk_unprepare and clk_disable are not mutually
 * exclusive.  In fact clk_disable must be called before clk_unprepare.
 */
void clk_unprepare(struct clk *clk)
{
        if (IS_ERR_OR_NULL(clk))
                return;

        clk_core_unprepare_lock(clk->core);
}
EXPORT_SYMBOL_GPL(clk_unprepare);

상위 클럭 소스까지 unprepare 시킨다

 

clk_core_unprepare_lock()

drivers/clk/clk.c

static void clk_core_unprepare_lock(struct clk_core *core)
{
        clk_prepare_lock();
        clk_core_unprepare(core);
        clk_prepare_unlock();
}

lock을 획득한 상태에서 상위 클럭 소스까지 unprepare 시킨다

 

clk_core_unprepare()

drivers/clk/clk.c

static void clk_core_unprepare(struct clk_core *core)
{
        lockdep_assert_held(&prepare_lock);

        if (!core)
                return;

        if (WARN(core->prepare_count == 0,
            "%s already unprepared\n", core->name))
                return;

        if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL,
            "Unpreparing critical %s\n", core->name))
                return;

        if (core->flags & CLK_SET_RATE_GATE)
                clk_core_rate_unprotect(core);

        if (--core->prepare_count > 0)
                return;

        WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name);

        trace_clk_unprepare(core);

        if (core->ops->unprepare)
                core->ops->unprepare(core->hw);

        clk_pm_runtime_put(core);

        trace_clk_unprepare_complete(core);
        clk_core_unprepare(core->parent);
}

상위 클럭 소스까지 unprepare 시킨다.

  • 코드 라인 5~6에서 이 함수는 재귀호출을 하게 하였으며 클럭이 지정되지 않으면 빠져나간다.
  • 코드 라인 8~10에서 이미 unprepare된 클럭 코어는 경고 메시지 출력과 함께 함수를 빠져나간다.
  • 코드 라인 12~14에서 클럭의 출력을 off하게 막은 클럭 코어의 경우 경고 메시지 출력과 함께 함수를 빠져나간다.
  • 코드 라인 16~17에서 이 클럭 코어가 rate를 변경할 때 반드시 gate가 닫혀있어야 하는 경우이다. 이제 unprepare하여 gate가 닫혀 rate를 바꿀 수 있는 상태가 되었으므로 protect를 해제한다.
  • 코드 라인 19~20에서 prepare_count를 1 감소시킨다. 실제 0이 되기 전까지 이 클럭 코어를 unprepare 하지 않는다.
    • 최상위 부모 클럭까지 재귀 호출되어 1씩 감소시킨다.
  • 코드 라인 26~27에서 클럭 디바이스 드라이버에 구현된 ops->unprepare 후크를 호출한다.
  • 코드 라인 29에서 절전 기능을 가진 클럭 코어인 경우 절전 상태에 진입한다.
  • 코드 라인 32에서 상위 부모 클럭 코어를 unprepare하도록 이 함수를 호출한다. 재귀 동작으로 최상위 클럭 코어까지 호출된다.

 


클럭 게이트 enable & disable

클럭 게이트 enable

clk_enable()

drivers/clk/clk.c

/**
 * clk_enable - ungate a clock
 * @clk: the clk being ungated
 *
 * clk_enable must not sleep, which differentiates it from clk_prepare.  In a
 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
 * if the operation will never sleep.  One example is a SoC-internal clk which
 * is controlled via simple register writes.  In the complex case a clk ungate
 * operation may require a fast and a slow part.  It is this reason that
 * clk_enable and clk_prepare are not mutually exclusive.  In fact clk_prepare
 * must be called before clk_enable.  Returns 0 on success, -EERROR
 * otherwise.
 */
int clk_enable(struct clk *clk)
{
        if (!clk)
                return 0;

        return clk_core_enable_lock(clk->core);
}
EXPORT_SYMBOL_GPL(clk_enable);

상위 클럭 소스까지 gate를 enable 시킨다.

 

clk_core_enable_lock()

drivers/clk/clk.c

static int clk_core_enable_lock(struct clk_core *core)
{
        unsigned long flags;
        int ret;

        flags = clk_enable_lock();
        ret = clk_core_enable(core);
        clk_enable_unlock(flags);

        return ret;
}

lock을 획득한 상태에서 상위 클럭 소스까지 gate를 enable 시킨다.

 

clk_core_enable()

drivers/clk/clk.c

static int clk_core_enable(struct clk_core *core)
{
        int ret = 0;

        lockdep_assert_held(&enable_lock);

        if (!core)
                return 0;

        if (WARN(core->prepare_count == 0,
            "Enabling unprepared %s\n", core->name))
                return -ESHUTDOWN;

        if (core->enable_count == 0) {
                ret = clk_core_enable(core->parent);

                if (ret)
                        return ret;

                trace_clk_enable_rcuidle(core);

                if (core->ops->enable)
                        ret = core->ops->enable(core->hw);

                trace_clk_enable_complete_rcuidle(core);

                if (ret) {
                        clk_core_disable(core->parent);
                        return ret;
                }
        }

        core->enable_count++;
        return 0;
}

상위 클럭 소스까지 gate를 enable 시킨다.

  • 코드 라인 7~8에서 이 함수는 재귀호출에서 사용되므로 클럭이 지정되지 않으면 빠져나간다.
  • 코드 라인 10~12에서 아직 unprepare된 클럭 코어는 경고 메시지 출력과 함께 함수를 빠져나간다.
  • 코드 라인 14~31에서 한 번도 enable 한 적이 없는 경우 다음과 같은 동작을 처리한다.
    • 부모 클럭에 대해 재귀 호출로 이 함수를 호출하여 enable 한다.
    • 이 클럭 디바이스에 구현된 ops->enable 후크 함수를 호출한다.
  • 코드 라인 33에서 이 클럭 코어의 enable_count를 1 증가시킨다.
  • 코드 라인 34에서 정상 값 0을 반환한다.

 

클럭 게이트 disable

clk_disable()

drivers/clk/clk.c

/**
 * clk_disable - gate a clock
 * @clk: the clk being gated
 *
 * clk_disable must not sleep, which differentiates it from clk_unprepare.  In
 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
 * clk if the operation is fast and will never sleep.  One example is a
 * SoC-internal clk which is controlled via simple register writes.  In the
 * complex case a clk gate operation may require a fast and a slow part.  It is
 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
 * In fact clk_disable must be called before clk_unprepare.
 */
void clk_disable(struct clk *clk)
{
        if (IS_ERR_OR_NULL(clk))
                return;

        clk_core_disable_lock(clk->core);
}
EXPORT_SYMBOL_GPL(clk_disable);

상위 클럭 소스까지 gate를 disable 시킨다.

 

clk_core_disable_lock()

drivers/clk/clk.c

static void clk_core_disable_lock(struct clk_core *core)
{
        unsigned long flags;

        flags = clk_enable_lock();
        clk_core_disable(core);
        clk_enable_unlock(flags);
}

lock을 획득한 상태에서 상위 클럭 소스까지 gate를 disable 시킨다.

 

clk_core_disable()

drivers/clk/clk.c

static void clk_core_disable(struct clk_core *core)
{
        lockdep_assert_held(&enable_lock);

        if (!core)
                return;

        if (WARN(core->enable_count == 0, "%s already disabled\n", core->name))
                return;

        if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL,
            "Disabling critical %s\n", core->name))
                return;

        if (--core->enable_count > 0)
                return;

        trace_clk_disable_rcuidle(core);

        if (core->ops->disable)
                core->ops->disable(core->hw);

        trace_clk_disable_complete_rcuidle(core);

        clk_core_disable(core->parent);
}

상위 클럭 소스까지 gate를 disable 시킨다.

  • 코드 라인 5~6에서 이 함수는 재귀호출에서 사용되므로 클럭이 지정되지 않으면 빠져나간다.
  • 코드 라인 8~9에서 아직 enable되지 않은 클럭 코어는 경고 메시지 출력과 함께 함수를 빠져나간다.
  • 코드 라인 11~13에서 클럭의 출력을 항상 유지해야 하는 critical 클럭 코어의 경우 disable하면 안되므로 경고 메시지 출력과 함께 함수를 빠져나간다.
  • 코드 라인 15~16에서 enable_count를 1 감소시킨다. 실제 0이 되기 전까지 이 클럭 코어를 disable 하지 않는다.
    • 최상위 부모 클럭까지 재귀 호출되어 1씩 감소시킨다.
  • 코드 라인 20~21 이 클럭 디바이스에 구현된 ops->disable 후크 함수를 호출한다.
  • 코드 라인 25에서 상위 부모 클럭에 대해 disable하기 위해 이 함수를 재귀호출한다. 최상위 클럭 코어까지 호출된다.

 


Rate 설정

clk_rate_request 구조체

include/linux/clk-provider.h

/**
 * struct clk_rate_request - Structure encoding the clk constraints that
 * a clock user might require.
 *
 * @rate:               Requested clock rate. This field will be adjusted by
 *                      clock drivers according to hardware capabilities.
 * @min_rate:           Minimum rate imposed by clk users.
 * @max_rate:           Maximum rate imposed by clk users.
 * @best_parent_rate:   The best parent rate a parent can provide to fulfill the
 *                      requested constraints.
 * @best_parent_hw:     The most appropriate parent clock that fulfills the
 *                      requested constraints.
 *
 */
struct clk_rate_request {
        unsigned long rate;
        unsigned long min_rate;
        unsigned long max_rate;
        unsigned long best_parent_rate;
        struct clk_hw *best_parent_hw;
};

rate에 관련한 API 사용 시 인자 수를 줄이기 위해 사용되는 구조체이다.

  • rate
    • rate 값
  • min_rate
    • 최소 rate 값
  • max_rate
    • 최대 rate 값
  • best_parent_rate
    • 가장 적합한 부모 클럭 rate 값
  • *best_parent_hw
    • 가장 적합한 부모 클럭 hw 포인터

다음 그림은 clk_set_rate() 함수가 rate를 변경하기 위해 호출하는 4 단계의 주요 함수만을 보여준다.

clk_set_rate()

drivers/clk/clk.c

/**
 * clk_set_rate - specify a new rate for clk
 * @clk: the clk whose rate is being changed
 * @rate: the new rate for clk
 *
 * In the simplest case clk_set_rate will only adjust the rate of clk.
 *
 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
 * propagate up to clk's parent; whether or not this happens depends on the
 * outcome of clk's .round_rate implementation.  If *parent_rate is unchanged
 * after calling .round_rate then upstream parent propagation is ignored.  If
 * *parent_rate comes back with a new rate for clk's parent then we propagate
 * up to clk's parent and set its rate.  Upward propagation will continue
 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
 * .round_rate stops requesting changes to clk's parent_rate.
 *
 * Rate changes are accomplished via tree traversal that also recalculates the
 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
 *
 * Returns 0 on success, -EERROR otherwise.
 */
int clk_set_rate(struct clk *clk, unsigned long rate)
{
        int ret; 

        if (!clk)
                return 0;

        /* prevent racing with updates to the clock topology */
        clk_prepare_lock();

        if (clk->exclusive_count)
                clk_core_rate_unprotect(clk->core);
 
        ret = clk_core_set_rate_nolock(clk->core, rate);

        if (clk->exclusive_count)
                clk_core_rate_protect(clk->core);
        
        clk_prepare_unlock();
                
        return ret;     
} 
EXPORT_SYMBOL_GPL(clk_set_rate);

클럭 rate를 변경 요청한다.

  • CLK_SET_RATE_PARENT 플래그가 사용된 클럭은 rate 설정 시 상위 클럭으로 전파되도록 한다.

 

clk_core_set_rate_nolock()

drivers/clk/clk.c

static int clk_core_set_rate_nolock(struct clk_core *core,
                                    unsigned long req_rate)
{
        struct clk_core *top, *fail_clk;
        unsigned long rate = req_rate;
        int ret = 0;

        if (!core)
                return 0;

        rate = clk_core_req_round_rate_nolock(core, req_rate);
        /* bail early if nothing to do */
        if (rate == clk_core_get_rate_nolock(core)
                return 0;

        /* fail on a direct rate set of a protected provider */
        if (clk_core_rate_is_protected(core))
                return -EBUSY;
        /* calculate new rates and get the topmost changed clock */
        top = clk_calc_new_rates(core, rate);
        if (!top)
                return -EINVAL;

        /* notify that we are about to change rates */
        fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
        if (fail_clk) {
                pr_debug("%s: failed to set %s rate\n", __func__,
                                fail_clk->name);
                clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
                return -EBUSY;
        }

        /* change the rates */
        clk_change_rate(top);

        clk->req_rate = req_rate;
err:
        clk_pm_runtime_put(core);
        return ret;
}

클럭의 rate 설정을 한다. 성공 시에 0을 반환한다. 적용 시 현재 클럭 curr에서 변경이 필요한 클럭 코어 top까지 상위로 이동하면서 new rate를 산출하고, top 클럭에서 관련 클럭들의 최하위 클럭 bottom까지 rate를 재산출한다.

1 단계 – new rate 변경 필요 확인 (top <- curr)
  • 코드 라인 11~14에서 @req_rate에 대해 클럭 hw가 지원하는 가장 근접한 rate 값을 알아와서 기존 설정되었던 rate 값과 변화가 있는지 사전 체크한다. 만일 rate 변화가 없으면 req_rate를 적용할 필요 없으므로 성공 값 0을 반환한다.
  • 코드 라인 17~18에서 rate 변경이 protect된 상태이다. 이러한 경우 -EBUSY를 반환한다.
    • exclusive된 클럭 코어의 경우 다른 유저가 rate를 설정할 수 없다.
    • CLK_SET_RATE_GATE 플래그 설정된 클럭 코어의 경우 클럭이 prepare되어 gate가 열린 상태에선 rate를 설정할 수 없다.
2 단계 – new rate & parent 산출 (top <- curr)
  • 코드 라인 20~22에서 @req_rate에 대해 해당 클럭 코어부터 변경이 필요한 상위 클럭 코어까지 위로 올라가며 클럭 hw가 지원하는 가장 근접한 new rate 값을 각각 산출해둔다. 그리고 변경이 필요한 최상위 최상위 클럭 코어를 알아온다.
    • CLK_SET_RATE_PARENT 플래그가 사용된 클럭 코어들은 그 상위 클럭 코어에서 rate를 결정한다.
    • rate를 결정하는 클럭 코어의 타입에 따라 다음과 같이 재계산된다.
      • Mux 타입
        • determine_rate() 함수를 사용하여 어떤 부모 클럭 코어를 사용해야 요청한 노드의 rate에 인접한 값이 나올 수 있는지 계산된다.
      • 그 외 rate 조절 타입
        • round_rate() 함수를 사용하여 어떤 배율의 클럭 코어를 사용해야 요청한 노드의 rate에 인접한 값이 나올 수 있는지 계산된다.
3 단계 – new rate & parent 적용 통지 체크 (top -> bottom)
  • 코드 라인 25~31에서 적용할 클럭 코어들에 PRE_RATE_CHANGE를 통지하여 rate를 재설정 준비를 한다. 이 과정에서 실패하면 ABORT_RATE_CHANGE를 통지하여 roll-back 한다.
    • 상위 부모 클럭 코어까지 rate를 설정하게 할 때 상위 전달(propagation) 과정에서 notify되는 함수에서 해당 클럭 코어의 설정을 허용할지 여부를 결정하게 한다.
4 단계 – new rate & parent 적용 (top -> bottom)
  • 코드 라인 34에서 top 클럭 코어부터 마지막 자식 클럭 코어까지 산출된 new rate 및 new 부모 클럭을 적용하고 통지한다.
  • 코드 라인 36에서 현재 클럭 코어의 req_rate를 설정한다.
  • 코드 라인 38에서 필요 시 절전 기능을 켠다.

 

다음 그림은 클럭 F에서 rate를 바꿀 때 클럭 F->G까지 어떠한 단계로 바뀌는지 그 과정을 보여준다. (성공 예)

 

다음 그림은 위의 방법으로 실패 사례를 보여준다.

 


1 단계 – new rate 변경 필요 확인

best rate 산출과 관련한 API들은 다음과 같다.

  • clk_round_rate()
  • __clk_round_rate()
    • 호출 전에 먼저 lock을 획득한 상태여야 한다.
  • __clk_determine_rate()
    • 호출 전에 먼저 lock을 획득한 상태여야 한다.
  • clk_hw_round_rate()

 

clk_round_rate()

drivers/clk/clk.c

/**
 * clk_round_rate - round the given rate for a clk
 * @clk: the clk for which we are rounding a rate
 * @rate: the rate which is to be rounded
 *
 * Takes in a rate as input and rounds it to a rate that the clk can actually
 * use which is then returned.  If clk doesn't support round_rate operation
 * then the parent rate is returned.
 */
long clk_round_rate(struct clk *clk, unsigned long rate)
{
        struct clk_rate_request req;
        int ret;

        if (!clk)
                return 0;

        clk_prepare_lock();

        if (clk->exclusive_count)
                clk_core_rate_unprotect(clk->core);

        clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
        req.rate = rate;

        ret = clk_core_round_rate_nolock(clk->core, &req);

        if (clk->exclusive_count)
                clk_core_rate_protect(clk->core);

        clk_prepare_unlock();

        if (ret)
                return ret;

        return req.rate;
}
EXPORT_SYMBOL_GPL(clk_round_rate);

요청 @rate에 대해 클럭 hw가 지원하는 가장 근접한 rate를 반환한다. 만일 클럭이 (*round_rate) ops를 지원하지 않으면 부모 클럭의 rate 값이 반환된다.

  • 코드 라인 11~12에서 클럭 사용자가 독점 관리하는 경우 가장 근접한 rate 계산을 위해 잠시 unprotect를 한다.
  • 코드 라인 14에서 자식 클럭들로부터 min_rate 및 max_rate 바운더리 값을 알아온다.
  • 코드 라인 17에서 요청 rate에 대해 클럭 hw가 지원하는 가장 근접한 rate를 반환한다. 만일 클럭이 ops->round_rate를 지원하지 않으면 부모 클럭의 rate 값이 반환된다.
  • 코드 라인 19~20에서 클럭 코어를 독점(exclusive)적으로 관리하는 경우 round rate 계산이 완료되었으므로 다시 protect를 한다.
  • 코드 라인 27에서 rate 값을 반환한다.

 

__clk_round_rate()

drivers/clk/clk.c

/**
 * __clk_round_rate - round the given rate for a clk
 * @clk: round the rate of this clock
 * @rate: the rate which is to be rounded
 *
 * Caller must hold prepare_lock.  Useful for clk_ops such as .set_rate
 */
unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
{
        unsigned long min_rate;
        unsigned long max_rate;

        if (!clk)
                return 0;

        clk_core_get_boundaries(clk->core, &min_rate, &max_rate);

        return clk_core_round_rate_nolock(clk->core, rate, min_rate, max_rate);
}
EXPORT_SYMBOL_GPL(__clk_round_rate);

요청 @rate에 대해 클럭 hw가 지원하는 가장 근접한 rate를 반환한다. 만일 클럭이 (*round_rate) ops를 지원하지 않으면 부모 클럭의 rate 값이 반환된다. 호출 시 prepare_lock을 잡아야 한다.

  • 코드 라인 16에서 자식 클럭들로부터 min_rate 및 max_rate 바운더리 값을 알아온다.
  • 코드 라인 18에서 요청 @rate에 대해 클럭 hw가 지원하는 가장 근접한 rate를 반환한다. 만일 클럭이 ops->round_rate를 지원하지 않으면 부모 클럭의 rate 값이 반환된다.

 

clk_hw_round_rate()

drivers/clk/clk.c

unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
{
        int ret;
        struct clk_rate_request req;

        clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
        req.rate = rate;

        ret = clk_core_round_rate_nolock(hw->core, &req);
        if (ret)
                return 0;

        return req.rate;
}
EXPORT_SYMBOL_GPL(clk_hw_round_rate);

요청 @rate에 대해 클럭 hw가 지원하는 가장 근접한  rate를 반환한다. 실패한 경우 0을 반환한다.

  • 코드 라인 6에서 자식 클럭들로부터 min_rate 및 max_rate 바운더리 값을 알아온다.
  • 코드 라인 9~11에서 클럭 hw가 지원하는 @rate에 근접한 rate를 반환한다. 만일 min_rate 및 max_rate 값을 초과하는 경우 실패 0 을 반환한다.
  • 코드 라인 13에서 결정된 rate 값을 반환한다.

 

__clk_determine_rate()

drivers/clk/clk.c

/**
 * __clk_determine_rate - get the closest rate actually supported by a clock
 * @hw: determine the rate of this clock
 * @req: target rate request
 *
 * Useful for clk_ops such as .set_rate and .determine_rate.
 */
int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
{
        if (!hw) {
                req->rate = 0;
                return 0;
        }

        return clk_core_round_rate_nolock(hw->core, req);
}
EXPORT_SYMBOL_GPL(__clk_determine_rate);

요청 rate에 대해 클럭 hw가 지원하는 가장 근접한  rate를 찾아 req->rate에 담아온다. 성공 시 0을 반환한다.

 

clk_core_req_round_rate_nolock()

drivers/clk/clk.c

static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
                                                     unsigned long req_rate)
{
        int ret, cnt;
        struct clk_rate_request req;

        lockdep_assert_held(&prepare_lock);

        if (!core)
                return 0;

        /* simulate what the rate would be if it could be freely set */
        cnt = clk_core_rate_nuke_protect(core);
        if (cnt < 0)
                return cnt;

        clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
        req.rate = req_rate;

        ret = clk_core_round_rate_nolock(core, &req);

        /* restore the protection */
        clk_core_rate_restore_protect(core, cnt);

        return ret ? 0 : req.rate;
}

클럭 hw가 지원하는 @req_rate에 가장 근접한  rate를 반환한다. 실패한 경우 0을 반환한다.

  • 코드 라인 9~10에서 클럭 코어가 지정되지 않은 경우 0을 반환한다.
  • 코드 라인 13~15에서  클럭 코어에 protect가 걸려있는 경우 잠시 해제한다.
  • 코드 라인 17에서 자식 클럭들로부터 min_rate 및 max_rate 바운더리 값을 알아온다.
  • 코드 라인 20에서 rate 요청에 대해 클럭 hw가 지원하는 가장 근접한 rate를 반환한다. 만일 min_rate 및 max_rate 값을 초과하는 경우 에러(음수) 값을 얻어온다.
  • 코드 라인 23에서 클럭 코어를 잠시 unprotect 한 경우 다시 원래 대로 복원한다.
  • 코드 라인 25에서 결정된 rate 값을 반환한다. 만일 실패한 경우 0을 반환한다.

 

다음 그림은 rate 변경이 필요한지 유무를 판단하기 위해 호출하는 과정을 보여준다.

 

min_rate & max_rate boundary 산출

clk_core_get_boundaries()

drivers/clk/clk.c

static void clk_core_get_boundaries(struct clk_core *core,
                                    unsigned long *min_rate,
                                    unsigned long *max_rate)
{
        struct clk *clk_user;

        lockdep_assert_held(&prepare_lock);

        *min_rate = core->min_rate;
        *max_rate = core->max_rate;

        hlist_for_each_entry(clk_user, &core->clks, clks_node)
                *min_rate = max(*min_rate, clk_user->min_rate);

        hlist_for_each_entry(clk_user, &core->clks, clks_node)
                *max_rate = min(*max_rate, clk_user->max_rate);
}

자식 클럭들로부터 min_rate 및 max_rate 바운더리 값을 알아온다.

  • min_rate
    • 자식 클럭들의 min_rate 값들 중 최대 min_rate
  • max_rate
    • 자식 클럭들의 max_rate 값들 중 최소 max_rate

 

다음 그림은 자식 클럭들로부터 min_rate 및 max_rate 바운더리 값을 알아오는 것을 보여준다. (min_rate가 max_rate 보다 큰 숫자임을 주의한다)

 

연속하여 다음 두 함수를 알아본다.

  • clk_core_round_rate_nolock()
  • clk_core_determine_round_nolock()

 

clk_core_round_rate_nolock()

drivers/clk/clk.c

static int clk_core_round_rate_nolock(struct clk_core *core,
                                      struct clk_rate_request *req)
{
        lockdep_assert_held(&prepare_lock);

        if (!core) {
                req->rate = 0;
                return 0;
        }

        clk_core_init_rate_req(core, req);

        if (clk_core_can_round(core))
                return clk_core_determine_round_nolock(core, req);
        else if (core->flags & CLK_SET_RATE_PARENT)
                return clk_core_round_rate_nolock(core->parent, req);

        req->rate = core->rate;
        return 0;
}

rate 요청에 대해 클럭 hw가 지원하는 가장 근접한 rate를 찾아 req->rate에 담고, 성공 시 0을 반환한다.

  • 코드 라인 6~9에서 재귀 호출되었을 때 클럭 코어가 없으면 req->rate에 0을 담고, 성공 값 0을 반환한다.
  • 코드 라인 11에서 rate 요청 전에 현재 부모 클럭 및 rate로 best 결과를 초기화한다.
  • 코드 라인 13~14에서 클럭이 ops->determine_rate를 지원하는 mux 타입 클럭인 경우 호출하여 hw에 맞춰 적절한 rate 값을 산출하여 반환한다.
  • 코드 라인 15~16에서 CLK_SET_RATE_PARENT 플래그를 사용한 클럭은 rate 산출을 위해 부모 클럭에게 위임하러 이 함수를 재귀 호출한다.
  • 코드 라인 18~19에서 그 외의 클럭 타입인 경우 현재 클럭의 rate로 결정하고, 성공 값 0을 반환한다.

 

clk_core_determine_round_nolock()

drivers/clk/clk.c

static int clk_core_determine_round_nolock(struct clk_core *core,
                                           struct clk_rate_request *req)
{
        long rate;

        lockdep_assert_held(&prepare_lock);

        if (!core)
                return 0;

        /*
         * At this point, core protection will be disabled if
         * - if the provider is not protected at all
         * - if the calling consumer is the only one which has exclusivity
         *   over the provider
         */
        if (clk_core_rate_is_protected(core)) {
                req->rate = core->rate;
        } else if (core->ops->determine_rate) {
                return core->ops->determine_rate(core->hw, req);
        } else if (core->ops->round_rate) {
                rate = core->ops->round_rate(core->hw, req->rate,
                                             &req->best_parent_rate);
                if (rate < 0)
                        return rate;

                req->rate = rate;
        } else {
                return -EINVAL;
        }

        return 0;
}

rate 요청에 대해 클럭 hw가 지원하는 가장 근접한 rate를 찾아 req->rate에 담고, 성공 시 0을 반환한다.

  • 코드 라인 8~9에서 재귀 호출되었을 때 클럭 코어가 없으면 0을 반환한다.
  • 코드 라인 17~18에서 클럭 코어가 protect된 경우 결과 값으로 현재 클럭 코어의 rate 값을 req->rate에 대입한다.
  • 코드 라인 19~20에서 mux 클럭 타입에서 지원하는 (*determine_rate) 후크 함수를 호출하여 산출된 rate 값을 반환한다.
  • 코드 라인 21~27에서 rate류 클럭 타입에서 지원하는 (*round_rate) 후크 함수를 호출하여 rate 값을 알아와서 req->rate에 설정하고, 성공 0을 반환한다.

 

rate 요청 전 초기화

clk_core_init_rate_req()

drivers/clk/clk.c

static void clk_core_init_rate_req(struct clk_core * const core,
                                   struct clk_rate_request *req)
{
        struct clk_core *parent;

        if (WARN_ON(!core || !req))
                return;

        parent = core->parent;
        if (parent) {
                req->best_parent_hw = parent->hw;
                req->best_parent_rate = parent->rate;
        } else {
                req->best_parent_hw = NULL;
                req->best_parent_rate = 0;
        }
}

현재 부모 클럭 및 rate로 best 결과를 초기화한다.

  • 현재 클럭의 best 부모 클럭 및 rate로 현재 부모 클럭 및 rate를 지정한다.

 

clk_core_can_round()

drivers/clk/clk.c

static bool clk_core_can_round(struct clk_core * const core)
{
        return core->ops->determine_rate || core->ops->round_rate;
}

round & determine rate를 지원하는 클럭인지 여부를 반환한다.

  • (*determine_rate) 후크 함수는 mux 클럭 타입에서 지원한다.
  • (*round_rate) 후크 함수는 rate 클럭 타입에서 지원한다.

 


2 단계 – new rate & parent 산출

clk_calc_new_rates()

drivers/clk/clk.c

/*
 * calculate the new rates returning the topmost clock that has to be
 * changed.
 */
static struct clk_core *clk_calc_new_rates(struct clk_core *core,
                                           unsigned long rate)
{
        struct clk_core *top = core;
        struct clk_core *old_parent, *parent;
        unsigned long best_parent_rate = 0;
        unsigned long new_rate;
        unsigned long min_rate;
        unsigned long max_rate;
        int p_index = 0;
        long ret;

        /* sanity */
        if (IS_ERR_OR_NULL(core))
                return NULL;

        /* save parent rate, if it exists */
        parent = old_parent = core->parent;
        if (parent)
                best_parent_rate = parent->rate;

        clk_core_get_boundaries(core, &min_rate, &max_rate);

        /* find the closest rate and parent clk/rate */
        if (clk_core_can_round(core)) {
                struct clk_rate_request req;

                req.rate = rate;
                req.min_rate = min_rate;
                req.max_rate = max_rate;

                clk_core_init_rate_req(core, &req);

                ret = clk_core_determine_round_nolock(core, &req);
                if (ret < 0)
                        return NULL;

                best_parent_rate = req.best_parent_rate;
                new_rate = req.rate;
                parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;

                if (new_rate < min_rate || new_rate > max_rate)
                        return NULL;
        } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
                /* pass-through clock without adjustable parent */
                core->new_rate = core->rate;
                return NULL;
        } else {
                /* pass-through clock with adjustable parent */
                top = clk_calc_new_rates(parent, rate);
                new_rate = parent->new_rate;
                goto out;
        }

        /* some clocks must be gated to change parent */
        if (parent != old_parent &&
            (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
                pr_debug("%s: %s not gated but wants to reparent\n",
                         __func__, core->name);
                return NULL;
        }

        /* try finding the new parent index */
        if (parent && core->num_parents > 1) {
                p_index = clk_fetch_parent_index(core, parent);
                if (p_index < 0) {
                        pr_debug("%s: clk %s can not be parent of clk %s\n",
                                 __func__, parent->name, core->name);
                        return NULL;
                }
        }

        if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
            best_parent_rate != parent->rate)
                top = clk_calc_new_rates(parent, best_parent_rate);

out:
        clk_calc_subtree(core, new_rate, parent, p_index);

        return top;
}

현재 클럭 코어로부터 변경이 필요한 상위 클럭까지 new rate를 산출하고, new rate 산출된 최상위 클럭 코어를 반환한다.

  • 코드 라인 18에서 요청 클럭 코어의 부모 클럭 코어를 알아와서 parent 및 old_parent에 보관한다.
  • 코드 라인 19~20에서 부모 클럭 코어가 존재하는 경우 부모 클럭 코어의 rate를 best_parent_rate에 보관한다.
  • 코드 라인 22에서 자식 클럭 코어들로부터 min_rate 및 max_rate 바운더리 값을 알아온다.
  • 코드 라인 25~43에서 rate 최적값을 구하는 후크 함수가 지원되는 경우 이를 호출하여 최적의 rate 값을 알아온다.
    • 참고:
      • drivers/clk/ti/mux.c – __clk_mux_determine_rate()
      • drivers/clk/ti/divider.c – ti_clk_divider_round_rate()
  • 코드 라인 44~47에서 부모 클럭이 없거나 CLK_SET_RATE_PARENT 플래그가 없는 경우 현재 클럭의 rate만을 변경한다.
  • 코드 라인 48~53에서 부모 클럭의 rate를 산출하기 위해 인수로 부모 클럭과 요청 rate 값을 가지고 이 함수를 재귀호출하여 new_rate를 알아온다.
    • ops->determine_rate 및 ops->round_rate가 없는 gate 타입의 클럭을 사용하는 경우 CLK_SET_RATE_PARENT 플래그가 사용되지 않을 때까지 상위 클럭으로 이동한다.
  • 코드 라인 56~61에서 mux 타입 클럭 코어에서 rate 변경 요청으로 인해 부모 클럭의 변경이 필요한 상태이며 현재 클럭 코어에 CLK_SET_PARENT_GATE 플래그가 설정된 경우일 때 gate가 열린 상태이면 null을 반환한다.
    • CLK_SET_PARENT_GATE 플래그 옵션을 사용하는 mux 클럭 코어인 경우 gate를 닫지 않으면 mux에서 부모 클럭 코어의 변경이 실패한다.
  • 코드 라인 64~71에서 mux 타입 클럭 코어에서 부모 클럭 코어가 2개 이상이면 현재 선택된 부모 인덱스 값을 알아온다. 만일 알아올 수 없으면 null을 반환한다.
  • 코드 라인 73~75에서 클럭 코어에 CLK_SET_RATE_PARENT 플래그가 설정되었고 parent의 rate가 변경된 경우 인수로 부모 클럭과 best_parent_rate 값으로 이 함수를 재귀호출하여 상위 클럭으로 올라가서 rate가 변경될 상위 부모 클럭을 알아온다.
    • CLK_SET_RATE_PARENT 플래그가 사용되면 사용자 요청에 의해 현재 클럭의 hw가 지원하는 rate가 설정 불가능하면 부모 클럭의 hw가 지원하는 rate를 변경한다.
  • 코드 라인 77~78에서 out: 레이블이다. 이 함수가 재귀 호출된 경우 rate가 변경될 상위 부모 클럭부터 시작하여 연결된 모든 자식 클럭 방향으로 rate를 재계산하게 한다.
  • 코드 라인 80에서 변경된 최상위 클럭 코어를 반환한다.

 

다음 그림은 요청한 rate에 대해 클럭 hw가 지원하는 가장 근접한 new rate를 산출하는 과정을 보여준다.

 

다음 그림은 클럭 F에서 rate를 바꾸고자 계산하는 경우 클럭 F->D까지 rate를 산출하고 다시 클럭 D->G까지 재계산하는 과정을 보여준다.

 

clk_fetch_parent_index()

drivers/clk/clk.c

static int clk_fetch_parent_index(struct clk_core *core,
                                  struct clk_core *parent)
{
        int i;

        if (!parent)
                return -EINVAL;

        for (i = 0; i < core->num_parents; i++) {
                /* Found it first try! */
                if (core->parents[i].core == parent)
                        return i;

                /* Something else is here, so keep looking */
                if (core->parents[i].core)
                        continue;

                /* Maybe core hasn't been cached but the hw is all we know? */
                if (core->parents[i].hw) {
                        if (core->parents[i].hw == parent->hw)
                                break;

                        /* Didn't match, but we're expecting a clk_hw */
                        continue;
                }

                /* Maybe it hasn't been cached (clk_set_parent() path) */
                if (parent == clk_core_get(core, i))
                        break;

                /* Fallback to comparing globally unique names */
                if (core->parents[i].name &&
                    !strcmp(parent->name, core->parents[i].name))
                        break;
        }

        if (i == core->num_parents)
                return -EINVAL;

        core->parents[i].core = parent;
        return i;
}

현재 클럭 코어 @core의 부모 @parent 클럭 코어에 해당하는 인덱스 값을 알아온다. 실패하는 경우 음수 에러가 반환된다.

  • 코드 라인 9~12에서 num_parents 수 만큼 순회하며 @parent와 동일한 경우 해당 인덱스를 반환한다.
  • 코드 라인 15~16에서 다른 값을 가진 경우 skip 한다.
  • 코드 라인 19~25에서 동일한 부모 hw를 찾은 경우 해당 인덱스를 반환하기 위해 루프를 벗어난다.
  • 코드 라인 28~29에서 인덱스에 해당하는 부모 클럭이 @parent와 동일하면 해당 인덱스를 반환하기 위해 루프를 벗어난다.
  • 코드 라인 32~34에서 이름으로 검색하여 동일한 이름을 가진 부모 클럭 코어를 찾은 경우 해당 인덱스를 반환하기 위해 루프를 벗어난다.
  • 코드 라인 37~38에서 검색이 실패한 경우 -EINVAL을 반환한다.
  • 코드 라인 40~41에서 parent[] 맵에 부모 클럭 코어를 연결하고 해당 인덱스를 반환한다.

 


3 단계 – new rate & parent 적용 통지 체크

Rate 변경에 따른 통지 체크

clk_propagate_rate_change()

drivers/clk/clk.c

/*
 * Notify about rate changes in a subtree. Always walk down the whole tree
 * so that in case of an error we can walk down the whole tree again and
 * abort the change.
 */
static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
                                                  unsigned long event)
{
        struct clk_core *child, *tmp_clk, *fail_clk = NULL;
        int ret = NOTIFY_DONE;

        if (core->rate == core->new_rate)
                return NULL;

        if (core->notifier_count) {
                ret = __clk_notify(core, event, core->rate, core->new_rate);
                if (ret & NOTIFY_STOP_MASK)
                        fail_clk = core;
        }

        hlist_for_each_entry(child, &core->children, child_node) {
                /* Skip children who will be reparented to another clock */
                if (child->new_parent && child->new_parent != core)
                        continue;
                tmp_clk = clk_propagate_rate_change(child, event);
                if (tmp_clk)
                        fail_clk = tmp_clk;
        }

        /* handle the new child who might not be in core->children yet */
        if (core->new_child) {
                tmp_clk = clk_propagate_rate_change(core->new_child, event);
                if (tmp_clk)
                        fail_clk = tmp_clk;
        }

        return fail_clk;
}

요청 클럭 코어부터 연결된 모든 하위 트리의 클럭 코어에 rate 변화를 통지한다. 성공 시 null을 반환하고, 실패하는 경우 실패한 클럭 코어를 반환한다.

  • 코드 라인 7~8에서 rate의 변화가 없는 경우 성공 값 null을 반환한다.
  • 코드 라인 10~14에서 통지 대상으로 등록된 클럭 코어에 대해 rate 변화 요청을 통지한다. 만일 결과가 NOTIFY_BAD 또는 NOTIFY_STOP을 갖는 경우 반환 값으로 사용할 fail_clk에 이 클럭 코어를 대입한다.
  • 코드 라인 16~19에서 자식 클럭 코어 수 만큼 루프를 돌며 요청 클럭이 이 자식 클럭 코어의 새로운 부모 클럭 코어가 아닌 경우 skip 한다.
  • 코드 라인 20~22에서 자식 클럭 코어들에 rate 변화 요청을 통지한다. 만일 실패한 경우 반환 값으로 사용할 fail_clk에 에러를 반환한 클럭 코어를 담는다.
  • 코드 라인 26~30에서 요청 클럭 코어에 새로 연결될(곧 children에 들어갈) 클럭 코어가 있는 경우 그 new_child 클럭에 대해서도 rate 변화 요청을 통지한다. 만일 실패한 경우 반환 값으로 사용할 fail_clk에 에러를 반환한 클럭 코어를 담는다.

 

다음 그림은 new rate로 변경하기 전에 통지 체크할 클럭에 확인하는 과정을 보여준다.

 

__clk_notify()

drivers/clk/clk.c

/**
 * __clk_notify - call clk notifier chain
 * @core: clk that is changing rate
 * @msg: clk notifier type (see include/linux/clk.h)
 * @old_rate: old clk rate
 * @new_rate: new clk rate
 *
 * Triggers a notifier call chain on the clk rate-change notification
 * for 'clk'.  Passes a pointer to the struct clk and the previous
 * and current rates to the notifier callback.  Intended to be called by
 * internal clock code only.  Returns NOTIFY_DONE from the last driver
 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
 * a driver returns that.
 */
static int __clk_notify(struct clk_core *core, unsigned long msg,
                unsigned long old_rate, unsigned long new_rate)
{
        struct clk_notifier *cn;
        struct clk_notifier_data cnd;
        int ret = NOTIFY_DONE;

        cnd.old_rate = old_rate;
        cnd.new_rate = new_rate;

        list_for_each_entry(cn, &clk_notifier_list, node) {
                if (cn->clk->core == core) {
                        cnd.clk = cn->clk;
                        ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
                                        &cnd);
                        if (ret & NOTIFY_STOP_MASK)
                                return ret;
                }
        }

        return ret;
}

클럭 통지 리스트에 등록된 현재 클럭의 notifier 체인에 연결된 항목에 대해 srcu를 사용하여 모두 통지한다. 성공한 경우 NOTIFY_DONE을 반환한다.

 

클럭 Rate 변경 통지(notify) 등록 API

clk_notifier_register()

drivers/clk/clk.c

/***        clk rate change notifiers        ***/

/**
 * clk_notifier_register - add a clk rate change notifier
 * @clk: struct clk * to watch
 * @nb: struct notifier_block * with callback info
 *
 * Request notification when clk's rate changes.  This uses an SRCU
 * notifier because we want it to block and notifier unregistrations are
 * uncommon.  The callbacks associated with the notifier must not
 * re-enter into the clk framework by calling any top-level clk APIs;
 * this will cause a nested prepare_lock mutex.
 *
 * In all notification cases cases (pre, post and abort rate change) the
 * original clock rate is passed to the callback via struct
 * clk_notifier_data.old_rate and the new frequency is passed via struct
 * clk_notifier_data.new_rate.
 *
 * clk_notifier_register() must be called from non-atomic context.
 * Returns -EINVAL if called with null arguments, -ENOMEM upon
 * allocation failure; otherwise, passes along the return value of
 * srcu_notifier_chain_register().
 */
int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
{
        struct clk_notifier *cn;
        int ret = -ENOMEM;

        if (!clk || !nb)
                return -EINVAL;

        clk_prepare_lock();

        /* search the list of notifiers for this clk */
        list_for_each_entry(cn, &clk_notifier_list, node)
                if (cn->clk == clk)
                        break;

        /* if clk wasn't in the notifier list, allocate new clk_notifier */
        if (cn->clk != clk) {
                cn = kzalloc(sizeof(*cn), GFP_KERNEL);
                if (!cn)
                        goto out;

                cn->clk = clk;
                srcu_init_notifier_head(&cn->notifier_head);

                list_add(&cn->node, &clk_notifier_list);
        }

        ret = srcu_notifier_chain_register(&cn->notifier_head, nb);

        clk->core->notifier_count++;

out:
        clk_prepare_unlock();

        return ret;
}
EXPORT_SYMBOL_GPL(clk_notifier_register);

클럭의 notify chain에 nofitier_block을 등록한다.

  • 코드 라인 12~14에서 clk_notifier_list에 요청한 클럭이 있는지 검색한다.
  • 코드 라인 17~26에서 검색되지 않는 경우 clk_notifier 구조체를 할당하고 클럭 정보를 대입한 후 clk_notifier_list에 등록한다.
  • 코드 라인 28에서 clk_notifier 구조체의 멤버 notifier_head에 요청한 notifier_block을 추가한다.
  • 코드 라인 30에서 클럭의 notifier_count 값을 1 증가시킨다.

 


4 단계 – 산출된 new rate & parent 적용

clk_change_rate()

drivers/clk/clk.c -1/2-

/*
 * walk down a subtree and set the new rates notifying the rate
 * change on the way
 */
static void clk_change_rate(struct clk_core *core)
{
        struct clk_core *child;
        struct hlist_node *tmp;
        unsigned long old_rate;
        unsigned long best_parent_rate = 0;
        bool skip_set_rate = false;
        struct clk_core *old_parent;
        struct clk_core *parent = NULL;

        old_rate = core->rate;

        if (core->new_parent) {
                parent = core->new_parent;
                best_parent_rate = core->new_parent->rate;
        } else if (core->parent) {
                parent = core->parent;
                best_parent_rate = core->parent->rate;
        }

        if (clk_pm_runtime_get(core))
                return;

        if (core->flags & CLK_SET_RATE_UNGATE) {
                unsigned long flags;

                clk_core_prepare(core);
                flags = clk_enable_lock();
                clk_core_enable(core);
                clk_enable_unlock(flags);
        }

        if (core->new_parent && core->new_parent != core->parent) {
                old_parent = __clk_set_parent_before(core, core->new_parent);
                trace_clk_set_parent(core, core->new_parent);

                if (core->ops->set_rate_and_parent) {
                        skip_set_rate = true;
                        core->ops->set_rate_and_parent(core->hw, core->new_rate,
                                        best_parent_rate,
                                        core->new_parent_index);
                } else if (core->ops->set_parent) {
                        core->ops->set_parent(core->hw, core->new_parent_index);
                }

                trace_clk_set_parent_complete(core, core->new_parent);
                __clk_set_parent_after(core, core->new_parent, old_parent);
        }

        if (core->flags & CLK_OPS_PARENT_ENABLE)
                clk_core_prepare_enable(parent);

        trace_clk_set_rate(core, core->new_rate);

        if (!skip_set_rate && core->ops->set_rate)
                core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);

        trace_clk_set_rate_complete(core, core->new_rate);

요청한 클럭 코어부터 마지막 자식 클럭 코어까지 산출된 new rate 및 new 부모 클럭을 적용하고 통지한다.

  • 코드 라인 11~19에서 현재 클럭 코어의 rate를 백업해두고, 변경될 부모 클럭 정보를 다음 변수에 지정한다.
    • best_parent & best_parent_rate
  • 코드 라인 21~22에서 절전 기능이 있는 클럭이 슬립된 상태이면 깨운다.
  • 코드 라인 24~31에서 gate가 열린 상태에서만 rate를 바꿀 수 있는 클럭 hw를 위해 임시로 잠시 이 클럭을 prepare & enable 한다.
  • 코드 라인 33~48에서 새 부모 클럭으로 변경된 경우 다음과 같이 처리한다.
    • new 부모 클럭(입력 소스 변경)을 선택(변경)하기 전에 처리할 일을 수행한다.
    • mux 타입과 rate 변경이 동시에 가능한(pll) 타입의 클럭 디바이스 드라이버에 구현된 ops->set_rate_and_parent 후크 함수를 호출하여 실제 hw 기능으로 new 부모 클럭(입력 클럭 소스) 및 new rate를 변경한다.
    • mux 타입만을 지원하는 클럭 디바이스 드라이버에 구현된 ops->set_parent 후크 함수를 호출하여 실제 hw 기능으로 new 부모 클럭(입력 클럭 소스)을 선택(변경)한다.
    • 코드 라인 27에서 new 부모 클럭(입력 소스 변경)을 선택한 후에 처리할 일을 수행한다
  • 코드 라인 50~51에서 부모 클럭이 enable된 상태에서만 operation을 수행할 수 있는 클럭 hw를 지원하기 위해 부모 클럭이 닫혀있으면 임시로 잠시 부모 클럭을 prepare & enable 한다.
  • 코드 라인 55~56에서 바로 위에서 rate 설정한 경우가 아닌 경우로 한정한다. 클럭 디바이스 드라이버의 ops->set_rate 후크 함수를 호출하여 클럭 hw의 rate를 설정한다.

 

drivers/clk/clk.c -2/2-

        core->rate = clk_recalc(core, best_parent_rate);

        if (core->flags & CLK_SET_RATE_UNGATE) {
                unsigned long flags;

                flags = clk_enable_lock();
                clk_core_disable(core);
                clk_enable_unlock(flags);
                clk_core_unprepare(core);
        }

        if (core->flags & CLK_OPS_PARENT_ENABLE)
                clk_core_disable_unprepare(parent);

        if (core->notifier_count && old_rate != core->rate)
                __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);

        if (core->flags & CLK_RECALC_NEW_RATES)
                (void)clk_calc_new_rates(core, core->new_rate);

        /*
         * Use safe iteration, as change_rate can actually swap parents
         * for certain clock types.
         */
        hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
                /* Skip children who will be reparented to another clock */
                if (child->new_parent && child->new_parent != core)
                        continue;
                clk_change_rate(child);
        }

        /* handle the new child who might not be in core->children yet */
        if (core->new_child)
                clk_change_rate(core->new_child);

        clk_pm_runtime_put(core);
}
  • 코드 라인 1에서 클럭 hw의 (*recalc_rate) ops를 호출하여 현재 클럭 코어에 재계산한 rate를 지정한다.
  • 코드 라인 3~10에서 임시로 잠시 현재 클럭 코어를 prepare & enable 한 경우 다시 disable & unprepare 한다.
  • 코드 라인 12~13에서 임시로 잠시 부모 클럭 코어를 prepare & enable 한 경우 disable & unprepare 한다.
  • 코드 라인 15~16에서 통지 대상 클럭에 대해 rate가 변경된 경우 POST_RATE_CHANGE를 보내 commit 통지한다.
  • 코드 라인 18~19에서 CLK_RECALC_NEW_RATES 플래그가 설정된 클럭은 rate가 변경된 경우  현재 클럭 코어로부터 변경이 필요한 상위 클럭까지 new rate를 다시 산출하게 한다.
    • exynos cpu의 경우 재산출을 통해 divider가 잘못 설정되는 일을 막아야 한다.
  • 코드 라인 25~30에서 하위 클럭 코어들의 부모 클럭이 변경된 경우 이 클럭 코어를 포함하고 그 하위 클럭 코어들에 대해 rate를 다시 산출한다. (마지막 child 클럭까지 재귀 호출된다)
  • 코드 라인 33~34에서 새로운 하위 클럭 코어가 추가된 경우 이 클럭 코어를 포함하고 그 하위 클럭 코어들에 대해 rate를 다시 산출한다. (마지막 child 클럭까지 재귀 호출된다)
  • 코드 라인 36에서 절전 기능이 있어 잠시 꺼둔 상태인 경우 필요 시 다시 슬립시킨다.

 

다음 그림은 최종 산출된 new rate를 결정 또는 취소할 때 호출되는 과정을 보여준다.

 

clk_calc_subtree()

drivers/clk/clk.c

static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
                             struct clk_core *new_parent, u8 p_index)
{
        struct clk_core *child;

        core->new_rate = new_rate;
        core->new_parent = new_parent;
        core->new_parent_index = p_index;
        /* include clk in new parent's PRE_RATE_CHANGE notifications */
        core->new_child = NULL;
        if (new_parent && new_parent != core->parent)
                new_parent->new_child = core;

        hlist_for_each_entry(child, &core->children, child_node) {
                child->new_rate = clk_recalc(child, new_rate);
                clk_calc_subtree(child, child->new_rate, NULL, 0);
        }
}

현재 클럭 코어 및 모든 연결된 하위 클럭 코어들에 대해 새 rate, 새 부모, 새 부모 등을 갱신하게 한다.

  • 코드 라인 6~8에서 현재 클럭 코어의 new_rate, new_parent, new_parent_index 값을 갱신한다.
  • 코드 라인 10~12에서 부모가 변경된 경우 new_child에 현재 클럭 코어를 대입한다. 그렇지 않은 경우 null을 대입한다.
  • 코드 라인 14~15에서 자식 클럭들 수 만큼 루프를 돌며 new_rate로 재계산하도록 한다.
  • 코드 라인 16에서 자식 노드에 대해 이 함수를 재귀 호출하여 계산하게 한다.

 

1개 클럭 rate 재산출(recalc)

clk_recalc()

drivers/clk/clk.c

static unsigned long clk_recalc(struct clk_core *core,
                                unsigned long parent_rate)
{
        unsigned long rate = parent_rate;

        if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
                rate = core->ops->recalc_rate(core->hw, parent_rate);
                clk_pm_runtime_put(core);
        }
        return rate;
}

@parent_rate 값을 사용하여 클럭 hw의 (*recalc_rate) ops를 호출하여 현재 클럭 코어의 재계산한 rate를 반환한다.

 


Rate 조회

clk_get_rate()

drivers/clk/clk.c

/**
 * clk_get_rate - return the rate of clk
 * @clk: the clk whose rate is being returned
 *
 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
 * is set, which means a recalc_rate will be issued.
 * If clk is NULL then returns 0.
 */
unsigned long clk_get_rate(struct clk *clk)
{
        if (!clk)
                return 0;

        return clk_core_get_rate(clk->core);
}
EXPORT_SYMBOL_GPL(clk_get_rate);

클럭의 rate 값을 반환한다.

 

clk_core_get_rate()

drivers/clk/clk.c

static unsigned long clk_core_get_rate(struct clk_core *core)
{
        unsigned long rate;

        clk_prepare_lock();

        if (core && (core->flags & CLK_GET_RATE_NOCACHE))
                __clk_recalc_rates(core, 0);

        rate = clk_core_get_rate_nolock(core);
        clk_prepare_unlock();

        return rate;
}

클럭 코어의 rate 값을 반환한다.

  • CLK_GET_RATE_NOCACHE 플래그를 사용한 클럭 코어는 캐시된 rate 값이 아니라 재산출한 rate 값을 반환한다.

 

클럭 rate 재산출(recalc) – 클럭 조회 및 부모 클럭 변경 시

__clk_recalc_rates()

drivers/clk/clk.c

/**
 * __clk_recalc_rates
 * @clk: first clk in the subtree
 * @msg: notification type (see include/linux/clk.h)
 *
 * Walks the subtree of clks starting with clk and recalculates rates as it
 * goes.  Note that if a clk does not implement the .recalc_rate callback then
 * it is assumed that the clock will take on the rate of its parent.
 *
 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
 * if necessary.
 */
static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
{
        unsigned long old_rate;
        unsigned long parent_rate = 0;
        struct clk_core *child;

        old_rate = core->rate;

        if (core->parent)
                parent_rate = core->parent->rate;

        core->rate = clk_recalc(core, parent_rate);

        /*
         * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
         * & ABORT_RATE_CHANGE notifiers
         */
        if (core->notifier_count && msg)
                __clk_notify(core, msg, old_rate, core->rate);

        hlist_for_each_entry(child, &core->children, child_node)
                __clk_recalc_rates(child, msg);
}

요청한 클럭 코어 및 모든 연결된 하위 클럭 코어들에 대해 rate를 재산출하여 갱신한다. 그리고 부모 클럭이 통지가 필요한 클럭 코어들에 @msg를 전달한다.

  • 코드 라인 7~10에서 현재 클럭 코어의 rate를 old_rate에 백업하고, 부모 클럭 코어의 rate도 parent_rate에 대입한다.
  • 코드 라인 12에서 부모 클럭 rate로 현재 클럭 코어의 rate를 재산출하여 반영한다.
  • 코드 라인 18~19에서 통지 대상 클럭 코어에 @msg를 통지하고, 결과 값은 무시한다.
    • rate 조회하는 clk_core_get_rate() 함수에서 이 함수를 호출한 경우 @msg에는 0이 전달되므로 통지하지 않는다.
  • 코드 라인 21~22에서 하위 클럭 코어들에 대해  이 함수를 재귀 호출하게 한다.

 

clk_core_get_rate_nolock()

drivers/clk/clk.c

static unsigned long clk_core_get_rate_nolock(struct clk_core *clk)
{
        if (!core)
                return 0;

        if (!core->num_parents || core->parent)
                return core->rate;

        /*
         * Clk must have a parent because num_parents > 0 but the parent isn't
         * known yet. Best to return 0 as the rate of this clk until we can
         * properly recalc the rate based on the parent's rate.
         */
        return 0;
}

클럭 코어의 rate 값을 반환한다.

  • 루트 클럭 코어의 경우 num_parents 값은 0이다.

 


부모 클럭 선택

부모 클럭을 변경한다는 것은 입력 클럭 소스가 바뀐다는 의미이고 gate된 상태가 아닌 상태에서 실시간으로 변경하는 경우 glitch가 발생됨을 유의해야 한다. glitch를 방지하려면 클럭 gate를 닫고 변경한 후 클럭 gate를 열어야야 한다. 클럭 코어에 CLK_SET_PARENT_GATE 플래그를 사용하면 gate된 상태에서 부모 클럭을 변경할 수 없게 할 수 있다.

 

다음 그림은 clk_set_parent() 함수 이후의 호출 관계를 보여준다.

 

clk_set_parent()

drivers/clk/clk.c

/**
 * clk_set_parent - switch the parent of a mux clk
 * @clk: the mux clk whose input we are switching
 * @parent: the new input to clk
 *
 * Re-parent clk to use parent as its new input source.  If clk is in
 * prepared state, the clk will get enabled for the duration of this call. If
 * that's not acceptable for a specific clk (Eg: the consumer can't handle
 * that, the reparenting is glitchy in hardware, etc), use the
 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
 *
 * After successfully changing clk's parent clk_set_parent will update the
 * clk topology, sysfs topology and propagate rate recalculation via
 * __clk_recalc_rates.
 *
 * Returns 0 on success, -EERROR otherwise.
 */
int clk_set_parent(struct clk *clk, struct clk *parent)
{
        int ret;

        if (!clk)
                return 0;

        clk_prepare_lock();

        if (clk->exclusive_count)
                clk_core_rate_unprotect(clk->core);

        ret = clk_core_set_parent_nolock(clk->core,
                                         parent ? parent->core : NULL);

        if (clk->exclusive_count)
                clk_core_rate_protect(clk->core);

        clk_prepare_unlock();

        return ret;
}
EXPORT_SYMBOL_GPL(clk_set_parent);

부모 클럭 코어를 선택한다. 성공 시 클럭 topology가 변경되며 rate 재산출이 일어난다. 성공 시 0을 반환한다.

  • 코드 라인 10~11에서 클럭 코어를 독점(exclusive)하여 관리하는 경우 parent 설정 전에 unprotect를 한다.
  • 코드 라인 13~14에서 부모 클럭 코어를 선택한다. (입력 클럭 소스 선택)
  • 코드 라인 16~17에서 클럭 코어를 독점(exclusive)하여 관리하는 경우 parent 설정이 완료되었으므로 다시 protect를 한다.

 

clk_core_set_parent_nolock()

drivers/clk/clk.c

static int clk_core_set_parent_nolock(struct clk_core *core,
                                      struct clk_core *parent)
{
        int ret = 0;
        int p_index = 0;
        unsigned long p_rate = 0;

        lockdep_assert_held(&prepare_lock);

        if (!core)
                return 0;

        if (core->parent == parent)
                return 0;

        /* verify ops for multi-parent clks */
        if (core->num_parents > 1 && !core->ops->set_parent)
                return -EPERM;

        /* check that we are allowed to re-parent if the clock is in use */
        if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
                return -EBUSY;

        if (clk_core_rate_is_protected(core))
                return -EBUSY;

        /* try finding the new parent index */
        if (parent) {
                p_index = clk_fetch_parent_index(core, parent);
                if (p_index < 0) {
                        pr_debug("%s: clk %s can not be parent of clk %s\n",
                                        __func__, parent->name, core->name);
                        return p_index;
                }
                p_rate = parent->rate;
        }

        ret = clk_pm_runtime_get(core);
        if (ret)
                return ret;

        /* propagate PRE_RATE_CHANGE notifications */
        ret = __clk_speculate_rates(core, p_rate);

        /* abort if a driver objects */
        if (ret & NOTIFY_STOP_MASK)
                goto runtime_put;

        /* do the re-parent */
        ret = __clk_set_parent(core, parent, p_index);

        /* propagate rate an accuracy recalculation accordingly */
        if (ret) {
                __clk_recalc_rates(core, ABORT_RATE_CHANGE);
        } else {
                __clk_recalc_rates(core, POST_RATE_CHANGE);
                __clk_recalc_accuracies(core);
        }

runtime_put:
        clk_pm_runtime_put(core);

        return ret;
}

부모 클럭(입력 클럭 소스) 코어를 선택한다. 성공 시 연결된 모든 자식 클럭들의 rate를 재계산하고 0을 반환한다.

  • 코드 라인 10~11 이 함수는 재귀호출에서 사용되므로 클럭 코어가 지정되지 않으면 함수를 빠져나간다.
  • 코드 라인 13~14에서 요청한 부모 클럭(입력 클럭 소스)이 이미 지정되어 있었던 경우 변경할 필요가 없으므로 성공(0) 결과로 함수를 빠져나간다.
  • 코드 라인 17~18에서 2개 이상의 부모 클럭(입력 클럭 소스)을 가진 mux 타입 클럭 디바이스 드라이버의 ops->set_parent 후크가 구현되어 있지 않은 경우 -ENOSYS 에러를 반환한다.
  • 코드 라인 21~22에서 CLK_SET_PARENT_GATE 플래그를 사용한 경우 prepare 상태(클럭이 출력되는)의 클럭 코어는 glitch를 방지하기 위해 부모 클럭의 선택을 허락하지 않는다. 따라서 -EBUSY 에러를 반환한다.
  •  코드라인 24~25에서 protect 걸린 클럭 코어의 경우 -EBUSY 에러를 반환한다.
  • 코드 라인 28~36에서 부모 클럭(입력 클럭 소스)의 인덱스와 rate 값을 알아온다.
  • 코드 라인 38~40에서 절전 기능이 있는 클럭이 슬립된 상태이면 깨운다.
  • 코드 라인 47에서 현재 클럭 이하 연결된 모든 자식 클럭에 대해 PRE_RATE_CHANGE를 통지한다. 만일 결과가 NOTIFY_STOP 또는 NOTIFY_BAD인 경우 함수를 빠져나간다.
  • 코드 라인 50~58에서 부모 클럭(입력 클럭 소스)을 선택한다. 만일 에러가 발생한 경우 ABORT_RATE_CHANGE를 하위 노드에 전파한다. 성공한 경우에는 POST_RATE_CHANGE를 하위 노드에 전파한다. 전파 중에는 rate가 재계산된다. 성공 시 accuracy도 재산출한다.
  • 코드 라인 60~61에서 runtime_put: 레이블이다. 절전 기능이 있는 클럭의 경우 슬립이 필요하면 슬립시킨다.

 

__clk_set_parent()

drivers/clk/clk.c

static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
                            u8 p_index)
{
        unsigned long flags;
        int ret = 0;
        struct clk_core *old_parent;

        old_parent = __clk_set_parent_before(core, parent);

        trace_clk_set_parent(core, parent);

        /* change clock input source */
        if (parent && core->ops->set_parent)
                ret = core->ops->set_parent(core->hw, p_index);

        trace_clk_set_parent_complete(core, parent);

        if (ret) {
                flags = clk_enable_lock();
                clk_reparent(core, old_parent);
                clk_enable_unlock(flags);
                __clk_set_parent_after(core, old_parent, parent);

                return ret;
        }

        __clk_set_parent_after(core, parent, old_parent);

        return 0;
}

부모 클럭(입력 클럭 소스)을 선택한다. 성공한 경우 0을 반환한다.

  • 코드 라인 8에서 입력 클럭 소스(부모 클럭)를 선택하기 전에 처리할 일을 수행한다.
  • 코드 라인 13~25에서 mux 타입 클럭의 디바이스 드라이버에 구현된 ops->set_parent 후크 함수를 호출하여 실제 hw 기능으로 부모 클럭(입력 클럭 소스)을 선택하게 한다. 에러가 발생한 경우 기존 부모 클럭(입력 클럭 소스)로 재 변경한다.
  • 코드 라인 27에서 입력 클럭 소스(부모 클럭)을 선택한 후에 처리할 일을 수행한다.

 

__clk_set_parent_before()

drivers/clk/clk.c

static struct clk_core *__clk_set_parent_before(struct clk_core *core,
                                           struct clk_core *parent)
{
        unsigned long flags;
        struct clk_core *old_parent = core->parent;

        /*
         * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
         *
         * 2. Migrate prepare state between parents and prevent race with
         * clk_enable().
         *
         * If the clock is not prepared, then a race with
         * clk_enable/disable() is impossible since we already have the
         * prepare lock (future calls to clk_enable() need to be preceded by
         * a clk_prepare()).
         *
         * If the clock is prepared, migrate the prepared state to the new
         * parent and also protect against a race with clk_enable() by
         * forcing the clock and the new parent on.  This ensures that all
         * future calls to clk_enable() are practically NOPs with respect to
         * hardware and software states.
         *
         * See also: Comment for clk_set_parent() below.
         */

        /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
        if (core->flags & CLK_OPS_PARENT_ENABLE) {
                clk_core_prepare_enable(old_parent);
                clk_core_prepare_enable(parent);
        }

        /* migrate prepare count if > 0 */
        if (core->prepare_count) {
                clk_core_prepare_enable(parent);
                clk_core_enable_lock(core);
        }

        /* update the clk tree topology */
        flags = clk_enable_lock();
        clk_reparent(core, parent);
        clk_enable_unlock(flags);

        return old_parent;
}

입력 클럭 소스(부모 클럭)를 선택하기 전에 처리할 일을 수행한다. 결과로 기존 부모 클럭을 반환한다.

  • 코드 라인 28~31에서 현재 클럭 코어에서 CLK_OPS_PARENT_ENABLE 플래그를 사용한 경우 기존 부모 클럭과 새 부모 클럭을 prepare하고 enable한다.
  • 코드 라인 34~37에서 현재 클럭 코어가 prepare 상태면 부모 클럭 코어도 prepare 및 enable 한다.
  • 코드 라인 40~44에서 클럭 topology를 갱신하고 기존 부모 클럭 코어를 반환한다.

 

__clk_set_parent_after()

drivers/clk/clk.c

static void __clk_set_parent_after(struct clk_core *core,
                                   struct clk_core *parent,
                                   struct clk_core *old_parent)
{
        /*
         * Finish the migration of prepare state and undo the changes done
         * for preventing a race with clk_enable().
         */
        if (core->prepare_count) {
                clk_core_disable_lock(core);
                clk_core_disable_unprepare(old_parent);
        }

        /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
        if (core->flags & CLK_OPS_PARENT_ENABLE) {
                clk_core_disable_unprepare(parent);
                clk_core_disable_unprepare(old_parent);
        }
}

입력 클럭 소스(부모 클럭)를 선택한 후에 처리할 일을 수행한다.

  • 코드 라인 9~12에서 클럭 코어가 prepare 상태인 경우 disable하고, 부모 클럭을 unprepare 상태로 변경한다.
  • 코드 라인 15~18에서 현재 클럭 코어에서 CLK_OPS_PARENT_ENABLE 플래그를 사용한 경우 기존 부모 클럭과 새 부모 클럭을 disable 하고 unprepare 한다.

 

clk_reparent()

drivers/clk/clk.c

static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
{
        bool was_orphan = core->orphan;

        hlist_del(&core->child_node);

        if (new_parent) {
                bool becomes_orphan = new_parent->orphan;

                /* avoid duplicate POST_RATE_CHANGE notifications */
                if (new_parent->new_child == core)
                        new_parent->new_child = NULL;

                hlist_add_head(&core->child_node, &new_parent->children);

                if (was_orphan != becomes_orphan)
                        clk_core_update_orphan_status(core, becomes_orphan);
        } else {
                hlist_add_head(&core->child_node, &clk_orphan_list);
                if (!was_orphan)
                        clk_core_update_orphan_status(core, true);
        }

        core->parent = new_parent;
}

clock tree 토플로지를 갱신한다.

  • 코드 라인 5에서 부모 클럭 코어(입력 클럭 소스)의 child_node에서 현재 클럭 코어를 제거한다.
  • 코드 라인 7~17에서 새 부모 클럭(입력 클럭 소스)이 지정된 경우 그 부모 클럭의 children으로 추가한다. 만일 새 부모 클럭의 new_child에 현재 클럭이 지정되어 있었던 경우라면 new_child에 null을 대입한다.
  • 코드 라인 18~22에서 새 부모 클럭이 지정되지 않은 경우 고아 리스트에 현재 클럭을 추가한다.
  • 코드 라인 24에서 현재 클럭 코어의 부모를 갱신한다.

 


Mux 클럭 rate 관련 ops

(*determine_rate) 후크 함수

mux 타입 클럭의 (*determine_rate) 후크 함수에서 사용되는 아래 함수를 알아본다.

 

clk_mux_determine_rate()

drivers/etc/clk-mux.c

static int clk_mux_determine_rate(struct clk_hw *hw,
                                  struct clk_rate_request *req)
{
        struct clk_mux *mux = to_clk_mux(hw);

        return clk_mux_determine_rate_flags(hw, req, mux->flags);
}

요청한 rate에 대해 mux 클럭 hw가 지원하는 가장 근접한 rate를 산출한다. 성공 시 0을 반환하고, req->rate 및 req->best_parent_rate에 산출된 rate가 저장되고, req->best_parent_rate에 산출된 rate와 관련된 부모 클럭 hw가 저장된다.

 

clk_mux_determine_rate_flags()

drivers/etc/clk-mux.c

int clk_mux_determine_rate_flags(struct clk_hw *hw,
                                 struct clk_rate_request *req,
                                 unsigned long flags)
{
        struct clk_core *core = hw->core, *parent, *best_parent = NULL;
        int i, num_parents, ret;
        unsigned long best = 0;
        struct clk_rate_request parent_req = *req;

        /* if NO_REPARENT flag set, pass through to current parent */
        if (core->flags & CLK_SET_RATE_NO_REPARENT) {
                parent = core->parent;
                if (core->flags & CLK_SET_RATE_PARENT) {
                        ret = __clk_determine_rate(parent ? parent->hw : NULL,
                                                   &parent_req);
                        if (ret)
                                return ret;

                        best = parent_req.rate;
                } else if (parent) {
                        best = clk_core_get_rate_nolock(parent);
                } else {
                        best = clk_core_get_rate_nolock(core);
                }

                goto out;
        }

        /* find the parent that can provide the fastest rate <= rate */
        num_parents = core->num_parents;
        for (i = 0; i < num_parents; i++) {
                parent = clk_core_get_parent_by_index(core, i);
                if (!parent)
                        continue;

                if (core->flags & CLK_SET_RATE_PARENT) {
                        parent_req = *req;
                        ret = __clk_determine_rate(parent->hw, &parent_req);
                        if (ret)
                                continue;
                } else {
                        parent_req.rate = clk_core_get_rate_nolock(parent);
                }

                if (mux_is_better_rate(req->rate, parent_req.rate,
                                       best, flags)) {
                        best_parent = parent;
                        best = parent_req.rate;
                }
        }

        if (!best_parent)
                return -EINVAL;

out:
        if (best_parent)
                req->best_parent_hw = best_parent->hw;
        req->best_parent_rate = best;
        req->rate = best;

        return 0;
}
EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags);

요청한 rate에 대해 mux 클럭 hw가 지원하는 가장 근접한 rate를 산출한다. 성공 시 0을 반환하고, req->rate 및 req->best_parent_rate에 산출된 rate가 저장되고, req->best_parent_rate에 산출된 rate와 관련된 부모 클럭 hw가 저장된다.

  • 코드 라인 8에서 기존 요청들을 parent_req에 백업해둔다.
  • 코드 라인 11~27에서 rate 변경 시 부모 클럭(입력 소스)이 변경되지 않는 클럭 코어인 경우 다음 값으로 pass through 처리한다.
    • 부모 클럭 먼저 rate 변경하게 요청한 경우 부모 클럭에서 먼저 가장 근접한 rate
    • 부모 클럭의 rate
    • 부모 클럭이 없으면 현재 클럭 코어의 rate
  • 코드 라인 30~50에서 부모 클럭 수만큼 순회하며 요청 rate에 대해 mux 클럭 hw가 지원하는 가장 근접한 rate를 산출하기 위해 다음과 같이 처리하고, 이들 중 가장 적합한 rate를 선택한다.
    • 부모 클럭 중에 더 상위 부모 클럭 먼저 rate 변경하게 요청한 경우 부모 클럭에서 먼저 가장 근접한 rate
    • 부모 클럭의 rate
  • 코드 라인 52~53에서 어떠한 부모 클럭도 요청을 만족하지 못한 경우 -EINVAL 에러를 반환한다.
  • 코드 라인 55~61에서 out: 레이블은 가장 근접한 rate를 찾은 경우이다. 성공 값 0을 반환하고, req->rate 및 req->best_parent_rate에 산출된 rate가 저장하고, req->best_parent_rate에 산출된 rate와 관련된 부모 클럭 hw를 저장한다.

 

mux 타입 rate 설정 API

__clk_mux_determine_rate()

drivers/clk/clk.c

/*
 * __clk_mux_determine_rate - clk_ops::determine_rate implementation for a mux type clk
 * @hw: mux type clk to determine rate on
 * @req: rate request, also used to return preferred parent and frequencies
 *
 * Helper for finding best parent to provide a given frequency. This can be used
 * directly as a determine_rate callback (e.g. for a mux), or from a more
 * complex clock that may combine a mux with other operations.
 *
 * Returns: 0 on success, -EERROR value on error
 */
int __clk_mux_determine_rate(struct clk_hw *hw,
                             struct clk_rate_request *req)
{
        return clk_mux_determine_rate_flags(hw, req, 0);
}
EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);

mux 타입 클럭 디바이스 드라이버의 ops->determine_rate에서 호출되는 콜백함수로도 사용된다. 요청한 rate 이하 값으로 가장 가까운 rate를 구한다. req->best_parent_rate에 산출한 최적의 rate가 담기고 req->best_parent_hw는 산출된 최적의 부모 클럭 hw를 가리킨다. 단 req->min_rate ~ req->max_rate 범위를 초과하는 경우 0을 반환한다. 디폴트 플래그로 0을 사용한다.

 

__clk_mux_determine_rate_closest()

drivers/clk/clk.c

int __clk_mux_determine_rate_closest(struct clk_hw *hw,
                                     struct clk_rate_request *req)
{
        return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
}
EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);

요청한 rate에 가장 가까운 rate를 구한다. req->best_parent_rate에 산출한 최적의 rate가 담기고 req->best_parent_hw는 산출된 최적의 부모 클럭hw를 가리킨다. 단 req->min_rate ~ req->max_rate 범위를 초과하는 경우 0을 반환한다. 디폴트 플래그로 CLK_MUX_ROUND_CLOSEST을 사용한다.

 

mux_is_better_rate()

drivers/clk/clk.c

static bool mux_is_better_rate(unsigned long rate, unsigned long now,
                           unsigned long best, unsigned long flags)
{
        if (flags & CLK_MUX_ROUND_CLOSEST)
                return abs(now - rate) < abs(best - rate);

        return now <= rate && now > best;
}

mux가 설정하고자 하는 rate 값에 now 값이 best 값보다 더 적절한 경우 true(1)를 반환한다. CLK_MUX_ROUND_CLOSEST 플래그의 사용 여부에 따라 적절한 값의 여부 판단이 바뀐다.

  • 사용하지 않는 경우 rate 이하 범위에서 now 값이 best 값 보다 더 가까운 경우 true(1)
  • 사용하는 경우 요청 rate에 now 값이 best 보다 더 가까운 경우 true(1)

 

 


Fixed Factor 클럭 rate 관련 ops

 

(*round_rate) 후크 함수

fixed factor 타입 클럭의 (*round_rate) 후크 함수에서 사용되는 아래 함수를 알아본다.

 

clk_factor_round_rate()

drivers/etc/clk-fixed-factor.c

static long clk_factor_round_rate(struct clk_hw *hw, unsigned long rate,
                                unsigned long *prate)
{
        struct clk_fixed_factor *fix = to_clk_fixed_factor(hw);

        if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
                unsigned long best_parent;

                best_parent = (rate / fix->mult) * fix->div;
                *prate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent);
        }

        return (*prate / fix->div) * fix->mult;
}

요청한 @rate에 대해 fixed factor 타입 클럭 hw가 지원하는 가장 근접한 rate를 산출한다. 성공 시 rate를 반환하고, *prate에는 부모 클럭의 rate를 저장해온다.

  • 코드 라인 6~11에서 부모 클럭 코어의 rate를 먼저 설정해야 하는 클럭 코어인 경우 변경을 원하는 rate에 따른 부모 클럭 rate를 산출하여 출력 인자 prate에 저장한다.
    • 예) 부모 클럭이 1Mhz였고, 하위 divider 클럭이 1/2를 적용하여 500Khz였는데, 원하는 클럭이 600Khz인 경우 가능하면 부모 클럭을 1.2Mhz로 변경한다.
  • 코드 라인 13에서  부모 클럭 rate로부터 factor 비율을 적용한 rate를 반환한다.
    • 예) 부모 클럭 rate가 10Mhz, fix->div=3, fix->mult=2인 경우
      • 10000000 / 3 * 2 = 6666666

 


클럭 드라이버 샘플

 

클럭 디바이스 트리

샘플에 사용한 디바이스 트리의 추가된 6개의 클럭 노드 내용은 다음과 같다.

  • fooclk1 (fixed-rate)
    • 1mhz 고정 rate
  • fooclk2 (divider)
    • fooclk1의 1mhz를 1~32 분주하여 사용한다.
  • fooclk3 (divider)
    • fooclk2에서 분주된 클럭을 1~8 분주하여 사용한다.
    • 원하는 rate 설정이 안되면 부모 클럭 rate도 변경한다.
  • fooclk4 (divider)
    • fooclk2에서 분주된 클럭을 1,2,4,8,16 분주하여 사용한다.
  • fooclk5 (mux)
    • fooclk1, fooclk2, fooclk3, fooclk4 클럭 중 하나를 선택하여 사용한다.
  • foo
    • 사용자 디바이스용으로 위의 클럭 5개를 사용할 수 있게하였다.
        fooclk1 {
                phandle = <0x8100>;
                clock-output-names = "fooclk1";
                clock-frequency = <1000000>;
                #clock-cells = <0x0>;
                compatible = "fixed-clock";
        };

        fooclk2 {
                phandle = <0x8200>;
                clock-output-names = "fooclk2";
                clocks = <0x8100>;
                #clock-cells = <0x0>;
                compatible = "foo,divider-clock";
                foo,max-div = <32>;
        };

        fooclk3 {
                phandle = <0x8300>;
                clock-output-names = "fooclk3";
                clocks = <0x8200>;
                #clock-cells = <0x0>;
                compatible = "foo,divider-clock";
                foo,max-div = <8>;
                foo,set-rate-parent;
        };

        fooclk4 {
                phandle = <0x8400>;
                clock-output-names = "fooclk4";
                clocks = <0x8200>;
                #clock-cells = <0x0>;
                compatible = "foo,divider-clock";
                foo,max-div = <5>;
                foo,index-power-of-two;
        };

        fooclk5 {
                phandle = <0x8500>;
                clock-output-names = "fooclk5";
                clocks = <0x8100 0x8200 0x8300 0x8400>;
                #clock-cells = <0x0>;
                compatible = "foo,mux-clock";
        };

        foo {
                compatible = "foo,foo";
                clock-names = "fooclk1", "fooclk2", "fooclk3", "fooclk4", "fooclk5";
                clocks = <0x8100 0x8200 0x8300 0x8400 0x8500>;
        };

 

rate  초기 상태

다음 그림은 5 개의 클럭이 처음 초기화된 상태를 보여준다.

  • 모든 분배기(divider)들이 1:1로 동작하고 있고, mux 클럭은 0번 입력으로 초기화된 상태이다.

 

다음은 5개의 클럭에 대해 prepare 및 enable한 상태이고, cat /sys/kernel/debug/clk/clk_summary 명령을 통해 확인한 결과이다.

$ insmod clk.ko
clk: loading out-of-tree module taints kernel.
foo: foo_probe
foo: devm_clk_get() clk1
foo: devm_clk_get() clk2
foo: devm_clk_get() clk3
foo: devm_clk_get() clk4
foo: devm_clk_get() clk5
foo: clk_prepare() clk1 rc=0
foo: clk_prepare() clk2 rc=0
foo: clk_prepare() clk3 rc=0
foo: clk_prepare() clk4 rc=0
foo: clk_prepare() clk5 rc=0
foo: clk_enable() clk1 rc=0
foo: clk_enable() clk2 rc=0
foo: clk_enable() clk3 rc=0
foo: clk_enable() clk4 rc=0
foo: clk_enable() clk5 rc=0
$ cat /sys/kernel/debug/clk/clk_summary
                                 enable  prepare  protect                                duty
   clock                          count    count    count        rate   accuracy phase  cycle
---------------------------------------------------------------------------------------------
 fooclk1                              0        0        0     1000000          0     0  50000
    fooclk5                           0        0        0     1000000          0     0  50000
    fooclk2                           0        0        0     1000000          0     0  50000
       fooclk4                        0        0        0     1000000          0     0  50000
       fooclk3                        0        0        0     1000000          0     0  50000
 clk24mhz                             4        5        0    24000000          0     0  50000

 

rate  설정 -1

다음 그림은 clk2, clk3, 및 clk4의 rate를 초기값으로 설정하고, clk5의 입력을 2번으로 선택한 상태이다.

 

다음은 각 클럭을 설정하는 과정을 보여준다.

$ echo 40000 > /sys/bus/platform/drivers/foo/foo2
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=25
foo_clk_divider_round_rate: rate=40000, prate=1000000, round=40000
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=25
foo_clk_divider_round_rate: rate=40000, prate=1000000, round=40000
foo_clk_divider_recalc_rate: parent_rate=40000
foo_readl: val=0
foo_clk_divider_recalc_rate: parent_rate=40000, round=40000
foo_clk_divider_recalc_rate: parent_rate=40000
foo_readl: val=0
foo_clk_divider_recalc_rate: parent_rate=40000, round=40000
foo_clk_divider_set_rate: rate=40000, parent_rate=1000000
foo_readl: val=0
foo_writel: val=24
foo_clk_divider_recalc_rate: parent_rate=1000000
foo_readl: val=24
foo_clk_divider_recalc_rate: parent_rate=1000000, round=40000
foo_clk_divider_set_rate: rate=40000, parent_rate=40000
foo_readl: val=0
foo_writel: val=0
foo_clk_divider_recalc_rate: parent_rate=40000
foo_readl: val=0
foo_clk_divider_recalc_rate: parent_rate=40000, round=40000
foo_clk_divider_set_rate: rate=40000, parent_rate=40000
foo_readl: val=0
foo_writel: val=0
foo_clk_divider_recalc_rate: parent_rate=40000
foo_readl: val=0
foo_clk_divider_recalc_rate: parent_rate=40000, round=40000
foo foo: clk_set_rate() clk2 val=40000 rc=0
$ echo 10000 > /sys/bus/platform/drivers/foo/foo3
foo_clk_divider_bestdiv: maxdiv=8
foo_clk_divider_bestdiv: maxdiv2=8
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=32
foo_clk_divider_round_rate: rate=10000, prate=1000000, round=31250
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=32
foo_clk_divider_round_rate: rate=20001, prate=1000000, round=31250
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=32
foo_clk_divider_round_rate: rate=30002, prate=1000000, round=31250
foo_clk_divider_round_rate: rate=10000, prate=40000, round=10000
foo_clk_divider_bestdiv: maxdiv=8
foo_clk_divider_bestdiv: maxdiv2=8
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=32
foo_clk_divider_round_rate: rate=10000, prate=1000000, round=31250
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=32
foo_clk_divider_round_rate: rate=20001, prate=1000000, round=31250
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=32
foo_clk_divider_round_rate: rate=30002, prate=1000000, round=31250
foo_clk_divider_round_rate: rate=10000, prate=40000, round=10000
foo_clk_divider_set_rate: rate=10000, parent_rate=40000
foo_readl: val=0
foo_writel: val=3
foo_clk_divider_recalc_rate: parent_rate=40000
foo_readl: val=3
foo_clk_divider_recalc_rate: parent_rate=40000, round=10000
foo foo: clk_set_rate() clk3 val=10000 rc=0
$ echo 5000 > /sys/bus/platform/drivers/foo/foo4
foo_clk_divider_bestdiv: maxdiv=8
foo_clk_divider_bestdiv: bestdiv=8
foo_clk_divider_round_rate: rate=5000, prate=40000, round=5000
foo_clk_divider_bestdiv: maxdiv=8
foo_clk_divider_bestdiv: bestdiv=8
foo_clk_divider_round_rate: rate=5000, prate=40000, round=5000
foo_clk_divider_set_rate: rate=5000, parent_rate=40000
foo_readl: val=0
foo_writel: val=3
foo_clk_divider_recalc_rate: parent_rate=40000
foo_readl: val=3
foo_clk_divider_recalc_rate: parent_rate=40000, round=5000
foo foo: clk_set_rate() clk4 val=5000 rc=0
foo_clk_divider_bestdiv: maxdiv=8
foo_clk_divider_bestdiv: bestdiv=8
foo_clk_divider_round_rate: rate=5000, prate=40000, round=5000
foo foo: clk_set_rate() clk4 val=5000 rc=0
$ echo 2 > /sys/bus/platform/drivers/foo/foo5
foo_clk_mux_set_parent: index=2
foo_readl: val=0
foo_writel: val=2
foo_clk_mux_set_parent: index2=2, val=2
foo foo: clk_set_parent() val=2, select=clk3 rc=0
$ cat /sys/kernel/debug/clk/clk_summary
                                 enable  prepare  protect                                duty
   clock                          count    count    count        rate   accuracy phase  cycle
---------------------------------------------------------------------------------------------
 fooclk1                              2        2        0     1000000          0     0  50000
    fooclk2                           3        3        0       40000          0     0  50000
       fooclk4                        1        1        0        5000          0     0  50000
       fooclk3                        2        2        0       10000          0     0  50000
          fooclk5                     1        1        0       10000          0     0  50000
 clk24mhz                             4        5        0    24000000          0     0  50000

 

rate  설정-2

다음 그림은 clk3의 rate를 25khz로 변경된 모습을 보여준다.

  • clk3 자체만으로 rate 설정이 불가능하여 부모 클럭인 clk2의 rate도 변경하였다. 그 후 clk4 및 clk5의 rate에도 영향을 끼친 것을 확인할 수 있다.

 

다음은 clk3의 rate를 25khz로 변경하는 과정을 보여준다.

$ echo 25000 > /sys/bus/platform/drivers/foo/foo3
foo_clk_divider_bestdiv: maxdiv=8
foo_clk_divider_bestdiv: maxdiv2=8
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=32
foo_clk_divider_round_rate: rate=25000, prate=1000000, round=31250
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=20
foo_clk_divider_round_rate: rate=50001, prate=1000000, round=50000
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=14
foo_clk_divider_round_rate: rate=75002, prate=1000000, round=71429
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=10
foo_clk_divider_round_rate: rate=100003, prate=1000000, round=100000
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=8
foo_clk_divider_round_rate: rate=125004, prate=1000000, round=125000
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=7
foo_clk_divider_round_rate: rate=150005, prate=1000000, round=142858
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=6
foo_clk_divider_round_rate: rate=175006, prate=1000000, round=166667
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=5
foo_clk_divider_round_rate: rate=200007, prate=1000000, round=200000
foo_clk_divider_bestdiv: bestdiv2=2, best_parent_rate=50000
foo_clk_divider_round_rate: rate=25000, prate=50000, round=25000
foo_clk_divider_bestdiv: maxdiv=8
foo_clk_divider_bestdiv: maxdiv2=8
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=32
foo_clk_divider_round_rate: rate=25000, prate=1000000, round=31250
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=20
foo_clk_divider_round_rate: rate=50001, prate=1000000, round=50000
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=14
foo_clk_divider_round_rate: rate=75002, prate=1000000, round=71429
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=10
foo_clk_divider_round_rate: rate=100003, prate=1000000, round=100000
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=8
foo_clk_divider_round_rate: rate=125004, prate=1000000, round=125000
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=7
foo_clk_divider_round_rate: rate=150005, prate=1000000, round=142858
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=6
foo_clk_divider_round_rate: rate=175006, prate=1000000, round=166667
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=5
foo_clk_divider_round_rate: rate=200007, prate=1000000, round=200000
foo_clk_divider_bestdiv: bestdiv2=2, best_parent_rate=50000
foo_clk_divider_round_rate: rate=25000, prate=50000, round=25000
foo_clk_divider_bestdiv: maxdiv=32
foo_clk_divider_bestdiv: bestdiv=20
foo_clk_divider_round_rate: rate=50000, prate=1000000, round=50000
foo_clk_divider_recalc_rate: parent_rate=50000
foo_readl: val=3
foo_clk_divider_recalc_rate: parent_rate=50000, round=6250
foo_clk_divider_recalc_rate: parent_rate=50000
foo_readl: val=3
foo_clk_divider_recalc_rate: parent_rate=50000, round=12500
foo_clk_divider_set_rate: rate=50000, parent_rate=1000000
foo_readl: val=24
foo_writel: val=19
foo_clk_divider_recalc_rate: parent_rate=1000000
foo_readl: val=19
foo_clk_divider_recalc_rate: parent_rate=1000000, round=50000
foo_clk_divider_set_rate: rate=6250, parent_rate=50000
foo_readl: val=3
foo_writel: val=3
foo_clk_divider_recalc_rate: parent_rate=50000
foo_readl: val=3
foo_clk_divider_recalc_rate: parent_rate=50000, round=6250
foo_clk_divider_set_rate: rate=25000, parent_rate=50000
foo_readl: val=3
foo_writel: val=1
foo_clk_divider_recalc_rate: parent_rate=50000
foo_readl: val=1
foo_clk_divider_recalc_rate: parent_rate=50000, round=25000
foo foo: clk_set_rate() clk3 val=25000 rc=0
$ cat /sys/kernel/debug/clk/clk_summary
                                 enable  prepare  protect                                duty
   clock                          count    count    count        rate   accuracy phase  cycle
---------------------------------------------------------------------------------------------
 fooclk1                              2        2        0     1000000          0     0  50000
    fooclk2                           3        3        0       50000          0     0  50000
       fooclk4                        1        1        0        6250          0     0  50000
       fooclk3                        2        2        0       25000          0     0  50000
          fooclk5                     1        1        0       25000          0     0  50000
 clk24mhz                             4        5        0    24000000          0     0  50000

 

샘플 클럭 드라이버

 

참고

 

Common Clock Framework -1- (초기화)

<kernel v5.4>

Common Clock Framework -1- (초기화)

CCF History

컴퓨터 하드웨어에는 많은 클럭 장치를 통해 클럭이 공급되고 있다. 시스템 내부에 cpu core에 들어가는 클럭부터 timer, i2c, uart 등 수 ~ 수십 종류의 클럭이 사용된다. 각각의 ARM SoC들은 개개의 클럭 디바이스 드라이버를 통해 클럭 설정을 하는데 하드웨어도 천차만별이고 구현된 드라이버도 기존 코드를 그대로 copy & paste를 통해 사용되고 심지어 clk 구조체 까지도 바꿔서 사용해오고 있다. 이를 해결해 보고자 CCF(Common Clock Framework)를 준비하였다. CCF는 커널 v3.4에서 처음 소개되었고 커널 v4.0에서 clk 구조체의 대부분을 clk_core 구조체로 옮겼다.

drivers/clk/clk.c를 제외하고 다음과 같이 많은 시스템에서 clk 구조체를 변경하여 사용했음을 알 수 있다. (커널 v4.0 기준)

  • ccf가 소개된 이후 개발된 ARM64 아키텍처에서는 clk 구조체를 변경하여 사용한 적이 없다.
./include/linux/sh_clk.h:37:struct clk {
./drivers/clk/clk.c:80:struct clk {
./arch/c6x/include/asm/clock.h:82:struct clk {
./arch/mips/include/asm/clock.h:20:struct clk {
./arch/mips/include/asm/mach-ar7/ar7.h:147:struct clk {
./arch/mips/ralink/clk.c:19:struct clk {
./arch/mips/jz4740/clock.h:45:struct clk {
./arch/mips/ath79/clock.c:31:struct clk {
./arch/mips/lantiq/clk.h:55:struct clk {
./arch/mips/bcm63xx/clk.c:19:struct clk {
./arch/blackfin/include/asm/clocks.h:61:struct clk {
./arch/blackfin/mach-common/clock.h:14:struct clk {
./arch/arm/mach-lpc32xx/clock.h:22:struct clk {
./arch/arm/mach-w90x900/clock.h:18:struct clk {
./arch/arm/mach-sa1100/clock.c:26:struct clk {
./arch/arm/mach-davinci/clock.h:87:struct clk {
./arch/arm/mach-mmp/clock.h:18:struct clk {
./arch/arm/mach-versatile/include/mach/clkdev.h:6:struct clk {
./arch/arm/mach-pxa/clock.h:11:struct clk {
./arch/arm/mach-omap1/clock.h:141:struct clk {
./arch/arm/mach-ep93xx/clock.c:30:struct clk {
./arch/m68k/include/asm/mcfclk.h:16:struct clk {
./arch/avr32/mach-at32ap/clock.h:20:struct clk {
./arch/unicore32/kernel/clock.c:30:struct clk {

 

다음과 같이 커널 v5.4에서 clk 구조체를 변경하여 사용한 코드가 일부 남아 있다.

./include/linux/sh_clk.h:38:struct clk {
./drivers/clk/clk.c:97:struct clk {
./arch/unicore32/kernel/clock.c:27:struct clk {
./arch/mips/include/asm/mach-ar7/ar7.h:134:struct clk {
./arch/mips/include/asm/clock.h:21:struct clk {
./arch/mips/ralink/clk.c:18:struct clk {
./arch/mips/bcm63xx/clk.c:21:struct clk {
./arch/mips/lantiq/clk.h:58:struct clk {
./arch/m68k/include/asm/mcfclk.h:17:struct clk {
./arch/arm/mach-ep93xx/clock.c:27:struct clk {
./arch/arm/mach-omap1/clock.h:138:struct clk {
./arch/arm/mach-mmp/clock.h:12:struct clk {
./arch/c6x/include/asm/clock.h:79:struct clk {

 

클럭 Diagram

다음 그림은 클럭 다이어그램 샘플을 보여준다.

  • Device A는 onboard 클럭을 10으로 분주(divide)한 클럭 rate를 공급받는다.
  • Devce B~E가 external 클럭을 공급받는 경우 PLL 설정이 필요하다.

 

클럭 Provider & Consumer

클럭을 공급하는 장치인 Clock Provider와 클럭을 가져다 사용하는 Clock Consumer의 특징을 알아본다.

클럭 Provider
  • CCF 기반의 클럭 디바이스 드라이버를 통해 구현된다.
  • common CCF를 사용하면 유사한 기능을 갖는 클럭 디바이스를 간단히 개발할 수 있다.
  • 클럭 rate, 분주 값 및 Multiplexer의 선택은 디바이스 트리를 통해 각각의 보드 상황에 맞게 설정할 수 있다.

 

클럭 Consumer
  • 디바이스 트리를 통해 사용할 클럭을 지정하여 사용할 수 있다.
  • 런타임에 수동으로 멀티플렉서나 Gate의 제어가 필요한 경우 클럭 API들을 통해서 제어할 수 있다.

 

다음 그림은 기본적인 Clock Provider와 Clock Consumer의 관계와 디바이스 트리에서의 표현을 보여준다.

  • OSC가 Clock Provider로 사용되었고, PLL이 Clock Consumer이자 Clock Provider로 사용된다.
  • UART는 Clock Consumer로 사용된다.

 

다음 그림은 Clock Provider와 Clock Consumer들을 보여준다.

  • PLL의 경우 별도의 custom 드라이버를 통해 구현되는데, 여러 가지 CCF 타입이 혼재되어 구현된다.
  • 노란색 클럭들은 상위 Clock에 대한 Clock Consumer이면서 하위 디바이스에 대한 Clock Provider를 수행한다.

 

Assigned-clock

몇가지 플랫폼에서는 다음과 같이 clock provider의 rate 설정 또는 mux(parent) 입력 소스 선택을 지원하는 속성을 제공한다.

  • assigned-clocks =
  • assigned-clock-parents =
  • assigned-clock-rates =

 

다음 그림은 clkcon 0의 입력 소스로 pll 2를 선택하게하고, pll 2의 rate를 460800hz로 지정하도록 하는 모습을 보여준다.

 


CCF 구조

drivers/clk 디렉토리를 살펴보면 수 백 개의 클럭 관련 디바이스 파일이 존재한다.

common clock framework에서 사용하는 구조체는 다음과 같다.

  • struct clk_core
    • 클럭 제공자(provider)로 고정 클럭을 포함하여 Rate, Mux, Gate 타입 등 여러 종류의 클럭 클럭을 포함한다.
  • struct clk
    • 클럭 사용자(consumer)로 사용할 clk_core에 연결된다.
    • 예외 사항으로 클럭 consumer가 아니지만 clk_core 생성 시에 따라 생성되는 clk도 있다. 이들의 용도는 clk_core가 삭제되지 않도록 참조한다.
    • 처음 소개된 CCF에서는 부모 관계 설정도 하였지만, 이러한 관계 설정은 clk_core로 이전되었다.
  • struct clk_hw
    • 클럭 hw는 클럭 core와 클럭을 연결한다.
  • struct clk_ops
    • 클럭 코어가 동작하는 여러 가지 후크를 가진다.
  • struct clk_init_data
    • 클럭 코어를 초기화할 때 사용하는 데이터가 담긴다.
  • struct clk_parent_data

 

다음 그림에서 provider로 사용된 clk 구조체와 consumer로 사용된 clk 구조체를 비교해본다.

 

다음 그림은 최근 커널의 Common Clk Framework를 보여준다.

  • 파란색 글씨는 커널 v4.0으로부터 추가된 항목들이다.

 

다음 그림은 Common Clk Framework이 소개되었을 당시의 모습을 보여준다.

 


구현된 기본 공통 클럭 타입

Basic clock implementations common

가장 많이 사용할 수 있는 공통 기본 클럭 구현이 다음과  같이 9개가 준비되어 있다. 공통 기본 클럭 구현만으로 구성 할 수 없는 시스템은 더 확장하여 구성할 수도 있다.

 

Rate 클럭들

Fixed rate 및 Fixed factor 타입 클럭을 제외하고 각자 hw를 제어하기 위해 custom 클럭 디바이스 드라이버가 구현되어야 한다.

  • Fixed rate clock 구현
    • 고정된 rate와 정밀도(accuracy)로 동작하고 gate는 제어할 수 없다.
    • compatible
      • rate를 조절하는 컨트롤 기능이 없는 경우 이 common 드라이버를 사용한다.
      • fixed-clock
  • Fixed Factor(multiplier and divider) clock 구현
    • 배율기 및 분배기로 동작하여 rate를 설정하고 gate는 제어할 수 없다.
    • rate  = 고정된 부모 clock의 rate / 분배값(div) * 배율값(mult)
    • compatible
      • 배율을 조절하는 컨트롤 기능이 없는 경우 이 common 드라이버를 사용한다.
      • fixed-factor-clock
  • divider clock 구현
    • 분배기로 rate가 설정되고 gate는 제어할 수 없다.
    • rate = 고정된 부모 clock의 rate / 분배값(div)
    • compatible
      • 실제 분배율을 hw로 제어해야 하기 때문에 각 클럭 컨트롤러 제조사의 레지스터 주소를 등록하여 사용하도록 각사의 디바이스 드라이버를 사용한다.
      • 예) “ti,divider-clock”
  • Fractional divider clock 구현
    • 분수 분배기로 rate를 설정하고 gate는 제어할 수 없다.
    • rate = 고정된 부모 clock의 rate * 분수(fractional)
    • compatible
      • 실제 분수 분배율을 hw로 제어해야 하기 때문에 각 클럭 컨트롤러 제조사의 레지스터를 컨트롤하는 각사의 디바이스 드라이버를 사용한다.
  • Multiplier clock 구현
    • 배율기로 동작한다.
    • rate = 고정된 부모 clock의 rate * mult
    • compatible
      • 동작에 따른 조작을 hw로 제어해야 하기 때문에 각 클럭 컨트롤러 제조사의 레지스터를 컨트롤하는 각사의 디바이스 드라이버를 사용한다.
      •  예) “allwinner,sun4i-a10-pll3-clk”
    • 참고: clk: Add a basic multiplier clock (2015, v4.4-rc1)

 

Mux 클럭들

각자 hw를 제어하기 위해 custom 클럭 디바이스 드라이버가 구현되어야 한다.

  • Simple multiplexer clock 구현
    • 간단한 멀티플렉서로 동작하여 dynamic하게 부모 클럭을 선택할 수 있고 gate는 제어할 수 없다.
    • compatible
      • 실제 입력 소스를 hw로 선택해야 하기 때문에 각 클럭 컨트롤러 제조사의 레지스터를 컨트롤하는 각사의 디바이스 드라이버를 사용한다.
      • 예) “ti,mux-clock”

 

Gate 클럭들

Gpio gated 타입 클럭은 common gpio 디바이스 드라이버를 통해 hw를 제외하므로 별도의 클럭 디바이스 드라이버의 구현이 필요 없고, 그 외의 gated 타입 클럭은 hw를 제어하기 위해 custom 클럭 디바이스 드라이버가 구현되어야 한다.

  • Gpio gated clock 구현
    • gpio를 사용하여 gate를 제어한다.
    • rate는 직접 지정할 수 없고 고정(fixed)된 부모로부터 rate는 상속받아야 한다.
    •  compatible
      • gate의 hw 조작을 common gpio 드라이버를 통해서 동작하므로 별도의 custome 디바이스 드라이버 제작없이 이 타입의 디바이스 드라이버를 사용한다.
      • gpio-gate-clock
  • Gated clock 구현
    • gate를 제어 할 수 있다.
    • rate는 직접 지정할 수 없고 고정(fixed)된 부모 clock으로부터 rate는 상속받아야 한다.
    • compatible
      • 실제 gate를 hw 제어해야하기 때문에 각 클럭 컨틀롤러 제조사의 레지스터를 컨트롤하는 각자의 디바이스 드라이버 필요하다.
      • 예) ti,gate-clock”

 

Composite 클럭들
  • Composite clock 구현
    • rate, mux(multiplexer) 및 gate 클럭들의 기능을 동시에 복합 구성하여 동작한다.
    • compatible
      • 복합 동작에 따른 조작을 hw 제어해야 하기 때문에 각 클럭 컨트롤러 제조사의 레지스터를 컨트롤하는 각사의 디바이스 드라이버를 사용한다.
      • 예)  “ti,composite-clock”

 

클럭 하드웨어 도면은 아래 사이트를 참고한다.

 

다음 그림은 9개의 클럭 디바이스 사용 예를 각각 보여준다.

 

다음 그림은 common 클럭 타입별 커스텀 드라이버가 제어를 해야할 레지스터 부분을 보여준다.

  • 아래 하늘색 박스와 같이 gpio gate 클럭 드라이버는 common gpio 드라이버를 사용하여 제어한다.
  • 9개의 common 클럭 구현이 준비되어 있다.
    • 3개의 공통 클럭 디바이스 드라이버는 device tree로 설정 가능하며 완성된 형태로 사용된다.
    • 나머지 6개는 custom 클럭 디바이스 드라이버를 통해 쉽게 구현이 가능하다. (ti, samsung, etc…)

 

다음 그림은 common 클럭 타입별 사용되는 ops 콜백 함수를 보여준다.

 

Device Tree 지원

클럭 디바이스 드라이버도 머신 형태의 각 개별 custom 드라이버를 사용하지 않고 점차 Device Tree를 지원하는 형태로 바뀌어 가고 있다. 즉 런타임에 Device Tree Blob 내용을 파싱하여 시스템에 맞게 클럭을 설정하는 형태이므로 모든 설정방법이 Device Tree Script에 표현되어 있어야 한다.

다음의 추가 구조체를 사용한다.

  • clock_provider
  • of_clk_provider

 

심플한 구현

common 클럭들을 사용하는 경우 hw 조작을 위해 관련 레지스터 주소와 함께 클럭을 등록하여 간단하게 디바이스 트리용 클럭 드라이버를 만들 수 있다. 다음 예는 가장 간단히 만들 수 있는 실제 divider 클럭 드라이버 코드를 보여준다.

drivers/clk/h8300/clk-div.c

#include <linux/clk-provider.h>
#include <linux/err.h>
#include <linux/io.h>
#include <linux/of.h>
#include <linux/of_address.h>

static DEFINE_SPINLOCK(clklock);

static void __init h8300_div_clk_setup(struct device_node *node)
{
        unsigned int num_parents;
        struct clk_hw *hw;
        const char *clk_name = node->name;
        const char *parent_name;
        void __iomem *divcr = NULL;
        int width;
        int offset;

        num_parents = of_clk_get_parent_count(node);
        if (!num_parents) {
                pr_err("%s: no parent found\n", clk_name);
                return;
        }

        divcr = of_iomap(node, 0);
        if (divcr == NULL) {
                pr_err("%s: failed to map divide register\n", clk_name);
                goto error;
        }
        offset = (unsigned long)divcr & 3;
        offset = (3 - offset) * 8;
        divcr = (void __iomem *)((unsigned long)divcr & ~3);

        parent_name = of_clk_get_parent_name(node, 0);
        of_property_read_u32(node, "renesas,width", &width);
        hw = clk_hw_register_divider(NULL, clk_name, parent_name,
                                   CLK_SET_RATE_GATE, divcr, offset, width,
                                   CLK_DIVIDER_POWER_OF_TWO, &clklock);
        if (!IS_ERR(hw)) {
                of_clk_add_hw_provider(node, of_clk_hw_simple_get, hw);
                return;
        }
        pr_err("%s: failed to register %s div clock (%ld)\n",
               __func__, clk_name, PTR_ERR(hw));
error:
        if (divcr)
                iounmap(divcr);
}

CLK_OF_DECLARE(h8300_div_clk, "renesas,h8300-div-clock", h8300_div_clk_setup);

2의 배수 단계로 나누는 divider 클럭이다.

  • n 비트 셀렉터 값
    • (레지스터(reg) 값 >> offset) & width
  • width=2인 경우 2 bit 셀렉터를 사용하여 4단계(1/1, 1/2, 1/4, 1/8)로 클럭 rate를 나눌 수 있다.

 

다음은 위 클럭 디바이스 드라이버를 사용하는 4단계 divier 클럭 디바이스를 정의한 디바이스 트리 설정이다. (core_clk 부분)

arch/h8300/boot/dts/h8300h_sim.dts

...
        xclk: oscillator {
                #clock-cells = <0>;
                compatible = "fixed-clock";
                clock-frequency = <20000000>;
                clock-output-names = "xtal";
        };
        core_clk: core_clk {
                compatible = "renesas,h8300-div-clock";
                clocks = <&xclk>;
                #clock-cells = <0>;
                reg = <0xfee01b 2>;
                renesas,width = <2>;
        };
        fclk: fclk {
                compatible = "fixed-factor-clock";
                clocks = <&core_clk>;
                #clock-cells = <0>;
                clock-div = <1>;
                clock-mult = <1>;
        };
...

20Mhz 고정 클럭 -> 4 단계 divider -> 1/1 Factor 클럭 순으로 클럭이 공급됨을 알 수 있다.

 


DEBUGFS 관리

Device Tree로 부팅한 rpi2에서 등록된 7개의 클럭을 다음의 디렉토리를 통해 살펴볼 수 있다.

# cd /sys/kernel/debug/clk
# ls -l
total 0
drwxr-xr-x 2 root root 0 Jan  1  1970 apb_pclk
-r--r--r-- 1 root root 0 Jan  1  1970 clk_dump
-r--r--r-- 1 root root 0 Jan  1  1970 clk_orphan_dump
-r--r--r-- 1 root root 0 Jan  1  1970 clk_orphan_summary
-r--r--r-- 1 root root 0 Jan  1  1970 clk_summary
drwxr-xr-x 2 root root 0 Jan  1  1970 clock
drwxr-xr-x 2 root root 0 Jan  1  1970 core
drwxr-xr-x 2 root root 0 Jan  1  1970 mmc
drwxr-xr-x 2 root root 0 Jan  1  1970 osc
drwxr-xr-x 2 root root 0 Jan  1  1970 pwm
drwxr-xr-x 2 root root 0 Jan  1  1970 uart0_pclk

# cat clk_summary
   clock                         enable_cnt  prepare_cnt        rate   accuracy   phase
----------------------------------------------------------------------------------------
 osc                                      0            0    19200000          0 0
 pwm                                      0            0   100000000          0 0
 apb_pclk                                 1            1   126000000          0 0
 uart0_pclk                               1            1    48000000          0 0
 mmc                                      0            0   250000000          0 0
 core                                     0            0   400000000          0 0
    clock                                 0            0   800000000          0 0

 

다음은 rpi4(Ubuntu 4.18.03)에서 사용된 클럭들을 보여준다. (커널 v4.19)

$ cd /sys/kernel/debug/clk
rpi4 /sys/kernel/debug/clk$ ls -l
total 0
drwxr-xr-x 2 root root 0 Jan  1  1970 aux_spi1
drwxr-xr-x 2 root root 0 Jan  1  1970 aux_spi2
drwxr-xr-x 2 root root 0 Jan  1  1970 aux_uart
drwxr-xr-x 2 root root 0 Jan  1  1970 aveo
drwxr-xr-x 2 root root 0 Jan  1  1970 cam0
drwxr-xr-x 2 root root 0 Jan  1  1970 cam1
-r--r--r-- 1 root root 0 Jan  1  1970 clk_dump
-r--r--r-- 1 root root 0 Jan  1  1970 clk_orphan_dump
-r--r--r-- 1 root root 0 Jan  1  1970 clk_orphan_summary
-r--r--r-- 1 root root 0 Jan  1  1970 clk_summary
drwxr-xr-x 2 root root 0 Jan  1  1970 dft
drwxr-xr-x 2 root root 0 Jan  1  1970 dpi
drwxr-xr-x 2 root root 0 Jan  1  1970 dsi0e
drwxr-xr-x 2 root root 0 Jan  1  1970 dsi0p
drwxr-xr-x 2 root root 0 Jan  1  1970 dsi1e
drwxr-xr-x 2 root root 0 Jan  1  1970 dsi1p
drwxr-xr-x 2 root root 0 Jan  1  1970 emmc
drwxr-xr-x 2 root root 0 Jan  1  1970 emmc2
drwxr-xr-x 2 root root 0 Jan  1  1970 gp0
drwxr-xr-x 2 root root 0 Jan  1  1970 gp1
drwxr-xr-x 2 root root 0 Jan  1  1970 gp2
drwxr-xr-x 2 root root 0 Jan  1  1970 h264
drwxr-xr-x 2 root root 0 Jan  1  1970 hsm
drwxr-xr-x 2 root root 0 Jan  1  1970 isp
drwxr-xr-x 2 root root 0 Jan  1  1970 osc
drwxr-xr-x 2 root root 0 Jan  1  1970 otg
drwxr-xr-x 2 root root 0 Jan  1  1970 otp
drwxr-xr-x 2 root root 0 Jan  1  1970 pcm
drwxr-xr-x 2 root root 0 Jan  1  1970 peri_image
drwxr-xr-x 2 root root 0 Jan  1  1970 plla
drwxr-xr-x 2 root root 0 Jan  1  1970 plla_ccp2
drwxr-xr-x 2 root root 0 Jan  1  1970 plla_core
drwxr-xr-x 2 root root 0 Jan  1  1970 plla_dsi0
drwxr-xr-x 2 root root 0 Jan  1  1970 plla_per
drwxr-xr-x 2 root root 0 Jan  1  1970 pllb
drwxr-xr-x 2 root root 0 Jan  1  1970 pllb_arm
drwxr-xr-x 2 root root 0 Jan  1  1970 pllc
drwxr-xr-x 2 root root 0 Jan  1  1970 pllc_core0
drwxr-xr-x 2 root root 0 Jan  1  1970 pllc_core1
drwxr-xr-x 2 root root 0 Jan  1  1970 pllc_core2
drwxr-xr-x 2 root root 0 Jan  1  1970 pllc_per
drwxr-xr-x 2 root root 0 Jan  1  1970 plld
drwxr-xr-x 2 root root 0 Jan  1  1970 plld_core
drwxr-xr-x 2 root root 0 Jan  1  1970 plld_dsi0
drwxr-xr-x 2 root root 0 Jan  1  1970 plld_dsi1
drwxr-xr-x 2 root root 0 Jan  1  1970 plld_per
drwxr-xr-x 2 root root 0 Jan  1  1970 pwm
drwxr-xr-x 2 root root 0 Jan  1  1970 sdram
drwxr-xr-x 2 root root 0 Jan  1  1970 slim
drwxr-xr-x 2 root root 0 Jan  1  1970 smi
drwxr-xr-x 2 root root 0 Jan  1  1970 tec
drwxr-xr-x 2 root root 0 Jan  1  1970 timer
drwxr-xr-x 2 root root 0 Jan  1  1970 tsens
drwxr-xr-x 2 root root 0 Jan  1  1970 uart
drwxr-xr-x 2 root root 0 Jan  1  1970 v3d
drwxr-xr-x 2 root root 0 Jan  1  1970 vec
drwxr-xr-x 2 root root 0 Jan  1  1970 vpu

rpi4 /sys/kernel/debug/clk$ cat clk_summary
                                 enable  prepare  protect                                duty
   clock                          count    count    count        rate   accuracy phase  cycle
---------------------------------------------------------------------------------------------
 otg                                  0        0        0   480000000          0     0  50000
 osc                                  5        5        0    54000000          0     0  50000
    tsens                             1        1        0     3375000          0     0  50000
    otp                               0        0        0    13500000          0     0  50000
    timer                             0        0        0     1000000          0     0  50000
    plld                              5        5        0  2999999988          0     0  50000
       plld_dsi1                      1        1        0    11718750          0     0  50000
       plld_dsi0                      1        1        0    11718750          0     0  50000
       plld_per                       3        3        0   749999997          0     0  50000
          emmc2                       1        1        0    99999999          0     0  50000
          uart                        1        1        0    47999999          0     0  50000
       plld_core                      1        1        0   599999998          0     0  50000
    pllc                              5        5        0  2999999988          0     0  50000
       pllc_per                       1        1        0   599999998          0     0  50000
          emmc                        0        0        0   199999999          0     0  50000
       pllc_core2                     1        1        0    11718750          0     0  50000
       pllc_core1                     1        1        0    11718750          0     0  50000
       pllc_core0                     2        2        0   499999998          0     0  50000
          vpu                         2        2        0   500000000          0     0  50000
             aux_spi2                 0        0        0   500000000          0     0  50000
             aux_spi1                 0        0        0   500000000          0     0  50000
             aux_uart                 0        0        0   500000000          0     0  50000
             peri_image               0        0        0   500000000          0     0  50000
    pllb                              2        2        0  2999999988          0     0  50000
       pllb_arm                       1        1        0  1499999994          0     0  50000
    plla                              2        2        0  2999999988          0     0  50000
       plla_ccp2                      0        0        0    11718750          0     0  50000
       plla_dsi0                      0        0        0    11718750          0     0  50000
       plla_per                       0        0        0    11718750          0     0  50000
       plla_core                      2        2        0   499999998          0     0  50000
          h264                        0        0        0   499999998          0     0  50000
          isp                         0        0        0   499999998          0     0  50000
          v3d                         1        1        0    31257631          0     0  50000
 dsi1p                                0        0        0           0          0     0  50000
 dsi0p                                0        0        0           0          0     0  50000
 dsi1e                                0        0        0           0          0     0  50000
 dsi0e                                0        0        0           0          0     0  50000
 cam1                                 0        0        0           0          0     0  50000
 cam0                                 0        0        0           0          0     0  50000
 dpi                                  0        0        0           0          0     0  50000
 tec                                  0        0        0           0          0     0  50000
 smi                                  0        0        0           0          0     0  50000
 slim                                 0        0        0           0          0     0  50000
 gp2                                  0        0        0           0          0     0  50000
 gp1                                  0        0        0           0          0     0  50000
 gp0                                  0        0        0           0          0     0  50000
 dft                                  0        0        0           0          0     0  50000
 aveo                                 0        0        0           0          0     0  50000
 pcm                                  0        0        0           0          0     0  50000
 pwm                                  0        0        0           0          0     0  50000
 sdram                                0        0        0           0          0     0  50000
 hsm                                  0        0        0           0          0     0  50000
 vec                                  0        0        0           0          0     0  50000

 


Device Tree로 Clk 초기화

of_clk_init()

drivers/clk/clk.c

/**
 * of_clk_init() - Scan and init clock providers from the DT
 * @matches: array of compatible values and init functions for providers.
 *
 * This function scans the device tree for matching clock providers
 * and calls their initialization functions. It also does it by trying
 * to follow the dependencies.
 */
void __init of_clk_init(const struct of_device_id *matches)
{
        const struct of_device_id *match;
        struct device_node *np;
        struct clock_provider *clk_provider, *next;
        bool is_init_done;
        bool force = false;
        LIST_HEAD(clk_provider_list);

        if (!matches)
                matches = &__clk_of_table;

        /* First prepare the list of the clocks providers */
        for_each_matching_node_and_match(np, matches, &match) {
                struct clock_provider *parent;

                if (!of_device_is_available(np))
                        continue;

                parent = kzalloc(sizeof(*parent), GFP_KERNEL);
                if (!parent) {
                        list_for_each_entry_safe(clk_provider, next,
                                                 &clk_provider_list, node) {
                                list_del(&clk_provider->node);
                                of_node_put(clk_provider->np);
                                kfree(clk_provider);
                        }
                        of_node_put(np);
                        return;
                }

                parent->clk_init_cb = match->data;
                parent->np = of_node_get(np);
                list_add_tail(&parent->node, &clk_provider_list);
        }

        while (!list_empty(&clk_provider_list)) {
                is_init_done = false;
                list_for_each_entry_safe(clk_provider, next,
                                        &clk_provider_list, node) {
                        if (force || parent_ready(clk_provider->np)) {

                                /* Don't populate platform devices */
                                of_node_set_flag(clk_provider->np,
                                                 OF_POPULATED);

                                clk_provider->clk_init_cb(clk_provider->np);
                                of_clk_set_defaults(clk_provider->np, true);

                                list_del(&clk_provider->node);
                                of_node_put(clk_provider->np);
                                kfree(clk_provider);
                                is_init_done = true;
                        }
                }

                /*
                 * We didn't manage to initialize any of the
                 * remaining providers during the last loop, so now we
                 * initialize all the remaining ones unconditionally
                 * in case the clock parent was not mandatory
                 */
                if (!is_init_done)
                        force = true;
        }
}

Device Tree에서 clock provider를 스캔하고 초기화한다.

  • 코드 라인 10~11에서 matches 값으로 null이 지정된 경우 컴파일 타임에 CLK_OF_DECLARE() 매크로로 만들어진 __clk_of_table 섹션에 있는 모든 클럭 엔트리를 대상으로 한다.
  • 코드 라인 14~35에서 Device Tree에서 matches와 엔트리와 비교하여 일치하는 항목들에 대해 루프를 돌며 clock_provider를 할당받아 구성하고 clk_provider_list에 추가한다. 메모리 부족 시에는 등록된 모든 clock_provider를 할당 해제하고함수를 빠져나간다.
    • clk_init_cb에는 클럭 초기화 함수가 대입된다.
    • np에는 device_node가 대입된다.
  • 코드 라인 37~38에서 clk_provider_list의 엔트리가 비워질때 까지 루프를 돈다. is_init_done을 false로 하여 다음 루프에서 초기화함수를 한 번도 호출하지 않은 경우 다음에 루프를 돌면 강제로 호출하게 만든다.
  • 코드 라인 39~40에서 clk_provide_list의 엔트리 수 만큼 루프를 돈다.
  • 코드 라인 41~54에서 이전 루프에서 한 번도 초기화 함수를 호출하지 않은 경우와 부모 클럭이 없거나 모두 초기화된 경우 해당 클럭의 초기화 함수를 호출하고 이 클럭을 default로 설정한다. 그런 후 clk_provider_list에서 제거하고 루프내에서 한 번이라도 초기화되었음을 알리도록 is_int_done을 true로 설정한다.
  • 코드 라인 63~64에서 이 전 루프를 돌 때 한 번도 초기화된 적 없으면 force를 true로 하여 다시 한 번 루프를 돌 때 남은 나머지 클럭을 무조건 초기화 처리하도록 한다.

 

다음 그림에서와 같이 rpi2는 clock@0 ~ clock@6 까지 총 7개의 clock을 사용하며 이에 대한 각각의 초기화 함수가 호출되는 것을 보여준다.

  • clock@0~4, 6의 6개의 클럭은 “fixed-clock” 디바이스 드라이버에서 구현된 fixed rate clock 타입으로 of_fixed_clk_setup() 함수를 호출한다.
  • clock@5를 사용하는 1 개의 uart 클럭은 “fixed-factor-clock” 디바이스 드라이버에서 구현된 Fixed multiplier and divider clock 타입으로 of_fixed_factor_clk_setup() 함수를 호출한다.
    • 이 클럭의 부모 클럭인 core clock에서 사용하는 rate 값인 250Mhz를 2배 곱하여(multiplex) 500Mhz로 uart clock의 rate로 동작시킨다.

 

다음 그림은 좌측의 A~I 까지의 클럭 디바이스 구성에 대해 부모(parent) 클럭 디바이스부터 초기화되는 과정을 보여준다.

  • 리스트에 H, C, D, B, E, A, F, G 클럭 프로바이더들이 등록되어 있다고 가정한다.

 

parent_ready()

drivers/clk/clk.c

/*
 * This function looks for a parent clock. If there is one, then it
 * checks that the provider for this parent clock was initialized, in
 * this case the parent clock will be ready.
 */
static int parent_ready(struct device_node *np)
{
        int i = 0;

        while (true) {
                struct clk *clk = of_clk_get(np, i);

                /* this parent is ready we can check the next one */
                if (!IS_ERR(clk)) {
                        clk_put(clk);
                        i++;
                        continue;
                }

                /* at least one parent is not ready, we exit now */
                if (PTR_ERR(clk) == -EPROBE_DEFER)
                        return 0;

                /*
                 * Here we make assumption that the device tree is
                 * written correctly. So an error means that there is
                 * no more parent. As we didn't exit yet, then the
                 * previous parent are ready. If there is no clock
                 * parent, no need to wait for them, then we can
                 * consider their absence as being ready
                 */           
                return 1;
        }
}

요청한 클럭 노드의 부모 클럭 노드들 모두가 초기화 되었는지 여부를 알아온다. 1=부모 클럭 노드가 없거나 모두 초기화 된 경우, 0=부모 클럭 노드들 중 하나라도 초기화 되지 않았을 경우

  • 코드 라인 5~6에서 클럭의 부모 노드가 여러 개일 수 있으므로 루프를 반복하고 지정한 인덱스의 부모 클럭을 알아온다.
  • 코드 라인 9~13에서 지정된 인덱스의 부모 클럭이 이미 초기화된 경우 인덱스를 증가시키고 skip 한다.
  • 코드 라인 16~17에서 부모 클럭 노드가 아직 초기화되지 않은 경우 0을 반환한다.
  • 코드 라인 27에서 부모가 없는 경우 1을 반환한다.

 

clk 검색 -1-

clk_core_lookup()

drivers/clk/clk.c

static struct clk_core *clk_core_lookup(const char *name)
{
        struct clk_core *root_clk;
        struct clk_core *ret;

        if (!name)
                return NULL;    

        /* search the 'proper' clk tree first */
        hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
                ret = __clk_lookup_subtree(name, root_clk);
                if (ret)
                        return ret;
        }

        /* if not found, then search the orphan tree */
        hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
                ret = __clk_lookup_subtree(name, root_clk);
                if (ret)
                        return ret;
        }

        return NULL;
}

루트 클럭 리스트와 고아(orphan) 클럭 리스트에 등록된 모든 하위 클럭들을 포함하여 요청한 이름의 클럭(clk_core)을 검색한다. 검색되지 않는 경우 null을 반환한다.

  • 코드 라인 10에서 clk_root_list에 등록된 루트 클럭들에 대해 루프를 돈다.
  • 코드 라인 11~13에서 루트 클럭을 포함해서 하위 트리에서 요청한 이름의 클럭(clk_core)을 찾아 반환한다.
  • 코드 라인 17~21에서 clk_orphan_list에 등록된 고아 클럭들에 대해 루프를 돌며 고아 클럭을 포함하여 하위 트리에서 요청한 이름의 클럭(clk_core)을 찾아 반환한다.

 

다음 그림은 “F”라는 이름의 클럭을 검색시 child 클럭을 검색하는 순서를 보여준다.

 

__clk_lookup_subtree()

drivers/clk/clk.c

static struct clk_core *__clk_lookup_subtree(const char *name,
                                             struct clk_core *clk)
{
        struct clk_core *child;
        struct clk_core *ret;

        if (!strcmp(clk->name, name))
                return clk;

        hlist_for_each_entry(child, &clk->children, child_node) {
                ret = __clk_lookup_subtree(name, child);
                if (ret)
                        return ret;
        }

        return NULL;
}

현재 클럭 및 그 자식 클럭에서 요청한 이름의 클럭(clk_core)을 검색한다. 검색되지 않는 경우 null을 반환한다.

  • 코드 라인 7~8에서 요청한 이름의 클럭인 경우 그 클럭을 반환한다.
  • 코드 라인 10에서 자식 클럭이 있는 경우 그 수 만큼 루프를 돈다.
  • 코드 라인 11에서 자식 클럭과 그 이하 서브 트리를 재귀 검색한다.
  • 코드 라인 12~13에서 클럭이 검색된 경우 반환한다.

 

clk 검색 -2- (부모 인덱스 사용)

clk_core_get_parent_by_index()

drivers/clk/clk.c

static struct clk_core *clk_core_get_parent_by_index(struct clk_core *clk,
                                                         u8 index)
{
        if (!clk || index >= clk->num_parents)
                return NULL;
        else if (!clk->parents)
                return clk_core_lookup(clk->parent_names[index]);
        else if (!clk->parents[index])
                return clk->parents[index] =
                        clk_core_lookup(clk->parent_names[index]);
        else
                return clk->parents[index];
}

부모 인덱스 값으로 부모 클럭을 찾아 반환한다.

  • 코드 라인 4~5에서 인덱스값이 num_parents를 초과하는 경우 null을 반환한다.
  • 코드 라인 7~8에서 인덱스에 해당하는 부모 클럭맵을 설정한다.
  • 코드 라인 11~12에서 인덱스에 해당하는 부모 클럭을 반환한다.

 

clk_core_fill_parent_index()

drivers/clk/clk.c

static void clk_core_fill_parent_index(struct clk_core *core, u8 index)
{
        struct clk_parent_map *entry = &core->parents[index];
        struct clk_core *parent = ERR_PTR(-ENOENT);

        if (entry->hw) {
                parent = entry->hw->core;
                /*
                 * We have a direct reference but it isn't registered yet?
                 * Orphan it and let clk_reparent() update the orphan status
                 * when the parent is registered.
                 */
                if (!parent)
                        parent = ERR_PTR(-EPROBE_DEFER);
        } else {
                parent = clk_core_get(core, index);
                if (IS_ERR(parent) && PTR_ERR(parent) == -ENOENT && entry->name)
                        parent = clk_core_lookup(entry->name);
        }

        /* Only cache it if it's not an error */
        if (!IS_ERR(parent))
                entry->core = parent;
}

@index에 해당하는 부모 클럭맵을 설정한다.

 


Device Tree로 클럭 셋업

Fixed Rate 타입 Clk 디바이스 셋업

of_fixed_clk_setup()

drivers/clk/clk-fixed-rate.c

/**
 * of_fixed_clk_setup() - Setup function for simple fixed rate clock
 */
void __init of_fixed_clk_setup(struct device_node *node)
{
        _of_fixed_clk_setup(node);
}
CLK_OF_DECLARE(fixed_clk, "fixed-clock", of_fixed_clk_setup);

Device Tree의 요청 클럭 노드 정보로 fixed rate 타입의 클럭을 설정한다.

 

_of_fixed_clk_setup()

drivers/clk/clk-fixed-rate.c

static struct clk *_of_fixed_clk_setup(struct device_node *node)
{
        struct clk *clk;
        const char *clk_name = node->name;
        u32 rate;
        u32 accuracy = 0;

        if (of_property_read_u32(node, "clock-frequency", &rate))
                return ERR_PTR(-EIO);

        of_property_read_u32(node, "clock-accuracy", &accuracy);

        of_property_read_string(node, "clock-output-names", &clk_name);

        clk = clk_register_fixed_rate_with_accuracy(NULL, clk_name, NULL,
                                                    0, rate, accuracy);
        if (IS_ERR(clk))
                return clk;

        ret = of_clk_add_provider(node, of_clk_src_simple_get, clk);
        if (ret) {
                clk_unregister(clk);
                return ERR_PTR(ret);
        }

        return clk;
}

Device Tree의 요청 클럭 노드 정보로 fixed rate 타입의 클럭을 설정한다.

  • 코드 라인 8~9에서 요청한 클럭 노드에서 “clock-frequency” 속성 값을 읽어 rate에 대입한다.
  • 코드 라인 11에서 “clock-accuracy” 값을 읽어 accuracy에 대입한다. 속성이 없는 경우 accuracy=0 이다.
  • 코드 라인 13에서 “clock-output-names” 속성 값(문자열)을 읽어 clk_name에 대입한다. 속성이 없는 경우 노드명을 사용한다.
  • 코드 라인 15~18에서 읽어들인 rate, accuracy 및 clk_name으로 fixed rate 타입의 루트 클럭으로 등록한다.
  • 코드 라인 20~26에서 등록이 성공된 경우 클럭 provider에 추가하고, 클럭을 반환한다.

 

Fixed Factor 타입 Clk 디바이스 셋업

of_fixed_factor_clk_setup()

drivers/clk/clk-fixed-factor.c

/**
 * of_fixed_factor_clk_setup() - Setup function for simple fixed factor clock
 */
void __init of_fixed_factor_clk_setup(struct device_node *node)
{
        _of_fixed_factor_clk_setup(node);
}
CLK_OF_DECLARE(fixed_factor_clk, "fixed-factor-clock",
                of_fixed_factor_clk_setup);

Device Tree의 요청 클럭 노드 정보로 fixed factor 타입의 클럭을 설정한다.

 

_of_fixed_factor_clk_setup()

drivers/clk/clk-fixed-factor.c

static struct clk_hw *_of_fixed_factor_clk_setup(struct device_node *node)
{
        struct clk *clk;
        const char *clk_name = node->name;
        const char *parent_name;
        u32 div, mult;
        int ret;

        if (of_property_read_u32(node, "clock-div", &div)) {
                pr_err("%s Fixed factor clock <%s> must have a clock-div property\n",
                        __func__, node->name);
                return ERR_PTR(-EIO);
        }

        if (of_property_read_u32(node, "clock-mult", &mult)) {
                pr_err("%s Fixed factor clock <%s> must have a clock-mult property\n",
                        __func__, node->name);
                return ERR_PTR(-EIO);
        }

        of_property_read_string(node, "clock-output-names", &clk_name);

        if (of_match_node(set_rate_parent_matches, node))
                flags |= CLK_SET_RATE_PARENT;

        hw = __clk_hw_register_fixed_factor(NULL, node, clk_name, NULL, 0,
                                            flags, mult, div);
        if (IS_ERR(hw)) {
                /*
                 * Clear OF_POPULATED flag so that clock registration can be
                 * attempted again from probe function.
                 */
                of_node_clear_flag(node, OF_POPULATED);
                return ERR_CAST(hw);
        }

        ret = of_clk_add_hw_provider(node, of_clk_hw_simple_get, hw);
        if (ret) {
                clk_hw_unregister_fixed_factor(hw);
                return ERR_PTR(ret);
        }

        return hw;
}

Device Tree의 요청 클럭 노드 정보로 fixed factor 타입의 클럭을 설정한다.

  • 코드 라인 9~13에서 요청한 클럭 노드에서 “clock-div” 속성 값을 읽어 div에 대입한다.
  • 코드 라인 15~19에서 “clock-mult” 값을 읽어 multi에 대입한다.
  • 코드 라인 21에서 “clock-output-names” 속성 값(문자열)을 읽어 clk_name에 대입한다. 속성이 없는 경우 노드명을 사용한다.
  • 코드 라인 23~24에서 디바이스 노드가 set_rate_parent_matches[]에 적합한 드라이버인 경우 CLK_SET_RATE_PARENT 플래그를 설정한다.
  • 코드 라인 26~35에서 읽어들인 div, multi, parent_name 및 clk_name으로 fixed factor 타입의 클럭으로 등록한다.
  • 코드 라인 37~43에서 등록이 성공된 경우 클럭 provider에 추가하고 clk_hw를 반환한다.

 

다음 그림은 rpi2의 Device Tree Script (커널 v4.10 기준)에서 각 clock에 대한 연관도를 보여준다.

  • /soc/cprman은 플랫폼 클럭 장치로 7개의 클럭을 제공하고 4개의 클럭 입력을 받는다.
  • /soc/aux 디바이스는 3 개의 gate clock 장치이다. (0번은 aux_uart용으로 spi1 및 spi2 장치와 인터럽트가 공유된다.)
  • /soc/dsi1 디바이스는 GPU용 DSI 장치이다.

 


타입별 클럭 등록 함수

9개 타입의 클럭에 대해 multiplier 타입을 제외한 등록 함수들이 API로 제공된다. 그리고 fixed rate 및 fixed factor 타입의 경우 디바이스 트리를 사용한 API도 제공된다. 소스 분석은 fixed rate 타입과 fixed factor 타입으로 제한하였다.

  • multiplier 타입의 경우는 아직 composite 타입에 연동하여 사용되므로 등록 함수는 제외된다.

 

다음 그림과 같이 클럭은 3가지 형태로 구분할 수 있으며 총 9개의 타입을 가지고 있으며 각각의 등록 함수를 보여준다.

common 클럭 타입별 ops

composite 타입을 제외한 ops를 알아본다. composite 타입의 경우 별도의 ops를 사용하지 않고, 다른 클럭 타입들 중 rate, mux 및 gate 클럭의 ops를 두 개 이상 지정하여 복합 구성할 수 있다.

 

clk_fixed_rate_ops

/drivers/clk/clk-fixed-rate.c

const struct clk_ops clk_fixed_rate_ops = {
        .recalc_rate = clk_fixed_rate_recalc_rate,
        .recalc_accuracy = clk_fixed_rate_recalc_accuracy,
};
EXPORT_SYMBOL_GPL(clk_fixed_rate_ops);

 

clk_fixed_factor_ops

/drivers/clk/clk-fixed-factor.c

const struct clk_ops clk_fixed_factor_ops = {
        .round_rate = clk_factor_round_rate,
        .set_rate = clk_factor_set_rate,
        .recalc_rate = clk_factor_recalc_rate,
};
EXPORT_SYMBOL_GPL(clk_fixed_factor_ops);

 

clk_divider_ops

/drivers/clk/clk-divider.c

const struct clk_ops clk_divider_ops = {
        .recalc_rate = clk_divider_recalc_rate,
        .round_rate = clk_divider_round_rate,
        .set_rate = clk_divider_set_rate,
};
EXPORT_SYMBOL_GPL(clk_divider_ops);

 

clk_divider_ro_ops

/drivers/clk/clk-divider.c

const struct clk_ops clk_divider_ro_ops = {
        .recalc_rate = clk_divider_recalc_rate,
        .round_rate = clk_divider_round_rate,
};
EXPORT_SYMBOL_GPL(clk_divider_ro_ops);
  • CLK_DIVIDER_READ_ONLY 플래그를 사용하는 경우 사용되는 ops이다.

 

clk_fractional_divider_ops

/drivers/clk/

const struct clk_ops clk_fractional_divider_ops = {
        .recalc_rate = clk_fd_recalc_rate,
        .round_rate = clk_fd_round_rate,
        .set_rate = clk_fd_set_rate,
};
EXPORT_SYMBOL_GPL(clk_fractional_divider_ops);

 

clk_multiplier_ops

/drivers/clk/clk-fractional-divider.c

const struct clk_ops clk_multiplier_ops = {
        .recalc_rate    = clk_multiplier_recalc_rate,
        .round_rate     = clk_multiplier_round_rate,
        .set_rate       = clk_multiplier_set_rate,
};
EXPORT_SYMBOL_GPL(clk_multiplier_ops);

 

clk_mux_ops

/drivers/clk/clk-mux.c

const struct clk_ops clk_mux_ops = {
        .get_parent = clk_mux_get_parent,
        .set_parent = clk_mux_set_parent,
        .determine_rate = clk_mux_determine_rate,
};
EXPORT_SYMBOL_GPL(clk_mux_ops);

 

clk_mux_ro_ops

/drivers/clk/clk-mux.c

const struct clk_ops clk_mux_ro_ops = {
        .get_parent = clk_mux_get_parent,
};
EXPORT_SYMBOL_GPL(clk_mux_ro_ops);
  • CLK_MUX_READ_ONLY 플래그를 사용하는 경우 사용되는 ops이다.

 

clk_gpio_gate_ops

/drivers/clk/clk-gpio.c

const struct clk_ops clk_gpio_gate_ops = {
        .enable = clk_gpio_gate_enable,
        .disable = clk_gpio_gate_disable,
        .is_enabled = clk_gpio_gate_is_enabled,
};
EXPORT_SYMBOL_GPL(clk_gpio_gate_ops);

 

clk_gate_ops

/drivers/clk/clk-gate.c

const struct clk_ops clk_gate_ops = {
        .enable = clk_gate_enable,
        .disable = clk_gate_disable,
        .is_enabled = clk_gate_is_enabled,
};
EXPORT_SYMBOL_GPL(clk_gate_ops);

 

Fixed Rate 타입 Clk 등록

clk_register_fixed_rate()

drivers/clk/clk-fixed-rate.c

struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
                const char *parent_name, unsigned long flags,
                unsigned long fixed_rate)
{
        return clk_register_fixed_rate_with_accuracy(dev, name, parent_name,
                                                     flags, fixed_rate, 0);
}
EXPORT_SYMBOL_GPL(clk_register_fixed_rate);

클럭 디바이스, @name, @parent_name, @flags, @fixed_rate 정보를 인수로 accuracy가 0인 fixed rate 타입의 클럭을 등록한다.

 

clk_register_fixed_rate_with_accuracy()

drivers/clk/clk-fixed-rate.c

struct clk *clk_register_fixed_rate_with_accuracy(struct device *dev,
                const char *name, const char *parent_name, unsigned long flags,
                unsigned long fixed_rate, unsigned long fixed_accuracy)
{
        struct clk_hw *hw;

        hw = clk_hw_register_fixed_rate_with_accuracy(dev, name, parent_name,
                        flags, fixed_rate, fixed_accuracy);
        if (IS_ERR(hw))
                return ERR_CAST(hw);
        return hw->clk;
}
EXPORT_SYMBOL_GPL(clk_register_fixed_rate_with_accuracy);

클럭 디바이스, @name, @parent_name, @flags, @fixed_rate, @fixed_accuracy 정보를 인수로 받아 fixed rate 타입의 클럭을 등록한다.

 

clk_hw_register_fixed_rate()

drivers/clk/clk-fixed-rate.c

/**
 * clk_hw_register_fixed_rate - register fixed-rate clock with the clock
 * framework
 * @dev: device that is registering this clock
 * @name: name of this clock
 * @parent_name: name of clock's parent
 * @flags: framework-specific flags
 * @fixed_rate: non-adjustable clock rate
 */
struct clk_hw *clk_hw_register_fixed_rate(struct device *dev, const char *name,
                const char *parent_name, unsigned long flags,
                unsigned long fixed_rate)
{
        return clk_hw_register_fixed_rate_with_accuracy(dev, name, parent_name,
                                                     flags, fixed_rate, 0);
}
EXPORT_SYMBOL_GPL(clk_hw_register_fixed_rate);

클럭 디바이스, @name, @parent_name, @flags, @fixed_rate 정보를 인수로 accuracy가 0인 fixed rate 타입의 클럭을 등록한다.

 

clk_hw_register_fixed_rate_with_accuracy()

drivers/clk/clk-fixed-rate.c

/**
 * clk_hw_register_fixed_rate_with_accuracy - register fixed-rate clock with
 * the clock framework
 * @dev: device that is registering this clock
 * @name: name of this clock
 * @parent_name: name of clock's parent
 * @flags: framework-specific flags
 * @fixed_rate: non-adjustable clock rate
 * @fixed_accuracy: non-adjustable clock rate
 */
struct clk_hw *clk_hw_register_fixed_rate_with_accuracy(struct device *dev,
                const char *name, const char *parent_name, unsigned long flags,
                unsigned long fixed_rate, unsigned long fixed_accuracy)
{
        struct clk_fixed_rate *fixed;
        struct clk_hw *hw;
        struct clk_init_data init;
        int ret;

        /* allocate fixed-rate clock */
        fixed = kzalloc(sizeof(*fixed), GFP_KERNEL);
        if (!fixed)
                return ERR_PTR(-ENOMEM);

        init.name = name;
        init.ops = &clk_fixed_rate_ops;
        init.flags = flags;
        init.parent_names = (parent_name ? &parent_name: NULL);
        init.num_parents = (parent_name ? 1 : 0);

        /* struct clk_fixed_rate assignments */
        fixed->fixed_rate = fixed_rate;
        fixed->fixed_accuracy = fixed_accuracy;
        fixed->hw.init = &init;

        /* register the clock */
        hw = &fixed->hw;
        ret = clk_hw_register(dev, hw);
        if (ret) {
                kfree(fixed);
                hw = ERR_PTR(ret);
        }

        return hw;
}
EXPORT_SYMBOL_GPL(clk_hw_register_fixed_rate_with_accuracy);

클럭 디바이스, @name, @parent_name, @flags, @fixed_rate, @fixed_accuracy 정보를 인수로 받아 fixed rate 타입의 클럭을 등록한다.

  • 코드 라인 11~19에서 clk_fixed_rate 구조체를 할당받아 fixed rate 클럭에대한 기본 정보를 구성한다.
  • 코드 라인 22~24에서 @fixed_rate, @fixed_accuracy  등을 추가하여 구성한다.
  • 코드 라인 27~34에서 클럭을 등록한 후 clk_hw를 반환한다.

 

다음 그림은 fixed rate 타입의 클럭을 등록하는 모습을 보여준다.

 

Fixed Factor 타입 Clk 등록

clk_register_fixed_factor()

drivers/clk/clk-fixed-factor.c

struct clk *clk_register_fixed_factor(struct device *dev, const char *name,
                const char *parent_name, unsigned long flags,
                unsigned int mult, unsigned int div)
{
        struct clk_hw *hw;

        hw = clk_hw_register_fixed_factor(dev, name, parent_name, flags, mult,
                                          div);
        if (IS_ERR(hw))
                return ERR_CAST(hw);
        return hw->clk;
}
EXPORT_SYMBOL_GPL(clk_register_fixed_factor);

클럭 디바이스, @name, @parent_name, @flags, @multi, @div 정보를 인수로 받아 fixed factor 타입의 클럭을 등록한다.

 

clk_hw_register_fixed_factor()

drivers/clk/clk-fixed-factor.c

struct clk_hw *clk_hw_register_fixed_factor(struct device *dev,
                const char *name, const char *parent_name, unsigned long flags,
                unsigned int mult, unsigned int div)
{
        return __clk_hw_register_fixed_factor(dev, NULL, name, parent_name, -1,
                                              flags, mult, div);
}
EXPORT_SYMBOL_GPL(clk_hw_register_fixed_factor);

클럭 디바이스, @name, @parent_name, @flags, @multi, @div 정보를 인수로 받아 fixed factor 타입의 클럭을 등록한다.

 

__clk_hw_register_fixed_factor()

drivers/clk/clk-fixed-factor.c

static struct clk_hw *
__clk_hw_register_fixed_factor(struct device *dev, struct device_node *np,
                const char *name, const char *parent_name, int index,
                unsigned long flags, unsigned int mult, unsigned int div)
{
        struct clk_fixed_factor *fix;
        struct clk_init_data init = { };
        struct clk_parent_data pdata = { .index = index };
        struct clk_hw *hw;
        int ret;

        fix = kmalloc(sizeof(*fix), GFP_KERNEL);
        if (!fix)
                return ERR_PTR(-ENOMEM);

        /* struct clk_fixed_factor assignments */
        fix->mult = mult;
        fix->div = div;
        fix->hw.init = &init;

        init.name = name;
        init.ops = &clk_fixed_factor_ops;
        init.flags = flags;
        if (parent_name)
                init.parent_names = &parent_name;
        else
                init.parent_data = &pdata;
        init.num_parents = 1;

        hw = &fix->hw;
        if (dev)
                ret = clk_hw_register(dev, hw);
        else
                ret = of_clk_hw_register(np, hw);
        if (ret) {
                kfree(fix);
                hw = ERR_PTR(ret);
        }

        return hw;
}

클럭 디바이스, @name, @parent_name, @flags, multi, @div 정보를 인수로 받아 fixed factor 타입의 클럭을 등록한다.

  • 코드 라인 12~14에서 clk_fixed_facotr 구조체를 할당받고 multi 값과 div 값 등을 구성한다.
  • 코드 라인 17~28에서 할당 받은 구조체를 fixed factor 타입으로 구성한다.
  • 코드 라인 30~40에서 클럭을 등록한다

 


Clk 등록

clk_register()

drivers/clk/clk.c

/**
 * clk_register - allocate a new clock, register it and return an opaque cookie
 * @dev: device that is registering this clock
 * @hw: link to hardware-specific clock data
 *
 * clk_register is the *deprecated* interface for populating the clock tree with
 * new clock nodes. Use clk_hw_register() instead.
 *
 * Returns: a pointer to the newly allocated struct clk which
 * cannot be dereferenced by driver code but may be used in conjunction with the
 * rest of the clock API.  In the event of an error clk_register will return an
 * error code; drivers must test for an error code after calling clk_register.
 */
struct clk *clk_register(struct device *dev, struct clk_hw *hw)
{
        return __clk_register(dev, dev_of_node(dev), hw);
}
EXPORT_SYMBOL_GPL(clk_register);

요청한 클럭 디바이스를 할당하여 등록한다.

  • 디바이스와 @hw 정보로 clk_core와 clk를 구성하여 등록하고 최상위 노드인 경우 초기화한다.

 

static struct clk *
__clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw)
{
        int ret;
        struct clk_core *core;
        const struct clk_init_data *init = hw->init;

        /*
         * The init data is not supposed to be used outside of registration path.
         * Set it to NULL so that provider drivers can't use it either and so that
         * we catch use of hw->init early on in the core.
         */
        hw->init = NULL;

        core = kzalloc(sizeof(*core), GFP_KERNEL);
        if (!core) {
                ret = -ENOMEM;
                goto fail_out;
        }

        core->name = kstrdup_const(init->name, GFP_KERNEL);
        if (!core->name) {
                ret = -ENOMEM;
                goto fail_name;
        }

        if (WARN_ON(!init->ops)) {
                ret = -EINVAL;
                goto fail_ops;
        }
        core->ops = init->ops;

        if (dev && pm_runtime_enabled(dev))
                core->rpm_enabled = true;
        core->dev = dev;
        core->of_node = np;
        if (dev && dev->driver)
                core->owner = dev->driver->owner;
        core->hw = hw;
        core->flags = init->flags;
        core->num_parents = init->num_parents;
        core->min_rate = 0;
        core->max_rate = ULONG_MAX;
        hw->core = core;

        ret = clk_core_populate_parent_map(core, init);
        if (ret)
                goto fail_parents;

        INIT_HLIST_HEAD(&core->clks);

        /*
         * Don't call clk_hw_create_clk() here because that would pin the
         * provider module to itself and prevent it from ever being removed.
         */
        hw->clk = alloc_clk(core, NULL, NULL);
        if (IS_ERR(hw->clk)) {
                ret = PTR_ERR(hw->clk);
                goto fail_create_clk;
        }

        clk_core_link_consumer(hw->core, hw->clk);

        ret = __clk_core_init(core);
        if (!ret)
                return hw->clk;

        clk_prepare_lock();
        clk_core_unlink_consumer(hw->clk);
        clk_prepare_unlock();

        free_clk(hw->clk);
        hw->clk = NULL;

fail_create_clk:
        clk_core_free_parent_map(core);
fail_parents:
fail_ops:
        kfree_const(core->name);
fail_name:
        kfree(core);
fail_out:
        return ERR_PTR(ret);
}

요청한 클럭 디바이스를 할당하여 등록한다.

  • 코드 라인 6에서 클럭 init 데이터를 클럭 core에 복사할 목적으로 init 포인터에 대입한다.
  • 코드 라인 15~19에서 clk_core 구조체를 할당받아 core에 대입한다.
  • 코드 라인 21~25에서 core->name에 hw->init->name의 복사본 문자열을 대입한다.
    • kstrdup_const()
      • 문자열이 rodata(읽기 전용 커널 데이터) 섹션에 위치한 경우 복제하지 않고 const 타입으로 그냥 사용한다.
  • 코드 라인 27~31에서 클럭 init 데이터로 넘겨 받은 클럭의 ops를 지정한다.
  • 코드 라인 33~44에서 클럭 init 데이터를 사용하여 clk_core 구조체를 구성한다.
  • 코드 라인 46~48에서 부모 클럭 맵을 준비한다.
  • 코드 라인 50에서 core->clks 리스트를 초기화한다.
  • 코드 라인 56~60에서 clk를 할당받아 구성한다.
  • 코드 라인 62에서 클럭 core를 클럭 리스트에 추가한다.
  • 코드 라인 64~66에서 클럭 core를 초기화하고, 성공한 경우 클럭을 반환한다.
  • 코드 라인 68~83에서 클럭 core 등록이 실패한 경우 생성했던 모든 객체들을 free 시키고 에러를 반환한다.

 

다음 그림은 부모 노드를 가진 클럭을 등록하는 모습을 보여준다.

 

다음 그림은 등록된 클럭의 부모 관계를 보여준다. 클럭을 사용하는 관점에서 Consumer용 클럭도 같이 표현하였다.

 

clk_core_populate_parent_map()

drivers/clk/clk.c

static int clk_core_populate_parent_map(struct clk_core *core,
                                        const struct clk_init_data *init)
{
        u8 num_parents = init->num_parents;
        const char * const *parent_names = init->parent_names;
        const struct clk_hw **parent_hws = init->parent_hws;
        const struct clk_parent_data *parent_data = init->parent_data;
        int i, ret = 0;
        struct clk_parent_map *parents, *parent;

        if (!num_parents)
                return 0;

        /*
         * Avoid unnecessary string look-ups of clk_core's possible parents by
         * having a cache of names/clk_hw pointers to clk_core pointers.
         */
        parents = kcalloc(num_parents, sizeof(*parents), GFP_KERNEL);
        core->parents = parents;
        if (!parents)
                return -ENOMEM;

        /* Copy everything over because it might be __initdata */
        for (i = 0, parent = parents; i < num_parents; i++, parent++) {
                parent->index = -1;
                if (parent_names) {
                        /* throw a WARN if any entries are NULL */
                        WARN(!parent_names[i],
                                "%s: invalid NULL in %s's .parent_names\n",
                                __func__, core->name);
                        ret = clk_cpy_name(&parent->name, parent_names[i],
                                           true);
                } else if (parent_data) {
                        parent->hw = parent_data[i].hw;
                        parent->index = parent_data[i].index;
                        ret = clk_cpy_name(&parent->fw_name,
                                           parent_data[i].fw_name, false);
                        if (!ret)
                                ret = clk_cpy_name(&parent->name,
                                                   parent_data[i].name,
                                                   false);
                } else if (parent_hws) {
                        parent->hw = parent_hws[i];
                } else {
                        ret = -EINVAL;
                        WARN(1, "Must specify parents if num_parents > 0\n");
                }

                if (ret) {
                        do {
                                kfree_const(parents[i].name);
                                kfree_const(parents[i].fw_name);
                        } while (--i >= 0);
                        kfree(parents);

                        return ret;
                }
        }

        return 0;
}

요청한 클럭의 부모들을 연결하는 부모 맵을 구성한다. @init에서 넘겨준 parent_names, parent_data 또는 parent_hws 라는 세 가지 정보들 중 하나로 부모 정보를 구성한다.

  • 코드 라인 11~12에서 부모 클럭이 없는 경우 성공 값으로 0을 반환한다.
  • 코드 라인 18~21에서 부모 수 만큼 clk_parent_data 구조체를 할당받는다.
  • 코드 라인 24~47에서 @init 정보에 포함된 부모 클럭 정보를 가져와서 구성한다. 다음 순서 중 하나를 선택하여 구성한다.
    • init->parent_names[] 정보가 제공된 경우 이 이름 정보만으로 구성한다.
    • init->parent_data[] 정보가 제공된 경우 이 구성 정보를 그대로 복사하여 구성한다.
    • init->parent_hws[] 정보가 제공된 경우 이 hws 정보를 그대로 이용한다.
  • 코드 라인 49~58에서 부모 정보가 제대로 제공되지 않은 경우 할당 했었던 정보를 할당 해제 후 에러를 반환한다.
  • 코드 라인 60에서 에러 없이 모든 부모 정보를 구성한 경우 성공 값으로 0을 반환한다.

 

다음 그림은 해당 클럭의 부모들 연결하는 부모 맵을 구성하는 과정을 보여준다.

 

alloc_clk()

drivers/clk/clk.c

/**
 * alloc_clk - Allocate a clk consumer, but leave it unlinked to the clk_core
 * @core: clk to allocate a consumer for
 * @dev_id: string describing device name
 * @con_id: connection ID string on device
 *
 * Returns: clk consumer left unlinked from the consumer list
 */
static struct clk *alloc_clk(struct clk_core *core, const char *dev_id,
                             const char *con_id)
{
        struct clk *clk;

        clk = kzalloc(sizeof(*clk), GFP_KERNEL);
        if (!clk)
                return ERR_PTR(-ENOMEM);

        clk->core = core;
        clk->dev_id = dev_id;
        clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
        clk->max_rate = ULONG_MAX;

        return clk;
}

클럭을 할당하고 구성한 후 반환한다.

 

__clk_core_init()

drivers/clk/clk.c -1/3-

/**
 * __clk_core_init - initialize the data structures in a struct clk_core
 * @core:       clk_core being initialized
 *
 * Initializes the lists in struct clk_core, queries the hardware for the
 * parent and rate and sets them both.
 */
static int __clk_core_init(struct clk_core *core)
{
        int ret;
        struct clk_core *orphan;
        struct hlist_node *tmp2;
        unsigned long rate;

        if (!core)
                return -EINVAL;

        clk_prepare_lock();

        ret = clk_pm_runtime_get(core);
        if (ret)
                goto unlock;

        /* check to see if a clock with this name is already registered */
        if (clk_core_lookup(core->name)) {
                pr_debug("%s: clk %s already initialized\n",
                                __func__, core->name);
                ret = -EEXIST;
                goto out;
        }

        /* check that clk_ops are sane.  See Documentation/driver-api/clk.rst */
        if (core->ops->set_rate &&
            !((core->ops->round_rate || core->ops->determine_rate) &&
              core->ops->recalc_rate)) {
                pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc__
rate\n",
                       __func__, core->name);
                ret = -EINVAL;
                goto out;
        }

        if (core->ops->set_parent && !core->ops->get_parent) {
                pr_err("%s: %s must implement .get_parent & .set_parent\n",
                       __func__, core->name);
                ret = -EINVAL;
                goto out;
        }

        if (core->num_parents > 1 && !core->ops->get_parent) {
                pr_err("%s: %s must implement .get_parent as it has multi parents\n",
                       __func__, core->name);
                ret = -EINVAL;
                goto out;
        }

        if (core->ops->set_rate_and_parent &&
                        !(core->ops->set_parent && core->ops->set_rate)) {
                pr_err("%s: %s must implement .set_parent & .set_rate\n",
                                __func__, core->name);
                ret = -EINVAL;
                goto out;
        }

        core->parent = __clk_init_parent(core);

클럭을 초기화한다. 이 때 부모 클럭 관계와 rate, accuracy 등을 설정한다.

  • 코드 라인 18~23에서 이름으로 클럭을 검색하여 이미 존재하는 경우 -EEXIST 에러를 반환한다.
  • 코드 라인 26~34에서 rate가 고정된 fixed rate 타입을 제외하고 나머지 rate를 설정할 수 있는 클럭들의 경우 (*set_rate) 후크 함수가 주어진다. 이 때 (*recalc_rate)가 준비되어야 하고, (*round_rate) 또는 (*determine_rate) 둘 중 하나의 후크 함수가 필요하므로 이를 체크한다.
    • fixed-factor, divider, multiplier, fractional-divider 클럭 타입등이 해당된다.
  • 코드 라인 36~41에서 mux 클럭의 경우 (*set_parent) 후크가 주어진다. 이 때 (*get_parent)도 필요하므로 체크한다.
  • 코드 라인 43~48에서 2개 이상의 부모를 가진 mux 클럭의 경우 (*get_parent) 후크 함수가 필요한다.
  • 코드 라인 50~56에서 부모의 클럭 rate를 변경할 수 있는 (*set_rate_and_parent) 후크 함수를 가진 클럭인 경우 (*set_parent)와 (*set_rate) 후크 함수를 필요로 한다.
    • 이 기능은 common 클럭 타입에는 없고, 일부 custom 클럭에서 제공되고 있다.
  • 코드 라인 58에서 최초 연결될 부모 클럭맵을 구성하고 부모 클럭을 알아온다.

 

drivers/clk/clk.c -2/3-

.       /*
         * Populate core->parent if parent has already been clk_core_init'd. If
         * parent has not yet been clk_core_init'd then place clk in the orphan
         * list.  If clk doesn't have any parents then place it in the root
         * clk list.
         *
         * Every time a new clk is clk_init'd then we walk the list of orphan
         * clocks and re-parent any that are children of the clock currently
         * being clk_init'd.
         */
        if (core->parent) {
                hlist_add_head(&core->child_node,
                                &core->parent->children);
                core->orphan = core->parent->orphan;
        } else if (!core->num_parents) {
                hlist_add_head(&core->child_node, &clk_root_list);
                core->orphan = false;
        } else {
                hlist_add_head(&core->child_node, &clk_orphan_list);
                core->orphan = true;
        }

        /*
         * optional platform-specific magic
         *
         * The .init callback is not used by any of the basic clock types, but
         * exists for weird hardware that must perform initialization magic.
         * Please consider other ways of solving initialization problems before
         * using this callback, as its use is discouraged.
         */
        if (core->ops->init)
                core->ops->init(core->hw);

        /*
         * Set clk's accuracy.  The preferred method is to use
         * .recalc_accuracy. For simple clocks and lazy developers the default
         * fallback is to use the parent's accuracy.  If a clock doesn't have a
         * parent (or is orphaned) then accuracy is set to zero (perfect
         * clock).
         */
        if (core->ops->recalc_accuracy)
                core->accuracy = core->ops->recalc_accuracy(core->hw,
                                        __clk_get_accuracy(core->parent));
        else if (core->parent)
                core->accuracy = core->parent->accuracy;
        else
                core->accuracy = 0;

        /*
         * Set clk's phase.
         * Since a phase is by definition relative to its parent, just
         * query the current clock phase, or just assume it's in phase.
         */
        if (core->ops->get_phase)
                core->phase = core->ops->get_phase(core->hw);
        else
                core->phase = 0;

        /*
         * Set clk's duty cycle.
         */
        clk_core_update_duty_cycle_nolock(core);

        /*
         * Set clk's rate.  The preferred method is to use .recalc_rate.  For
         * simple clocks and lazy developers the default fallback is to use the
         * parent's rate.  If a clock doesn't have a parent (or is orphaned)
         * then rate is set to zero.
         */
        if (core->ops->recalc_rate)
                rate = core->ops->recalc_rate(core->hw,
                                clk_core_get_rate_nolock(core->parent));
        else if (core->parent)
                rate = core->parent->rate;
        else
                rate = 0;
        core->rate = core->req_rate = rate;
  • 코드 라인 11~21에서 클럭의 하이라키를 구성한다.
    • 부모 클럭이 지정된 경우 부모 클럭의 children 리스트에 등록한다.
    • 루트 클럭인 경우 clk_root_list에 등록한다.
    • 고아(orphan) 클럭인 경우 clk_orphan_list에 등록한다.
      • clk_root_list에 root 클럭만 추가되듯이,
      • clk_orphan_list에는 고아 클럭들 중 상위 부모만 추가된다.
        • 예) (A 고아) — B  –  C
          • A만 clk_orphan_list에 추가된다.
      • 모든 clock core들은 반드시 다음 3가지 리스트 중 하나에 연결된다.
        • clk_root_list
        • clk_orphan_list
        • 부모의 children 리스트
  • 코드 라인 31~32에서 클럭 초기화를 위해 아키텍처가 지원하는 별도의 (*init) 후크 함수를 수행한다.
    • common 클럭 타입에는 없고, 일부 custom 클럭에서 제공되고 있다.
  • 코드 라인 41~47에서 클럭의 정확도(accuracy)를 설정한다.
    • fixed rate 타입의 클럭인 경우 (*recalc_accuracy) 후크 함수를 수행하여 클럭의 정확도(accuracy)를 설정한다.
    • 그렇지 않은 경우 부모 클럭이 있으면 부모 클럭의 accuracy 값을 사용하고, 없으면 0을 설정한다.
  • 코드 라인 54~57에서 custom 클럭들 중 클럭의 위상(phase)을 지정하는 클럭들이 있다. 이러한 클럭의 위상(phase)을 설정한다.
    • (*get_phase) 후크 함수를 수행하여 phase 값을 알아오고, 해당 후크 함수가 없으면 0으로 설정한다.
  • 코드 라인 62에서 클럭의 duty cycle을 지정한다.
    • duty 사이클은 on에 해당하는 시간 / 1 주기 시간에 대한 백분율이다.
    • 참고: Duty Cycle | Wikipedia
  • 코드 라인 70~77에서 클럭의 rate를 설정한다.
    • rate 타입의 클럭인 경우 (*recalc_rate) 후크 함수를 지원하므로 이를 통해 rate 값을 구해온다.
    • rate 타입의 클럭이 아닌 경우 부모의 rate를 사용하거나 부모가 없으면 0으로 설정한다.

 

drivers/clk/clk.c -3/3-

        /*
         * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
         * don't get accidentally disabled when walking the orphan tree and
         * reparenting clocks
         */
        if (core->flags & CLK_IS_CRITICAL) {
                unsigned long flags;

                clk_core_prepare(core);

                flags = clk_enable_lock();
                clk_core_enable(core);
                clk_enable_unlock(flags);
        }

        /*
         * walk the list of orphan clocks and reparent any that newly finds a
         * parent.
         */
        hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
                struct clk_core *parent = __clk_init_parent(orphan);

                /*
                 * We need to use __clk_set_parent_before() and _after() to
                 * to properly migrate any prepare/enable count of the orphan
                 * clock. This is important for CLK_IS_CRITICAL clocks, which
                 * are enabled during init but might not have a parent yet.
                 */
                if (parent) {
                        /* update the clk tree topology */
                        __clk_set_parent_before(orphan, parent);
                        __clk_set_parent_after(orphan, parent, NULL);
                        __clk_recalc_accuracies(orphan);
                        __clk_recalc_rates(orphan, 0);
                }
        }

        kref_init(&core->ref);
out:
        clk_pm_runtime_put(core);
unlock:
        clk_prepare_unlock();

        if (!ret)
                clk_debug_register(core);

        return ret;
}
  • 코드 라인 6~14에서 CLK_IS_CRITICAL 플래그를 사용한 경우 일시적으로 고아(orphan) 클럭이었다가 부모 클럭이 활성화되지 않은 상태에 놓여있어도 클럭이 항상 동작하게 한다.
  • 코드 라인 20~36에서 고아 클럭 리스트인 clk_orphan_list에 등록된 클럭들을 순회하며 부모 클럭이 발견되면 이에 대한 accuracy 및 rate 설정을 다시 한다.

 

다음 그림은 clock 루트 리스트에 포함되는 클럭과 clock 고아 리스트에 포함되는 클럭의 속성 차이를 보여준다.

 

__clk_init_parent()

drivers/clk/clk.c

static struct clk_core *__clk_init_parent(struct clk_core *core)
{
        u8 index = 0;

        if (core->num_parents > 1 && core->ops->get_parent)
                index = core->ops->get_parent(core->hw);

        return clk_core_get_parent_by_index(core, index);
}

부모 클럭을 알아와서 연결한다.

 


클럭 Provider

다음과 같이 클럭 provider를 추가하는 두 개의 API가 지원된다.

 

클럭 provider 추가 -1-

of_clk_add_provider()

drivers/clk/clk.c

/**
 * of_clk_add_provider() - Register a clock provider for a node
 * @np: Device node pointer associated with clock provider
 * @clk_src_get: callback for decoding clock
 * @data: context pointer for @clk_src_get callback.
 *
 * This function is *deprecated*. Use of_clk_add_hw_provider() instead.
 */
int of_clk_add_provider(struct device_node *np,
                        struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
                                                   void *data),
                        void *data)
{
        struct of_clk_provider *cp;
        int ret;

        cp = kzalloc(sizeof(*cp), GFP_KERNEL);
        if (!cp)
                return -ENOMEM;

        cp->node = of_node_get(np);
        cp->data = data;
        cp->get = clk_src_get;

        mutex_lock(&of_clk_mutex);
        list_add(&cp->link, &of_clk_providers);
        mutex_unlock(&of_clk_mutex);
        pr_debug("Added clock from %pOF\n", np);

        ret = of_clk_set_defaults(np, true);
        if (ret < 0)
                of_clk_del_provider(np);

        return ret;
}
EXPORT_SYMBOL_GPL(of_clk_add_provider);

디바이스 노드의 클럭 provder를 추가한다. 추가 시 clk를 반환하는 함수를 지정한다. 성공하면 0을 반환한다.

  • 코드 라인 9~15에서 of_clk_provider 구조체를 할당받아 구성한다.
  • 코드 라인 17~20에서 of_clk_providers에 추가하고 디버그 메시지를 출력한다.
  • 코드 라인 22~24에서 default 부모 클럭과 rate를 설정한다. 만일 실패하는 경우 provider 리스트에서 제거한다.
  • 코드 라인 26에서 클럭 설정 결과를 반환한다. 성공 시 0을 반환한다.

 

클럭 공급자 추가 -2-

of_clk_add_hw_provider()

drivers/clk/clk.c

/**
 * of_clk_add_hw_provider() - Register a clock provider for a node
 * @np: Device node pointer associated with clock provider
 * @get: callback for decoding clk_hw
 * @data: context pointer for @get callback.
 */
int of_clk_add_hw_provider(struct device_node *np,
                           struct clk_hw *(*get)(struct of_phandle_args *clkspec,
                                                 void *data),
                           void *data)
{
        struct of_clk_provider *cp;
        int ret;

        cp = kzalloc(sizeof(*cp), GFP_KERNEL);
        if (!cp)
                return -ENOMEM;

        cp->node = of_node_get(np);
        cp->data = data;
        cp->get_hw = get;

        mutex_lock(&of_clk_mutex);
        list_add(&cp->link, &of_clk_providers);
        mutex_unlock(&of_clk_mutex);
        pr_debug("Added clk_hw provider from %pOF\n", np);

        ret = of_clk_set_defaults(np, true);
        if (ret < 0)
                of_clk_del_provider(np);

        return ret;
}
EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);

디바이스 노드의 클럭 provider를 추가한다. 추가 시 clk_hw를 반환하는 함수를 지정한다. 성공하면 0을 반환한다.

  • 코드 라인 9~15에서 of_clk_provider 구조체를 할당받아 구성한다.
  • 코드 라인 17~20에서 of_clk_providers에 추가하고 디버그 메시지를 출력한다.
  • 코드 라인 22~24에서 default 부모 클럭과 rate를 설정한다. 만일 실패하는 경우 provider 리스트에서 제거한다.
  • 코드 라인 26에서 클럭 설정 결과를 반환한다. 성공 시 0을 반환한다.

 

Provider에 등록된 클럭 사용

다음 그림은 provider에 등록된 클럭을 사용하는 함수 이후의 호출 과정을 보여준다.

 

of_clk_get_from_provider()

drivers/clk/clk.c

/**
 * of_clk_get_from_provider() - Lookup a clock from a clock provider
 * @clkspec: pointer to a clock specifier data structure
 *
 * This function looks up a struct clk from the registered list of clock
 * providers, an input is a clock specifier data structure as returned
 * from the of_parse_phandle_with_args() function call.
 */
struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
{
        struct clk_hw *hw = of_clk_get_hw_from_clkspec(clkspec);

        return clk_hw_create_clk(NULL, hw, NULL, __func__);
}
EXPORT_SYMBOL_GPL(of_clk_get_from_provider);

클럭 providers 리스트에서 of_phandle_args 값으로 클럭을 검색하여 반환한다.

 

of_clk_get_hw_from_clkspec()

drivers/clk/clk.c

static struct clk_hw *
of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec)
{
        struct of_clk_provider *provider;
        struct clk_hw *hw = ERR_PTR(-EPROBE_DEFER);

        if (!clkspec)
                return ERR_PTR(-EINVAL);

        mutex_lock(&of_clk_mutex);
        list_for_each_entry(provider, &of_clk_providers, link) {
                if (provider->node == clkspec->np) {
                        hw = __of_clk_get_hw_from_provider(provider, clkspec);
                        if (!IS_ERR(hw))
                                break;
                }
        }
        mutex_unlock(&of_clk_mutex);

        return hw;
}

of_clk_providers 리스트에서 루프를 돌며 요청 노드를 찾은 후 등록된 클럭 hw를 찾아 반환한다.

 

__of_clk_get_hw_from_provider()

drivers/clk/clk.c

static struct clk_hw *
__of_clk_get_hw_from_provider(struct of_clk_provider *provider,
                              struct of_phandle_args *clkspec)
{
        struct clk *clk;

        if (provider->get_hw)
                return provider->get_hw(clkspec, provider->data);

        clk = provider->get(clkspec, provider->data);
        if (IS_ERR(clk))
                return ERR_CAST(clk);
        return __clk_get_hw(clk);
}

클럭 hw를 반환한다.

  • 코드 라인 6~7에서 (*get_hw) 후크를 호출하여 클럭 hw를 찾아 반환한다.
  • 코드 라인 9~12에서 위의 후크 함수가 없으면 호환을 위해 사용되는 (*get) 후크 함수를 호출하여 클럭을 찾고, 그 클럭의 클럭 hw를 반환한다.

 

다음 그림은 uart 디바이스에 사용하는 클럭을 알아오는 모습을 보여준다.

 

__of_clk_get_from_provider()

drivers/clk/clk.c

struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
                                       const char *dev_id, const char *con_id)
{
        struct of_clk_provider *provider;
        struct clk *clk = ERR_PTR(-EPROBE_DEFER);

        /* Check if we have such a provider in our array */
        list_for_each_entry(provider, &of_clk_providers, link) {
                if (provider->node == clkspec->np) 
                        clk = provider->get(clkspec, provider->data);
                if (!IS_ERR(clk)) {
                        clk = __clk_create_clk(__clk_get_hw(clk), dev_id,
                                               con_id);
 
                        if (!IS_ERR(clk) && !__clk_get(clk)) {
                                __clk_free_clk(clk);
                                clk = ERR_PTR(-ENOENT);
                        }

                        break;
                }
        }

        return clk;       
}

of_clk_providers 리스트에서 루프를 돌며 요청 device_node를 찾은 후 등록된 get 후크 함수를 호출하여 clk을 찾게되면 clk를 새로 할당받아 구성한 후 반환한다.

  • 코드 라인 8~10에서 of_clk_providers 리스트를 루프를 돌며 등록된 of_clk_provider 엔트리와 요청한 노드가 동일한 경우
  • 코드 라인 11~21에서 clk를 새로 할당하고 알아온 clk->core->hw 정보와 인수로 받은 dev_id, con_id 등으로 구성하여 반환한다.

 


클럭 consumer 디바이스

클럭 consumer 할당 및 클럭과 연결

clk_hw_create_clk()

drivers/clk/clk.c

/**
 * clk_hw_create_clk: Allocate and link a clk consumer to a clk_core given
 * a clk_hw
 * @dev: clk consumer device
 * @hw: clk_hw associated with the clk being consumed
 * @dev_id: string describing device name
 * @con_id: connection ID string on device
 *
 * This is the main function used to create a clk pointer for use by clk
 * consumers. It connects a consumer to the clk_core and clk_hw structures
 * used by the framework and clk provider respectively.
 */
struct clk *clk_hw_create_clk(struct device *dev, struct clk_hw *hw,
                              const char *dev_id, const char *con_id)
{
        struct clk *clk;
        struct clk_core *core;

        /* This is to allow this function to be chained to others */
        if (IS_ERR_OR_NULL(hw))
                return ERR_CAST(hw);

        core = hw->core;
        clk = alloc_clk(core, dev_id, con_id);
        if (IS_ERR(clk))
                return clk;
        clk->dev = dev;

        if (!try_module_get(core->owner)) {
                free_clk(clk);
                return ERR_PTR(-ENOENT);
        }

        kref_get(&core->ref);
        clk_core_link_consumer(core, clk);

        return clk;
}

클럭 consumer를 할당하고 클럭 core에 연결하고, 클럭을 반환한다.

  • 코드 라인 11~15에서 clk 구조체를 할당하고 디바이스 정보로 초기화한다.
  • 코드 라인 17~20에서 클럭에 대한 모듈이 없으면 -ENOENT 에러를 반환한다.
  • 코드 라인 22에서 클럭 참조 카운터를 증가시키고, 클럭 consumer와 클럭 core를 연결한다.
  • 코드 라인 24에서 클럭을 반환한다.

 

클럭 사용

다음 그림은 of_clk_get() 함수 이후의 호출 과정을 보여준다.

 

of_clk_get()

drivers/clk/clk.c

struct clk *of_clk_get(struct device_node *np, int index)
{
        return __of_clk_get(np, index, np->full_name, NULL);
}
EXPORT_SYMBOL(of_clk_get);

지정한 디바이스 노드에서 부모 @index에 해당하는 클럭을 알아온다.

  • np가 클럭 소스인 경우
    • “clocks=” 속성이 주어진 경우 인덱스가 지정하는 부모 클럭을 알아온다.
  • np가 클럭을 사용할 디바이스인 경우
    • “clocks=” 속성이 주어진 경우 인덱스가 지정하는 부모 클럭을 알아온다. 없는 경우 에러

 

다음 그림은 부모 인덱스 값으로 부모 클럭을 알아오는 모습을 보여준다.

 

__of_clk_get()

drivers/clk/clk.c

static struct clk *__of_clk_get(struct device_node *np,
                                int index, const char *dev_id,
                                const char *con_id)
{
        struct clk_hw *hw = of_clk_get_hw(np, index, con_id);

        return clk_hw_create_clk(NULL, hw, dev_id, con_id);
}

디바이스 노드에서 부모 index 또는 @con_id(이름)로 부모 클럭을 알아온다.

  • 코드 라인 5에서 @index로 지정한 부모 클럭 hw를 알아온다.
  • 코드 라인 7에서 clk 구조체를 생성하고, 클럭 코어에 연동한다. 이 때 클럭 코어의 참조 카운터가 1 증가된다.

 

of_clk_get_hw()

drivers/clk/clk.c

struct clk_hw *of_clk_get_hw(struct device_node *np, int index,
                             const char *con_id)
{
        int ret;
        struct clk_hw *hw;
        struct of_phandle_args clkspec;

        ret = of_parse_clkspec(np, index, con_id, &clkspec);
        if (ret)
                return ERR_PTR(ret);

        hw = of_clk_get_hw_from_clkspec(&clkspec);
        of_node_put(clkspec.np);

        return hw;
}

디바이스 노드에서 @con_id(이름) 또는 부모 @index로 부모 클럭 hw를 알아온다.

  • 코드 라인 8~10에서 디바이스 노드에서 index에 해당하는 클럭 argument 값들을 알아온다.
  • 코드 라인 12~15에서 클럭 argument들을 분석하여 해당 클럭 hw를 알아와서 반환한다.

 

of_parse_clkspec()

drivers/clk/clk.c

/**
 * of_parse_clkspec() - Parse a DT clock specifier for a given device node
 * @np: device node to parse clock specifier from
 * @index: index of phandle to parse clock out of. If index < 0, @name is used
 * @name: clock name to find and parse. If name is NULL, the index is used
 * @out_args: Result of parsing the clock specifier
 *
 * Parses a device node's "clocks" and "clock-names" properties to find the
 * phandle and cells for the index or name that is desired. The resulting clock
 * specifier is placed into @out_args, or an errno is returned when there's a
 * parsing error. The @index argument is ignored if @name is non-NULL.
 *
 * Example:
 *
 * phandle1: clock-controller@1 {
 *      #clock-cells = <2>;
 * }
 *
 * phandle2: clock-controller@2 {
 *      #clock-cells = <1>;
 * }
 *
 * clock-consumer@3 {
 *      clocks = <&phandle1 1 2 &phandle2 3>;
 *      clock-names = "name1", "name2";
 * }
 *
 * To get a device_node for `clock-controller@2' node you may call this
 * function a few different ways:
 *
 *   of_parse_clkspec(clock-consumer@3, -1, "name2", &args);
 *   of_parse_clkspec(clock-consumer@3, 1, NULL, &args);
 *   of_parse_clkspec(clock-consumer@3, 1, "name2", &args);
 *
 * Return: 0 upon successfully parsing the clock specifier. Otherwise, -ENOENT
 * if @name is NULL or -EINVAL if @name is non-NULL and it can't be found in
 * the "clock-names" property of @np.
 */
static int of_parse_clkspec(const struct device_node *np, int index,
                            const char *name, struct of_phandle_args *out_args)
{
        int ret = -ENOENT;

        /* Walk up the tree of devices looking for a clock property that matches */
        while (np) {
                /*
                 * For named clocks, first look up the name in the
                 * "clock-names" property.  If it cannot be found, then index
                 * will be an error code and of_parse_phandle_with_args() will
                 * return -EINVAL.
                 */
                if (name)
                        index = of_property_match_string(np, "clock-names", name);
                ret = of_parse_phandle_with_args(np, "clocks", "#clock-cells",
                                                 index, out_args);
                if (!ret)
                        break;
                if (name && index >= 0)
                        break;

                /*
                 * No matching clock found on this node.  If the parent node
                 * has a "clock-ranges" property, then we can try one of its
                 * clocks.
                 */
                np = np->parent;
                if (np && !of_get_property(np, "clock-ranges", NULL))
                        break;
                index = 0;
        }

        return ret;
}

디바이스 노드에서 @name 또는 @index에 해당하는 phandle 및 argument 값들을 알아와서 출력 인자 @out_args에 대입해온다.

  • 코드 라인 7에서 요청한 디바이스 노드부터 시작하여 최상위 노드까지 검색을 수행한다.
  • 코드 라인 14~15에서 @name이 주어진 경우 해당 인덱스를 찾는다.
  • 코드 라인 16~21에서 인덱스에 해당하는 phandle 및 argument 값들을 알아와서 출력 인자 @out_args에 대입해온다. 만일 찾은 경우 성공을 반환하기 위해 루프를 벗어난다.
  • 코드 라인 28~32에서 못찾은 경우 부모 노드로 이동하고 재시도한다. 단 이동한 후 “clock-ranges” 속성을 발견하면 에러를 반환하기 위해 루프를 벗어난다.

 


clk 관련 기타 of_api 들

of_clk_set_defaults()

drivers/clk/clk-conf.c

/**
 * of_clk_set_defaults() - parse and set assigned clocks configuration
 * @node: device node to apply clock settings for
 * @clk_supplier: true if clocks supplied by @node should also be considered
 *
 * This function parses 'assigned-{clocks/clock-parents/clock-rates}' properties
 * and sets any specified clock parents and rates. The @clk_supplier argument
 * should be set to true if @node may be also a clock supplier of any clock
 * listed in its 'assigned-clocks' or 'assigned-clock-parents' properties.
 * If @clk_supplier is false the function exits returnning 0 as soon as it
 * determines the @node is also a supplier of any of the clocks.
 */
int of_clk_set_defaults(struct device_node *node, bool clk_supplier)
{
        int rc;

        if (!node)
                return 0;

        rc = __set_clk_parents(node, clk_supplier);
        if (rc < 0)
                return rc;

        return __set_clk_rates(node, clk_supplier);
}
EXPORT_SYMBOL_GPL(of_clk_set_defaults);

요청한 클럭 디바이스의 부모 클럭을 설정하고 rate를 설정한다.

 

부모 클럭 선택

__set_clk_parents()

drivers/clk/clk-conf.c

static int __set_clk_parents(struct device_node *node, bool clk_supplier)
{
        struct of_phandle_args clkspec;
        int index, rc, num_parents;
        struct clk *clk, *pclk;

        num_parents = of_count_phandle_with_args(node, "assigned-clock-parents",
                                                 "#clock-cells");
        if (num_parents == -EINVAL)
                pr_err("clk: invalid value of clock-parents property at %pOF\n",
                       node);

        for (index = 0; index < num_parents; index++) {
                rc = of_parse_phandle_with_args(node, "assigned-clock-parents",
                                        "#clock-cells", index, &clkspec);
                if (rc < 0) {
                        /* skip empty (null) phandles */
                        if (rc == -ENOENT)
                                continue;
                        else
                                return rc;
                }
                if (clkspec.np == node && !clk_supplier)
                        return 0;
                pclk = of_clk_get_from_provider(&clkspec);
                if (IS_ERR(pclk)) {
                        if (PTR_ERR(pclk) != -EPROBE_DEFER)
                                pr_warn("clk: couldn't get parent clock %d for %pOF\n",
                                        index, node);
                        return PTR_ERR(pclk);
                }

                rc = of_parse_phandle_with_args(node, "assigned-clocks",
                                        "#clock-cells", index, &clkspec);
                if (rc < 0)
                        goto err;
                if (clkspec.np == node && !clk_supplier) {
                        rc = 0;
                        goto err;
                }
                clk = of_clk_get_from_provider(&clkspec);
                if (IS_ERR(clk)) {
                        if (PTR_ERR(clk) != -EPROBE_DEFER)
                                pr_warn("clk: couldn't get assigned clock %d for %pOF\n",
                                        index, node);
                        rc = PTR_ERR(clk);
                        goto err;
                }

                rc = clk_set_parent(clk, pclk);
                if (rc < 0)
                        pr_err("clk: failed to reparent %s to %s: %d\n",
                               __clk_get_name(clk), __clk_get_name(pclk), rc);
                clk_put(clk);
                clk_put(pclk);
        }
        return 0;
err:
        clk_put(pclk);
        return rc;
}

부모 클럭을 선택한다.

  • 코드 라인 7~11에서 “#clock-cells” 속성값을 사용하여 “assigned-clock-parents” 속성에서 phandle의 수를 알아온다.  읽어올 수 없는 경우 에러 메시지를 출력한다.
    • 예) #clock-cells = <1>; assigned-clocks = <&cru PLL_GPLL>, <&cru PLL_CPLL>; -> 2 개
  • 코드 라인 13~15에서 부모 수만큼 루프를 돌며 “assigned-clock-parents” 속성에서 요청한 index의 phandle 값을 알아온다.
  • 코드 라인 16~22에서 알아온 phandle 값이 null이면 skip하고 에러인 경우 함수를 빠져나간다.
  • 코드 라인 23~24에서 clk_supplier 값이 0 이면서 알아온 부모 노드가 요청 노드와 동일한 경우 성공(0)리에 함수를 빠져나간다.
  • 코드 라인 25~31에서 clkspec으로 클럭을 알아온다.
  • 코드 라인 33~36에서 “assigned-clocks” 속성에서 부모 index로 clkspec 값을 알아온다.
  • 코드 라인 37~40에서 clk_supplier 값이 0 이면서 알아온 부모 노드가 요청 노드와 동일한 경우 성공(0)리에 함수를 빠져나간다.
  • 코드 라인 41~48에서 clkspec 값으로 클럭을 알아온다. 검색이 실패하는 경우 경고 메시지를 출력하고 에러를 반환한다.
  • 코드 라인 50~53에서 부모 클럭으로 pclk를 지정한다.

 

Rate 설정

__set_clk_rates()

drivers/clk/clk-conf.c

static int __set_clk_rates(struct device_node *node, bool clk_supplier)
{
        struct of_phandle_args clkspec;
        struct property *prop;
        const __be32 *cur;
        int rc, index = 0;
        struct clk *clk;
        u32 rate;

        of_property_for_each_u32(node, "assigned-clock-rates", prop, cur, rate) {
                if (rate) {
                        rc = of_parse_phandle_with_args(node, "assigned-clocks",
                                        "#clock-cells", index, &clkspec);
                        if (rc < 0) {
                                /* skip empty (null) phandles */
                                if (rc == -ENOENT)
                                        continue;
                                else
                                        return rc;
                        }
                        if (clkspec.np == node && !clk_supplier)
                                return 0;

                        clk = of_clk_get_from_provider(&clkspec);
                        if (IS_ERR(clk)) {
                                if (PTR_ERR(clk) != -EPROBE_DEFER)
                                        pr_warn("clk: couldn't get clock %d for %pOF\n",
                                                index, node);
                                return PTR_ERR(clk);
                        }

                        rc = clk_set_rate(clk, rate);
                        if (rc < 0)
                                pr_err("clk: couldn't set %s clk rate to %u (%d), current rate: %lu\\
n",
                                       __clk_get_name(clk), rate, rc,
                                       clk_get_rate(clk));
                        clk_put(clk);
                }
                index++;
        }
        return 0;
}

요청한 클럭 디바이스의 rate를 설정한다.

  • 코드 라인 10에서 요청한 노드 이하에서 “assigned-clock-rates” 속성 값들을 대상으로 루프를 돈다.
  • 코드 라인 11~13에서 rate 값이 0보다 큰 경우 “assigned-clocks” 속성에서 읽은 index 번호의 부모 클럭 노드의 “#clock-cells” 값 길이 만큼의 phandle 뒤의 argument를 읽어들여 clkspec에 대입한다.
  • 코드 라인 14~20에서 알아온 phandle 값이 null이면 skip하고 에러인 경우 함수를 빠져나간다.
  • 코드 라인 21~22에서 clk_supplier 값이 0 이면서 알아온 부모 노드가 요청 노드와 동일한 경우 성공(0)리에 함수를 빠져나간다.
  • 코드 라인 24~30에서 clkspec 값으로 클럭을 알아온다. 검색이 실패하는 경우 경고 메시지를 출력하고 에러를 반환한다.
  • 코드 라인 32~37에서 클럭의 rate를 설정한다.

 

다음 스크립트를 보면 pwm 노드에서 사용할 클럭 소스로 clocks BCM2835_CLOCK_PWN을 지정하였고 이 클럭을 10Mhz로 설정하는 것을 알 수 있다.

arch/arm/boot/dts/bcm283x.dtsi – raspberrypi 커널 v4.9.y

                clocks: cprman@7e101000 {
                        compatible = "brcm,bcm2835-cprman";
                        #clock-cells = <1>;
                        reg = <0x7e101000 0x2000>;
                        clocks = <&clk_osc>,
                                <&dsi0 0>, <&dsi0 1>, <&dsi0 2>,
                                <&dsi1 0>, <&dsi1 1>, <&dsi1 2>;
                };

                pwm: pwm@7e20c000 {
                        compatible = "brcm,bcm2835-pwm";
                        reg = <0x7e20c000 0x28>;
                        clocks = <&clocks BCM2835_CLOCK_PWM>;
                        assigned-clocks = <&clocks BCM2835_CLOCK_PWM>;
                        assigned-clock-rates = <10000000>;
                        #pwm-cells = <2>;
                        status = "disabled";
                };

 


클럭 등록 시 사용하는 플래그

다음의 플래그들은 최상위 framework인 common clock framework에서 유효하다.

include/linux/clk-provider.h

/*
 * flags used across common struct clk.  these flags should only affect the
 * top-level framework.  custom flags for dealing with hardware specifics
 * belong in struct clk_foo
 *
 * Please update clk_flags[] in drivers/clk/clk.c when making changes here!
 */
#define CLK_SET_RATE_GATE       BIT(0) /* must be gated across rate change */
#define CLK_SET_PARENT_GATE     BIT(1) /* must be gated across re-parent */
#define CLK_SET_RATE_PARENT     BIT(2) /* propagate rate change up one level */
#define CLK_IGNORE_UNUSED       BIT(3) /* do not gate even if unused */
                                /* unused */
                                /* unused */
#define CLK_GET_RATE_NOCACHE    BIT(6) /* do not use the cached clk rate */
#define CLK_SET_RATE_NO_REPARENT BIT(7) /* don't re-parent on rate change */
#define CLK_GET_ACCURACY_NOCACHE BIT(8) /* do not use the cached clk accuracy */
#define CLK_RECALC_NEW_RATES    BIT(9) /* recalc rates after notifications */
#define CLK_SET_RATE_UNGATE     BIT(10) /* clock needs to run to set rate */
#define CLK_IS_CRITICAL         BIT(11) /* do not gate, ever */
/* parents need enable during gate/ungate, set rate and re-parent */
#define CLK_OPS_PARENT_ENABLE   BIT(12)
/* duty cycle call may be forwarded to the parent clock */
#define CLK_DUTY_CYCLE_PARENT   BIT(13)
  • CLK_SET_RATE_GATE
    • rate 변경 시 반드시 gate가 닫혀 있어야 한다.
  • CLK_SET_PARENT_GATE
    • 입력 클럭 소스(부모 클럭)를 선택 시 반드시 gate가 닫혀 있어야 한다.
  • CLK_SET_RATE_PARENT
    • 현재 클럭 hw가 지원하는 rate 변경이 불가능한 경우 부모에 전파(propogation) 하여 부모 클럭부터 rate를 변경하게 한다.
  • CLK_IGNORE_UNUSED
    • 사용하지 않아도 gate를 닫지 않는다.
  • CLK_GET_RATE_NOCACHE
  • CLK_SET_RATE_NO_REPARENT
  • CLK_GET_ACCURACY_NOCACHE
  • CLK_RECALC_NEW_RATES
  • CLK_SET_RATE_UNGATE
  • CLK_IS_CRITICAL
  • CLK_OPS_PARENT_ENABLE
    • 부모 클럭이 enable된 상태에서만 operation을 수행할 수 있는 클럭 hw를 지원한다.
    • CCF는 부모 클럭이 닫혀있으면 자동으로 잠시 prepare & enable하고 이 클럭의 operation을 수행한 후 다시 disable & unprepare를 수행한다.
    • 참고: clk: core: support clocks which requires parents enable (part 1) (2016, v4.8-rc1)
  • CLK_DUTY_CYCLE_PARENT
  • CLK_IS_ROOT (deleted)
  • CLK_IS_BASIC (deleted)
    • clk_foo()와 같은 파생 클럭이 아닌 클럭이다.
    • common clock framework에 구현되어 있는 8개의 클럭 디바이스 드라이버는 모두 CLK_IS_BASIC 플래그가 설정되어 있다.
    • 참고: clk: Remove CLK_IS_BASIC clk flag (2019, v5.2-rc1)

 


구조체

clk 구조체

drivers/clk/clk.c

struct clk {
        struct clk_core *core;
        struct device *dev;
        const char *dev_id;
        const char *con_id;
        unsigned long min_rate;
        unsigned long max_rate;
        unsigned int exclusive_count;
        struct hlist_node clks_node;
};
  • *core
    • 클럭 코어 포인터
  • *dev
    • 디바이스 포인터
  • *dev_id
    • 디바이스 명
  • *con_id
    • connection id 문자열
  • min_rate
    • 최소 rate
  • max_rate
    • 최대 rate
  • exclusive_count
    • 베타적 사용 카운터
  • clks_node
    • 클럭 코어의 clks 리스트에 연결될 때 사용되는 노드

 

clk_core 구조체

drivers/clk/clk.c

struct clk_core {
        const char              *name;
        const struct clk_ops    *ops;
        struct clk_hw           *hw;
        struct module           *owner;
        struct device           *dev;
        struct device_node      *of_node;
        struct clk_core         *parent;
        struct clk_parent_map   *parents;
        u8                      num_parents;
        u8                      new_parent_index;
        unsigned long           rate;
        unsigned long           req_rate;
        unsigned long           new_rate;
        struct clk_core         *new_parent;
        struct clk_core         *new_child;
        unsigned long           flags;
        bool                    orphan;
        bool                    rpm_enabled;
        unsigned int            enable_count;
        unsigned int            prepare_count;
        unsigned int            protect_count;
        unsigned long           min_rate;
        unsigned long           max_rate;
        unsigned long           accuracy;
        int                     phase;
        struct clk_duty         duty;
        struct hlist_head       children;
        struct hlist_node       child_node;
        struct hlist_head       clks;
        unsigned int            notifier_count;
#ifdef CONFIG_DEBUG_FS
        struct dentry           *dentry;
        struct hlist_node       debug_node;
#endif
        struct kref             ref;
};
  • *name
    • 클럭명
  • *ops
    • 클럭 opearation 포인터
  • *hw
    • 클럭 hw 포인터
  • *owner
    • 모듈 포인터
  • *dev
    • 디바이스 포인터
  • of_node
    • 디바이스 노드 포인터
  • *parent
    • 부모 클럭 코어 포인터
  • *parents
    • 부모 클럭 맵 배열 포인터
  • num_parents
    • 부모 클럭 코어 수
    • 루트 클럭일 때 0이다.
  • new_parent_index
    • 새 부모 클럭 인덱스
  • rate
    • 현재 rate (클럭 hw가 지원하는 값)
  • req_rate
    • 요청 rate
  • new_rate
    • 변경될 새 rate
  • *new_parent
    • 변경될 새 부모 클럭 코어 포인터
  • *new_child
    • 변경될 새 child 클럭 코어 포인터
  • flags
    • 요청 플래그들
  • orphan
    • 고아 클럭 코어 여부
    • 참고로 부모가 고아 클럭 코어이면 연결된 자식 클럭 코어도 고아 상태이다.
  • rpm_enabled
    • 절전 기능 사용 여부
  • enable_count
    • clk_enable() 카운터 수
  • prepare_count
    • clk_prepare() 카운터 수
  • protect_count
    • protection 카운터 수
  • min_rate
    • 최소 rate
  • max_rate
    • 최대 rate
  • accuracy
    • 정확도
  • phase
    • 위상
  • duty
    • duty (pulse on 비율)
  • children
    • 자식 클럭 코어 리스트
    • clk_core->child_node들이 연결된다.
  • child_node
    • 부모 클럭 코어의 children 리스트에 연결할 때 사용되는 노드
  • clks
    • 클럭 Consumer 리스트
    • clk->clks_node들이 연결된다.
  • notifier_count
    • 통지 카운터
  • ref
    • 참조 카운터

 

clk_ops 구조체

drivers/clk/clk.c

/**
 * struct clk_ops -  Callback operations for hardware clocks; these are to
 * be provided by the clock implementation, and will be called by drivers
 * through the clk_* api.
 *
 * @prepare:    Prepare the clock for enabling. This must not return until
 *              the clock is fully prepared, and it's safe to call clk_enable.
 *              This callback is intended to allow clock implementations to
 *              do any initialisation that may sleep. Called with
 *              prepare_lock held.
 *
 * @unprepare:  Release the clock from its prepared state. This will typically
 *              undo any work done in the @prepare callback. Called with
 *              prepare_lock held.
 *
 * @is_prepared: Queries the hardware to determine if the clock is prepared.
 *              This function is allowed to sleep. Optional, if this op is not
 *              set then the prepare count will be used.
 *
 * @unprepare_unused: Unprepare the clock atomically.  Only called from
 *              clk_disable_unused for prepare clocks with special needs.
 *              Called with prepare mutex held. This function may sleep.
 *
 * @enable:     Enable the clock atomically. This must not return until the
 *              clock is generating a valid clock signal, usable by consumer
 *              devices. Called with enable_lock held. This function must not
 *              sleep.
 *
 * @disable:    Disable the clock atomically. Called with enable_lock held.
 *              This function must not sleep.
 *
 * @is_enabled: Queries the hardware to determine if the clock is enabled.
 *              This function must not sleep. Optional, if this op is not
 *              set then the enable count will be used.
 *
 * @disable_unused: Disable the clock atomically.  Only called from
 *              clk_disable_unused for gate clocks with special needs.
 *              Called with enable_lock held.  This function must not
 *              sleep.
 *
 * @save_context: Save the context of the clock in prepration for poweroff.
 *
 * @restore_context: Restore the context of the clock after a restoration
 *              of power.
 *
 * @recalc_rate Recalculate the rate of this clock, by querying hardware. The
 *              parent rate is an input parameter.  It is up to the caller to
 *              ensure that the prepare_mutex is held across this call.
 *              Returns the calculated rate.  Optional, but recommended - if
 *              this op is not set then clock rate will be initialized to 0.
 *
 * @round_rate: Given a target rate as input, returns the closest rate actually
 *              supported by the clock. The parent rate is an input/output
 *              parameter.
 *
 * @determine_rate: Given a target rate as input, returns the closest rate
 *              actually supported by the clock, and optionally the parent clock
 *              that should be used to provide the clock rate.
 *
 * @set_parent: Change the input source of this clock; for clocks with multiple
 *              possible parents specify a new parent by passing in the index
 *              as a u8 corresponding to the parent in either the .parent_names
 *              or .parents arrays.  This function in affect translates an
 *              array index into the value programmed into the hardware.
 *              Returns 0 on success, -EERROR otherwise.
 *
 * @get_parent: Queries the hardware to determine the parent of a clock.  The
 *              return value is a u8 which specifies the index corresponding to
 *              the parent clock.  This index can be applied to either the
 *              .parent_names or .parents arrays.  In short, this function
 *              translates the parent value read from hardware into an array
 *              index.  Currently only called when the clock is initialized by
 *              __clk_init.  This callback is mandatory for clocks with
 *              multiple parents.  It is optional (and unnecessary) for clocks
 *              with 0 or 1 parents.
 *
 * @set_rate:   Change the rate of this clock. The requested rate is specified
 *              by the second argument, which should typically be the return
 *              of .round_rate call.  The third argument gives the parent rate
 *              which is likely helpful for most .set_rate implementation.
 *              Returns 0 on success, -EERROR otherwise.
 *
 * @set_rate_and_parent: Change the rate and the parent of this clock. The
 *              requested rate is specified by the second argument, which
 *              should typically be the return of .round_rate call.  The
 *              third argument gives the parent rate which is likely helpful
 *              for most .set_rate_and_parent implementation. The fourth
 *              argument gives the parent index. This callback is optional (and
 *              unnecessary) for clocks with 0 or 1 parents as well as
 *              for clocks that can tolerate switching the rate and the parent
 *              separately via calls to .set_parent and .set_rate.
 *              Returns 0 on success, -EERROR otherwise.
 *
 * @recalc_accuracy: Recalculate the accuracy of this clock. The clock accuracy
 *              is expressed in ppb (parts per billion). The parent accuracy is
 *              an input parameter.
 *              Returns the calculated accuracy.  Optional - if this op is not
 *              set then clock accuracy will be initialized to parent accuracy
 *              or 0 (perfect clock) if clock has no parent.
 *
 * @get_phase:  Queries the hardware to get the current phase of a clock.
 *              Returned values are 0-359 degrees on success, negative
 *              error codes on failure.
 *
 * @set_phase:  Shift the phase this clock signal in degrees specified
 *              by the second argument. Valid values for degrees are
 *              0-359. Return 0 on success, otherwise -EERROR.
 *
 * @get_duty_cycle: Queries the hardware to get the current duty cycle ratio
 *              of a clock. Returned values denominator cannot be 0 and must be
 *              superior or equal to the numerator.
 *
 * @set_duty_cycle: Apply the duty cycle ratio to this clock signal specified by
 *              the numerator (2nd argurment) and denominator (3rd  argument).
 *              Argument must be a valid ratio (denominator > 0
 *              and >= numerator) Return 0 on success, otherwise -EERROR.
 *
 * @init:       Perform platform-specific initialization magic.
 *              This is not not used by any of the basic clock types.
 *              Please consider other ways of solving initialization problems
 *              before using this callback, as its use is discouraged.
 *
 * @debug_init: Set up type-specific debugfs entries for this clock.  This
 *              is called once, after the debugfs directory entry for this
 *              clock has been created.  The dentry pointer representing that
 *              directory is provided as an argument.  Called with
 *              prepare_lock held.  Returns 0 on success, -EERROR otherwise.
 *
 *
 * The clk_enable/clk_disable and clk_prepare/clk_unprepare pairs allow
 * implementations to split any work between atomic (enable) and sleepable
 * (prepare) contexts.  If enabling a clock requires code that might sleep,
 * this must be done in clk_prepare.  Clock enable code that will never be
 * called in a sleepable context may be implemented in clk_enable.
 *
 * Typically, drivers will call clk_prepare when a clock may be needed later
 * (eg. when a device is opened), and clk_enable when the clock is actually
 * required (eg. from an interrupt). Note that clk_prepare MUST have been
 * called before clk_enable.
 */
struct clk_ops {
        int             (*prepare)(struct clk_hw *hw);
        void            (*unprepare)(struct clk_hw *hw);
        int             (*is_prepared)(struct clk_hw *hw);
        void            (*unprepare_unused)(struct clk_hw *hw);
        int             (*enable)(struct clk_hw *hw);
        void            (*disable)(struct clk_hw *hw);
        int             (*is_enabled)(struct clk_hw *hw);
        void            (*disable_unused)(struct clk_hw *hw);
        int             (*save_context)(struct clk_hw *hw);
        void            (*restore_context)(struct clk_hw *hw);
        unsigned long   (*recalc_rate)(struct clk_hw *hw,
                                        unsigned long parent_rate);
        long            (*round_rate)(struct clk_hw *hw, unsigned long rate,
                                        unsigned long *parent_rate);
        int             (*determine_rate)(struct clk_hw *hw,
                                          struct clk_rate_request *req);
        int             (*set_parent)(struct clk_hw *hw, u8 index);
        u8              (*get_parent)(struct clk_hw *hw);
        int             (*set_rate)(struct clk_hw *hw, unsigned long rate,
                                    unsigned long parent_rate);
        int             (*set_rate_and_parent)(struct clk_hw *hw,
                                    unsigned long rate,
                                    unsigned long parent_rate, u8 index);
        unsigned long   (*recalc_accuracy)(struct clk_hw *hw,
                                           unsigned long parent_accuracy);
        int             (*get_phase)(struct clk_hw *hw);
        int             (*set_phase)(struct clk_hw *hw, int degrees);
        int             (*get_duty_cycle)(struct clk_hw *hw,
                                          struct clk_duty *duty);
        int             (*set_duty_cycle)(struct clk_hw *hw,
                                          struct clk_duty *duty);
        void            (*init)(struct clk_hw *hw);
        void            (*debug_init)(struct clk_hw *hw, struct dentry *dentry);
};
  • (*prepare)
    • 클럭이 출력되도록 prepare하는 후크 함수로 슬립할 수 있다.
    • 단 gated 클럭의 경우 enable까지 해야 클럭이 출력된다.
  • (*unprepare)
    • 출력 중인 클럭이 unprepare되도록하는 후크 함수로 슬립할 수 있다.
  • (*is_prepared)
    • 클럭이 준비되었는지 여부를 알려주는 후크 함수
  • (unprepare_unused)
    • 사용중이지 않은 클럭을 unprepre하는 후크 함수로 슬립할 수 있다.
  • (*enable)
    • atomic 하게 클럭의 enable(gate open)하는 후크 함수로 슬립하지 않는다.
  • (*disable)
    • atomic 하게 클럭을 disable(gate close)하는 후크 함수로 슬립하지 않는다.
  • (is_enabled)
    • 클럭의 enable 여부를 알려주는 후크 함수
  • (*disable_unused)
    • 사용중이지 않은 클럭을 atomic하게 disable하는 후크 함수
  • (*save_context)
    • power-off를 준비하기 위한 클럭의 context를 저장하는 후크 함수
  • (*restore_context)
    • 저장된 클럭 context를 읽어오는 후크 함수
  • (*recalc_rate)
    • rate 재산출 후크 함수
    • 부모 클럭 코어의 rate 변경 시 연동된다.
  • (*round_rate)
    • 요청한 rate에 대해 클럭 코어의 hw가 지원하는 가장 가까운 rate를 산출하는 후크 함수
    • rate (divider, multiplier, pll, ..) 류의 클럭 코어에서 사용된다.
  • (*determine_rate)
    • 요청한 rate에 대해 클럭 코어의 hw가 지원하는 가장 가까운 rate를 산출하는 후크 함수
    • mux, pll 류의 클럭 코어에서 rate가 조절될 때 사용된다.
  • (*set_parent)
    • 부모 클럭을 변경하는 후크 함수
    • mux 클럭에서 클럭 소스를 선택한다.
  • (*get_parent)
    • 연결된 부모 클럭에 대한 인덱스를 알아오는 후크 함수
  • (*set_rate)
    • rate 설정 후크 함수
  • (*set_rate_and_parent)
    • rate를 변경하고 부모 클럭을 변경하는 후크 함수
  • (*get_phase)
    • 위상 값을 알아오는 후크 함수
  • (*set_phase)
    • 위상 값을 설정하는 후크 함수
  • (*get_duty_cycle)
    • duty cycle 값을 알아오는 후크 함수
  • (*set_duty_cycle)
    • duty cycle 값을 설정하는 후크 함수
  • (*init)
    • 클럭 코어의 초기화 후크 함수

 

clk_hw 구조체

drivers/clk/clk.c

/**
 * struct clk_hw - handle for traversing from a struct clk to its corresponding
 * hardware-specific structure.  struct clk_hw should be declared within struct
 * clk_foo and then referenced by the struct clk instance that uses struct
 * clk_foo's clk_ops
 *
 * @core: pointer to the struct clk_core instance that points back to this
 * struct clk_hw instance
 *
 * @clk: pointer to the per-user struct clk instance that can be used to call
 * into the clk API
 *
 * @init: pointer to struct clk_init_data that contains the init data shared
 * with the common clock framework. This pointer will be set to NULL once
 * a clk_register() variant is called on this clk_hw pointer.
 */
struct clk_hw {
        struct clk_core *core;
        struct clk *clk;
        const struct clk_init_data *init;
};
  • *core
    • 클럭 코어 포인터
  • *clk
    • 클럭 포인터
  • *init
    • 클럭 초기화 데이터

 

clk_parent_data 구조체

drivers/clk/clk.c

/**
 * struct clk_parent_data - clk parent information
 * @hw: parent clk_hw pointer (used for clk providers with internal clks)
 * @fw_name: parent name local to provider registering clk
 * @name: globally unique parent name (used as a fallback)
 * @index: parent index local to provider registering clk (if @fw_name absent)
 */
struct clk_parent_data {
        const struct clk_hw     *hw;
        const char              *fw_name;
        const char              *name;
        int                     index;
};
  • *hw
    • 부코 클럭 hw 포인터
  • *fw_name
    • 부모 클럭 provider 명
  • *name
    • 유니크한 부모 클럭 명
  • index
    • 부모 클럭 코어에 대한 인덱스

 

참고

 

 

Timer -1- (Lowres Timer)

<kernel v5.4>

Lowres(Low Resolution) Timer

커널에서 사용하는 jifffies 기반의 타이머 tick을 소프트웨어 기법으로 구현한 타이머이다. 커널이 타이머를 요청할 때 만료 시간을 기재하는데 lowres 타이머는 정확하게 그 만료시점에 깨어나는 것을 보장하지 못하고 요청한 만료 시간의 최대 1/8(12.5%)의 지연된 오차를 가진다. 그 외 특징으로 lowres 타이머는 특정 아키텍처와 무관한 구조이다.

 

새로운 non-cascading wheel 구조 (v4.8~)

lowres 타이머는 커널 v4.8-rc1 에서 새로운 변화를 보여주면서 lowres 타이머에 대한 전반적인 설계가 변경되어 non-cascading wheel 모델이 소개되었다.

  • 타이머 휠에 등록된 타이머들은 cascading 하는 작업이 없어졌으므로 이로 인한 오버헤드가 줄었다.
  • fast lookup: 만료 타이머를 찾기 위한 lookup이 빨라졌다.
  • slack 관련한 동작과 API들도 더 이상 필요없게되어 삭제되었다.
  • 직전 implementation한 로직과 유사하게 요청한 만료 시각에 오차가 발생하는데, 최대 약 1/8(12.5%) 지연된 오차를 가진다.
    • 예) 40시간 후에 타이머를 동작시키게 하였지만 4.6시간 더 오래 걸려 타이머가 동작한다.
  • 참고:

 

타이머 리스트

admin 권한으로 타이머 리스트를 보려면 다음과 같이 한다. (커널 v4.8~)

$ cat /proc/timer_list
Timer List Version: v0.8
HRTIMER_MAX_CLOCK_BASES: 8
now at 79245435304 nsecs

cpu: 0
 clock 0:
  .base:       (____ptrval____)
  .index:      0
  .resolution: 1 nsecs
  .get_time:   ktime_get
  .offset:     0 nsecs
active timers:
 #0: <(____ptrval____)>, tick_sched_timer, S:01
 # expires at 79260000000-79260000000 nsecs [in 14564696 to 14564696 nsecs]
 #1: <(____ptrval____)>, it_real_fn, S:01
 # expires at 79374561805-79374561805 nsecs [in 129126501 to 129126501 nsecs]
 #2: <(____ptrval____)>, hrtimer_wakeup, S:01
 # expires at 77374835623-79874835623 nsecs [in -1870599681 to 629400319 nsecs]
...

 


타이머 및 벡터 관리 구조

두 개의 타이머 베이스

다음 그림과 같이 nohz에서 사용하는 타이머 베이스까지 두 개로 나뉘어 관리된다. 스케줄틱이 발생할 때마다 두 개의 타이머 베이스의 만료된 타이머들을 깨워 콜백함수를 호출한다.

 

다음 그림은 타이머들이 해시 벡터리스트로 구현되어 처리되는 모습을 보여준다. 100hz 시스템의 경우 최대 8 단계, 그 외의 시스템은 최대 9 단계 레벨로 구성된다.

  • 각 단계의 단위 틱은 2^(lvl*3) 이다. 즉 레벨 0는 1틱, 레벨 1은 8틱, 레벨 2는 64틱이다.
  • 스케줄 틱마다 base->clk의 값도 증가된다. 이 값으로 각 레벨의 해시 인덱스를 산출하고 이 인덱스 값에 따른 타이머들을 깨워 호출한다.

 

timer_list 구조체

include/linux/timer.h

struct timer_list {
        /*
         * All fields that change during normal runtime grouped to the
         * same cacheline
         */
        struct hlist_node       entry;
        unsigned long           expires;
        void                    (*function)(struct timer_list *);
        u32                     flags;

#ifdef CONFIG_LOCKDEP
        struct lockdep_map      lockdep_map;
#endif
};

타이머마다 만료 시각과 호출될 콜백 함수 정보가 담겨있다.

  •  entry
    • 동적 타이머들을 타이머 벡터 리스트로 연결 시 사용한다.
  • expires
    • 타이머가 만료될 미래의 jiffies 시점을 지정한다.
  • function
    • 타이머 만료 시 실행할 함수의 주소를 저장한다.
  • flags
    • TIMER_CPUMASK
      • 타이머 휠 인덱스  값으로 10비트를 사용하여 저장된다.
    • TIMER_MIGRATING
      • migration 진행중인 타이머 여부 (타이머 cpu 이동중)
    • TIMER_DEFERRABLE
      • 지연 타이머 여부
      • deferrable 전용 타이머 베이스를 사용한다.
    • TIMER_PINNED
      • cpu 고정(pinned) 타이머 여부
    • TIMER_IRQSAFE
      • 타이머가 인터럽트 핸들러에서 사용되지 않음을 나타낸다.
      • 인터럽트 latency 성능을 위해 추가되는 플래그로 내부에서 spinlock 시 인터럽트를 disable 하지 않는다.

 

timer_base 구조체

tvec_base 구조체명이 timer_base 구조체명으로 변경되었다.

kernel/time/timer.c

struct timer_base {
        raw_spinlock_t          lock;
        struct timer_list       *running_timer;
#ifdef CONFIG_PREEMPT_RT
        spinlock_t              expiry_lock;
        atomic_t                timer_waiters;
#endif
        unsigned long           clk;
        unsigned long           next_expiry;
        unsigned int            cpu;
        bool                    is_idle;
        bool                    must_forward_clk;
        DECLARE_BITMAP(pending_map, WHEEL_SIZE);
        struct hlist_head       vectors[WHEEL_SIZE];
} ____cacheline_aligned;

타이머들이 관리되는 타이머 베이스는 per-cpu로 관리되며, 타이머 리스트는 해시 벡터 휠로 관리된다.

  •  lock
    • 타이머 벡터 리스트 조작시 사용할 lock
  • running_timer
    • 타이머가 만료되어 현재 함수를 실행중인 타이머를 가리킨다.
  • clk
    • 처리할 시각(틱)이다. nohz 상태에 따라 동작이 상이하다.
      • nohz가 동작하지 않는 경우 이 시각은 현재 시각(jiffies)과 동일한 값이 되도록 매 스케줄 틱마다 증가된다.
      • nohz가 동작하는 경우에는 jiffies 보다 지연될 수 있다. cpu가 nohz에서 탈출할 때 지연된 시간만큼 처리한다.
        • 지연된 시간만큼 루프를 반복하며 틱을 처리하면 성능이 떨어지므로 nohz optimization 기법을 사용하여 처리하지 않아도 되는 틱은 생략하게 한다.
  • next_expiry
    • 다음 만료될 타이머의 시각(틱)
  • cpu
    • 현재 타이머 벡터 관리가 동작하는 cpu id
  • is_idle
    • 타이머 베이스의 idle 상태
  • must_forward_clk
    • nohz에서 타이머 베이스의 clk를 forward 한다. (nohz optimization)
  • pending_map
    • 펜딩 비트맵으로 타이머 휠 인덱스마다 1비트를 사용한다.
    • 타이머 휠의 인덱스 비트가 1로 설정되면 타이머가 1 개 이상이 대기중임을 의미한다.
  • vectors[]
    • 타이머들이 대기하는 타이머 휠 리스트이며, 해시 벡터로 구현되었다.

 

timer_bases[]

kernel/time/timer.c

static DEFINE_PER_CPU(struct timer_base, timer_bases[NR_BASES]);

timer_base는 성능향상을 위해 per-cpu를 사용하여 관리한다. nohz를 사용하지 않으면 하나의 타이머 베이스에 관리된다. 그러나 nohz를 사용하는 경우 다음과 같이 두 개의 타이머 휠로 나누어 관리된다.

  • BASE_STD
    • 기본(standard) 타이머를 위해 사용된다.
  • BASE_DEF
    • defferable 타이머를 위해 사용된다.

 

jiffies & 초기값

jiffies 값은 스케줄 틱이 발생할 때 마다 1씩 증가한다. 스케줄 틱은 CONFIG_HZ에 정한 시간에 한 번씩 발생한다.

  • 예) 100hz 시스템인 경우 1초에 100번의 스케줄 틱이 발생한다. 따라서 10ms 주기마다 1 tick이 발생하고 jiffies 값도 1씩 증가된다.

 

INITIAL_JIFFIES

include/linux/jiffies.h

/*
 * Have the 32 bit jiffies value wrap 5 minutes after boot
 * so jiffies wrap bugs show up earlier.
 */
#define INITIAL_JIFFIES ((unsigned long)(unsigned int) (-300*HZ))

jiffies 초기값은 32bit 시스템에서 부트 후 약 5분이내에 overflow 될 수 있는 값을 주었다. 64비트 시스템에서는 overflow되려면 어마 어마한 시간이 흘러야 하므로 overflow될 걱정이 없다.

  • 초기값
    • 32bit 예) HZ=250
      • 0xfffe_db08 (-75000)
    • 64bit 예) HZ=250
      • 0x0000_0000_fffe_db08

 


APIs

주요 API

컴파일 타임에 정적으로 타이머 생성 및 초기화

  • DEFINE_TIMER()

런타임에 동적으로 타이머 생성 및 초기화

  • timer_setup()
  • timer_setup_on_stack()

타이머 베이스에 타이머 추가/변경/삭제

  • add_timer()
  • mod_timer()
  • del_timer()

 

시간 비교 API

jiffies 값을 읽어 시간을 직접 비교하는 경우 jiffies overflow 되는 시점에서 시간 비교가 의도치 않는 반대의 결과를 얻을 수 있다. 따라서 다음 함수들을 사용하여야 정확한 결과를 나타내게 할 수 있으므로 절대 jiffies 시간을 직접 비교 사용하는 일이 없도록 해야 한다.

  • 2개 시간 비교 함수
    • time_before(a,b)
      • a가 b 보다 먼저 시간값이면 true
    • time_after(a,b)
      • a가 b 보다 나중 시간값이면 true
    • time_before_eq(a,b)
      • a 가 b보다 먼저이거나 같은 시간이면 true
    • time_after_eq(a,b)
      • a 가 b 보다 나중이거나 같은 시간이면 true
  • jiffies와 시간 비교 함수
    • time_is_before_jiffies(a)
      • a가 jiffies 보다 먼저 시간값이면 true
    • time_is_after_jiffies(a)
      • a가 jiffies 보다 나중 시간값이면 true
    • time_is_before_eq_jiffies(a)
      • a 가 jiffies 보다 먼저이거나 같은 시간이면 true
    • time_is_after_eq_jiffies(a)
      • a 가 jiffies 보다 나중이거나 같은 시간이면 true

 

관련 API

  • msleep()
  • msleep_interruptible()
  • schedule_timeout_interruptible()
  • schedule_timeout_killable()
  • schedule_timeout_uninterruptible()
  • schedule_timeout_idle()
  • schedule_timeout()

 


타이머 추가/삭제

타이머 추가

add_timer()

kernel/time/timer.c

/**
 * add_timer - start a timer
 * @timer: the timer to be added
 *
 * The kernel will do a ->function(->data) callback from the
 * timer interrupt at the ->expires point in the future. The
 * current time is 'jiffies'.
 *
 * The timer's ->expires, ->function (and if the handler uses it, ->data)
 * fields must be set prior calling this function.
 *
 * Timers with an ->expires field in the past will be executed in the next
 * timer tick.
 */
void add_timer(struct timer_list *timer)
{
        BUG_ON(timer_pending(timer));
        mod_timer(timer, timer->expires);
}
EXPORT_SYMBOL(add_timer);

동적 타이머를 요청한다.

  • mod_timer()를 호출하여 만료 시간을 변경한다.

 

타이머 삭제

del_timer()

kernel/time/timer.c

/**
 * del_timer - deactive a timer.
 * @timer: the timer to be deactivated
 *
 * del_timer() deactivates a timer - this works on both active and inactive
 * timers.
 *
 * The function returns whether it has deactivated a pending timer or not.
 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
 * active timer returns 1.)
 */
int del_timer(struct timer_list *timer)
{
        struct tvec_base *base;
        unsigned long flags;
        int ret = 0;

        debug_assert_init(timer);

        if (timer_pending(timer)) {
                base = lock_timer_base(timer, &flags);
                ret = detach_if_pending(timer, base, true);
                spin_unlock_irqrestore(&base->lock, flags);
        }

        return ret;
}
EXPORT_SYMBOL(del_timer);

타이머를 타이머 베이스에서 제거하여 비활성화한다.

  • 코드 라인 7에서 타이머에서 사용자 트래킹 정보를 클리어한다.
  • 코드 라인 9~13에서 타이머 벡터 리스트에 등록되어 대기중인 타이머인 경우 그 리스트에서 제거한다.
  • 코드 라인 15에서 타이머가 활성화 상태였었으면 1을 반환하고, 그렇지 않은 경우 0을 반환한다.

 

펜딩 타이머 제거

detach_if_pending()

kernel/time/timer.c

static int detach_if_pending(struct timer_list *timer, struct timer_base *base,
                             bool clear_pending)
{
        unsigned idx = timer_get_idx(timer);

        if (!timer_pending(timer))
                return 0;

        if (hlist_is_singular_node(&timer->entry, base->vectors + idx))
                __clear_bit(idx, base->pending_map);

        detach_timer(timer, clear_pending);
        return 1;
}

펜딩된 타이머인 경우 리스트에서 제거한다. 활동(pending) 중인 타이머를 제거한 경우 1을 반환한다.

  • 코드 라인 6~7에서 타이머 벡터 리스트에 등록되어 대기하고 있는 타이머가 아닌 경우 이미 deactivate된 경우이므로 0을 반환한다.
  • 코드 라인 9~10에서 타이머의 인덱스에 해당하는 타이머 벡터 리스트에 자신 1건만 등록된 경우라면 해당 인덱스의 펜딩맵을 클리어한다.
  • 코드 라인 12~13에서 타이머를 타이머 베이스 휠에서 제거하고 1을 반환한다.

 

타이머 제거

detach_timer()

kernel/time/timer.c

static inline void detach_timer(struct timer_list *timer, bool clear_pending)
{
        struct list_head *entry = &timer->entry;

        debug_deactivate(timer);

        __hlist_del(entry);
        if (clear_pending)
                entry->pprev = NULL;
        entry->next = LIST_POISON2;
}

타이머 리스트에서 요청 타이머를 제거한다. 만일 clear pending 요청이 있는 경우 타이머 엔트리가 다음 타이머와 연결되지 않도록 분리한다.

 


타이머 변경

mod_timer()

kernel/time/timer.c

/**
 * mod_timer - modify a timer's timeout
 * @timer: the timer to be modified
 * @expires: new timeout in jiffies
 *
 * mod_timer() is a more efficient way to update the expire field of an
 * active timer (if the timer is inactive it will be activated)
 *
 * mod_timer(timer, expires) is equivalent to:
 *
 *     del_timer(timer); timer->expires = expires; add_timer(timer);
 *
 * Note that if there are multiple unserialized concurrent users of the
 * same timer, then mod_timer() is the only safe way to modify the timeout,
 * since add_timer() cannot modify an already running timer.
 *
 * The function returns whether it has modified a pending timer or not.
 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
 * active timer returns 1.)
 */
int mod_timer(struct timer_list *timer, unsigned long expires)
{
        return __mod_timer(timer, expires, 0);
}
EXPORT_SYMBOL(mod_timer);

요청한 타이머를 제거하고 인수로 받은 만료 시점(timerout을 적용한 새 jiffies 시점)으로 조절하고 설정한다. inactive된 타이머도 active 시킨다.

 

__mod_timer()

kernel/time/timer.c

static inline int
__mod_timer(struct timer_list *timer, unsigned long expires, unsigned int options)
{
        struct timer_base *base, *new_base;
        unsigned int idx = UINT_MAX;
        unsigned long clk = 0, flags;
        int ret = 0;

        BUG_ON(!timer->function);

        /*
         * This is a common optimization triggered by the networking code - if
         * the timer is re-modified to have the same timeout or ends up in the
         * same array bucket then just return:
         */
        if (timer_pending(timer)) {
                /*
                 * The downside of this optimization is that it can result in
                 * larger granularity than you would get from adding a new
                 * timer with this expiry.
                 */
                long diff = timer->expires - expires;

                if (!diff)
                        return 1;
                if (options & MOD_TIMER_REDUCE && diff <= 0)
                        return 1;

                /*
                 * We lock timer base and calculate the bucket index right
                 * here. If the timer ends up in the same bucket, then we
                 * just update the expiry time and avoid the whole
                 * dequeue/enqueue dance.
                 */
                base = lock_timer_base(timer, &flags);
                forward_timer_base(base);

                if (timer_pending(timer) && (options & MOD_TIMER_REDUCE) &&
                    time_before_eq(timer->expires, expires)) {
                        ret = 1;
                        goto out_unlock;
                }

                clk = base->clk;
                idx = calc_wheel_index(expires, clk);

                /*
                 * Retrieve and compare the array index of the pending
                 * timer. If it matches set the expiry to the new value so a
                 * subsequent call will exit in the expires check above.
                 */
                if (idx == timer_get_idx(timer)) {
                        if (!(options & MOD_TIMER_REDUCE))
                                timer->expires = expires;
                        else if (time_after(timer->expires, expires))
                                timer->expires = expires;
                        ret = 1;
                        goto out_unlock;
                }
        } else {
                base = lock_timer_base(timer, &flags);
                forward_timer_base(base);
        }

요청한 타이머를 제거하고 최종 결정된 만료 시점으로 타이머를 설정한 후 다시 추가 하고 active 시킨다. 타이머가 active된 상태이면 1로 반환된다.

  • 코드 라인 16에서 타이머가 expire 되지 않고 아직 타이머 휠에서 기다리고 있는 중이다.
  • 코드 라인 22~27에서 새로운 만료 시각(@expires)으로 변경을 하려할 때 차이가 없으면 1을 결과로 함수를 빠져나간다.
    • 만일 만료 시각이 앞당겨졌는데 만료 시각을 앞으로 당기지 못하게 한 MOD_TIMER_REDUCE 옵션을 사용한 경우도 결과를 1로 함수를 빠져나간다.
  • 코드 라인 35~45에서 타이머가 등록된 타이머 베이스에서 산출한 타이머 휠 인덱스를 알아온다.
    • lock을 획득 한 후 타이머 베이스의 시각(clk)을 forward 하도록 갱신하고, 다시 한번 체크한다. 만료 시각이 앞당겨졌는데 만료 시각을 앞으로 당기지 못하게 한 MOD_TIMER_REDUCE 옵션을 사용한 경우도 결과를 1로 함수를 빠져나간다.
  • 코드 라인 52~59에서 타이머가 등록된 타이머 베이스에서 해당 타이머 휠 인덱스와 위에서 산출한 휠 인덱스가 동일하면 만료 시각을 갱신하고 결과를 1로 변경하고 함수를 빠져나간다.
  • 코드 라인 60~63에서 요청한 타이머가 타이머 휠에 없는 경우 타이머를 위한 타이머 베이스를 준비하고, 타이머 베이스의 시각(clk)을 forward 하도록 갱신한다.

 

        ret = detach_if_pending(timer, base, false);
        if (!ret && (options & MOD_TIMER_PENDING_ONLY))
                goto out_unlock;

        new_base = get_target_base(base, timer->flags);

        if (base != new_base) {
                /*
                 * We are trying to schedule the timer on the new base.
                 * However we can't change timer's base while it is running,
                 * otherwise del_timer_sync() can't detect that the timer's
                 * handler yet has not finished. This also guarantees that the
                 * timer is serialized wrt itself.
                 */
                if (likely(base->running_timer != timer)) {
                        /* See the comment in lock_timer_base() */
                        timer->flags |= TIMER_MIGRATING;

                        raw_spin_unlock(&base->lock);
                        base = new_base;
                        raw_spin_lock(&base->lock);
                        WRITE_ONCE(timer->flags,
                                   (timer->flags & ~TIMER_BASEMASK) | base->cpu);
                        forward_timer_base(base);
                }
        }

        debug_timer_activate(timer);

        timer->expires = expires;
        /*
         * If 'idx' was calculated above and the base time did not advance
         * between calculating 'idx' and possibly switching the base, only
         * enqueue_timer() and trigger_dyntick_cpu() is required. Otherwise
         * we need to (re)calculate the wheel index via
         * internal_add_timer().
         */
        if (idx != UINT_MAX && clk == base->clk) {
                enqueue_timer(base, timer, idx);
                trigger_dyntick_cpu(base, timer);
        } else {
                internal_add_timer(base, timer);
        }

out_unlock:
        raw_spin_unlock_irqrestore(&base->lock, flags);

        return ret;
}
  • 코드 라인 1~3에서 pending된 타이머인 경우에 한해 해당 타이머 벡터 리스트에서 제거한다. 실패 시 pending only인 경우 처리를 중단하고 빠져나간다.
  • 코드 라인 5~26에서 타이머의 cpu 변경이 필요한 경우 migrating을 한다.
  • 코드 라인 30~43에서 타이머의 만료 시점을 갱신하고 적절한 타이머 벡터 리스트에 새로 추가한다.

 

다음 그림은 타이머를 추가 또는 변경 시 호출되는 __mod_timer() 함수를 통해 타이머 베이스의 벡터 리스트에 타이머가 추가되는 모습을 보여준다.

  • 레벨이 올라갈수록 만료 시각에 대한 정확도는 레벨 당 8배 단위로 커지는 Granularity 값만큼 비례하여 떨어진다. 레벨 0의 오차가 8틱 이하였지만, 그 다음 레벨 1은 8배 커진 64틱 이하인것을 확인할 수 있다.

 

타이머 추가(internal)

internal_add_timer()

kernel/time/timer.c

static void
internal_add_timer(struct timer_base *base, struct timer_list *timer)
{
        __internal_add_timer(base, timer);
        trigger_dyntick_cpu(base, timer);
}

요청 타이머 베이스에 타이머를 추가한다.

 

__internal_add_timer()

kernel/time/timer.c

static void
__internal_add_timer(struct timer_base *base, struct timer_list *timer)
{
        unsigned int idx;

        idx = calc_wheel_index(timer->expires, base->clk);
        enqueue_timer(base, timer, idx);
}

요청 타이머 베이스에 타이머를 추가한다.

  • 코드 라인 5에서 타이머 베이스에서 사용할 타이머 휠의 인덱스 값을 알아온다.
  • 코드 라인 6에서 산출한 인덱스의 테이머 벡터 리스트에 타이머를 추가한다.
    • vectors[idx] 리스트 <— 타이머 추가

 

enqueue_timer()

kernel/time/timer.c

/*
 * Enqueue the timer into the hash bucket, mark it pending in
 * the bitmap and store the index in the timer flags.
 */
static void enqueue_timer(struct timer_base *base, struct timer_list *timer,
                          unsigned int idx)
{
        hlist_add_head(&timer->entry, base->vectors + idx);
        __set_bit(idx, base->pending_map);
        timer_set_idx(timer, idx);

        trace_timer_start(timer, timer->expires, timer->flags);
}

요청 타이머 베이스에서 인덱스에 해당하는 타이머 벡터 리스트에 타이머를 추가한다.

  • 코드 라인 4에서 요청 인덱스에 해당하는 타이머 벡터 리스트에 타이머를 추가한다.
  • 코드 라인 5에서 펜딩 맵의 인덱스에 해당하는 비트를 설정한다.
  • 코드 라인 6에서 타이머의 플래그 중 10비트 공간을 사용하여 인덱스를 기록한다.

 

timer_set_idx()

kernel/time/timer.c

static inline void timer_set_idx(struct timer_list *timer, unsigned int idx)
{
        timer->flags = (timer->flags & ~TIMER_ARRAYMASK) |
                        idx << TIMER_ARRAYSHIFT;
}

타이머의 플래그 bits[31..22]에 인덱스를 기록한다. (10 비트)

 

timer_pending()

include/linux/timer.h

/**
 * timer_pending - is a timer pending?
 * @timer: the timer in question
 *
 * timer_pending will tell whether a given timer is currently pending,
 * or not. Callers must ensure serialization wrt. other operations done
 * to this timer, eg. interrupt contexts, or other CPUs on SMP.
 *              
 * return value: 1 if the timer is pending, 0 if not.
 */     
static inline int timer_pending(const struct timer_list * timer)
{       
        return timer->entry.pprev != NULL;
}

타이머 벡터 리스트에서 대기중인 타이머인지 여부를 반환한다.

  • 타이머 벡터 리스트는 환형 더블리스트이므로 타이머가 혼자 등록되어 있어도 head와 연결되는 구조이다.  따라서 null이 들어가 있는 경우는 환형 더블리스트에서 제거된 경우밖에 없다. 즉 리스트에  존재하면 항상 true이다.

 

no-hz용 타이머 베이스 시각 forward

forward_timer_base()

kernel/time/timer.c

static inline void forward_timer_base(struct timer_base *base)
{
#ifdef CONFIG_NO_HZ_COMMON
        unsigned long jnow;

        /*
         * We only forward the base when we are idle or have just come out of
         * idle (must_forward_clk logic), and have a delta between base clock
         * and jiffies. In the common case, run_timers will take care of it.
         */
        if (likely(!base->must_forward_clk))
                return;

        jnow = READ_ONCE(jiffies);
        base->must_forward_clk = base->is_idle;
        if ((long)(jnow - base->clk) < 2)
                return;

        /*
         * If the next expiry value is > jiffies, then we fast forward to
         * jiffies otherwise we forward to the next expiry value.
         */
        if (time_after(base->next_expiry, jnow))
                base->clk = jnow;
        else
                base->clk = base->next_expiry;
#endif
}

nohz를 위해 현재 요청한 타이머 베이스의 시각을 forward 한다.

  • 코드 라인 11~12에서 nohz를 위해 타이머 베이스가 idle 상태인 경우 타이머 베이스를 forward할 수 있게 하였다. 그 경우가 아니면 함수를 빠져나간다. 다음 함수들에서 must_forward_clk 플래그를 1로 만든다.
    • timers_prepare_cpu()
    • get_next_timer_interrupt()
  • 코드 라인 14~17에서 must_forward_clk 값을 idle 상태에서만 사용할 수 있게 지정한다. 타이머 베이스가 현재 틱과 비교하여 2틱 미만인 경우 함수를 빠져나간다.
  • 코드 라인 23~26에서 만료 시각이 아직 남아 있는 경우 타이머 베이스의 시각에 현재 시각을 대입한다. 만일 만료 시각이 지났으면 다음 만료 시각으로 갱신한다.

 


휠 인덱스 산출

calc_wheel_index()

kernel/time/timer.c

static int calc_wheel_index(unsigned long expires, unsigned long clk)
{
        unsigned long delta = expires - clk;
        unsigned int idx;

        if (delta < LVL_START(1)) {
                idx = calc_index(expires, 0);
        } else if (delta < LVL_START(2)) {
                idx = calc_index(expires, 1);
        } else if (delta < LVL_START(3)) {
                idx = calc_index(expires, 2);
        } else if (delta < LVL_START(4)) {
                idx = calc_index(expires, 3);
        } else if (delta < LVL_START(5)) {
                idx = calc_index(expires, 4);
        } else if (delta < LVL_START(6)) {
                idx = calc_index(expires, 5);
        } else if (delta < LVL_START(7)) {
                idx = calc_index(expires, 6);
        } else if (LVL_DEPTH > 8 && delta < LVL_START(8)) {
                idx = calc_index(expires, 7);
        } else if ((long) delta < 0) {
                idx = clk & LVL_MASK;
        } else {
                /*
                 * Force expire obscene large timeouts to expire at the
                 * capacity limit of the wheel.
                 */
                if (expires >= WHEEL_TIMEOUT_CUTOFF)
                        expires = WHEEL_TIMEOUT_MAX;

                idx = calc_index(expires, LVL_DEPTH - 1);
        }
        return idx;
}

만료 시각과 클럭 베이스의 시각의 차이로 타이머 휠 인덱스 값을 산출한다.

  • 코드 라인 3에서 만료 시각과 클럭 베이스의 시각의 차이 틱을 구해  delta에 대입한다.
  • 코드 라인 6~7에서 delta 값이 1 레벨 시작 틱 값보다 작은 경우 0 레벨 기준으로 휠 인덱스를 산출한다.
  • 코드 라인 8~21에서 delta 값을 사용하여 1~7레벨까지를 기준으로 휠 인덱스를 산출한다.
  • 코드 라인 22~23에서 0보다 작은 delta 값에 해당하는 휠 인덱스를 산출한다.
  • 코드 라인 24~33에서 범위를 초과하는 delta 값인 경우 최대 값으로 휠 인덱스를 산출한다.
  • 코드 라인 34에서 산출한 휠 인덱스를 반환한다.

 

calc_index()

kernel/time/timer.c

/*
 * Helper function to calculate the array index for a given expiry
 * time.
 */
static inline unsigned calc_index(unsigned expires, unsigned lvl)
{
        expires = (expires + LVL_GRAN(lvl)) >> LVL_SHIFT(lvl);
        return LVL_OFFS(lvl) + (expires & LVL_MASK);
}

만료 시각과 레벨로 타이머 휠 인덱스를 알아온다.

  • 예) expires=0x43c0, lvl=2
    • new expires = (0x43c0 + 0x40) >> 6 = 0x110
    • return 0x80 + 0x10 = 0x90 (144)

 

매크로 함수들

kernel/time/timer.c

/*
 * The timer wheel has LVL_DEPTH array levels. Each level provides an array of
 * LVL_SIZE buckets. Each level is driven by its own clock and therefor each
 * level has a different granularity.
 *
 * The level granularity is:            LVL_CLK_DIV ^ lvl
 * The level clock frequency is:        HZ / (LVL_CLK_DIV ^ level)
 *
 * The array level of a newly armed timer depends on the relative expiry
 * time. The farther the expiry time is away the higher the array level and
 * therefor the granularity becomes.
 *
 * Contrary to the original timer wheel implementation, which aims for 'exact'
 * expiry of the timers, this implementation removes the need for recascading
 * the timers into the lower array levels. The previous 'classic' timer wheel
 * implementation of the kernel already violated the 'exact' expiry by adding
 * slack to the expiry time to provide batched expiration. The granularity
 * levels provide implicit batching.
 *
 * This is an optimization of the original timer wheel implementation for the
 * majority of the timer wheel use cases: timeouts. The vast majority of
 * timeout timers (networking, disk I/O ...) are canceled before expiry. If
 * the timeout expires it indicates that normal operation is disturbed, so it
 * does not matter much whether the timeout comes with a slight delay.
 *
 * The only exception to this are networking timers with a small expiry
 * time. They rely on the granularity. Those fit into the first wheel level,
 * which has HZ granularity.
 *
 * We don't have cascading anymore. timers with a expiry time above the
 * capacity of the last wheel level are force expired at the maximum timeout
 * value of the last wheel level. From data sampling we know that the maximum
 * value observed is 5 days (network connection tracking), so this should not
 * be an issue.
 *
 * The currently chosen array constants values are a good compromise between
 * array size and granularity.
 *
 * This results in the following granularity and range levels:
 *
 * HZ 1000 steps
 * Level Offset  Granularity            Range
 *  0      0         1 ms                0 ms -         63 ms
 *  1     64         8 ms               64 ms -        511 ms
 *  2    128        64 ms              512 ms -       4095 ms (512ms - ~4s)
 *  3    192       512 ms             4096 ms -      32767 ms (~4s - ~32s)
 *  4    256      4096 ms (~4s)      32768 ms -     262143 ms (~32s - ~4m)
 *  5    320     32768 ms (~32s)    262144 ms -    2097151 ms (~4m - ~34m)
 *  6    384    262144 ms (~4m)    2097152 ms -   16777215 ms (~34m - ~4h)
 *  7    448   2097152 ms (~34m)  16777216 ms -  134217727 ms (~4h - ~1d)
 *  8    512  16777216 ms (~4h)  134217728 ms - 1073741822 ms (~1d - ~12d)
 *
 * HZ  300
 * Level Offset  Granularity            Range
 *  0      0         3 ms                0 ms -        210 ms
 *  1     64        26 ms              213 ms -       1703 ms (213ms - ~1s)
 *  2    128       213 ms             1706 ms -      13650 ms (~1s - ~13s)
 *  3    192      1706 ms (~1s)      13653 ms -     109223 ms (~13s - ~1m)
 *  4    256     13653 ms (~13s)    109226 ms -     873810 ms (~1m - ~14m)
 *  5    320    109226 ms (~1m)     873813 ms -    6990503 ms (~14m - ~1h)
 *  6    384    873813 ms (~14m)   6990506 ms -   55924050 ms (~1h - ~15h)
 *  7    448   6990506 ms (~1h)   55924053 ms -  447392423 ms (~15h - ~5d)
 *  8    512  55924053 ms (~15h) 447392426 ms - 3579139406 ms (~5d - ~41d)
 *
 * HZ  250
 * Level Offset  Granularity            Range
 *  0      0         4 ms                0 ms -        255 ms
 *  1     64        32 ms              256 ms -       2047 ms (256ms - ~2s)
 *  2    128       256 ms             2048 ms -      16383 ms (~2s - ~16s)
 *  3    192      2048 ms (~2s)      16384 ms -     131071 ms (~16s - ~2m)
 *  4    256     16384 ms (~16s)    131072 ms -    1048575 ms (~2m - ~17m)
 *  5    320    131072 ms (~2m)    1048576 ms -    8388607 ms (~17m - ~2h)
 *  6    384   1048576 ms (~17m)   8388608 ms -   67108863 ms (~2h - ~18h)
 *  7    448   8388608 ms (~2h)   67108864 ms -  536870911 ms (~18h - ~6d)
 *  8    512  67108864 ms (~18h) 536870912 ms - 4294967288 ms (~6d - ~49d)
 *
 * HZ  100
 * Level Offset  Granularity            Range
 *  0      0         10 ms               0 ms -        630 ms
 *  1     64         80 ms             640 ms -       5110 ms (640ms - ~5s)
 *  2    128        640 ms            5120 ms -      40950 ms (~5s - ~40s)
 *  3    192       5120 ms (~5s)     40960 ms -     327670 ms (~40s - ~5m)
 *  4    256      40960 ms (~40s)   327680 ms -    2621430 ms (~5m - ~43m)
 *  5    320     327680 ms (~5m)   2621440 ms -   20971510 ms (~43m - ~5h)
 *  6    384    2621440 ms (~43m) 20971520 ms -  167772150 ms (~5h - ~1d)
 *  7    448   20971520 ms (~5h) 167772160 ms - 1342177270 ms (~1d - ~15d)
 */

 

LVL_SHIFT() & LVL_GRAN()

kernel/time/timer.c

/* Clock divisor for the next level */
#define LVL_CLK_SHIFT   3
#define LVL_CLK_DIV     (1UL << LVL_CLK_SHIFT)
#define LVL_CLK_MASK    (LVL_CLK_DIV - 1)
#define LVL_SHIFT(n)    ((n) * LVL_CLK_SHIFT)
#define LVL_GRAN(n)     (1UL << LVL_SHIFT(n))
  • LVL_SHIFT(n)
    • 레벨별 비트 수
      • =n * 3
  • LVL_GRAN(n)
    • 레벨별 틱 단위
      • =2^(n*3) 틱
      • 예) 0 레벨 = 2^0 = 1 틱
      • 예) 1 레벨 = 2^3 = 8 틱
      • 예) 2 레벨 = 2^6 = 64 틱
      • 예) 3 레벨 = 2^6 = 512 틱
      • 예) 4 레벨 = 2^6 = 4096 틱
      • 예) 5 레벨 = 2^6 = 32768 틱
      • 예) 6 레벨 = 2^6 = 262144 틱
      • 예) 7 레벨 = 2^21=2097152 틱
      • 예) 8 레벨 = 2^24=16777216 틱

 

LVL_OFFS()

kernel/time/timer.c

/* Size of each clock level */
#define LVL_BITS        6
#define LVL_SIZE        (1UL << LVL_BITS)
#define LVL_MASK        (LVL_SIZE - 1)
#define LVL_OFFS(n)     ((n) * LVL_SIZE)
  • LVL_OFFS(n)
    • 레벨별 벡터 offset
      • = n * 64
      • 예) 0 레벨 = 0 * 64 = 0
      • 예) 1 레벨 = 1 * 64 = 64
      • 예) 2 레벨 = 2 * 64 = 128
      • 예) 3 레벨 = 3 * 64 = 192
      • 예) 4 레벨 = 4 * 64 = 256
      • 예) 5 레벨 = 5 * 64 = 320
      • 예) 6 레벨 = 6 * 64 = 384
      • 예) 7 레벨 = 7 * 64 = 448
      • 예) 8 레벨 = 8 * 64 = 512

 

LVL_START()

kernel/time/timer.c

/*
 * The time start value for each level to select the bucket at enqueue
 * time.
 */
#define LVL_START(n)    ((LVL_SIZE - 1) << (((n) - 1) * LVL_CLK_SHIFT))
  • 레벨별 시작 틱 값
    • = 63 << (n-1) * 3
      • 예) 레벨 0 = 63 << (0-1) * 3 = 0 틱
      • 예) 레벨 1 = 63 << (1-1) * 3 = 63 틱
      • 예) 레벨 2 = 63 << (2-1) * 3 = 504 틱
      • 예) 레벨 3 = 63 << (3-1) * 3 =4032 틱
      • 예) 레벨 4 = 63 << (4-1) * 3 =32256 틱
      • 예) 레벨 5 = 63 << (5-1) * 3 = 258048 틱
      • 예) 레벨 6 = 63 << (6-1) * 3 = 2064384 틱
      • 예) 레벨 7 = 63 << (7-1) * 3 = 16515072 틱
      • 예) 레벨 8 = 63 << (8-1) * 3 = 132120576 틱

 


타이머 베이스

타이머 베이스 락 획득

lock_timer_base()

kernel/time/timer.c

/*
 * We are using hashed locking: Holding per_cpu(timer_bases[x]).lock means
 * that all timers which are tied to this base are locked, and the base itself
 * is locked too.
 *
 * So __run_timers/migrate_timers can safely modify all timers which could
 * be found in the base->vectors array.
 *
 * When a timer is migrating then the TIMER_MIGRATING flag is set and we need
 * to wait until the migration is done.
 */
static struct timer_base *lock_timer_base(struct timer_list *timer,
                                          unsigned long *flags)
        __acquires(timer->base->lock)
{
        for (;;) {
                struct timer_base *base;
                u32 tf;

                /*
                 * We need to use READ_ONCE() here, otherwise the compiler
                 * might re-read @tf between the check for TIMER_MIGRATING
                 * and spin_lock().
                 */
                tf = READ_ONCE(timer->flags);

                if (!(tf & TIMER_MIGRATING)) {
                        base = get_timer_base(tf);
                        raw_spin_lock_irqsave(&base->lock, *flags);
                        if (timer->flags == tf)
                                return base;
                        raw_spin_unlock_irqrestore(&base->lock, *flags);
                }
                cpu_relax();
        }
}

타이머가 사용할 현재 cpu의 타이머 베이스 락을 획득한다.

  • 만일 타이머가 migration 중이면 TIMER_MIGRATING 플래그를 사용하는데 이 플래그가 없어질 때까지 루프를 반복하며 lock을 잡을 때까지 기다린다.
  • 또한 lock을 잡은 후에 플래그가 변경된 경우 루프를 반복한다.

 

타이머 베이스 알아오기

get_timer_base()

kernel/time/timer.c

static inline struct timer_base *get_timer_base(u32 tflags)
{
        return get_timer_cpu_base(tflags, tflags & TIMER_CPUMASK);
}

현재 cpu에 대한 타이머 베이스를 반환한다.

 

get_target_base()

kernel/time/timer.c

static inline struct timer_base *
get_target_base(struct timer_base *base, unsigned tflags)
{
#if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
        if (static_branch_likely(&timers_migration_enabled) &&
            !(tflags & TIMER_PINNED))
                return get_timer_cpu_base(tflags, get_nohz_timer_target());
#endif
        return get_timer_this_cpu_base(tflags);
}

동작할 cpu의 타이머 베이스를 반환한다.

  • 코드 라인 5~7에서 nohz 시스템에서 이주 가능한 타이머인 경우 현재 cpu가 idle 상태일 때 절전을 위해 타이머가 동작될 인접한 busy cpu를 찾아 해당 cpu의 타이머 베이스를 반환한다.
  • 코드 라인 9에서 현재 cpu의 타이머 베이스를 반환한다.

 

get_timer_cpu_base()

kernel/time/timer.c

static inline struct timer_base *get_timer_cpu_base(u32 tflags, u32 cpu)
{
        struct timer_base *base = per_cpu_ptr(&timer_bases[BASE_STD], cpu);

        /*
         * If the timer is deferrable and NO_HZ_COMMON is set then we need
         * to use the deferrable base.
         */
        if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && (tflags & TIMER_DEFERRABLE))
                base = per_cpu_ptr(&timer_bases[BASE_DEF], cpu);
        return base;
}

요청 cpu에 대한 타이머 베이스를 반환한다.

  • per-cpu로 관리되는 타이머 베이스는 nohz를 사용하는 경우 두 개로 나뉘어 관리되며 지연 가능한 타이머는 별도의 타이머 베이스를 사용한다.

 

get_timer_this_cpu_base()

kernel/time/timer.c

static inline struct timer_base *get_timer_this_cpu_base(u32 tflags)
{
        struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);

        /*
         * If the timer is deferrable and NO_HZ_COMMON is set then we need
         * to use the deferrable base.
         */
        if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && (tflags & TIMER_DEFERRABLE))
                base = this_cpu_ptr(&timer_bases[BASE_DEF]);
        return base;
}

현재 cpu에 대한 타이머 베이스를 반환한다.

  • per-cpu로 관리되는 타이머 베이스는 nohz를 사용하는 경우 두 개로 나뉘어 관리되며 지연 가능한 타이머는 별도의 타이머 베이스를 사용한다.

 

nohz 타이머용 cpu

get_nohz_timer_target()

kernel/sched/core.c

/*
 * In the semi idle case, use the nearest busy CPU for migrating timers
 * from an idle CPU.  This is good for power-savings.
 *
 * We don't do similar optimization for completely idle system, as
 * selecting an idle CPU will add more delays to the timers than intended
 * (as that CPU's timer base may not be uptodate wrt jiffies etc).
 */
int get_nohz_timer_target(void)
{
        int i, cpu = smp_processor_id();
        struct sched_domain *sd;

        if (!idle_cpu(cpu) && housekeeping_cpu(cpu, HK_FLAG_TIMER))
                return cpu;

        rcu_read_lock();
        for_each_domain(cpu, sd) {
                for_each_cpu(i, sched_domain_span(sd)) {
                        if (cpu == i)
                                continue;

                        if (!idle_cpu(i) && housekeeping_cpu(i, HK_FLAG_TIMER)) {
                                cpu = i;
                                goto unlock;
                        }
                }
        }

        if (!housekeeping_cpu(cpu, HK_FLAG_TIMER))
                cpu = housekeeping_any_cpu(HK_FLAG_TIMER);
unlock:
        rcu_read_unlock();
        return cpu;
}

절전을 위해 nohz 타이머를 위한 타겟 cpu를 알아온다.

  • 코드 라인 6~7에서 로컬 cpu가 busy 상태이면서 타이머에 대한 housekeeping이 가능하면 로컬 cpu를 반환한다.
  • 코드 라인 10~20에서 cpu가 속한 스케쥴 domain 수 만큼 순회하고 내부에서 순회중인 스케줄 도메인만큼 cpu를 순회하며 busy 상태이면서 타이머에 대한 housekeeping이 가능한 해당 cpu를 반환한다.
  • 코드 라인 22~23에서 찾지 못한 경우이다. cpu가 타이머에 대한 housekeeping이 불가능하면 housekeeping이 가능한 어떠한 cpu라도 찾아낸다.
  • 코드 라인 26에서 nohz 타이머로 사용할 cpu를 반환한다.

 


SOFTIRQ – TIMER 수행

lowres 타이머의 bottom-half로 동작하는 softirq 핸들러를 알아본다.

 

run_timer_softirq()

kernel/time/timer.c

/*
 * This function runs timers and the timer-tq in bottom half context.
 */
static __latent_entropy void run_timer_softirq(struct softirq_action *h)
{
        struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);

        __run_timers(base);
        if (IS_ENABLED(CONFIG_NO_HZ_COMMON))
                __run_timers(this_cpu_ptr(&timer_bases[BASE_DEF]));
}

로컬 cpu에 타이머 tick이 인입될 때 마다 호출되는 timer softirq에 등록한 함수이다. 만료된 타이머들의 해당 타이머 함수를 호출한다.

  • 코드 라인 5에서 스탠다드 타이머 베이스에서 만료된 타이머들의 해당 타이머 함수를 호출한다.
  • 코드 라인 6~7에서 deferrable 타이머 베이스에서 만료된 타이머들의 해당 타이머 함수를 호출한다.

 

__run_timers()

kernel/time/timer.c

/**
 * __run_timers - run all expired timers (if any) on this CPU.
 * @base: the timer vector to be processed.
 */
static inline void __run_timers(struct timer_base *base)
{
        struct hlist_head heads[LVL_DEPTH];
        int levels;

        if (!time_after_eq(jiffies, base->clk))
                return;

        timer_base_lock_expiry(base);
        raw_spin_lock_irq(&base->lock);

        /*
         * timer_base::must_forward_clk must be cleared before running
         * timers so that any timer functions that call mod_timer() will
         * not try to forward the base. Idle tracking / clock forwarding
         * logic is only used with BASE_STD timers.
         *
         * The must_forward_clk flag is cleared unconditionally also for
         * the deferrable base. The deferrable base is not affected by idle
         * tracking and never forwarded, so clearing the flag is a NOOP.
         *
         * The fact that the deferrable base is never forwarded can cause
         * large variations in granularity for deferrable timers, but they
         * can be deferred for long periods due to idle anyway.
         */
        base->must_forward_clk = false;

        while (time_after_eq(jiffies, base->clk)) {

                levels = collect_expired_timers(base, heads);
                base->clk++;

                while (levels--)
                        expire_timers(base, heads + levels);
        }
        raw_spin_unlock_irq(&base->lock);
        timer_base_unlock_expiry(base);
}

요청 타이머 베이스에서 만료된 타이머들의 함수를 실행한다.

  • 코드 라인 6~7에서 jiffies < base->clk 이다. 가장 가까운 타이머 시각이 아직 처리할 시각이 안되었으므로 함수를 빠져나간다.
  • 코드 라인 9~10에서 타이머 함수 호출 루틴이 동시에 호출되는 것을 막기 위해 lock을 획득한다. 또한 타이머 베이스를 조작하기 위한 lock도 획득한다.
  • 코드 라인 26에서 타이머 베이스에서 forward 처리를 하지 못하도록 막는다.
  • 코드 라인 28~35에서 타이머 베이스의 clk가 만료된 경우 다음과 같이 처리한다.
    • 만료된 타이머들을 heads 리스트에 모아온다. levels에는 최고 레벨이 저장된다.
    • 다음 루프 처리를 위해 타이머 베이스의 clk을 미리 증가시킨다.
    • 레벨별로 만료된 타이머에 해당하는 콜백 함수들을 호출한다.
  • 코드 라인 36~37에서 걸었던 lock들을 해제한다.

 

만료된 타이머들 수집

collect_expired_timers()

kernel/time/timer.c

static int collect_expired_timers(struct timer_base *base,
                                  struct hlist_head *heads)
{
        unsigned long now = READ_ONCE(jiffies);

        /*
         * NOHZ optimization. After a long idle sleep we need to forward the
         * base to current jiffies. Avoid a loop by searching the bitfield for
         * the next expiring timer.
         */
        if ((long)(now - base->clk) > 2) {
                unsigned long next = __next_timer_interrupt(base);

                /*
                 * If the next timer is ahead of time forward to current
                 * jiffies, otherwise forward to the next expiry time:
                 */
                if (time_after(next, now)) {
                        /*
                         * The call site will increment base->clk and then
                         * terminate the expiry loop immediately.
                         */
                        base->clk = now;
                        return 0;
                }
                base->clk = next;
        }
        return __collect_expired_timers(base, heads);
}

요청 타이머 베이스에서 만료된 타이머들을 @heads 리스트에 추가한다. levels에는 수집한 최고 레벨 + 1이 저장된다.

  • 코드 라인 11~27에서 nohz를 사용하면 타이머 베이스를 매 틱마다 처리하지 못할 수 있다. 이 때 3틱 이상 밀려 있는 상태면 매 틱마다 처리하지 않고 nohz optimization을 수행한다. 이 때 다음 타이머의 만료 시각이 현재 시각을 초과했는지 여부에 따라 다음과 같이 처리한다.
    • 아직 만료 시각이 남은 경우 지연된 base->clk을 현재 시각으로 갱신하고, 결과 값 0으로 함수를 빠져나간다.
    • 만료 시각이 지난 경우 base->clk에 다음 타이머 만료 시각을 대입하고, 다음 루틴을 계속 처리한다.
  • 코드 라인 28에서 만료된 타이머 함수들을 @heds 리스트로 수집한다.

 

__collect_expired_timers()

kernel/time/timer.c

static int __collect_expired_timers(struct timer_base *base,
                                    struct hlist_head *heads)
{
        unsigned long clk = base->clk;
        struct hlist_head *vec;
        int i, levels = 0;
        unsigned int idx;

        for (i = 0; i < LVL_DEPTH; i++) {
                idx = (clk & LVL_MASK) + i * LVL_SIZE;

                if (__test_and_clear_bit(idx, base->pending_map)) {
                        vec = base->vectors + idx;
                        hlist_move_list(vec, heads++);
                        levels++;
                }
                /* Is it time to look at the next level? */
                if (clk & LVL_CLK_MASK)
                        break;
                /* Shift clock for the next level granularity */
                clk >>= LVL_CLK_SHIFT;
        }
        return levels;
}

요청 타이머 베이스에서 만료된 타이머들을 @heads 리스트로 수집한다.

  • 코드 라인 9~17에서 0~마지막 레벨(100hz 이하에서 8, 초과 시 9)까지 순회하며 타이머 휠 인덱스 값에 해당하는 펜딩 맵에 비트가 설정된 경우 이를 클리어하고, 이에 해당하는 인덱스의 타이머 휠 벡터 리스트를 heads 리스트에 옮기고, 반환할 levels를 1 증가시킨다.
  • 코드 라인 19~23에서 다음 레벨을 처리할 필요가 없는 경우 루프를 벗어난다. 그렇지 않고 남은 경우 다음 레벨을 처리하기 위해 clk을 3 비트 쉬프트한다.
    • clk 값의 해당 레벨의 6비트 중 하위 3비트 값이 0인 경우 다음 레벨로 넘어간다.
  • 코드 라인 24에서 수집한 최고 레벨 + 1 값을 반환한다.

 

다음 그림은 base->clk 값에 의해 호출되는 레벨별 타이머 벡터 리스트들을 보여준다.

  • 레벨별로 쉬프트된 6bit clk 값의 하위 3비트가 0인 경우 다음 레벨도 처리하기 위해 이동한다.
  • clk 값이 0x4400 값인 경우 lvl 0 ~ lvl 3까지 이동하며 각 레벨의 6bit clk값 + lvl * 64로 idx를 산출한다. 그런 후 idx에 해당하는 펜딩맵이 설정된 벡터 리스트의 타이머들이 선택된다.

 

만료된 타이머들의 콜백 함수 호출

expire_timers()

kernel/time/timer.c

static void expire_timers(struct timer_base *base, struct hlist_head *head)
{
        /*
         * This value is required only for tracing. base->clk was
         * incremented directly before expire_timers was called. But expiry
         * is related to the old base->clk value.
         */
        unsigned long baseclk = base->clk - 1;

        while (!hlist_empty(head)) {
                struct timer_list *timer;
                void (*fn)(struct timer_list *);

                timer = hlist_entry(head->first, struct timer_list, entry);

                base->running_timer = timer;
                detach_timer(timer, true);

                fn = timer->function;

                if (timer->flags & TIMER_IRQSAFE) {
                        raw_spin_unlock(&base->lock);
                        call_timer_fn(timer, fn, baseclk);
                        base->running_timer = NULL;
                        raw_spin_lock(&base->lock);
                } else {
                        raw_spin_unlock_irq(&base->lock);
                        call_timer_fn(timer, fn, baseclk);
                        base->running_timer = NULL;
                        timer_sync_wait_running(base);
                        raw_spin_lock_irq(&base->lock);
                }
        }
}

@head 리스트의 만료된 타이머들의 콜백 함수들을 호출한다.

  • 호출하는 동안 base->running_timer에 타이머가 기록되고, 호출이 완료되면 null이 대입된다.
  • TIMER_IRQSAFE 플래그를 사용하면 인터럽트를 disable하지 않은 상태에서 spinlock을 사용한다.

 

타이머 콜백 함수 호출

call_timer_fn()

kernel/time/timer.c

static void call_timer_fn(struct timer_list *timer,
                          void (*fn)(struct timer_list *),
                          unsigned long baseclk)
{
        int count = preempt_count();

#ifdef CONFIG_LOCKDEP
        /*
         * It is permissible to free the timer from inside the
         * function that is called from it, this we need to take into
         * account for lockdep too. To avoid bogus "held lock freed"
         * warnings as well as problems when looking into
         * timer->lockdep_map, make a copy and use that here.
         */
        struct lockdep_map lockdep_map;

        lockdep_copy_map(&lockdep_map, &timer->lockdep_map);
#endif
        /*
         * Couple the lock chain with the lock chain at
         * del_timer_sync() by acquiring the lock_map around the fn()
         * call here and in del_timer_sync().
         */
        lock_map_acquire(&lockdep_map);

        trace_timer_expire_entry(timer, baseclk);
        fn(timer);
        trace_timer_expire_exit(timer);

        lock_map_release(&lockdep_map);

        if (count != preempt_count()) {
                WARN_ONCE(1, "timer: %pS preempt leak: %08x -> %08x\n",
                          fn, count, preempt_count());
                /*
                 * Restore the preempt count. That gives us a decent
                 * chance to survive and extract information. If the
                 * callback kept a lock held, bad luck, but not worse
                 * than the BUG() we had.
                 */
                preempt_count_set(count);
        }
}

인수로 받은 fn은 타이머에 설정된 함수이다. 디버그를 위해 trace 출력등이 사용되었다.

 


타이머 설정

컴파일 타임에 정적 타이머 생성 및 초기화

include/linux/timer.h

DEFINE_TIMER()

#define DEFINE_TIMER(_name, _function)                          \
        struct timer_list _name =                               \
                __TIMER_INITIALIZER(_function, 0)

컴파일 타임에 타이머를 초기화한다. 인자로 타이머 이름과 콜백 함수를 지정한다.

  • 플래그는 사용하지 않고 0을 전달한다.

 

__TIMER_INITIALIZER()

include/linux/timer.h

#define __TIMER_INITIALIZER(_function, _flags) {                \
                .entry = { .next = TIMER_ENTRY_STATIC },        \
                .function = (_function),                        \
                .flags = (_flags),                              \
                __TIMER_LOCKDEP_MAP_INITIALIZER(                \
                        __FILE__ ":" __stringify(__LINE__))     \
        }

 

런타임에 동적 타이머 생성 및 초기화

timer_setup()

include/linux/timer.h

/**
 * timer_setup - prepare a timer for first use
 * @timer: the timer in question
 * @callback: the function to call when timer expires
 * @flags: any TIMER_* flags
 *
 * Regular timer initialization should use either DEFINE_TIMER() above,
 * or timer_setup(). For timers on the stack, timer_setup_on_stack() must
 * be used and must be balanced with a call to destroy_timer_on_stack().
 */
#define timer_setup(timer, callback, flags)                     \
        __init_timer((timer), (callback), (flags))

타이머를 사용하기 위해 준비한다. 인자로는 타이머와 콜백 함수 및 플래그를 지정한다.

 

__init_timer()

include/linux/timer.h

#define __init_timer(_timer, _fn, _flags)                               \
        init_timer_key((_timer), (_fn), (_flags), NULL, NULL)

 

timer_setup_on_stack()

include/linux/timer.h

#define setup_timer_on_stack(timer, callback, flags)                    \
        __init_timer_on_stack((timer), (callback), (flags))

타이머를 전달 받은 인자들로 초기화한다.

 

__init_timer_on_stack()

include/linux/timer.h

#define __init_timer_on_stack(_timer, _fn, _flags)                           \
        init_timer_on_stack_key((_timer), (_fn), (_flags), NULL, NULL)

 

init_timer_on_stack_key()

include/linux/timer.h

static inline void init_timer_on_stack_key(struct timer_list *timer,
                                           void (*func)(struct timer_list *),
                                           unsigned int flags, 
                                           const char *name,
                                           struct lock_class_key *key)
{
        init_timer_key(timer, func, flags, name, key);
}

 

init_timer_key()

kernel/time/timer.c

/**
 * init_timer_key - initialize a timer
 * @timer: the timer to be initialized
 * @func: timer callback function
 * @flags: timer flags
 * @name: name of the timer
 * @key: lockdep class key of the fake lock used for tracking timer
 *       sync lock dependencies
 *
 * init_timer_key() must be done to a timer prior calling *any* of the
 * other timer functions.
 */
void init_timer_key(struct timer_list *timer, 
                    void (*func)(struct timer_list *), unsigned int flags,
                    const char *name, struct lock_class_key *key)
{
        debug_init(timer);
        do_init_timer(timer, func, flags, name, key);
}
EXPORT_SYMBOL(init_timer_key);

타이머를 전달 받은 인자들로 초기화한다.

 

do_init_timer()

kernel/time/timer.c

static void do_init_timer(struct timer_list *timer,
                          void (*func)(struct timer_list *),
                          unsigned int flags,
                          const char *name, struct lock_class_key *key)
{
        timer->entry.pprev = NULL;
        timer->function = func;
        timer->flags = flags | raw_smp_processor_id();
        lockdep_init_map(&timer->lockdep_map, name, key, 0);
}

타이머를 전달 받은 인자들로 초기화한다.

 


초기화

init_timers()

kernel/time/timer.c

void __init init_timers(void)
{
        init_timer_cpus();
        open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
}

타이머를 사용할 수 있도록 초기화한다.

  • 코드 라인 3에서 cpu별로 타이머를 사용할 수 있도록 초기화한다.
  • 코드 라인 4에서 타이머 softirq를 열고 run_timer_softirq() 함수가 호출되도록 준비한다.

 

init_timer_cpus()

kernel/time/timer.c

static void __init init_timer_cpus(void)
{
        int cpu;

        for_each_possible_cpu(cpu)
                init_timer_cpu(cpu);
}

모든 possible cpu에 대해 타이머를 사용할 수 있도록 초기화한다.

 

init_timer_cpu()

kernel/time/timer.c

static void __init init_timer_cpu(int cpu)
{
        struct timer_base *base;
        int i;

        for (i = 0; i < NR_BASES; i++) {
                base = per_cpu_ptr(&timer_bases[i], cpu);
                base->cpu = cpu;
                raw_spin_lock_init(&base->lock);
                base->clk = jiffies;
                timer_base_init_expiry_lock(base);
        }
}

요청 cpu에 대해 타이머를 사용할 수 있도록 초기화한다.

 


기존 커널 관련 (~ 커널 v4.7)

 

기존 cascading wheel 구조

요청된 타이머의 관리를 위해 cpu 마다 타이머휠이 사용된다. 타이머 휠마다 tv1 ~ tv5까지의 5개 벡터로 나누어 관리하며, 각 벡터는 각각 256, 64, 64, 64, 64개의 리스트로 이루어진다.

  • CONFIG_BASE_SMALL 사용 시 커널 사이즈를 최소화하기 위해 리스트들은 각각 64, 16, 16, 16, 16개로 1/4로 줄어든다.

 

다음 그림은 타이머 벡터 간의 cascade 조건을 보여준다.

예) base->timer_jiffies 값이 0x400_0000인 경우 tv2 -> tv1, tv3 -> tv2, tv4 -> tv3, tv5 -> tv4의 순서로 full cascade 처리된다.

 

다음 그림은 만료시각이 다른 각종 타이머들을 추가했을 때 타이머휠에 등록된 타이머 상태들을 보여준다.

 

  • expires – base->timer_jiffies 하여 산출된 tick 값에 따라 512(256+64+64+64+64)개의 리스트 중 하나가 선택된다.
    • 예) timer_jiffies=100, hz=100, 7초 후에 timer가 호출되게 하려할 때 expires 값과 추가될 타이머 벡터 리스트는?
      • 7초면 700 tick이 필요하므로 expires=100+700이 대입되고 tv2.vec[32]에 추가된다.

 

다음 그림은 jiffies값이 35인 시점에 cpu#1에 tick이 발생하고 그 동안 tick이 발생하지 못해 처리 못했던 jiffies 들에 대한 처리를 한꺼번에 처리하도록 한다.

  • jiffies=35인 시점에 26~35까지 처리 못했던 만료된 타이머들의 함수들을 처리한다.

 

다음 그림은 tv2에 있는 두 개의 타이머가 tv1으로 cascade되는 모습을 보여준다.

 

Slack으로 만료 시간 조정

만료 시간을 조정하여 유사한 만료 시간들끼리 모아 한꺼번에 처리하도록 slack 정렬한다. 타이머 인터럽트가 조금이라도 덜 발생하도록하여 절전과 처리 성능에 도움을 준다.

 

아래 그림은 100hz 시스템에서 slack으로 9 tick을 준 경우와 timeout으로 2311 tick을 준 경우에 대해 만료 값이 어떻게 변화하는지를 보여준다.

 

커널 v4.7까지에서는 타이머 리스트가 disable 된 상태에 있기 때문에 아래와 같이 inactive 출력된다.

$ cat /proc/timer_stats
Timer Stats Version: v0.3
Sample period: 0.000 s
Collection: inactive
0 total events

 

다음과 같이 enable 시킨다.

# echo "1" > /proc/timer_stats

 

그런 후 출력이 가능하다. (lowres 타이머와 hrtimer가 섞여 있다)

# cat /proc/timer_stats
Timer Stats Version: v0.3
Sample period: 4.530 s
Collection: active
  453,     0 swapper/2        hrtimer_start_range_ns (tick_sched_timer)
   38,     0 swapper/0        hrtimer_start_range_ns (tick_sched_timer)
   50,     7 rcu_preempt      rcu_gp_kthread (process_timeout)
    5,  1632 ifplugd          hrtimer_start_range_ns (hrtimer_wakeup)
   16,     0 swapper/1        hrtimer_start (tick_sched_timer)
   36,     0 swapper/0        hrtimer_start (tick_sched_timer)
    4,  3992 sshd             sk_reset_timer (tcp_write_timer)
   17,     0 swapper/1        usb_hcd_poll_rh_status (rh_timer_func)
    4,  1582 ifplugd          hrtimer_start_range_ns (hrtimer_wakeup)
    4,  2230 ntpd             hrtimer_start_range_ns (posix_timer_fn)
    4,  3467 kworker/u8:2     queue_delayed_work_on (delayed_work_timer_fn)
   10,     0 swapper/0        sk_reset_timer (tcp_delack_timer)
    3, 26686 kworker/2:1      queue_delayed_work_on (delayed_work_timer_fn)
    3,    44 kworker/0:1      queue_delayed_work_on (delayed_work_timer_fn)
    1,  2058 thd              hrtimer_start_range_ns (hrtimer_wakeup)
   25,     0 swapper/1        hrtimer_start_range_ns (tick_sched_timer)
673 total events, 148.565 events/sec

 

참고