// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2020 Linaro Limited, All rights reserved.
* Author: Mike Leach <mike.leach@linaro.org>
*/
#include <linux/platform_device.h>
#include <linux/slab.h>
#include "coresight-config.h"
#include "coresight-etm-perf.h"
#include "coresight-syscfg.h"
#include "coresight-syscfg-configfs.h"
/*
* cscfg_ API manages configurations and features for the entire coresight
* infrastructure.
*
* It allows the loading of configurations and features, and loads these into
* coresight devices as appropriate.
*/
/* protect the cscsg_data and device */
static DEFINE_MUTEX(cscfg_mutex);
/* only one of these */
static struct cscfg_manager *cscfg_mgr;
/* load features and configuations into the lists */
/* get name feature instance from a coresight device list of features */
static struct cscfg_feature_csdev *
cscfg_get_feat_csdev(struct coresight_device *csdev, const char *name)
{
struct cscfg_feature_csdev *feat_csdev = NULL;
list_for_each_entry(feat_csdev, &csdev->feature_csdev_list, node) {
if (strcmp(feat_csdev->feat_desc->name, name) == 0)
return feat_csdev;
}
return NULL;
}
/* allocate the device config instance - with max number of used features */
static struct cscfg_config_csdev *
cscfg_alloc_csdev_cfg(struct coresight_device *csdev, int nr_feats)
{
struct cscfg_config_csdev *config_csdev = NULL;
struct device *dev = csdev->dev.parent;
/* this is being allocated using the devm for the coresight device */
config_csdev = devm_kzalloc(dev,
offsetof(struct cscfg_config_csdev, feats_csdev[nr_feats]),
GFP_KERNEL);
if (!config_csdev)
return NULL;
config_csdev->csdev = csdev;
return config_csdev;
}
/* Load a config into a device if there are any feature matches between config and device */
static int cscfg_add_csdev_cfg(struct coresight_device *csdev,
struct cscfg_config_desc *config_desc)
{
struct cscfg_config_csdev *config_csdev = NULL;
struct cscfg_feature_csdev *feat_csdev;
unsigned long flags;
int i;
/* look at each required feature and see if it matches any feature on the device */
for (i = 0; i < config_desc->nr_feat_refs; i++) {
/* look for a matching name */
feat_csdev = cscfg_get_feat_csdev(csdev, config_desc->feat_ref_names[i]);
if (feat_csdev) {
/*
* At least one feature on this device matches the config
* add a config instance to the device and a reference to the feature.
*/
if (!config_csdev) {
config_csdev = cscfg_alloc_csdev_cfg(csdev,
config_desc->nr_feat_refs);
if (!config_csdev)
return -ENOMEM;
config_csdev->config_desc = config_desc;
}
config_csdev->feats_csdev[config_csdev->nr_feat++] = feat_csdev;
}
}
/* if matched features, add config to device.*/
if (config_csdev) {
spin_lock_irqsave(&csdev->cscfg_csdev_lock, flags);
list_add(&config_csdev->node, &csdev->config_csdev_list);
spin_unlock_irqrestore(&csdev->cscfg_csdev_lock, flags);
}
return 0;
}
/*
* Add the config to the set of registered devices - call with mutex locked.
* Iterates through devices - any device that matches one or more of the
* configuration features will load it, the others will ignore it.
*/
static int cscfg_add_cfg_to_csdevs(struct cscfg_config_desc *config_desc)
{
struct cscfg_registered_csdev *csdev_item;
int err;
list_for_each_entry(