// SPDX-License-Identifier: GPL-2.0
/*
* dpll_core.c - DPLL subsystem kernel-space interface implementation.
*
* Copyright (c) 2023 Meta Platforms, Inc. and affiliates
* Copyright (c) 2023 Intel Corporation.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/device.h>
#include <linux/err.h>
#include <linux/idr.h>
#include <linux/module.h>
#include <linux/property.h>
#include <linux/slab.h>
#include <linux/string.h>
#include "dpll_core.h"
#include "dpll_netlink.h"
/* Mutex lock to protect DPLL subsystem devices and pins */
DEFINE_MUTEX(dpll_lock);
DEFINE_XARRAY_FLAGS(dpll_device_xa, XA_FLAGS_ALLOC);
DEFINE_XARRAY_FLAGS(dpll_pin_xa, XA_FLAGS_ALLOC);
static RAW_NOTIFIER_HEAD(dpll_notifier_chain);
static DEFINE_IDA(dpll_pin_idx_ida);
static u32 dpll_device_xa_id;
static u32 dpll_pin_xa_id;
#define ASSERT_DPLL_REGISTERED(d) \
WARN_ON_ONCE(!xa_get_mark(&dpll_device_xa, (d)->id, DPLL_REGISTERED))
#define ASSERT_DPLL_NOT_REGISTERED(d) \
WARN_ON_ONCE(xa_get_mark(&dpll_device_xa, (d)->id, DPLL_REGISTERED))
#define ASSERT_DPLL_PIN_REGISTERED(p) \
WARN_ON_ONCE(!xa_get_mark(&dpll_pin_xa, (p)->id, DPLL_REGISTERED))
struct dpll_device_registration {
struct list_head list;
const struct dpll_device_ops *ops;
void *priv;
dpll_tracker tracker;
};
struct dpll_pin_registration {
struct list_head list;
const struct dpll_pin_ops *ops;
void *priv;
void *cookie;
dpll_tracker tracker;
};
static int call_dpll_notifiers(unsigned long action, void *info)
{
lockdep_assert_held(&dpll_lock);
return raw_notifier_call_chain(&dpll_notifier_chain, action, info);
}
void dpll_device_notify(struct dpll_device *dpll, unsigned long action)
{
struct dpll_device_notifier_info info = {
.dpll = dpll,
.id = dpll->id,
.idx = dpll->device_idx,
.clock_id = dpll->clock_id,
.type = dpll->type,
};
call_dpll_notifiers(action, &info);
}
void dpll_pin_notify(struct dpll_pin *pin, u64 src_clock_id,
unsigned long action)
{
struct dpll_pin_notifier_info info = {
.pin = pin,
.id = pin->id,
.idx = pin->pin_idx,
.clock_id = pin->clock_id,
.fwnode = pin->fwnode,
.prop = &pin->prop,
.src_clock_id = src_clock_id,
};
call_dpll_notifiers(action, &info);
}
static void dpll_device_tracker_alloc(struct dpll_device *dpll,
dpll_tracker *tracker)
{
#ifdef CONFIG_DPLL_REFCNT_TRACKER
ref_tracker_alloc(&dpll->refcnt_tracker, tracker, GFP_KERNEL);
#endif
}
static void dpll_device_tracker_free(struct dpll_device *dpll,
dpll_tracker *tracker)
{
#ifdef CONFIG_DPLL_REFCNT_TRACKER
ref_tracker_free(&dpll->refcnt_tracker, tracker);
#endif
}
static void __dpll_device_hold(struct dpll_device *dpll, dpll_tracker *tracker)
{
dpll_device_tracker_alloc(dpll, tracker);
refcount_inc(&dpll->refcount);
}
static void __dpll_device_put(struct dpll_device