#include <linux/acpi.h>
#include <linux/bitfield.h>
#include <linux/build_bug.h>
#include <linux/cpu_pm.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/device.h>
#include <linux/io.h>
#include <linux/err.h>
#include <linux/export.h>
#include <linux/slab.h>
#include <linux/stringhash.h>
#include <linux/mutex.h>
#include <linux/clk.h>
#include <linux/coresight.h>
#include <linux/property.h>
#include <linux/delay.h>
#include <linux/pm_runtime.h>
#include <linux/panic_notifier.h>
#include "coresight-etm-perf.h"
#include "coresight-priv.h"
#include "coresight-syscfg.h"
#include "coresight-trace-id.h"
DEFINE_MUTEX(coresight_mutex);
static DEFINE_PER_CPU(struct coresight_device *, csdev_sink);
static DEFINE_RAW_SPINLOCK(coresight_dev_lock);
static DEFINE_PER_CPU(struct coresight_device *, csdev_source);
static DEFINE_PER_CPU(bool, percpu_pm_failed);
struct coresight_node {
struct coresight_device *csdev;
struct list_head link;
};
const u32 coresight_barrier_pkt[4] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff};
EXPORT_SYMBOL_GPL(coresight_barrier_pkt);
static LIST_HEAD(coresight_dev_idx_list);
static const struct cti_assoc_op *cti_assoc_ops;
static struct coresight_node *
coresight_path_first_node(struct coresight_path *path)
{
if (list_empty(&path->path_list))
return NULL;
return list_first_entry(&path->path_list, struct coresight_node, link);
}
static struct coresight_node *
coresight_path_last_node(struct coresight_path *path)
{
if (list_empty(&path->path_list))
return NULL;
return list_last_entry(&path->path_list, struct coresight_node, link);
}
void coresight_set_cti_ops(const struct cti_assoc_op *cti_op)
{
cti_assoc_ops = cti_op;
}
EXPORT_SYMBOL_GPL(coresight_set_cti_ops);
void coresight_remove_cti_ops(void)
{
cti_assoc_ops = NULL;
}
EXPORT_SYMBOL_GPL(coresight_remove_cti_ops);
void coresight_set_percpu_sink(int cpu, struct coresight_device *csdev)
{
per_cpu(csdev_sink, cpu) = csdev;
}
EXPORT_SYMBOL_GPL(coresight_set_percpu_sink);
struct coresight_device *coresight_get_percpu_sink(int cpu)
{
return per_cpu(csdev_sink, cpu);
}
EXPORT_SYMBOL_GPL(coresight_get_percpu_sink);
static void coresight_set_percpu_source(struct coresight_device *csdev)
{
if (!csdev || !coresight_is_percpu_source(csdev))
return;
guard(raw_spinlock_irqsave)(&coresight_dev_lock);
WARN_ON(per_cpu(csdev_source, csdev->cpu));
per_cpu(csdev_source, csdev->cpu) = csdev;
}
static void coresight_clear_percpu_source(struct coresight_device *csdev)
{
if (!csdev || !coresight_is_percpu_source(csdev))
return;
per_cpu(percpu_pm_failed, csdev->cpu) = false;
guard(raw_spinlock_irqsave)(&coresight_dev_lock);
WARN_ON(per_cpu(csdev_source, csdev->cpu) != csdev);
per_cpu(csdev_source, csdev->cpu) = NULL;
}
struct coresight_device *coresight_get_percpu_source_ref(int cpu)
{
struct coresight_device *csdev;
if (WARN_ON(cpu < 0))
return NULL;
guard(raw_spinlock_irqsave)(&coresight_dev_lock);
csdev = per_cpu(csdev_source, cpu);
if (!csdev)
return NULL;
get_device(&csdev->dev);
return csdev;
}
void coresight_put_percpu_source_ref(struct coresight_device *csdev)
{
if (!csdev || !coresight_is_percpu_source(csdev))
return;
guard(raw_spinlock_irqsave)(&coresight_dev_lock);
put_device(&csdev->dev);
}
struct coresight_device *coresight_get_source(struct coresight_path *path)
{
struct coresight_device *csdev;
struct coresight_node *nd;
if (!path)
return NULL;
nd = coresight_path_first_node(path);
if (!nd)
return NULL;
csdev = nd->csdev;
if (!coresight_is_device_source(csdev))
return NULL;
return csdev;
}
static bool coresight_blocks_source(struct coresight_device *src,
struct coresight_connection *conn)
{
return conn->filter_src_fwnode && (conn->filter_src_dev != src);
}
static struct coresight_connection *
coresight_find_out_connection(struct coresight_device *csdev,
struct coresight_device *out_dev,
struct coresight_device *trace_src)
{
int i;
struct coresight_connection *conn;
for (i = 0; i < csdev->pdata->nr_outconns; i++) {
conn = csdev->pdata->out_conns[i];
if (coresight_blocks_source(trace_src, conn))
continue;
if (conn->dest_dev == out_dev)
return conn;
}
dev_err(&csdev->dev,
"couldn't find output connection, csdev: %s, out_dev: %s\n",
dev_name(&csdev->dev), dev_name(&out_dev->dev));
return ERR_PTR(-ENODEV);
}
static u32 coresight_read_claim_tags_unlocked(struct coresight_device *csdev)
{
return FIELD_GET(CORESIGHT_CLAIM_MASK,
csdev_access_relaxed_read32(&csdev->access, CORESIGHT_CLAIMCLR));
}
static void coresight_set_self_claim_tag_unlocked(struct coresight_device *csdev)
{
csdev_access_relaxed_write32(&csdev->access, CORESIGHT_CLAIM_SELF_HOSTED,
CORESIGHT_CLAIMSET);
isb();
}
void coresight_clear_self_claim_tag(struct csdev_access *csa)
{
if (csa->io_mem)
CS_UNLOCK(csa->base);
coresight_clear_self_claim_tag_unlocked(csa);
if (csa->io_mem)
CS_LOCK(csa->base);
}
EXPORT_SYMBOL_GPL(coresight_clear_self_claim_tag);
void coresight_clear_self_claim_tag_unlocked(struct csdev_access *csa)
{
csdev_access_relaxed_write32(csa, CORESIGHT_CLAIM_SELF_HOSTED,
CORESIGHT_CLAIMCLR);
isb();
}
EXPORT_SYMBOL_GPL(coresight_clear_self_claim_tag_unlocked);
int coresight_claim_device_unlocked(struct coresight_device *csdev)
{
int tag;
struct csdev_access *csa;
if (WARN_ON(!csdev))
return -EINVAL;
csa = &csdev->access;
tag = coresight_read_claim_tags_unlocked(csdev);
switch (tag) {
case CORESIGHT_CLAIM_FREE:
coresight_set_self_claim_tag_unlocked(csdev);
if (coresight_read_claim_tags_unlocked(csdev) == CORESIGHT_CLAIM_SELF_HOSTED)
return 0;
coresight_clear_self_claim_tag_unlocked(csa);
dev_dbg(&csdev->dev, "Busy: Couldn't set self claim tag");
return -EBUSY;
case CORESIGHT_CLAIM_EXTERNAL:
dev_dbg(&csdev->dev, "Busy: Claimed by external debugger");
return -EBUSY;
default:
case CORESIGHT_CLAIM_SELF_HOSTED:
case CORESIGHT_CLAIM_INVALID:
dev_err_once(&csdev->dev, "Invalid claim tag state: %x", tag);
return -EBUSY;
}
}
EXPORT_SYMBOL_GPL(coresight_claim_device_unlocked);
int coresight_claim_device(struct coresight_device *csdev)
{
int rc;
if (WARN_ON(!csdev))
return -EINVAL;
CS_UNLOCK(csdev->access.base);
rc = coresight_claim_device_unlocked(csdev);
CS_LOCK(csdev->access.base);
return rc;
}
EXPORT_SYMBOL_GPL(coresight_claim_device);
void coresight_disclaim_device_unlocked(struct coresight_device *csdev)
{
if (WARN_ON(!csdev))
return;
if (coresight_read_claim_tags_unlocked(csdev) == CORESIGHT_CLAIM_SELF_HOSTED)
coresight_clear_self_claim_tag_unlocked(&csdev->access);
else
dev_WARN_ONCE(&csdev->dev, 1, "External agent took claim tag");
}
EXPORT_SYMBOL_GPL(coresight_disclaim_device_unlocked);
void coresight_disclaim_device(struct coresight_device *csdev)
{
if (WARN_ON(!csdev))
return;
CS_UNLOCK(csdev->access.base);
coresight_disclaim_device_unlocked(csdev);
CS_LOCK(csdev->access.base);
}
EXPORT_SYMBOL_GPL(coresight_disclaim_device);
void coresight_add_helper(struct coresight_device *csdev,
struct coresight_device *helper)
{
int i;
struct coresight_connection conn = {};
struct coresight_connection *new_conn;
mutex_lock(&coresight_mutex);
conn.dest_fwnode = fwnode_handle_get(dev_fwnode(&helper->dev));
conn.dest_dev = helper;
conn.dest_port = conn.src_port = -1;
conn.src_dev = csdev;
for (i = 0; i < csdev->pdata->nr_outconns; ++i)
if (csdev->pdata->out_conns[i]->dest_fwnode == conn.dest_fwnode)
goto unlock;
new_conn = coresight_add_out_conn(csdev->dev.parent, csdev->pdata,
&conn);
if (!IS_ERR(new_conn))
coresight_add_in_conn(new_conn);
unlock:
mutex_unlock(&coresight_mutex);
}
EXPORT_SYMBOL_GPL(coresight_add_helper);
static int coresight_enable_sink(struct coresight_device *csdev,
enum cs_mode mode,
struct coresight_path *path)
{
return sink_ops(csdev)->enable(csdev, mode, path);
}
static void coresight_disable_sink(struct coresight_device *csdev)
{
sink_ops(csdev)->disable(csdev);
}
static int coresight_enable_link(struct coresight_device *csdev,
struct coresight_device *parent,
struct coresight_device *child,
struct coresight_device *source)
{
int link_subtype;
struct coresight_connection *inconn, *outconn;
if (!parent || !child)
return -EINVAL;
inconn = coresight_find_out_connection(parent, csdev, source);
outconn = coresight_find_out_connection(csdev, child, source);
link_subtype = csdev->subtype.link_subtype;
if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG && IS_ERR(inconn))
return PTR_ERR(inconn);
if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT && IS_ERR(outconn))
return PTR_ERR(outconn);
return link_ops(csdev)->enable(csdev, inconn, outconn);
}
static void coresight_disable_link(struct coresight_device *csdev,
struct coresight_device *parent,
struct coresight_device *child,
struct coresight_device *source)
{
struct coresight_connection *inconn, *outconn;
if (!parent || !child)
return;
inconn = coresight_find_out_connection(parent, csdev, source);
outconn = coresight_find_out_connection(csdev, child, source);
link_ops(csdev)->disable(csdev, inconn, outconn);
}
static bool coresight_is_helper(struct coresight_device *csdev)
{
return csdev->type == CORESIGHT_DEV_TYPE_HELPER;
}
static int coresight_enable_helper(struct coresight_device *csdev,
enum cs_mode mode,
struct coresight_path *path)
{
return helper_ops(csdev)->enable(csdev, mode, path);
}
static void coresight_disable_helper(struct coresight_device *csdev,
struct coresight_path *path)
{
helper_ops(csdev)->disable(csdev, path);
}
static void coresight_disable_helpers(struct coresight_device *csdev,
struct coresight_path *path)
{
int i;
struct coresight_device *helper;
for (i = 0; i < csdev->pdata->nr_outconns; ++i) {
helper = csdev->pdata->out_conns[i]->dest_dev;
if (helper && coresight_is_helper(helper))
coresight_disable_helper(helper, path);
}
}
int coresight_enable_source(struct coresight_device *csdev,
struct perf_event *event, enum cs_mode mode,
struct coresight_path *path)
{
int ret;
if (!coresight_is_device_source(csdev))
return -EINVAL;
ret = source_ops(csdev)->enable(csdev, event, mode, path);
if (ret)
return ret;
if (!coresight_is_percpu_source(csdev))
csdev->path = path;
return 0;
}
void coresight_disable_source(struct coresight_device *csdev, void *data)
{
if (!coresight_is_device_source(csdev))
return;
if (!coresight_is_percpu_source(csdev))
csdev->path = NULL;
source_ops(csdev)->disable(csdev, data);
}
EXPORT_SYMBOL_GPL(coresight_disable_source);
void coresight_pause_source(struct coresight_device *csdev)
{
if (!coresight_is_percpu_source(csdev))
return;
if (source_ops(csdev)->pause_perf)
source_ops(csdev)->pause_perf(csdev);
}
EXPORT_SYMBOL_GPL(coresight_pause_source);
int coresight_resume_source(struct coresight_device *csdev)
{
if (!coresight_is_percpu_source(csdev))
return -EOPNOTSUPP;
if (!source_ops(csdev)->resume_perf)
return -EOPNOTSUPP;
return source_ops(csdev)->resume_perf(csdev);
}
EXPORT_SYMBOL_GPL(coresight_resume_source);
static bool coresight_path_nodes_in_order(struct coresight_path *path,
struct coresight_node *from,
struct coresight_node *to)
{
struct coresight_node *nd;
if (WARN_ON_ONCE(!from || !to))
return false;
nd = from;
list_for_each_entry_from(nd, &path->path_list, link) {
if (nd == to)
return true;
}
return false;
}
static void coresight_disable_path_from_to(struct coresight_path *path,
struct coresight_node *from,
struct coresight_node *to)
{
u32 type;
struct coresight_device *csdev, *parent, *child;
struct coresight_node *nd;
if (!coresight_path_nodes_in_order(path, from, to))
return;
nd = from;
list_for_each_entry_from(nd, &path->path_list, link) {
csdev = nd->csdev;
type = csdev->type;
if (type == CORESIGHT_DEV_TYPE_LINKSINK)
type = (csdev == coresight_get_sink(path)) ?
CORESIGHT_DEV_TYPE_SINK :
CORESIGHT_DEV_TYPE_LINK;
switch (type) {
case CORESIGHT_DEV_TYPE_SINK:
coresight_disable_sink(csdev);
break;
case CORESIGHT_DEV_TYPE_SOURCE:
break;
case CORESIGHT_DEV_TYPE_LINK:
parent = list_prev_entry(nd, link)->csdev;
child = list_next_entry(nd, link)->csdev;
coresight_disable_link(csdev, parent, child,
coresight_get_source(path));
break;
default:
break;
}
coresight_disable_helpers(csdev, path);
if (nd == to)
break;
}
}
void coresight_disable_path(struct coresight_path *path)
{
coresight_disable_path_from_to(path,
coresight_path_first_node(path),
coresight_path_last_node(path));
}
EXPORT_SYMBOL_GPL(coresight_disable_path);
static int coresight_enable_helpers(struct coresight_device *csdev,
enum cs_mode mode,
struct coresight_path *path)
{
int i, ret = 0;
struct coresight_device *helper;
for (i = 0; i < csdev->pdata->nr_outconns; ++i) {
helper = csdev->pdata->out_conns[i]->dest_dev;
if (!helper || !coresight_is_helper(helper))
continue;
ret = coresight_enable_helper(helper, mode, path);
if (ret)
goto err;
}
return 0;
err:
while (i--) {
helper = csdev->pdata->out_conns[i]->dest_dev;
if (helper && coresight_is_helper(helper))
coresight_disable_helper(helper, path);
}
return ret;
}
static int coresight_enable_path_from_to(struct coresight_path *path,
enum cs_mode mode,
struct coresight_node *from,
struct coresight_node *to)
{
int ret = 0;
u32 type;
struct coresight_node *nd;
struct coresight_device *csdev, *parent, *child;
if (!coresight_path_nodes_in_order(path, from, to))
return -EINVAL;
nd = to;
list_for_each_entry_from_reverse(nd, &path->path_list, link) {
csdev = nd->csdev;
type = csdev->type;
ret = coresight_enable_helpers(csdev, mode, path);
if (ret)
goto err_disable_path;
if (type == CORESIGHT_DEV_TYPE_LINKSINK)
type = (csdev == coresight_get_sink(path)) ?
CORESIGHT_DEV_TYPE_SINK :
CORESIGHT_DEV_TYPE_LINK;
switch (type) {
case CORESIGHT_DEV_TYPE_SINK:
ret = coresight_enable_sink(csdev, mode, path);
if (ret) {
coresight_disable_helpers(csdev, path);
goto out;
}
break;
case CORESIGHT_DEV_TYPE_SOURCE:
break;
case CORESIGHT_DEV_TYPE_LINK:
parent = list_prev_entry(nd, link)->csdev;
child = list_next_entry(nd, link)->csdev;
ret = coresight_enable_link(csdev, parent, child,
coresight_get_source(path));
if (ret)
goto err_disable_helpers;
break;
default:
ret = -EINVAL;
goto err_disable_helpers;
}
if (nd == from)
break;
}
out:
return ret;
err_disable_helpers:
coresight_disable_helpers(csdev, path);
err_disable_path:
if (nd == to)
goto out;
nd = list_next_entry(nd, link);
coresight_disable_path_from_to(path, nd, to);
goto out;
}
int coresight_enable_path(struct coresight_path *path, enum cs_mode mode)
{
return coresight_enable_path_from_to(path, mode,
coresight_path_first_node(path),
coresight_path_last_node(path));
}
struct coresight_device *coresight_get_sink(struct coresight_path *path)
{
struct coresight_device *csdev;
struct coresight_node *nd;
if (!path)
return NULL;
nd = coresight_path_last_node(path);
if (!nd)
return NULL;
csdev = nd->csdev;
if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
return NULL;
return csdev;
}
EXPORT_SYMBOL_GPL(coresight_get_sink);
u32 coresight_get_sink_id(struct coresight_device *csdev)
{
if (!csdev->ea)
return 0;
return (u32) (unsigned long) csdev->ea->var;
}
static int coresight_sink_by_id(struct device *dev, const void *data)
{
struct coresight_device *csdev = to_coresight_device(dev);
if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
if (coresight_get_sink_id(csdev) == *(u32 *)data)
return 1;
}
return 0;
}
struct coresight_device *coresight_get_sink_by_id(u32 id)
{
struct device *dev = NULL;
dev = bus_find_device(&coresight_bustype, NULL, &id,
coresight_sink_by_id);
return dev ? to_coresight_device(dev) : NULL;
}
static bool coresight_get_ref(struct coresight_device *csdev)
{
struct device *dev = &csdev->dev;
struct device *parent = csdev->dev.parent;
struct device_driver *drv;
get_device(dev);
get_device(parent);
drv = parent->driver;
if (!drv || !try_module_get(drv->owner))
goto err_module;
pm_runtime_get_sync(parent);
return true;
err_module:
put_device(parent);
put_device(dev);
return false;
}
static void coresight_put_ref(struct coresight_device *csdev)
{
struct device *dev = &csdev->dev;
struct device *parent = csdev->dev.parent;
struct device_driver *drv = parent->driver;
pm_runtime_put(parent);
if (drv)
module_put(drv->owner);
put_device(parent);
put_device(dev);
}
static int coresight_grab_device(struct coresight_device *csdev)
{
int i;
for (i = 0; i < csdev->pdata->nr_outconns; i++) {
struct coresight_device *child;
child = csdev->pdata->out_conns[i]->dest_dev;
if (child && coresight_is_helper(child))
if (!coresight_get_ref(child))
goto err;
}
if (coresight_get_ref(csdev))
return 0;
err:
for (i--; i >= 0; i--) {
struct coresight_device *child;
child = csdev->pdata->out_conns[i]->dest_dev;
if (child && coresight_is_helper(child))
coresight_put_ref(child);
}
return -ENODEV;
}
static void coresight_drop_device(struct coresight_device *csdev)
{
int i;
coresight_put_ref(csdev);
for (i = 0; i < csdev->pdata->nr_outconns; i++) {
struct coresight_device *child;
child = csdev->pdata->out_conns[i]->dest_dev;
if (child && coresight_is_helper(child))
coresight_put_ref(child);
}
}
static int coresight_get_trace_id(struct coresight_device *csdev,
enum cs_mode mode,
struct coresight_device *sink)
{
if (coresight_ops(csdev)->trace_id)
return coresight_ops(csdev)->trace_id(csdev, mode, sink);
return 0;
}
int coresight_path_assign_trace_id(struct coresight_path *path,
enum cs_mode mode)
{
struct coresight_device *sink = coresight_get_sink(path);
struct coresight_node *nd;
int trace_id;
list_for_each_entry(nd, &path->path_list, link) {
trace_id = coresight_get_trace_id(nd->csdev, mode, sink);
if (trace_id == 0)
continue;
if (!IS_VALID_CS_TRACE_ID(trace_id))
return -EINVAL;
path->trace_id = trace_id;
return 0;
}
return -EINVAL;
}
static int _coresight_build_path(struct coresight_device *csdev,
struct coresight_device *source,
struct coresight_device *sink,
struct coresight_path *path)
{
int i, ret;
bool found = false;
struct coresight_node *node;
if (csdev == sink)
goto out;
if (coresight_is_percpu_source(csdev) && coresight_is_percpu_sink(sink) &&
sink == per_cpu(csdev_sink, csdev->cpu)) {
if (_coresight_build_path(sink, source, sink, path) == 0) {
found = true;
goto out;
}
}
for (i = 0; i < csdev->pdata->nr_outconns; i++) {
struct coresight_device *child_dev;
child_dev = csdev->pdata->out_conns[i]->dest_dev;
if (coresight_blocks_source(source, csdev->pdata->out_conns[i]))
continue;
if (child_dev &&
_coresight_build_path(child_dev, source, sink, path) == 0) {
found = true;
break;
}
}
if (!found)
return -ENODEV;
out:
ret = coresight_grab_device(csdev);
if (ret)
return ret;
node = kzalloc_obj(struct coresight_node);
if (!node)
return -ENOMEM;
node->csdev = csdev;
list_add(&node->link, &path->path_list);
return 0;
}
struct coresight_path *coresight_build_path(struct coresight_device *source,
struct coresight_device *sink)
{
struct coresight_path *path;
int rc;
if (!sink)
return ERR_PTR(-EINVAL);
path = kzalloc_obj(struct coresight_path);
if (!path)
return ERR_PTR(-ENOMEM);
INIT_LIST_HEAD(&path->path_list);
rc = _coresight_build_path(source, source, sink, path);
if (rc) {
kfree(path);
return ERR_PTR(rc);
}
return path;
}
void coresight_release_path(struct coresight_path *path)
{
struct coresight_device *csdev;
struct coresight_node *nd, *next;
list_for_each_entry_safe(nd, next, &path->path_list, link) {
csdev = nd->csdev;
coresight_drop_device(csdev);
list_del(&nd->link);
kfree(nd);
}
kfree(path);
}
static bool coresight_is_def_sink_type(struct coresight_device *csdev)
{
if (((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
(csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) &&
(csdev->subtype.sink_subtype >= CORESIGHT_DEV_SUBTYPE_SINK_BUFFER))
return true;
return false;
}
static struct coresight_device *
coresight_select_best_sink(struct coresight_device *sink, int *depth,
struct coresight_device *new_sink, int new_depth)
{
bool update = false;
if (!sink) {
update = true;
} else if (new_sink->subtype.sink_subtype >
sink->subtype.sink_subtype) {
update = true;
} else if ((new_sink->subtype.sink_subtype ==
sink->subtype.sink_subtype) &&
(*depth > new_depth)) {
update = true;
}
if (update)
*depth = new_depth;
return update ? new_sink : sink;
}
static struct coresight_device *
coresight_find_sink(struct coresight_device *csdev, int *depth)
{
int i, curr_depth = *depth + 1, found_depth = 0;
struct coresight_device *found_sink = NULL;
if (coresight_is_def_sink_type(csdev)) {
found_depth = curr_depth;
found_sink = csdev;
if (csdev->type == CORESIGHT_DEV_TYPE_SINK)
goto return_def_sink;
}
for (i = 0; i < csdev->pdata->nr_outconns; i++) {
struct coresight_device *child_dev, *sink = NULL;
int child_depth = curr_depth;
child_dev = csdev->pdata->out_conns[i]->dest_dev;
if (child_dev)
sink = coresight_find_sink(child_dev, &child_depth);
if (sink)
found_sink = coresight_select_best_sink(found_sink,
&found_depth,
sink,
child_depth);
}
return_def_sink:
if (found_sink)
*depth = found_depth;
return found_sink;
}
struct coresight_device *
coresight_find_default_sink(struct coresight_device *csdev)
{
int depth = 0;
if (!csdev->def_sink) {
if (coresight_is_percpu_source(csdev))
csdev->def_sink = per_cpu(csdev_sink, csdev->cpu);
if (!csdev->def_sink)
csdev->def_sink = coresight_find_sink(csdev, &depth);
}
return csdev->def_sink;
}
EXPORT_SYMBOL_GPL(coresight_find_default_sink);
static int coresight_remove_sink_ref(struct device *dev, void *data)
{
struct coresight_device *sink = data;
struct coresight_device *source = to_coresight_device(dev);
if (source->def_sink == sink)
source->def_sink = NULL;
return 0;
}
static void coresight_clear_default_sink(struct coresight_device *csdev)
{
if ((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
(csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) {
bus_for_each_dev(&coresight_bustype, NULL, csdev,
coresight_remove_sink_ref);
}
}
static void coresight_device_release(struct device *dev)
{
struct coresight_device *csdev = to_coresight_device(dev);
fwnode_handle_put(csdev->dev.fwnode);
free_percpu(csdev->perf_sink_id_map.cpu_map);
kfree(csdev);
}
static int coresight_orphan_match(struct device *dev, void *data)
{
int i, ret = 0;
bool still_orphan = false;
struct coresight_device *dst_csdev = data;
struct coresight_device *src_csdev = to_coresight_device(dev);
struct coresight_connection *conn;
bool fixup_self = (src_csdev == dst_csdev);
if (!src_csdev->orphan)
return 0;
for (i = 0; i < src_csdev->pdata->nr_outconns; i++) {
conn = src_csdev->pdata->out_conns[i];
if (conn->filter_src_fwnode && !conn->filter_src_dev) {
if (dst_csdev &&
(conn->filter_src_fwnode == dst_csdev->dev.fwnode) &&
!WARN_ON_ONCE(!coresight_is_device_source(dst_csdev)))
conn->filter_src_dev = dst_csdev;
else
still_orphan = true;
}
if (conn->dest_dev)
continue;
if (fixup_self)
dst_csdev = coresight_find_csdev_by_fwnode(
conn->dest_fwnode);
if (dst_csdev && conn->dest_fwnode == dst_csdev->dev.fwnode) {
ret = coresight_make_links(src_csdev, conn, dst_csdev);
if (ret)
return ret;
conn->dest_dev = dst_csdev;
conn->src_dev = src_csdev;
ret = coresight_add_in_conn(conn);
if (ret)
return ret;
} else {
still_orphan = true;
}
}
src_csdev->orphan = still_orphan;
return 0;
}
static int coresight_fixup_orphan_conns(struct coresight_device *csdev)
{
return bus_for_each_dev(&coresight_bustype, NULL,
csdev, coresight_orphan_match);
}
static int coresight_clear_filter_source(struct device *dev, void *data)
{
int i;
struct coresight_device *source = data;
struct coresight_device *csdev = to_coresight_device(dev);
for (i = 0; i < csdev->pdata->nr_outconns; ++i) {
if (csdev->pdata->out_conns[i]->filter_src_dev == source)
csdev->pdata->out_conns[i]->filter_src_dev = NULL;
}
return 0;
}
static void coresight_remove_conns(struct coresight_device *csdev)
{
int i, j;
struct coresight_connection *conn;
if (coresight_is_device_source(csdev))
bus_for_each_dev(&coresight_bustype, NULL, csdev,
coresight_clear_filter_source);
for (i = 0; i < csdev->pdata->nr_outconns; i++) {
conn = csdev->pdata->out_conns[i];
if (conn->filter_src_fwnode) {
conn->filter_src_dev = NULL;
fwnode_handle_put(conn->filter_src_fwnode);
}
if (!conn->dest_dev)
continue;
coresight_remove_links(csdev, conn);
for (j = 0; j < conn->dest_dev->pdata->nr_inconns; ++j)
if (conn->dest_dev->pdata->in_conns[j] == conn) {
conn->dest_dev->pdata->in_conns[j] = NULL;
break;
}
}
for (i = 0; i < csdev->pdata->nr_inconns; ++i) {
conn = csdev->pdata->in_conns[i];
if (!conn)
continue;
conn->src_dev->orphan = true;
coresight_remove_links(conn->src_dev, conn);
conn->dest_dev = NULL;
}
coresight_remove_conns_sysfs_group(csdev);
}
int coresight_timeout_action(struct csdev_access *csa, u32 offset,
int position, int value,
coresight_timeout_cb_t cb)
{
int i;
u32 val;
for (i = TIMEOUT_US; i > 0; i--) {
val = csdev_access_read32(csa, offset);
if (value) {
if (val & BIT(position))
return 0;
} else {
if (!(val & BIT(position)))
return 0;
}
if (cb)
cb(csa, offset, position, value);
if (i - 1)
udelay(1);
}
return -EAGAIN;
}
EXPORT_SYMBOL_GPL(coresight_timeout_action);
int coresight_timeout(struct csdev_access *csa, u32 offset,
int position, int value)
{
return coresight_timeout_action(csa, offset, position, value, NULL);
}
EXPORT_SYMBOL_GPL(coresight_timeout);
u32 coresight_relaxed_read32(struct coresight_device *csdev, u32 offset)
{
return csdev_access_relaxed_read32(&csdev->access, offset);
}
u32 coresight_read32(struct coresight_device *csdev, u32 offset)
{
return csdev_access_read32(&csdev->access, offset);
}
void coresight_relaxed_write32(struct coresight_device *csdev,
u32 val, u32 offset)
{
csdev_access_relaxed_write32(&csdev->access, val, offset);
}
void coresight_write32(struct coresight_device *csdev, u32 val, u32 offset)
{
csdev_access_write32(&csdev->access, val, offset);
}
u64 coresight_relaxed_read64(struct coresight_device *csdev, u32 offset)
{
return csdev_access_relaxed_read64(&csdev->access, offset);
}
u64 coresight_read64(struct coresight_device *csdev, u32 offset)
{
return csdev_access_read64(&csdev->access, offset);
}
void coresight_relaxed_write64(struct coresight_device *csdev,
u64 val, u32 offset)
{
csdev_access_relaxed_write64(&csdev->access, val, offset);
}
void coresight_write64(struct coresight_device *csdev, u64 val, u32 offset)
{
csdev_access_write64(&csdev->access, val, offset);
}
void coresight_release_platform_data(struct device *dev,
struct coresight_platform_data *pdata)
{
int i;
struct coresight_connection **conns = pdata->out_conns;
for (i = 0; i < pdata->nr_outconns; i++) {
fwnode_handle_put(conns[i]->dest_fwnode);
conns[i]->dest_fwnode = NULL;
devm_kfree(dev, conns[i]);
}
devm_kfree(dev, pdata->out_conns);
devm_kfree(dev, pdata->in_conns);
devm_kfree(dev, pdata);
}
static struct coresight_device *
coresight_init_device(struct coresight_desc *desc)
{
struct coresight_device *csdev;
csdev = kzalloc_obj(*csdev);
if (!csdev)
return ERR_PTR(-ENOMEM);
csdev->pdata = desc->pdata;
csdev->type = desc->type;
csdev->subtype = desc->subtype;
csdev->ops = desc->ops;
csdev->access = desc->access;
csdev->orphan = true;
if (desc->flags & CORESIGHT_DESC_CPU_BOUND) {
csdev->cpu = desc->cpu;
} else {
if (coresight_is_percpu_source(csdev) ||
coresight_is_percpu_sink(csdev)) {
kfree(csdev);
return ERR_PTR(-EINVAL);
}
csdev->cpu = -1;
}
csdev->dev.type = &coresight_dev_type[desc->type];
csdev->dev.groups = desc->groups;
csdev->dev.parent = desc->dev;
csdev->dev.release = coresight_device_release;
csdev->dev.bus = &coresight_bustype;
return csdev;
}
struct coresight_device *coresight_register(struct coresight_desc *desc)
{
int ret;
struct coresight_device *csdev;
bool registered = false;
csdev = coresight_init_device(desc);
if (IS_ERR(csdev)) {
ret = PTR_ERR(csdev);
goto err_out;
}
if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
raw_spin_lock_init(&csdev->perf_sink_id_map.lock);
csdev->perf_sink_id_map.cpu_map = alloc_percpu(atomic_t);
if (!csdev->perf_sink_id_map.cpu_map) {
kfree(csdev);
ret = -ENOMEM;
goto err_out;
}
}
csdev->dev.fwnode = fwnode_handle_get(dev_fwnode(desc->dev));
dev_set_name(&csdev->dev, "%s", desc->name);
mutex_lock(&coresight_mutex);
ret = device_register(&csdev->dev);
if (ret) {
put_device(&csdev->dev);
goto out_unlock;
}
registered = true;
ret = etm_perf_add_symlink_sink(csdev);
if (ret && ret != -EOPNOTSUPP)
goto out_unlock;
ret = coresight_create_conns_sysfs_group(csdev);
if (ret)
goto out_unlock;
ret = coresight_fixup_orphan_conns(csdev);
if (ret)
goto out_unlock;
coresight_set_percpu_source(csdev);
mutex_unlock(&coresight_mutex);
if (cti_assoc_ops && cti_assoc_ops->add)
cti_assoc_ops->add(csdev);
return csdev;
out_unlock:
mutex_unlock(&coresight_mutex);
if (registered) {
coresight_unregister(csdev);
return ERR_PTR(ret);
}
err_out:
coresight_release_platform_data(desc->dev, desc->pdata);
return ERR_PTR(ret);
}
EXPORT_SYMBOL_GPL(coresight_register);
void coresight_unregister(struct coresight_device *csdev)
{
if (cti_assoc_ops && cti_assoc_ops->remove)
cti_assoc_ops->remove(csdev);
mutex_lock(&coresight_mutex);
coresight_clear_percpu_source(csdev);
etm_perf_del_symlink_sink(csdev);
coresight_remove_conns(csdev);
coresight_clear_default_sink(csdev);
coresight_release_platform_data(csdev->dev.parent, csdev->pdata);
device_unregister(&csdev->dev);
mutex_unlock(&coresight_mutex);
}
EXPORT_SYMBOL_GPL(coresight_unregister);
static struct coresight_dev_list *
coresight_allocate_device_list(const char *prefix)
{
struct coresight_dev_list *list;
list_for_each_entry(list, &coresight_dev_idx_list, node) {
if (!strcmp(list->pfx, prefix))
return list;
}
list = kzalloc(sizeof(*list), GFP_KERNEL);
if (!list)
return NULL;
list->pfx = kstrdup(prefix, GFP_KERNEL);
if (!list->pfx) {
kfree(list);
return NULL;
}
list_add(&list->node, &coresight_dev_idx_list);
return list;
}
static int coresight_allocate_device_idx(struct coresight_dev_list *list,
struct device *dev)
{
struct fwnode_handle **fwnode_list;
struct fwnode_handle *fwnode = dev_fwnode(dev);
int idx;
for (idx = 0; idx < list->nr_idx; idx++)
if (list->fwnode_list[idx] == fwnode)
return idx;
idx = list->nr_idx;
fwnode_list = krealloc_array(list->fwnode_list,
idx + 1, sizeof(*list->fwnode_list),
GFP_KERNEL);
if (!fwnode_list)
return -ENOMEM;
fwnode_list[idx] = fwnode;
list->fwnode_list = fwnode_list;
list->nr_idx = idx + 1;
return idx;
}
static bool coresight_compare_type(enum coresight_dev_type type_a,
union coresight_dev_subtype subtype_a,
enum coresight_dev_type type_b,
union coresight_dev_subtype subtype_b)
{
if (type_a != type_b)
return false;
switch (type_a) {
case CORESIGHT_DEV_TYPE_SINK:
return subtype_a.sink_subtype == subtype_b.sink_subtype;
case CORESIGHT_DEV_TYPE_LINK:
return subtype_a.link_subtype == subtype_b.link_subtype;
case CORESIGHT_DEV_TYPE_LINKSINK:
return subtype_a.link_subtype == subtype_b.link_subtype &&
subtype_a.sink_subtype == subtype_b.sink_subtype;
case CORESIGHT_DEV_TYPE_SOURCE:
return subtype_a.source_subtype == subtype_b.source_subtype;
case CORESIGHT_DEV_TYPE_HELPER:
return subtype_a.helper_subtype == subtype_b.helper_subtype;
default:
return false;
}
}
struct coresight_device *
coresight_find_input_type(struct coresight_platform_data *pdata,
enum coresight_dev_type type,
union coresight_dev_subtype subtype)
{
int i;
struct coresight_connection *conn;
for (i = 0; i < pdata->nr_inconns; ++i) {
conn = pdata->in_conns[i];
if (conn &&
coresight_compare_type(type, subtype, conn->src_dev->type,
conn->src_dev->subtype))
return conn->src_dev;
}
return NULL;
}
EXPORT_SYMBOL_GPL(coresight_find_input_type);
struct coresight_device *
coresight_find_output_type(struct coresight_platform_data *pdata,
enum coresight_dev_type type,
union coresight_dev_subtype subtype)
{
int i;
struct coresight_connection *conn;
for (i = 0; i < pdata->nr_outconns; ++i) {
conn = pdata->out_conns[i];
if (conn->dest_dev &&
coresight_compare_type(type, subtype, conn->dest_dev->type,
conn->dest_dev->subtype))
return conn->dest_dev;
}
return NULL;
}
EXPORT_SYMBOL_GPL(coresight_find_output_type);
bool coresight_loses_context_with_cpu(struct device *dev)
{
return fwnode_property_present(dev_fwnode(dev),
"arm,coresight-loses-context-with-cpu");
}
EXPORT_SYMBOL_GPL(coresight_loses_context_with_cpu);
char *coresight_alloc_device_name(const char *prefix, struct device *dev)
{
struct coresight_dev_list *list;
char *name = NULL;
int idx;
mutex_lock(&coresight_mutex);
list = coresight_allocate_device_list(prefix);
if (!list)
goto done;
idx = coresight_allocate_device_idx(list, dev);
if (idx < 0)
goto done;
name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", list->pfx, idx);
done:
mutex_unlock(&coresight_mutex);
return name;
}
EXPORT_SYMBOL_GPL(coresight_alloc_device_name);
static void coresight_release_device_list(void)
{
struct coresight_dev_list *list, *next;
int i;
list_for_each_entry_safe(list, next, &coresight_dev_idx_list, node) {
for (i = 0; i < list->nr_idx; i++)
list->fwnode_list[i] = NULL;
list->nr_idx = 0;
list_del(&list->node);
kfree(list->pfx);
kfree(list->fwnode_list);
kfree(list);
}
}
static struct coresight_path *coresight_cpu_get_active_path(enum cs_mode mode)
{
struct coresight_device *source;
bool is_active = false;
source = coresight_get_percpu_source_ref(smp_processor_id());
if (!source)
return NULL;
if (coresight_get_mode(source) & mode)
is_active = true;
coresight_put_percpu_source_ref(source);
return is_active ? source->path : NULL;
}
static int coresight_pm_is_needed(struct coresight_path *path)
{
struct coresight_device *source, *sink;
if (this_cpu_read(percpu_pm_failed))
return -EIO;
if (!path)
return 0;
source = coresight_get_source(path);
sink = coresight_get_sink(path);
if (!source || !sink)
return 0;
if (coresight_ops(source)->pm_save_disable &&
coresight_ops(source)->pm_restore_enable)
return 1;
if (coresight_ops(sink)->pm_save_disable &&
coresight_ops(sink)->pm_restore_enable) {
pr_warn_once("coresight PM failed: source has no PM callbacks; "
"cannot safely control sink\n");
return -EINVAL;
}
return 0;
}
static int coresight_pm_device_save(struct coresight_device *csdev)
{
if (!csdev || !coresight_ops(csdev)->pm_save_disable)
return 0;
return coresight_ops(csdev)->pm_save_disable(csdev);
}
static void coresight_pm_device_restore(struct coresight_device *csdev)
{
if (!csdev || !coresight_ops(csdev)->pm_restore_enable)
return;
coresight_ops(csdev)->pm_restore_enable(csdev);
}
static int coresight_pm_save(struct coresight_path *path)
{
struct coresight_device *source = coresight_get_source(path);
struct coresight_node *from, *to;
int ret;
ret = coresight_pm_device_save(source);
if (ret)
return ret;
from = coresight_path_first_node(path);
to = list_prev_entry(coresight_path_last_node(path), link);
coresight_disable_path_from_to(path, from, to);
coresight_pm_device_save(coresight_get_sink(path));
return 0;
}
static void coresight_pm_restore(struct coresight_path *path)
{
struct coresight_device *source = coresight_get_source(path);
struct coresight_device *sink = coresight_get_sink(path);
struct coresight_node *from, *to;
int ret;
coresight_pm_device_restore(sink);
from = coresight_path_first_node(path);
to = list_prev_entry(coresight_path_last_node(path), link);
ret = coresight_enable_path_from_to(path, coresight_get_mode(source),
from, to);
if (ret)
goto path_failed;
coresight_pm_device_restore(source);
return;
path_failed:
coresight_pm_device_save(sink);
pr_err("Failed in coresight PM restore on CPU%d: %d\n",
smp_processor_id(), ret);
this_cpu_write(percpu_pm_failed, true);
}
static int coresight_cpu_pm_notify(struct notifier_block *nb, unsigned long cmd,
void *v)
{
struct coresight_path *path =
coresight_cpu_get_active_path(CS_MODE_SYSFS | CS_MODE_PERF);
int ret;
ret = coresight_pm_is_needed(path);
if (ret <= 0)
return ret ? NOTIFY_BAD : NOTIFY_DONE;
switch (cmd) {
case CPU_PM_ENTER:
if (coresight_pm_save(path))
return NOTIFY_BAD;
break;
case CPU_PM_EXIT:
case CPU_PM_ENTER_FAILED:
coresight_pm_restore(path);
break;
default:
return NOTIFY_DONE;
}
return NOTIFY_OK;
}
static struct notifier_block coresight_cpu_pm_nb = {
.notifier_call = coresight_cpu_pm_notify,
};
static int coresight_dying_cpu(unsigned int cpu)
{
struct coresight_path *path;
path = coresight_cpu_get_active_path(CS_MODE_SYSFS);
if (!path)
return 0;
coresight_disable_sysfs(coresight_get_source(path));
return 0;
}
static int __init coresight_pm_setup(void)
{
int ret;
ret = cpu_pm_register_notifier(&coresight_cpu_pm_nb);
if (ret)
return ret;
ret = cpuhp_setup_state_nocalls(CPUHP_AP_ARM_CORESIGHT_ONLINE,
"arm/coresight-core:dying",
NULL, coresight_dying_cpu);
if (ret)
cpu_pm_unregister_notifier(&coresight_cpu_pm_nb);
return ret;
}
static void coresight_pm_cleanup(void)
{
cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CORESIGHT_ONLINE);
cpu_pm_unregister_notifier(&coresight_cpu_pm_nb);
}
const struct bus_type coresight_bustype = {
.name = "coresight",
};
static int coresight_panic_sync(struct device *dev, void *data)
{
int mode;
struct coresight_device *csdev;
csdev = container_of(dev, struct coresight_device, dev);
mode = coresight_get_mode(csdev);
if ((mode == CS_MODE_SYSFS) || (mode == CS_MODE_PERF)) {
if (panic_ops(csdev))
panic_ops(csdev)->sync(csdev);
}
return 0;
}
static int coresight_panic_cb(struct notifier_block *self,
unsigned long v, void *p)
{
bus_for_each_dev(&coresight_bustype, NULL, NULL,
coresight_panic_sync);
return 0;
}
static struct notifier_block coresight_notifier = {
.notifier_call = coresight_panic_cb,
};
static int __init coresight_init(void)
{
int ret;
ret = bus_register(&coresight_bustype);
if (ret)
return ret;
ret = etm_perf_init();
if (ret)
goto exit_bus_unregister;
ret = atomic_notifier_chain_register(&panic_notifier_list,
&coresight_notifier);
if (ret)
goto exit_perf;
ret = cscfg_init();
if (ret)
goto exit_notifier;
ret = coresight_pm_setup();
if (!ret)
return 0;
cscfg_exit();
exit_notifier:
atomic_notifier_chain_unregister(&panic_notifier_list,
&coresight_notifier);
exit_perf:
etm_perf_exit();
exit_bus_unregister:
bus_unregister(&coresight_bustype);
return ret;
}
static void __exit coresight_exit(void)
{
coresight_pm_cleanup();
cscfg_exit();
atomic_notifier_chain_unregister(&panic_notifier_list,
&coresight_notifier);
etm_perf_exit();
bus_unregister(&coresight_bustype);
coresight_release_device_list();
}
module_init(coresight_init);
module_exit(coresight_exit);
int coresight_init_driver_with_owner(const char *drv, struct amba_driver *amba_drv,
struct platform_driver *pdev_drv, struct module *owner,
const char *mod_name)
{
int ret;
ret = __amba_driver_register(amba_drv, owner);
if (ret) {
pr_err("%s: error registering AMBA driver\n", drv);
return ret;
}
ret = __platform_driver_register(pdev_drv, owner, mod_name);
if (!ret)
return 0;
pr_err("%s: error registering platform driver\n", drv);
amba_driver_unregister(amba_drv);
return ret;
}
EXPORT_SYMBOL_GPL(coresight_init_driver_with_owner);
void coresight_remove_driver(struct amba_driver *amba_drv,
struct platform_driver *pdev_drv)
{
amba_driver_unregister(amba_drv);
platform_driver_unregister(pdev_drv);
}
EXPORT_SYMBOL_GPL(coresight_remove_driver);
int coresight_etm_get_trace_id(struct coresight_device *csdev, enum cs_mode mode,
struct coresight_device *sink)
{
int cpu, trace_id;
if (csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
return -EINVAL;
cpu = csdev->cpu;
switch (mode) {
case CS_MODE_SYSFS:
trace_id = coresight_trace_id_get_cpu_id(cpu);
break;
case CS_MODE_PERF:
if (WARN_ON(!sink))
return -EINVAL;
trace_id = coresight_trace_id_get_cpu_id_map(cpu, &sink->perf_sink_id_map);
break;
default:
trace_id = -EINVAL;
break;
}
if (!IS_VALID_CS_TRACE_ID(trace_id))
dev_err(&csdev->dev,
"Failed to allocate trace ID on CPU%d\n", cpu);
return trace_id;
}
EXPORT_SYMBOL_GPL(coresight_etm_get_trace_id);
int coresight_get_enable_clocks(struct device *dev, struct clk **pclk,
struct clk **atclk)
{
WARN_ON(!pclk);
if (has_acpi_companion(dev))
return 0;
if (!dev_is_amba(dev)) {
*pclk = devm_clk_get_optional_enabled(dev, "apb_pclk");
if (!*pclk)
*pclk = devm_clk_get_optional_enabled(dev, "apb");
if (IS_ERR(*pclk))
return PTR_ERR(*pclk);
}
if (atclk) {
*atclk = devm_clk_get_optional_enabled(dev, "atclk");
if (IS_ERR(*atclk))
return PTR_ERR(*atclk);
}
return 0;
}
EXPORT_SYMBOL_GPL(coresight_get_enable_clocks);
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
MODULE_DESCRIPTION("Arm CoreSight tracer driver");