xref: /openbmc/linux/drivers/hwtracing/stm/core.c (revision d279a380)
19ea393d8SAlexander Shishkin // SPDX-License-Identifier: GPL-2.0
27bd1d409SAlexander Shishkin /*
37bd1d409SAlexander Shishkin  * System Trace Module (STM) infrastructure
47bd1d409SAlexander Shishkin  * Copyright (c) 2014, Intel Corporation.
57bd1d409SAlexander Shishkin  *
67bd1d409SAlexander Shishkin  * STM class implements generic infrastructure for  System Trace Module devices
77bd1d409SAlexander Shishkin  * as defined in MIPI STPv2 specification.
87bd1d409SAlexander Shishkin  */
97bd1d409SAlexander Shishkin 
108e0469a4SAlexander Shishkin #include <linux/pm_runtime.h>
117bd1d409SAlexander Shishkin #include <linux/uaccess.h>
127bd1d409SAlexander Shishkin #include <linux/kernel.h>
137bd1d409SAlexander Shishkin #include <linux/module.h>
147bd1d409SAlexander Shishkin #include <linux/device.h>
157bd1d409SAlexander Shishkin #include <linux/compat.h>
167bd1d409SAlexander Shishkin #include <linux/kdev_t.h>
177bd1d409SAlexander Shishkin #include <linux/srcu.h>
187bd1d409SAlexander Shishkin #include <linux/slab.h>
197bd1d409SAlexander Shishkin #include <linux/stm.h>
207bd1d409SAlexander Shishkin #include <linux/fs.h>
217bd1d409SAlexander Shishkin #include <linux/mm.h>
22806e3087SGreg Kroah-Hartman #include <linux/vmalloc.h>
237bd1d409SAlexander Shishkin #include "stm.h"
247bd1d409SAlexander Shishkin 
257bd1d409SAlexander Shishkin #include <uapi/linux/stm.h>
267bd1d409SAlexander Shishkin 
277bd1d409SAlexander Shishkin static unsigned int stm_core_up;
287bd1d409SAlexander Shishkin 
297bd1d409SAlexander Shishkin /*
307bd1d409SAlexander Shishkin  * The SRCU here makes sure that STM device doesn't disappear from under a
317bd1d409SAlexander Shishkin  * stm_source_write() caller, which may want to have as little overhead as
327bd1d409SAlexander Shishkin  * possible.
337bd1d409SAlexander Shishkin  */
347bd1d409SAlexander Shishkin static struct srcu_struct stm_source_srcu;
357bd1d409SAlexander Shishkin 
367bd1d409SAlexander Shishkin static ssize_t masters_show(struct device *dev,
377bd1d409SAlexander Shishkin 			    struct device_attribute *attr,
387bd1d409SAlexander Shishkin 			    char *buf)
397bd1d409SAlexander Shishkin {
407bd1d409SAlexander Shishkin 	struct stm_device *stm = to_stm_device(dev);
417bd1d409SAlexander Shishkin 	int ret;
427bd1d409SAlexander Shishkin 
437bd1d409SAlexander Shishkin 	ret = sprintf(buf, "%u %u\n", stm->data->sw_start, stm->data->sw_end);
447bd1d409SAlexander Shishkin 
457bd1d409SAlexander Shishkin 	return ret;
467bd1d409SAlexander Shishkin }
477bd1d409SAlexander Shishkin 
487bd1d409SAlexander Shishkin static DEVICE_ATTR_RO(masters);
497bd1d409SAlexander Shishkin 
507bd1d409SAlexander Shishkin static ssize_t channels_show(struct device *dev,
517bd1d409SAlexander Shishkin 			     struct device_attribute *attr,
527bd1d409SAlexander Shishkin 			     char *buf)
537bd1d409SAlexander Shishkin {
547bd1d409SAlexander Shishkin 	struct stm_device *stm = to_stm_device(dev);
557bd1d409SAlexander Shishkin 	int ret;
567bd1d409SAlexander Shishkin 
577bd1d409SAlexander Shishkin 	ret = sprintf(buf, "%u\n", stm->data->sw_nchannels);
587bd1d409SAlexander Shishkin 
597bd1d409SAlexander Shishkin 	return ret;
607bd1d409SAlexander Shishkin }
617bd1d409SAlexander Shishkin 
627bd1d409SAlexander Shishkin static DEVICE_ATTR_RO(channels);
637bd1d409SAlexander Shishkin 
648e996a28SAlexander Shishkin static ssize_t hw_override_show(struct device *dev,
658e996a28SAlexander Shishkin 				struct device_attribute *attr,
668e996a28SAlexander Shishkin 				char *buf)
678e996a28SAlexander Shishkin {
688e996a28SAlexander Shishkin 	struct stm_device *stm = to_stm_device(dev);
698e996a28SAlexander Shishkin 	int ret;
708e996a28SAlexander Shishkin 
718e996a28SAlexander Shishkin 	ret = sprintf(buf, "%u\n", stm->data->hw_override);
728e996a28SAlexander Shishkin 
738e996a28SAlexander Shishkin 	return ret;
748e996a28SAlexander Shishkin }
758e996a28SAlexander Shishkin 
768e996a28SAlexander Shishkin static DEVICE_ATTR_RO(hw_override);
778e996a28SAlexander Shishkin 
787bd1d409SAlexander Shishkin static struct attribute *stm_attrs[] = {
797bd1d409SAlexander Shishkin 	&dev_attr_masters.attr,
807bd1d409SAlexander Shishkin 	&dev_attr_channels.attr,
818e996a28SAlexander Shishkin 	&dev_attr_hw_override.attr,
827bd1d409SAlexander Shishkin 	NULL,
837bd1d409SAlexander Shishkin };
847bd1d409SAlexander Shishkin 
857bd1d409SAlexander Shishkin ATTRIBUTE_GROUPS(stm);
867bd1d409SAlexander Shishkin 
877bd1d409SAlexander Shishkin static struct class stm_class = {
887bd1d409SAlexander Shishkin 	.name		= "stm",
897bd1d409SAlexander Shishkin 	.dev_groups	= stm_groups,
907bd1d409SAlexander Shishkin };
917bd1d409SAlexander Shishkin 
927bd1d409SAlexander Shishkin static int stm_dev_match(struct device *dev, const void *data)
937bd1d409SAlexander Shishkin {
947bd1d409SAlexander Shishkin 	const char *name = data;
957bd1d409SAlexander Shishkin 
967bd1d409SAlexander Shishkin 	return sysfs_streq(name, dev_name(dev));
977bd1d409SAlexander Shishkin }
987bd1d409SAlexander Shishkin 
997bd1d409SAlexander Shishkin /**
1007bd1d409SAlexander Shishkin  * stm_find_device() - find stm device by name
1017bd1d409SAlexander Shishkin  * @buf:	character buffer containing the name
1027bd1d409SAlexander Shishkin  *
1037bd1d409SAlexander Shishkin  * This is called when either policy gets assigned to an stm device or an
1047bd1d409SAlexander Shishkin  * stm_source device gets linked to an stm device.
1057bd1d409SAlexander Shishkin  *
1067bd1d409SAlexander Shishkin  * This grabs device's reference (get_device()) and module reference, both
1077bd1d409SAlexander Shishkin  * of which the calling path needs to make sure to drop with stm_put_device().
1087bd1d409SAlexander Shishkin  *
1097bd1d409SAlexander Shishkin  * Return:	stm device pointer or null if lookup failed.
1107bd1d409SAlexander Shishkin  */
1117bd1d409SAlexander Shishkin struct stm_device *stm_find_device(const char *buf)
1127bd1d409SAlexander Shishkin {
1137bd1d409SAlexander Shishkin 	struct stm_device *stm;
1147bd1d409SAlexander Shishkin 	struct device *dev;
1157bd1d409SAlexander Shishkin 
1167bd1d409SAlexander Shishkin 	if (!stm_core_up)
1177bd1d409SAlexander Shishkin 		return NULL;
1187bd1d409SAlexander Shishkin 
1197bd1d409SAlexander Shishkin 	dev = class_find_device(&stm_class, NULL, buf, stm_dev_match);
1207bd1d409SAlexander Shishkin 	if (!dev)
1217bd1d409SAlexander Shishkin 		return NULL;
1227bd1d409SAlexander Shishkin 
1237bd1d409SAlexander Shishkin 	stm = to_stm_device(dev);
1247bd1d409SAlexander Shishkin 	if (!try_module_get(stm->owner)) {
125f7c81c71SAlexander Shishkin 		/* matches class_find_device() above */
1267bd1d409SAlexander Shishkin 		put_device(dev);
1277bd1d409SAlexander Shishkin 		return NULL;
1287bd1d409SAlexander Shishkin 	}
1297bd1d409SAlexander Shishkin 
1307bd1d409SAlexander Shishkin 	return stm;
1317bd1d409SAlexander Shishkin }
1327bd1d409SAlexander Shishkin 
1337bd1d409SAlexander Shishkin /**
1347bd1d409SAlexander Shishkin  * stm_put_device() - drop references on the stm device
1357bd1d409SAlexander Shishkin  * @stm:	stm device, previously acquired by stm_find_device()
1367bd1d409SAlexander Shishkin  *
1377bd1d409SAlexander Shishkin  * This drops the module reference and device reference taken by
138f7c81c71SAlexander Shishkin  * stm_find_device() or stm_char_open().
1397bd1d409SAlexander Shishkin  */
1407bd1d409SAlexander Shishkin void stm_put_device(struct stm_device *stm)
1417bd1d409SAlexander Shishkin {
1427bd1d409SAlexander Shishkin 	module_put(stm->owner);
1437bd1d409SAlexander Shishkin 	put_device(&stm->dev);
1447bd1d409SAlexander Shishkin }
1457bd1d409SAlexander Shishkin 
1467bd1d409SAlexander Shishkin /*
1477bd1d409SAlexander Shishkin  * Internally we only care about software-writable masters here, that is the
1487bd1d409SAlexander Shishkin  * ones in the range [stm_data->sw_start..stm_data..sw_end], however we need
1497bd1d409SAlexander Shishkin  * original master numbers to be visible externally, since they are the ones
1507bd1d409SAlexander Shishkin  * that will appear in the STP stream. Thus, the internal bookkeeping uses
1517bd1d409SAlexander Shishkin  * $master - stm_data->sw_start to reference master descriptors and such.
1527bd1d409SAlexander Shishkin  */
1537bd1d409SAlexander Shishkin 
1547bd1d409SAlexander Shishkin #define __stm_master(_s, _m)				\
1557bd1d409SAlexander Shishkin 	((_s)->masters[(_m) - (_s)->data->sw_start])
1567bd1d409SAlexander Shishkin 
1577bd1d409SAlexander Shishkin static inline struct stp_master *
1587bd1d409SAlexander Shishkin stm_master(struct stm_device *stm, unsigned int idx)
1597bd1d409SAlexander Shishkin {
1607bd1d409SAlexander Shishkin 	if (idx < stm->data->sw_start || idx > stm->data->sw_end)
1617bd1d409SAlexander Shishkin 		return NULL;
1627bd1d409SAlexander Shishkin 
1637bd1d409SAlexander Shishkin 	return __stm_master(stm, idx);
1647bd1d409SAlexander Shishkin }
1657bd1d409SAlexander Shishkin 
1667bd1d409SAlexander Shishkin static int stp_master_alloc(struct stm_device *stm, unsigned int idx)
1677bd1d409SAlexander Shishkin {
1687bd1d409SAlexander Shishkin 	struct stp_master *master;
1697bd1d409SAlexander Shishkin 	size_t size;
1707bd1d409SAlexander Shishkin 
1717bd1d409SAlexander Shishkin 	size = ALIGN(stm->data->sw_nchannels, 8) / 8;
1727bd1d409SAlexander Shishkin 	size += sizeof(struct stp_master);
1737bd1d409SAlexander Shishkin 	master = kzalloc(size, GFP_ATOMIC);
1747bd1d409SAlexander Shishkin 	if (!master)
1757bd1d409SAlexander Shishkin 		return -ENOMEM;
1767bd1d409SAlexander Shishkin 
1777bd1d409SAlexander Shishkin 	master->nr_free = stm->data->sw_nchannels;
1787bd1d409SAlexander Shishkin 	__stm_master(stm, idx) = master;
1797bd1d409SAlexander Shishkin 
1807bd1d409SAlexander Shishkin 	return 0;
1817bd1d409SAlexander Shishkin }
1827bd1d409SAlexander Shishkin 
1837bd1d409SAlexander Shishkin static void stp_master_free(struct stm_device *stm, unsigned int idx)
1847bd1d409SAlexander Shishkin {
1857bd1d409SAlexander Shishkin 	struct stp_master *master = stm_master(stm, idx);
1867bd1d409SAlexander Shishkin 
1877bd1d409SAlexander Shishkin 	if (!master)
1887bd1d409SAlexander Shishkin 		return;
1897bd1d409SAlexander Shishkin 
1907bd1d409SAlexander Shishkin 	__stm_master(stm, idx) = NULL;
1917bd1d409SAlexander Shishkin 	kfree(master);
1927bd1d409SAlexander Shishkin }
1937bd1d409SAlexander Shishkin 
1947bd1d409SAlexander Shishkin static void stm_output_claim(struct stm_device *stm, struct stm_output *output)
1957bd1d409SAlexander Shishkin {
1967bd1d409SAlexander Shishkin 	struct stp_master *master = stm_master(stm, output->master);
1977bd1d409SAlexander Shishkin 
198cde4ad83SAlexander Shishkin 	lockdep_assert_held(&stm->mc_lock);
199cde4ad83SAlexander Shishkin 	lockdep_assert_held(&output->lock);
200cde4ad83SAlexander Shishkin 
2017bd1d409SAlexander Shishkin 	if (WARN_ON_ONCE(master->nr_free < output->nr_chans))
2027bd1d409SAlexander Shishkin 		return;
2037bd1d409SAlexander Shishkin 
2047bd1d409SAlexander Shishkin 	bitmap_allocate_region(&master->chan_map[0], output->channel,
2057bd1d409SAlexander Shishkin 			       ilog2(output->nr_chans));
2067bd1d409SAlexander Shishkin 
2077bd1d409SAlexander Shishkin 	master->nr_free -= output->nr_chans;
2087bd1d409SAlexander Shishkin }
2097bd1d409SAlexander Shishkin 
2107bd1d409SAlexander Shishkin static void
2117bd1d409SAlexander Shishkin stm_output_disclaim(struct stm_device *stm, struct stm_output *output)
2127bd1d409SAlexander Shishkin {
2137bd1d409SAlexander Shishkin 	struct stp_master *master = stm_master(stm, output->master);
2147bd1d409SAlexander Shishkin 
215cde4ad83SAlexander Shishkin 	lockdep_assert_held(&stm->mc_lock);
216cde4ad83SAlexander Shishkin 	lockdep_assert_held(&output->lock);
217cde4ad83SAlexander Shishkin 
2187bd1d409SAlexander Shishkin 	bitmap_release_region(&master->chan_map[0], output->channel,
2197bd1d409SAlexander Shishkin 			      ilog2(output->nr_chans));
2207bd1d409SAlexander Shishkin 
2217bd1d409SAlexander Shishkin 	output->nr_chans = 0;
2227bd1d409SAlexander Shishkin 	master->nr_free += output->nr_chans;
2237bd1d409SAlexander Shishkin }
2247bd1d409SAlexander Shishkin 
2257bd1d409SAlexander Shishkin /*
2267bd1d409SAlexander Shishkin  * This is like bitmap_find_free_region(), except it can ignore @start bits
2277bd1d409SAlexander Shishkin  * at the beginning.
2287bd1d409SAlexander Shishkin  */
2297bd1d409SAlexander Shishkin static int find_free_channels(unsigned long *bitmap, unsigned int start,
2307bd1d409SAlexander Shishkin 			      unsigned int end, unsigned int width)
2317bd1d409SAlexander Shishkin {
2327bd1d409SAlexander Shishkin 	unsigned int pos;
2337bd1d409SAlexander Shishkin 	int i;
2347bd1d409SAlexander Shishkin 
2357bd1d409SAlexander Shishkin 	for (pos = start; pos < end + 1; pos = ALIGN(pos, width)) {
2367bd1d409SAlexander Shishkin 		pos = find_next_zero_bit(bitmap, end + 1, pos);
2377bd1d409SAlexander Shishkin 		if (pos + width > end + 1)
2387bd1d409SAlexander Shishkin 			break;
2397bd1d409SAlexander Shishkin 
2407bd1d409SAlexander Shishkin 		if (pos & (width - 1))
2417bd1d409SAlexander Shishkin 			continue;
2427bd1d409SAlexander Shishkin 
2437bd1d409SAlexander Shishkin 		for (i = 1; i < width && !test_bit(pos + i, bitmap); i++)
2447bd1d409SAlexander Shishkin 			;
2457bd1d409SAlexander Shishkin 		if (i == width)
2467bd1d409SAlexander Shishkin 			return pos;
2477bd1d409SAlexander Shishkin 	}
2487bd1d409SAlexander Shishkin 
2497bd1d409SAlexander Shishkin 	return -1;
2507bd1d409SAlexander Shishkin }
2517bd1d409SAlexander Shishkin 
252f45f40adSLucas Tanure static int
2537bd1d409SAlexander Shishkin stm_find_master_chan(struct stm_device *stm, unsigned int width,
2547bd1d409SAlexander Shishkin 		     unsigned int *mstart, unsigned int mend,
2557bd1d409SAlexander Shishkin 		     unsigned int *cstart, unsigned int cend)
2567bd1d409SAlexander Shishkin {
2577bd1d409SAlexander Shishkin 	struct stp_master *master;
2587bd1d409SAlexander Shishkin 	unsigned int midx;
2597bd1d409SAlexander Shishkin 	int pos, err;
2607bd1d409SAlexander Shishkin 
2617bd1d409SAlexander Shishkin 	for (midx = *mstart; midx <= mend; midx++) {
2627bd1d409SAlexander Shishkin 		if (!stm_master(stm, midx)) {
2637bd1d409SAlexander Shishkin 			err = stp_master_alloc(stm, midx);
2647bd1d409SAlexander Shishkin 			if (err)
2657bd1d409SAlexander Shishkin 				return err;
2667bd1d409SAlexander Shishkin 		}
2677bd1d409SAlexander Shishkin 
2687bd1d409SAlexander Shishkin 		master = stm_master(stm, midx);
2697bd1d409SAlexander Shishkin 
2707bd1d409SAlexander Shishkin 		if (!master->nr_free)
2717bd1d409SAlexander Shishkin 			continue;
2727bd1d409SAlexander Shishkin 
2737bd1d409SAlexander Shishkin 		pos = find_free_channels(master->chan_map, *cstart, cend,
2747bd1d409SAlexander Shishkin 					 width);
2757bd1d409SAlexander Shishkin 		if (pos < 0)
2767bd1d409SAlexander Shishkin 			continue;
2777bd1d409SAlexander Shishkin 
2787bd1d409SAlexander Shishkin 		*mstart = midx;
2797bd1d409SAlexander Shishkin 		*cstart = pos;
2807bd1d409SAlexander Shishkin 		return 0;
2817bd1d409SAlexander Shishkin 	}
2827bd1d409SAlexander Shishkin 
2837bd1d409SAlexander Shishkin 	return -ENOSPC;
2847bd1d409SAlexander Shishkin }
2857bd1d409SAlexander Shishkin 
2867bd1d409SAlexander Shishkin static int stm_output_assign(struct stm_device *stm, unsigned int width,
2877bd1d409SAlexander Shishkin 			     struct stp_policy_node *policy_node,
2887bd1d409SAlexander Shishkin 			     struct stm_output *output)
2897bd1d409SAlexander Shishkin {
2907bd1d409SAlexander Shishkin 	unsigned int midx, cidx, mend, cend;
2917bd1d409SAlexander Shishkin 	int ret = -EINVAL;
2927bd1d409SAlexander Shishkin 
2937bd1d409SAlexander Shishkin 	if (width > stm->data->sw_nchannels)
2947bd1d409SAlexander Shishkin 		return -EINVAL;
2957bd1d409SAlexander Shishkin 
296cb6102bdSAlexander Shishkin 	/* We no longer accept policy_node==NULL here */
297cb6102bdSAlexander Shishkin 	if (WARN_ON_ONCE(!policy_node))
298cb6102bdSAlexander Shishkin 		return -EINVAL;
299cb6102bdSAlexander Shishkin 
300cb6102bdSAlexander Shishkin 	/*
301cb6102bdSAlexander Shishkin 	 * Also, the caller holds reference to policy_node, so it won't
302cb6102bdSAlexander Shishkin 	 * disappear on us.
303cb6102bdSAlexander Shishkin 	 */
304cb6102bdSAlexander Shishkin 	stp_policy_node_get_ranges(policy_node, &midx, &mend, &cidx, &cend);
3057bd1d409SAlexander Shishkin 
3067bd1d409SAlexander Shishkin 	spin_lock(&stm->mc_lock);
307cde4ad83SAlexander Shishkin 	spin_lock(&output->lock);
3087bd1d409SAlexander Shishkin 	/* output is already assigned -- shouldn't happen */
3097bd1d409SAlexander Shishkin 	if (WARN_ON_ONCE(output->nr_chans))
3107bd1d409SAlexander Shishkin 		goto unlock;
3117bd1d409SAlexander Shishkin 
3127bd1d409SAlexander Shishkin 	ret = stm_find_master_chan(stm, width, &midx, mend, &cidx, cend);
313f45f40adSLucas Tanure 	if (ret < 0)
3147bd1d409SAlexander Shishkin 		goto unlock;
3157bd1d409SAlexander Shishkin 
3167bd1d409SAlexander Shishkin 	output->master = midx;
3177bd1d409SAlexander Shishkin 	output->channel = cidx;
3187bd1d409SAlexander Shishkin 	output->nr_chans = width;
319c7fd62bcSAlexander Shishkin 	if (stm->pdrv->output_open) {
320c7fd62bcSAlexander Shishkin 		void *priv = stp_policy_node_priv(policy_node);
321c7fd62bcSAlexander Shishkin 
322c7fd62bcSAlexander Shishkin 		if (WARN_ON_ONCE(!priv))
323c7fd62bcSAlexander Shishkin 			goto unlock;
324c7fd62bcSAlexander Shishkin 
325c7fd62bcSAlexander Shishkin 		/* configfs subsys mutex is held by the caller */
326c7fd62bcSAlexander Shishkin 		ret = stm->pdrv->output_open(priv, output);
327c7fd62bcSAlexander Shishkin 		if (ret)
328c7fd62bcSAlexander Shishkin 			goto unlock;
329c7fd62bcSAlexander Shishkin 	}
330c7fd62bcSAlexander Shishkin 
3317bd1d409SAlexander Shishkin 	stm_output_claim(stm, output);
3327bd1d409SAlexander Shishkin 	dev_dbg(&stm->dev, "assigned %u:%u (+%u)\n", midx, cidx, width);
3337bd1d409SAlexander Shishkin 
3347bd1d409SAlexander Shishkin 	ret = 0;
3357bd1d409SAlexander Shishkin unlock:
336c7fd62bcSAlexander Shishkin 	if (ret)
337c7fd62bcSAlexander Shishkin 		output->nr_chans = 0;
338c7fd62bcSAlexander Shishkin 
339cde4ad83SAlexander Shishkin 	spin_unlock(&output->lock);
3407bd1d409SAlexander Shishkin 	spin_unlock(&stm->mc_lock);
3417bd1d409SAlexander Shishkin 
3427bd1d409SAlexander Shishkin 	return ret;
3437bd1d409SAlexander Shishkin }
3447bd1d409SAlexander Shishkin 
3457bd1d409SAlexander Shishkin static void stm_output_free(struct stm_device *stm, struct stm_output *output)
3467bd1d409SAlexander Shishkin {
3477bd1d409SAlexander Shishkin 	spin_lock(&stm->mc_lock);
348cde4ad83SAlexander Shishkin 	spin_lock(&output->lock);
3497bd1d409SAlexander Shishkin 	if (output->nr_chans)
3507bd1d409SAlexander Shishkin 		stm_output_disclaim(stm, output);
351c7fd62bcSAlexander Shishkin 	if (stm->pdrv && stm->pdrv->output_close)
352c7fd62bcSAlexander Shishkin 		stm->pdrv->output_close(output);
353cde4ad83SAlexander Shishkin 	spin_unlock(&output->lock);
3547bd1d409SAlexander Shishkin 	spin_unlock(&stm->mc_lock);
3557bd1d409SAlexander Shishkin }
3567bd1d409SAlexander Shishkin 
357cde4ad83SAlexander Shishkin static void stm_output_init(struct stm_output *output)
358cde4ad83SAlexander Shishkin {
359cde4ad83SAlexander Shishkin 	spin_lock_init(&output->lock);
360cde4ad83SAlexander Shishkin }
361cde4ad83SAlexander Shishkin 
3627bd1d409SAlexander Shishkin static int major_match(struct device *dev, const void *data)
3637bd1d409SAlexander Shishkin {
3647bd1d409SAlexander Shishkin 	unsigned int major = *(unsigned int *)data;
3657bd1d409SAlexander Shishkin 
3667bd1d409SAlexander Shishkin 	return MAJOR(dev->devt) == major;
3677bd1d409SAlexander Shishkin }
3687bd1d409SAlexander Shishkin 
369c7fd62bcSAlexander Shishkin /*
370c7fd62bcSAlexander Shishkin  * Framing protocol management
371c7fd62bcSAlexander Shishkin  * Modules can implement STM protocol drivers and (un-)register them
372c7fd62bcSAlexander Shishkin  * with the STM class framework.
373c7fd62bcSAlexander Shishkin  */
374c7fd62bcSAlexander Shishkin static struct list_head stm_pdrv_head;
375c7fd62bcSAlexander Shishkin static struct mutex stm_pdrv_mutex;
376c7fd62bcSAlexander Shishkin 
377c7fd62bcSAlexander Shishkin struct stm_pdrv_entry {
378c7fd62bcSAlexander Shishkin 	struct list_head			entry;
379c7fd62bcSAlexander Shishkin 	const struct stm_protocol_driver	*pdrv;
380c7fd62bcSAlexander Shishkin 	const struct config_item_type		*node_type;
381c7fd62bcSAlexander Shishkin };
382c7fd62bcSAlexander Shishkin 
383c7fd62bcSAlexander Shishkin static const struct stm_pdrv_entry *
384c7fd62bcSAlexander Shishkin __stm_lookup_protocol(const char *name)
385c7fd62bcSAlexander Shishkin {
386c7fd62bcSAlexander Shishkin 	struct stm_pdrv_entry *pe;
387c7fd62bcSAlexander Shishkin 
388c7fd62bcSAlexander Shishkin 	/*
389c7fd62bcSAlexander Shishkin 	 * If no name is given (NULL or ""), fall back to "p_basic".
390c7fd62bcSAlexander Shishkin 	 */
391c7fd62bcSAlexander Shishkin 	if (!name || !*name)
392c7fd62bcSAlexander Shishkin 		name = "p_basic";
393c7fd62bcSAlexander Shishkin 
394c7fd62bcSAlexander Shishkin 	list_for_each_entry(pe, &stm_pdrv_head, entry) {
395c7fd62bcSAlexander Shishkin 		if (!strcmp(name, pe->pdrv->name))
396c7fd62bcSAlexander Shishkin 			return pe;
397c7fd62bcSAlexander Shishkin 	}
398c7fd62bcSAlexander Shishkin 
399c7fd62bcSAlexander Shishkin 	return NULL;
400c7fd62bcSAlexander Shishkin }
401c7fd62bcSAlexander Shishkin 
402c7fd62bcSAlexander Shishkin int stm_register_protocol(const struct stm_protocol_driver *pdrv)
403c7fd62bcSAlexander Shishkin {
404c7fd62bcSAlexander Shishkin 	struct stm_pdrv_entry *pe = NULL;
405c7fd62bcSAlexander Shishkin 	int ret = -ENOMEM;
406c7fd62bcSAlexander Shishkin 
407c7fd62bcSAlexander Shishkin 	mutex_lock(&stm_pdrv_mutex);
408c7fd62bcSAlexander Shishkin 
409c7fd62bcSAlexander Shishkin 	if (__stm_lookup_protocol(pdrv->name)) {
410c7fd62bcSAlexander Shishkin 		ret = -EEXIST;
411c7fd62bcSAlexander Shishkin 		goto unlock;
412c7fd62bcSAlexander Shishkin 	}
413c7fd62bcSAlexander Shishkin 
414c7fd62bcSAlexander Shishkin 	pe = kzalloc(sizeof(*pe), GFP_KERNEL);
415c7fd62bcSAlexander Shishkin 	if (!pe)
416c7fd62bcSAlexander Shishkin 		goto unlock;
417c7fd62bcSAlexander Shishkin 
418c7fd62bcSAlexander Shishkin 	if (pdrv->policy_attr) {
419c7fd62bcSAlexander Shishkin 		pe->node_type = get_policy_node_type(pdrv->policy_attr);
420c7fd62bcSAlexander Shishkin 		if (!pe->node_type)
421c7fd62bcSAlexander Shishkin 			goto unlock;
422c7fd62bcSAlexander Shishkin 	}
423c7fd62bcSAlexander Shishkin 
424c7fd62bcSAlexander Shishkin 	list_add_tail(&pe->entry, &stm_pdrv_head);
425c7fd62bcSAlexander Shishkin 	pe->pdrv = pdrv;
426c7fd62bcSAlexander Shishkin 
427c7fd62bcSAlexander Shishkin 	ret = 0;
428c7fd62bcSAlexander Shishkin unlock:
429c7fd62bcSAlexander Shishkin 	mutex_unlock(&stm_pdrv_mutex);
430c7fd62bcSAlexander Shishkin 
431c7fd62bcSAlexander Shishkin 	if (ret)
432c7fd62bcSAlexander Shishkin 		kfree(pe);
433c7fd62bcSAlexander Shishkin 
434c7fd62bcSAlexander Shishkin 	return ret;
435c7fd62bcSAlexander Shishkin }
436c7fd62bcSAlexander Shishkin EXPORT_SYMBOL_GPL(stm_register_protocol);
437c7fd62bcSAlexander Shishkin 
438c7fd62bcSAlexander Shishkin void stm_unregister_protocol(const struct stm_protocol_driver *pdrv)
439c7fd62bcSAlexander Shishkin {
440c7fd62bcSAlexander Shishkin 	struct stm_pdrv_entry *pe, *iter;
441c7fd62bcSAlexander Shishkin 
442c7fd62bcSAlexander Shishkin 	mutex_lock(&stm_pdrv_mutex);
443c7fd62bcSAlexander Shishkin 
444c7fd62bcSAlexander Shishkin 	list_for_each_entry_safe(pe, iter, &stm_pdrv_head, entry) {
445c7fd62bcSAlexander Shishkin 		if (pe->pdrv == pdrv) {
446c7fd62bcSAlexander Shishkin 			list_del(&pe->entry);
447c7fd62bcSAlexander Shishkin 
448c7fd62bcSAlexander Shishkin 			if (pe->node_type) {
449c7fd62bcSAlexander Shishkin 				kfree(pe->node_type->ct_attrs);
450c7fd62bcSAlexander Shishkin 				kfree(pe->node_type);
451c7fd62bcSAlexander Shishkin 			}
452c7fd62bcSAlexander Shishkin 			kfree(pe);
453c7fd62bcSAlexander Shishkin 			break;
454c7fd62bcSAlexander Shishkin 		}
455c7fd62bcSAlexander Shishkin 	}
456c7fd62bcSAlexander Shishkin 
457c7fd62bcSAlexander Shishkin 	mutex_unlock(&stm_pdrv_mutex);
458c7fd62bcSAlexander Shishkin }
459c7fd62bcSAlexander Shishkin EXPORT_SYMBOL_GPL(stm_unregister_protocol);
460c7fd62bcSAlexander Shishkin 
461c7fd62bcSAlexander Shishkin static bool stm_get_protocol(const struct stm_protocol_driver *pdrv)
462c7fd62bcSAlexander Shishkin {
463c7fd62bcSAlexander Shishkin 	return try_module_get(pdrv->owner);
464c7fd62bcSAlexander Shishkin }
465c7fd62bcSAlexander Shishkin 
466c7fd62bcSAlexander Shishkin void stm_put_protocol(const struct stm_protocol_driver *pdrv)
467c7fd62bcSAlexander Shishkin {
468c7fd62bcSAlexander Shishkin 	module_put(pdrv->owner);
469c7fd62bcSAlexander Shishkin }
470c7fd62bcSAlexander Shishkin 
471c7fd62bcSAlexander Shishkin int stm_lookup_protocol(const char *name,
472c7fd62bcSAlexander Shishkin 			const struct stm_protocol_driver **pdrv,
473c7fd62bcSAlexander Shishkin 			const struct config_item_type **node_type)
474c7fd62bcSAlexander Shishkin {
475c7fd62bcSAlexander Shishkin 	const struct stm_pdrv_entry *pe;
476c7fd62bcSAlexander Shishkin 
477c7fd62bcSAlexander Shishkin 	mutex_lock(&stm_pdrv_mutex);
478c7fd62bcSAlexander Shishkin 
479c7fd62bcSAlexander Shishkin 	pe = __stm_lookup_protocol(name);
480c7fd62bcSAlexander Shishkin 	if (pe && pe->pdrv && stm_get_protocol(pe->pdrv)) {
481c7fd62bcSAlexander Shishkin 		*pdrv = pe->pdrv;
482c7fd62bcSAlexander Shishkin 		*node_type = pe->node_type;
483c7fd62bcSAlexander Shishkin 	}
484c7fd62bcSAlexander Shishkin 
485c7fd62bcSAlexander Shishkin 	mutex_unlock(&stm_pdrv_mutex);
486c7fd62bcSAlexander Shishkin 
487c7fd62bcSAlexander Shishkin 	return pe ? 0 : -ENOENT;
488c7fd62bcSAlexander Shishkin }
489c7fd62bcSAlexander Shishkin 
4907bd1d409SAlexander Shishkin static int stm_char_open(struct inode *inode, struct file *file)
4917bd1d409SAlexander Shishkin {
4927bd1d409SAlexander Shishkin 	struct stm_file *stmf;
4937bd1d409SAlexander Shishkin 	struct device *dev;
4947bd1d409SAlexander Shishkin 	unsigned int major = imajor(inode);
495a0ebf519SJohan Hovold 	int err = -ENOMEM;
4967bd1d409SAlexander Shishkin 
4977bd1d409SAlexander Shishkin 	dev = class_find_device(&stm_class, NULL, &major, major_match);
4987bd1d409SAlexander Shishkin 	if (!dev)
4997bd1d409SAlexander Shishkin 		return -ENODEV;
5007bd1d409SAlexander Shishkin 
5017bd1d409SAlexander Shishkin 	stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
5027bd1d409SAlexander Shishkin 	if (!stmf)
503a0ebf519SJohan Hovold 		goto err_put_device;
5047bd1d409SAlexander Shishkin 
505a0ebf519SJohan Hovold 	err = -ENODEV;
506cde4ad83SAlexander Shishkin 	stm_output_init(&stmf->output);
5077bd1d409SAlexander Shishkin 	stmf->stm = to_stm_device(dev);
5087bd1d409SAlexander Shishkin 
5097bd1d409SAlexander Shishkin 	if (!try_module_get(stmf->stm->owner))
5107bd1d409SAlexander Shishkin 		goto err_free;
5117bd1d409SAlexander Shishkin 
5127bd1d409SAlexander Shishkin 	file->private_data = stmf;
5137bd1d409SAlexander Shishkin 
5147bd1d409SAlexander Shishkin 	return nonseekable_open(inode, file);
5157bd1d409SAlexander Shishkin 
5167bd1d409SAlexander Shishkin err_free:
517a0ebf519SJohan Hovold 	kfree(stmf);
518a0ebf519SJohan Hovold err_put_device:
519f7c81c71SAlexander Shishkin 	/* matches class_find_device() above */
520f7c81c71SAlexander Shishkin 	put_device(dev);
5217bd1d409SAlexander Shishkin 
5227bd1d409SAlexander Shishkin 	return err;
5237bd1d409SAlexander Shishkin }
5247bd1d409SAlexander Shishkin 
5257bd1d409SAlexander Shishkin static int stm_char_release(struct inode *inode, struct file *file)
5267bd1d409SAlexander Shishkin {
5277bd1d409SAlexander Shishkin 	struct stm_file *stmf = file->private_data;
528cc842407SAlexander Shishkin 	struct stm_device *stm = stmf->stm;
5297bd1d409SAlexander Shishkin 
530cc842407SAlexander Shishkin 	if (stm->data->unlink)
531cc842407SAlexander Shishkin 		stm->data->unlink(stm->data, stmf->output.master,
532cc842407SAlexander Shishkin 				  stmf->output.channel);
533cc842407SAlexander Shishkin 
534cc842407SAlexander Shishkin 	stm_output_free(stm, &stmf->output);
535f7c81c71SAlexander Shishkin 
536f7c81c71SAlexander Shishkin 	/*
537f7c81c71SAlexander Shishkin 	 * matches the stm_char_open()'s
538f7c81c71SAlexander Shishkin 	 * class_find_device() + try_module_get()
539f7c81c71SAlexander Shishkin 	 */
540cc842407SAlexander Shishkin 	stm_put_device(stm);
5417bd1d409SAlexander Shishkin 	kfree(stmf);
5427bd1d409SAlexander Shishkin 
5437bd1d409SAlexander Shishkin 	return 0;
5447bd1d409SAlexander Shishkin }
5457bd1d409SAlexander Shishkin 
546cb6102bdSAlexander Shishkin static int
547cb6102bdSAlexander Shishkin stm_assign_first_policy(struct stm_device *stm, struct stm_output *output,
548cb6102bdSAlexander Shishkin 			char **ids, unsigned int width)
5497bd1d409SAlexander Shishkin {
550cb6102bdSAlexander Shishkin 	struct stp_policy_node *pn;
551cb6102bdSAlexander Shishkin 	int err, n;
5527bd1d409SAlexander Shishkin 
553cb6102bdSAlexander Shishkin 	/*
554cb6102bdSAlexander Shishkin 	 * On success, stp_policy_node_lookup() will return holding the
555cb6102bdSAlexander Shishkin 	 * configfs subsystem mutex, which is then released in
556cb6102bdSAlexander Shishkin 	 * stp_policy_node_put(). This allows the pdrv->output_open() in
557cb6102bdSAlexander Shishkin 	 * stm_output_assign() to serialize against the attribute accessors.
558cb6102bdSAlexander Shishkin 	 */
559cb6102bdSAlexander Shishkin 	for (n = 0, pn = NULL; ids[n] && !pn; n++)
560cb6102bdSAlexander Shishkin 		pn = stp_policy_node_lookup(stm, ids[n]);
5617bd1d409SAlexander Shishkin 
562cb6102bdSAlexander Shishkin 	if (!pn)
563cb6102bdSAlexander Shishkin 		return -EINVAL;
5647bd1d409SAlexander Shishkin 
565cb6102bdSAlexander Shishkin 	err = stm_output_assign(stm, width, pn, output);
5667bd1d409SAlexander Shishkin 
567cb6102bdSAlexander Shishkin 	stp_policy_node_put(pn);
568cb6102bdSAlexander Shishkin 
569cb6102bdSAlexander Shishkin 	return err;
5707bd1d409SAlexander Shishkin }
5717bd1d409SAlexander Shishkin 
572d279a380SAlexander Shishkin /**
573d279a380SAlexander Shishkin  * stm_data_write() - send the given payload as data packets
574d279a380SAlexander Shishkin  * @data:	stm driver's data
575d279a380SAlexander Shishkin  * @m:		STP master
576d279a380SAlexander Shishkin  * @c:		STP channel
577d279a380SAlexander Shishkin  * @ts_first:	timestamp the first packet
578d279a380SAlexander Shishkin  * @buf:	data payload buffer
579d279a380SAlexander Shishkin  * @count:	data payload size
580d279a380SAlexander Shishkin  */
581d279a380SAlexander Shishkin ssize_t notrace stm_data_write(struct stm_data *data, unsigned int m,
582d279a380SAlexander Shishkin 			       unsigned int c, bool ts_first, const void *buf,
583d279a380SAlexander Shishkin 			       size_t count)
584d279a380SAlexander Shishkin {
585d279a380SAlexander Shishkin 	unsigned int flags = ts_first ? STP_PACKET_TIMESTAMPED : 0;
586d279a380SAlexander Shishkin 	ssize_t sz;
587d279a380SAlexander Shishkin 	size_t pos;
588d279a380SAlexander Shishkin 
589d279a380SAlexander Shishkin 	for (pos = 0, sz = 0; pos < count; pos += sz) {
590d279a380SAlexander Shishkin 		sz = min_t(unsigned int, count - pos, 8);
591d279a380SAlexander Shishkin 		sz = data->packet(data, m, c, STP_PACKET_DATA, flags, sz,
592d279a380SAlexander Shishkin 				  &((u8 *)buf)[pos]);
593d279a380SAlexander Shishkin 		if (sz <= 0)
594d279a380SAlexander Shishkin 			break;
595d279a380SAlexander Shishkin 
596d279a380SAlexander Shishkin 		if (ts_first) {
597d279a380SAlexander Shishkin 			flags = 0;
598d279a380SAlexander Shishkin 			ts_first = false;
599d279a380SAlexander Shishkin 		}
600d279a380SAlexander Shishkin 	}
601d279a380SAlexander Shishkin 
602d279a380SAlexander Shishkin 	return sz < 0 ? sz : pos;
603d279a380SAlexander Shishkin }
604d279a380SAlexander Shishkin EXPORT_SYMBOL_GPL(stm_data_write);
605d279a380SAlexander Shishkin 
6069dfed80dSChunyan Zhang static ssize_t notrace stm_write(struct stm_data *data, unsigned int master,
6077bd1d409SAlexander Shishkin 			  unsigned int channel, const char *buf, size_t count)
6087bd1d409SAlexander Shishkin {
609d279a380SAlexander Shishkin 	const unsigned char nil = 0;
6107bd1d409SAlexander Shishkin 	ssize_t sz;
6117bd1d409SAlexander Shishkin 
612d279a380SAlexander Shishkin 	sz = stm_data_write(data, master, channel, true, buf, count);
613d279a380SAlexander Shishkin 	if (sz > 0)
614d279a380SAlexander Shishkin 		data->packet(data, master, channel, STP_PACKET_FLAG, 0, 0,
615d279a380SAlexander Shishkin 			     &nil);
616f8560a9bSAlexander Shishkin 
617d279a380SAlexander Shishkin 	return sz;
6187bd1d409SAlexander Shishkin }
6197bd1d409SAlexander Shishkin 
6207bd1d409SAlexander Shishkin static ssize_t stm_char_write(struct file *file, const char __user *buf,
6217bd1d409SAlexander Shishkin 			      size_t count, loff_t *ppos)
6227bd1d409SAlexander Shishkin {
6237bd1d409SAlexander Shishkin 	struct stm_file *stmf = file->private_data;
6247bd1d409SAlexander Shishkin 	struct stm_device *stm = stmf->stm;
6257bd1d409SAlexander Shishkin 	char *kbuf;
6267bd1d409SAlexander Shishkin 	int err;
6277bd1d409SAlexander Shishkin 
628f08b1826SAlexander Shishkin 	if (count + 1 > PAGE_SIZE)
629f08b1826SAlexander Shishkin 		count = PAGE_SIZE - 1;
630f08b1826SAlexander Shishkin 
6317bd1d409SAlexander Shishkin 	/*
632cb6102bdSAlexander Shishkin 	 * If no m/c have been assigned to this writer up to this
633cb6102bdSAlexander Shishkin 	 * point, try to use the task name and "default" policy entries.
6347bd1d409SAlexander Shishkin 	 */
6357bd1d409SAlexander Shishkin 	if (!stmf->output.nr_chans) {
636cb6102bdSAlexander Shishkin 		char comm[sizeof(current->comm)];
637cb6102bdSAlexander Shishkin 		char *ids[] = { comm, "default", NULL };
638cb6102bdSAlexander Shishkin 
639cb6102bdSAlexander Shishkin 		get_task_comm(comm, current);
640cb6102bdSAlexander Shishkin 
641cb6102bdSAlexander Shishkin 		err = stm_assign_first_policy(stmf->stm, &stmf->output, ids, 1);
6427bd1d409SAlexander Shishkin 		/*
6437bd1d409SAlexander Shishkin 		 * EBUSY means that somebody else just assigned this
6447bd1d409SAlexander Shishkin 		 * output, which is just fine for write()
6457bd1d409SAlexander Shishkin 		 */
646cb6102bdSAlexander Shishkin 		if (err)
6477bd1d409SAlexander Shishkin 			return err;
6487bd1d409SAlexander Shishkin 	}
6497bd1d409SAlexander Shishkin 
6507bd1d409SAlexander Shishkin 	kbuf = kmalloc(count + 1, GFP_KERNEL);
6517bd1d409SAlexander Shishkin 	if (!kbuf)
6527bd1d409SAlexander Shishkin 		return -ENOMEM;
6537bd1d409SAlexander Shishkin 
6547bd1d409SAlexander Shishkin 	err = copy_from_user(kbuf, buf, count);
6557bd1d409SAlexander Shishkin 	if (err) {
6567bd1d409SAlexander Shishkin 		kfree(kbuf);
6577bd1d409SAlexander Shishkin 		return -EFAULT;
6587bd1d409SAlexander Shishkin 	}
6597bd1d409SAlexander Shishkin 
6608e0469a4SAlexander Shishkin 	pm_runtime_get_sync(&stm->dev);
6618e0469a4SAlexander Shishkin 
662f8560a9bSAlexander Shishkin 	count = stm_write(stm->data, stmf->output.master, stmf->output.channel,
663f8560a9bSAlexander Shishkin 			  kbuf, count);
6647bd1d409SAlexander Shishkin 
6658e0469a4SAlexander Shishkin 	pm_runtime_mark_last_busy(&stm->dev);
6668e0469a4SAlexander Shishkin 	pm_runtime_put_autosuspend(&stm->dev);
6677bd1d409SAlexander Shishkin 	kfree(kbuf);
6687bd1d409SAlexander Shishkin 
6697bd1d409SAlexander Shishkin 	return count;
6707bd1d409SAlexander Shishkin }
6717bd1d409SAlexander Shishkin 
6728e0469a4SAlexander Shishkin static void stm_mmap_open(struct vm_area_struct *vma)
6738e0469a4SAlexander Shishkin {
6748e0469a4SAlexander Shishkin 	struct stm_file *stmf = vma->vm_file->private_data;
6758e0469a4SAlexander Shishkin 	struct stm_device *stm = stmf->stm;
6768e0469a4SAlexander Shishkin 
6778e0469a4SAlexander Shishkin 	pm_runtime_get(&stm->dev);
6788e0469a4SAlexander Shishkin }
6798e0469a4SAlexander Shishkin 
6808e0469a4SAlexander Shishkin static void stm_mmap_close(struct vm_area_struct *vma)
6818e0469a4SAlexander Shishkin {
6828e0469a4SAlexander Shishkin 	struct stm_file *stmf = vma->vm_file->private_data;
6838e0469a4SAlexander Shishkin 	struct stm_device *stm = stmf->stm;
6848e0469a4SAlexander Shishkin 
6858e0469a4SAlexander Shishkin 	pm_runtime_mark_last_busy(&stm->dev);
6868e0469a4SAlexander Shishkin 	pm_runtime_put_autosuspend(&stm->dev);
6878e0469a4SAlexander Shishkin }
6888e0469a4SAlexander Shishkin 
6898e0469a4SAlexander Shishkin static const struct vm_operations_struct stm_mmap_vmops = {
6908e0469a4SAlexander Shishkin 	.open	= stm_mmap_open,
6918e0469a4SAlexander Shishkin 	.close	= stm_mmap_close,
6928e0469a4SAlexander Shishkin };
6938e0469a4SAlexander Shishkin 
6947bd1d409SAlexander Shishkin static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)
6957bd1d409SAlexander Shishkin {
6967bd1d409SAlexander Shishkin 	struct stm_file *stmf = file->private_data;
6977bd1d409SAlexander Shishkin 	struct stm_device *stm = stmf->stm;
6987bd1d409SAlexander Shishkin 	unsigned long size, phys;
6997bd1d409SAlexander Shishkin 
7007bd1d409SAlexander Shishkin 	if (!stm->data->mmio_addr)
7017bd1d409SAlexander Shishkin 		return -EOPNOTSUPP;
7027bd1d409SAlexander Shishkin 
7037bd1d409SAlexander Shishkin 	if (vma->vm_pgoff)
7047bd1d409SAlexander Shishkin 		return -EINVAL;
7057bd1d409SAlexander Shishkin 
7067bd1d409SAlexander Shishkin 	size = vma->vm_end - vma->vm_start;
7077bd1d409SAlexander Shishkin 
7087bd1d409SAlexander Shishkin 	if (stmf->output.nr_chans * stm->data->sw_mmiosz != size)
7097bd1d409SAlexander Shishkin 		return -EINVAL;
7107bd1d409SAlexander Shishkin 
7117bd1d409SAlexander Shishkin 	phys = stm->data->mmio_addr(stm->data, stmf->output.master,
7127bd1d409SAlexander Shishkin 				    stmf->output.channel,
7137bd1d409SAlexander Shishkin 				    stmf->output.nr_chans);
7147bd1d409SAlexander Shishkin 
7157bd1d409SAlexander Shishkin 	if (!phys)
7167bd1d409SAlexander Shishkin 		return -EINVAL;
7177bd1d409SAlexander Shishkin 
7188e0469a4SAlexander Shishkin 	pm_runtime_get_sync(&stm->dev);
7198e0469a4SAlexander Shishkin 
7207bd1d409SAlexander Shishkin 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
7217bd1d409SAlexander Shishkin 	vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
7228e0469a4SAlexander Shishkin 	vma->vm_ops = &stm_mmap_vmops;
7237bd1d409SAlexander Shishkin 	vm_iomap_memory(vma, phys, size);
7247bd1d409SAlexander Shishkin 
7257bd1d409SAlexander Shishkin 	return 0;
7267bd1d409SAlexander Shishkin }
7277bd1d409SAlexander Shishkin 
7287bd1d409SAlexander Shishkin static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg)
7297bd1d409SAlexander Shishkin {
7307bd1d409SAlexander Shishkin 	struct stm_device *stm = stmf->stm;
7317bd1d409SAlexander Shishkin 	struct stp_policy_id *id;
732cb6102bdSAlexander Shishkin 	char *ids[] = { NULL, NULL };
7337bd1d409SAlexander Shishkin 	int ret = -EINVAL;
7347bd1d409SAlexander Shishkin 	u32 size;
7357bd1d409SAlexander Shishkin 
7367bd1d409SAlexander Shishkin 	if (stmf->output.nr_chans)
7377bd1d409SAlexander Shishkin 		return -EBUSY;
7387bd1d409SAlexander Shishkin 
7397bd1d409SAlexander Shishkin 	if (copy_from_user(&size, arg, sizeof(size)))
7407bd1d409SAlexander Shishkin 		return -EFAULT;
7417bd1d409SAlexander Shishkin 
74271c488f3SDan Carpenter 	if (size < sizeof(*id) || size >= PATH_MAX + sizeof(*id))
7437bd1d409SAlexander Shishkin 		return -EINVAL;
7447bd1d409SAlexander Shishkin 
7457bd1d409SAlexander Shishkin 	/*
7467bd1d409SAlexander Shishkin 	 * size + 1 to make sure the .id string at the bottom is terminated,
7477bd1d409SAlexander Shishkin 	 * which is also why memdup_user() is not useful here
7487bd1d409SAlexander Shishkin 	 */
7497bd1d409SAlexander Shishkin 	id = kzalloc(size + 1, GFP_KERNEL);
7507bd1d409SAlexander Shishkin 	if (!id)
7517bd1d409SAlexander Shishkin 		return -ENOMEM;
7527bd1d409SAlexander Shishkin 
7537bd1d409SAlexander Shishkin 	if (copy_from_user(id, arg, size)) {
7547bd1d409SAlexander Shishkin 		ret = -EFAULT;
7557bd1d409SAlexander Shishkin 		goto err_free;
7567bd1d409SAlexander Shishkin 	}
7577bd1d409SAlexander Shishkin 
7587bd1d409SAlexander Shishkin 	if (id->__reserved_0 || id->__reserved_1)
7597bd1d409SAlexander Shishkin 		goto err_free;
7607bd1d409SAlexander Shishkin 
7617bd1d409SAlexander Shishkin 	if (id->width < 1 ||
7627bd1d409SAlexander Shishkin 	    id->width > PAGE_SIZE / stm->data->sw_mmiosz)
7637bd1d409SAlexander Shishkin 		goto err_free;
7647bd1d409SAlexander Shishkin 
765cb6102bdSAlexander Shishkin 	ids[0] = id->id;
766cb6102bdSAlexander Shishkin 	ret = stm_assign_first_policy(stmf->stm, &stmf->output, ids,
767cb6102bdSAlexander Shishkin 				      id->width);
7687bd1d409SAlexander Shishkin 	if (ret)
7697bd1d409SAlexander Shishkin 		goto err_free;
7707bd1d409SAlexander Shishkin 
7717bd1d409SAlexander Shishkin 	if (stm->data->link)
7727bd1d409SAlexander Shishkin 		ret = stm->data->link(stm->data, stmf->output.master,
7737bd1d409SAlexander Shishkin 				      stmf->output.channel);
7747bd1d409SAlexander Shishkin 
775f7c81c71SAlexander Shishkin 	if (ret)
7767bd1d409SAlexander Shishkin 		stm_output_free(stmf->stm, &stmf->output);
7777bd1d409SAlexander Shishkin 
7787bd1d409SAlexander Shishkin err_free:
7797bd1d409SAlexander Shishkin 	kfree(id);
7807bd1d409SAlexander Shishkin 
7817bd1d409SAlexander Shishkin 	return ret;
7827bd1d409SAlexander Shishkin }
7837bd1d409SAlexander Shishkin 
7847bd1d409SAlexander Shishkin static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg)
7857bd1d409SAlexander Shishkin {
7867bd1d409SAlexander Shishkin 	struct stp_policy_id id = {
7877bd1d409SAlexander Shishkin 		.size		= sizeof(id),
7887bd1d409SAlexander Shishkin 		.master		= stmf->output.master,
7897bd1d409SAlexander Shishkin 		.channel	= stmf->output.channel,
7907bd1d409SAlexander Shishkin 		.width		= stmf->output.nr_chans,
7917bd1d409SAlexander Shishkin 		.__reserved_0	= 0,
7927bd1d409SAlexander Shishkin 		.__reserved_1	= 0,
7937bd1d409SAlexander Shishkin 	};
7947bd1d409SAlexander Shishkin 
7957bd1d409SAlexander Shishkin 	return copy_to_user(arg, &id, id.size) ? -EFAULT : 0;
7967bd1d409SAlexander Shishkin }
7977bd1d409SAlexander Shishkin 
7987bd1d409SAlexander Shishkin static long
7997bd1d409SAlexander Shishkin stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
8007bd1d409SAlexander Shishkin {
8017bd1d409SAlexander Shishkin 	struct stm_file *stmf = file->private_data;
8027bd1d409SAlexander Shishkin 	struct stm_data *stm_data = stmf->stm->data;
8037bd1d409SAlexander Shishkin 	int err = -ENOTTY;
8047bd1d409SAlexander Shishkin 	u64 options;
8057bd1d409SAlexander Shishkin 
8067bd1d409SAlexander Shishkin 	switch (cmd) {
8077bd1d409SAlexander Shishkin 	case STP_POLICY_ID_SET:
8087bd1d409SAlexander Shishkin 		err = stm_char_policy_set_ioctl(stmf, (void __user *)arg);
8097bd1d409SAlexander Shishkin 		if (err)
8107bd1d409SAlexander Shishkin 			return err;
8117bd1d409SAlexander Shishkin 
8127bd1d409SAlexander Shishkin 		return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
8137bd1d409SAlexander Shishkin 
8147bd1d409SAlexander Shishkin 	case STP_POLICY_ID_GET:
8157bd1d409SAlexander Shishkin 		return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
8167bd1d409SAlexander Shishkin 
8177bd1d409SAlexander Shishkin 	case STP_SET_OPTIONS:
8187bd1d409SAlexander Shishkin 		if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64)))
8197bd1d409SAlexander Shishkin 			return -EFAULT;
8207bd1d409SAlexander Shishkin 
8217bd1d409SAlexander Shishkin 		if (stm_data->set_options)
8227bd1d409SAlexander Shishkin 			err = stm_data->set_options(stm_data,
8237bd1d409SAlexander Shishkin 						    stmf->output.master,
8247bd1d409SAlexander Shishkin 						    stmf->output.channel,
8257bd1d409SAlexander Shishkin 						    stmf->output.nr_chans,
8267bd1d409SAlexander Shishkin 						    options);
8277bd1d409SAlexander Shishkin 
8287bd1d409SAlexander Shishkin 		break;
8297bd1d409SAlexander Shishkin 	default:
8307bd1d409SAlexander Shishkin 		break;
8317bd1d409SAlexander Shishkin 	}
8327bd1d409SAlexander Shishkin 
8337bd1d409SAlexander Shishkin 	return err;
8347bd1d409SAlexander Shishkin }
8357bd1d409SAlexander Shishkin 
8367bd1d409SAlexander Shishkin #ifdef CONFIG_COMPAT
8377bd1d409SAlexander Shishkin static long
8387bd1d409SAlexander Shishkin stm_char_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
8397bd1d409SAlexander Shishkin {
8407bd1d409SAlexander Shishkin 	return stm_char_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
8417bd1d409SAlexander Shishkin }
8427bd1d409SAlexander Shishkin #else
8437bd1d409SAlexander Shishkin #define stm_char_compat_ioctl	NULL
8447bd1d409SAlexander Shishkin #endif
8457bd1d409SAlexander Shishkin 
8467bd1d409SAlexander Shishkin static const struct file_operations stm_fops = {
8477bd1d409SAlexander Shishkin 	.open		= stm_char_open,
8487bd1d409SAlexander Shishkin 	.release	= stm_char_release,
8497bd1d409SAlexander Shishkin 	.write		= stm_char_write,
8507bd1d409SAlexander Shishkin 	.mmap		= stm_char_mmap,
8517bd1d409SAlexander Shishkin 	.unlocked_ioctl	= stm_char_ioctl,
8527bd1d409SAlexander Shishkin 	.compat_ioctl	= stm_char_compat_ioctl,
8537bd1d409SAlexander Shishkin 	.llseek		= no_llseek,
8547bd1d409SAlexander Shishkin };
8557bd1d409SAlexander Shishkin 
8567bd1d409SAlexander Shishkin static void stm_device_release(struct device *dev)
8577bd1d409SAlexander Shishkin {
8587bd1d409SAlexander Shishkin 	struct stm_device *stm = to_stm_device(dev);
8597bd1d409SAlexander Shishkin 
860b5e2ced9SAlexander Shishkin 	vfree(stm);
8617bd1d409SAlexander Shishkin }
8627bd1d409SAlexander Shishkin 
8637bd1d409SAlexander Shishkin int stm_register_device(struct device *parent, struct stm_data *stm_data,
8647bd1d409SAlexander Shishkin 			struct module *owner)
8657bd1d409SAlexander Shishkin {
8667bd1d409SAlexander Shishkin 	struct stm_device *stm;
8677bd1d409SAlexander Shishkin 	unsigned int nmasters;
8687bd1d409SAlexander Shishkin 	int err = -ENOMEM;
8697bd1d409SAlexander Shishkin 
8707bd1d409SAlexander Shishkin 	if (!stm_core_up)
8717bd1d409SAlexander Shishkin 		return -EPROBE_DEFER;
8727bd1d409SAlexander Shishkin 
8737bd1d409SAlexander Shishkin 	if (!stm_data->packet || !stm_data->sw_nchannels)
8747bd1d409SAlexander Shishkin 		return -EINVAL;
8757bd1d409SAlexander Shishkin 
8767b3bb0e7SChunyan Zhang 	nmasters = stm_data->sw_end - stm_data->sw_start + 1;
877b5e2ced9SAlexander Shishkin 	stm = vzalloc(sizeof(*stm) + nmasters * sizeof(void *));
8787bd1d409SAlexander Shishkin 	if (!stm)
8797bd1d409SAlexander Shishkin 		return -ENOMEM;
8807bd1d409SAlexander Shishkin 
8817bd1d409SAlexander Shishkin 	stm->major = register_chrdev(0, stm_data->name, &stm_fops);
8827bd1d409SAlexander Shishkin 	if (stm->major < 0)
8837bd1d409SAlexander Shishkin 		goto err_free;
8847bd1d409SAlexander Shishkin 
8857bd1d409SAlexander Shishkin 	device_initialize(&stm->dev);
8867bd1d409SAlexander Shishkin 	stm->dev.devt = MKDEV(stm->major, 0);
8877bd1d409SAlexander Shishkin 	stm->dev.class = &stm_class;
8887bd1d409SAlexander Shishkin 	stm->dev.parent = parent;
8897bd1d409SAlexander Shishkin 	stm->dev.release = stm_device_release;
8907bd1d409SAlexander Shishkin 
891389b6699SAlexander Shishkin 	mutex_init(&stm->link_mutex);
892389b6699SAlexander Shishkin 	spin_lock_init(&stm->link_lock);
893389b6699SAlexander Shishkin 	INIT_LIST_HEAD(&stm->link_list);
894389b6699SAlexander Shishkin 
895389b6699SAlexander Shishkin 	/* initialize the object before it is accessible via sysfs */
896389b6699SAlexander Shishkin 	spin_lock_init(&stm->mc_lock);
897389b6699SAlexander Shishkin 	mutex_init(&stm->policy_mutex);
898389b6699SAlexander Shishkin 	stm->sw_nmasters = nmasters;
899389b6699SAlexander Shishkin 	stm->owner = owner;
900389b6699SAlexander Shishkin 	stm->data = stm_data;
901389b6699SAlexander Shishkin 	stm_data->stm = stm;
902389b6699SAlexander Shishkin 
9037bd1d409SAlexander Shishkin 	err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name);
9047bd1d409SAlexander Shishkin 	if (err)
9057bd1d409SAlexander Shishkin 		goto err_device;
9067bd1d409SAlexander Shishkin 
9077bd1d409SAlexander Shishkin 	err = device_add(&stm->dev);
9087bd1d409SAlexander Shishkin 	if (err)
9097bd1d409SAlexander Shishkin 		goto err_device;
9107bd1d409SAlexander Shishkin 
9118e0469a4SAlexander Shishkin 	/*
9128e0469a4SAlexander Shishkin 	 * Use delayed autosuspend to avoid bouncing back and forth
9138e0469a4SAlexander Shishkin 	 * on recurring character device writes, with the initial
9148e0469a4SAlexander Shishkin 	 * delay time of 2 seconds.
9158e0469a4SAlexander Shishkin 	 */
9168e0469a4SAlexander Shishkin 	pm_runtime_no_callbacks(&stm->dev);
9178e0469a4SAlexander Shishkin 	pm_runtime_use_autosuspend(&stm->dev);
9188e0469a4SAlexander Shishkin 	pm_runtime_set_autosuspend_delay(&stm->dev, 2000);
9198e0469a4SAlexander Shishkin 	pm_runtime_set_suspended(&stm->dev);
9208e0469a4SAlexander Shishkin 	pm_runtime_enable(&stm->dev);
9218e0469a4SAlexander Shishkin 
9227bd1d409SAlexander Shishkin 	return 0;
9237bd1d409SAlexander Shishkin 
9247bd1d409SAlexander Shishkin err_device:
925cbe4a61dSAlexander Shishkin 	unregister_chrdev(stm->major, stm_data->name);
926cbe4a61dSAlexander Shishkin 
927f7c81c71SAlexander Shishkin 	/* matches device_initialize() above */
9287bd1d409SAlexander Shishkin 	put_device(&stm->dev);
9297bd1d409SAlexander Shishkin err_free:
930b5e2ced9SAlexander Shishkin 	vfree(stm);
9317bd1d409SAlexander Shishkin 
9327bd1d409SAlexander Shishkin 	return err;
9337bd1d409SAlexander Shishkin }
9347bd1d409SAlexander Shishkin EXPORT_SYMBOL_GPL(stm_register_device);
9357bd1d409SAlexander Shishkin 
936b4ca34aaSAlexander Shishkin static int __stm_source_link_drop(struct stm_source_device *src,
9377bd1d409SAlexander Shishkin 				  struct stm_device *stm);
9387bd1d409SAlexander Shishkin 
9397bd1d409SAlexander Shishkin void stm_unregister_device(struct stm_data *stm_data)
9407bd1d409SAlexander Shishkin {
9417bd1d409SAlexander Shishkin 	struct stm_device *stm = stm_data->stm;
9427bd1d409SAlexander Shishkin 	struct stm_source_device *src, *iter;
943b4ca34aaSAlexander Shishkin 	int i, ret;
9447bd1d409SAlexander Shishkin 
9458e0469a4SAlexander Shishkin 	pm_runtime_dont_use_autosuspend(&stm->dev);
9468e0469a4SAlexander Shishkin 	pm_runtime_disable(&stm->dev);
9478e0469a4SAlexander Shishkin 
948c74f7e82SAlexander Shishkin 	mutex_lock(&stm->link_mutex);
9497bd1d409SAlexander Shishkin 	list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) {
950b4ca34aaSAlexander Shishkin 		ret = __stm_source_link_drop(src, stm);
951b4ca34aaSAlexander Shishkin 		/*
952b4ca34aaSAlexander Shishkin 		 * src <-> stm link must not change under the same
953b4ca34aaSAlexander Shishkin 		 * stm::link_mutex, so complain loudly if it has;
954b4ca34aaSAlexander Shishkin 		 * also in this situation ret!=0 means this src is
955b4ca34aaSAlexander Shishkin 		 * not connected to this stm and it should be otherwise
956b4ca34aaSAlexander Shishkin 		 * safe to proceed with the tear-down of stm.
957b4ca34aaSAlexander Shishkin 		 */
958b4ca34aaSAlexander Shishkin 		WARN_ON_ONCE(ret);
9597bd1d409SAlexander Shishkin 	}
960c74f7e82SAlexander Shishkin 	mutex_unlock(&stm->link_mutex);
9617bd1d409SAlexander Shishkin 
9627bd1d409SAlexander Shishkin 	synchronize_srcu(&stm_source_srcu);
9637bd1d409SAlexander Shishkin 
9647bd1d409SAlexander Shishkin 	unregister_chrdev(stm->major, stm_data->name);
9657bd1d409SAlexander Shishkin 
9667bd1d409SAlexander Shishkin 	mutex_lock(&stm->policy_mutex);
9677bd1d409SAlexander Shishkin 	if (stm->policy)
9687bd1d409SAlexander Shishkin 		stp_policy_unbind(stm->policy);
9697bd1d409SAlexander Shishkin 	mutex_unlock(&stm->policy_mutex);
9707bd1d409SAlexander Shishkin 
97173a3ed19SChunyan Zhang 	for (i = stm->data->sw_start; i <= stm->data->sw_end; i++)
9727bd1d409SAlexander Shishkin 		stp_master_free(stm, i);
9737bd1d409SAlexander Shishkin 
9747bd1d409SAlexander Shishkin 	device_unregister(&stm->dev);
9757bd1d409SAlexander Shishkin 	stm_data->stm = NULL;
9767bd1d409SAlexander Shishkin }
9777bd1d409SAlexander Shishkin EXPORT_SYMBOL_GPL(stm_unregister_device);
9787bd1d409SAlexander Shishkin 
979c74f7e82SAlexander Shishkin /*
980c74f7e82SAlexander Shishkin  * stm::link_list access serialization uses a spinlock and a mutex; holding
981c74f7e82SAlexander Shishkin  * either of them guarantees that the list is stable; modification requires
982c74f7e82SAlexander Shishkin  * holding both of them.
983c74f7e82SAlexander Shishkin  *
984c74f7e82SAlexander Shishkin  * Lock ordering is as follows:
985c74f7e82SAlexander Shishkin  *   stm::link_mutex
986c74f7e82SAlexander Shishkin  *     stm::link_lock
987c74f7e82SAlexander Shishkin  *       src::link_lock
988c74f7e82SAlexander Shishkin  */
989c74f7e82SAlexander Shishkin 
9907bd1d409SAlexander Shishkin /**
9917bd1d409SAlexander Shishkin  * stm_source_link_add() - connect an stm_source device to an stm device
9927bd1d409SAlexander Shishkin  * @src:	stm_source device
9937bd1d409SAlexander Shishkin  * @stm:	stm device
9947bd1d409SAlexander Shishkin  *
9957bd1d409SAlexander Shishkin  * This function establishes a link from stm_source to an stm device so that
9967bd1d409SAlexander Shishkin  * the former can send out trace data to the latter.
9977bd1d409SAlexander Shishkin  *
9987bd1d409SAlexander Shishkin  * Return:	0 on success, -errno otherwise.
9997bd1d409SAlexander Shishkin  */
10007bd1d409SAlexander Shishkin static int stm_source_link_add(struct stm_source_device *src,
10017bd1d409SAlexander Shishkin 			       struct stm_device *stm)
10027bd1d409SAlexander Shishkin {
1003cb6102bdSAlexander Shishkin 	char *ids[] = { NULL, "default", NULL };
1004cb6102bdSAlexander Shishkin 	int err = -ENOMEM;
10057bd1d409SAlexander Shishkin 
1006c74f7e82SAlexander Shishkin 	mutex_lock(&stm->link_mutex);
10077bd1d409SAlexander Shishkin 	spin_lock(&stm->link_lock);
10087bd1d409SAlexander Shishkin 	spin_lock(&src->link_lock);
10097bd1d409SAlexander Shishkin 
10107bd1d409SAlexander Shishkin 	/* src->link is dereferenced under stm_source_srcu but not the list */
10117bd1d409SAlexander Shishkin 	rcu_assign_pointer(src->link, stm);
10127bd1d409SAlexander Shishkin 	list_add_tail(&src->link_entry, &stm->link_list);
10137bd1d409SAlexander Shishkin 
10147bd1d409SAlexander Shishkin 	spin_unlock(&src->link_lock);
10157bd1d409SAlexander Shishkin 	spin_unlock(&stm->link_lock);
1016c74f7e82SAlexander Shishkin 	mutex_unlock(&stm->link_mutex);
10177bd1d409SAlexander Shishkin 
1018cb6102bdSAlexander Shishkin 	ids[0] = kstrdup(src->data->name, GFP_KERNEL);
1019cb6102bdSAlexander Shishkin 	if (!ids[0])
1020cb6102bdSAlexander Shishkin 		goto fail_detach;
10217bd1d409SAlexander Shishkin 
1022cb6102bdSAlexander Shishkin 	err = stm_assign_first_policy(stm, &src->output, ids,
1023cb6102bdSAlexander Shishkin 				      src->data->nr_chans);
1024cb6102bdSAlexander Shishkin 	kfree(ids[0]);
10257bd1d409SAlexander Shishkin 
10267bd1d409SAlexander Shishkin 	if (err)
10277bd1d409SAlexander Shishkin 		goto fail_detach;
10287bd1d409SAlexander Shishkin 
10297bd1d409SAlexander Shishkin 	/* this is to notify the STM device that a new link has been made */
10307bd1d409SAlexander Shishkin 	if (stm->data->link)
10317bd1d409SAlexander Shishkin 		err = stm->data->link(stm->data, src->output.master,
10327bd1d409SAlexander Shishkin 				      src->output.channel);
10337bd1d409SAlexander Shishkin 
10347bd1d409SAlexander Shishkin 	if (err)
10357bd1d409SAlexander Shishkin 		goto fail_free_output;
10367bd1d409SAlexander Shishkin 
10377bd1d409SAlexander Shishkin 	/* this is to let the source carry out all necessary preparations */
10387bd1d409SAlexander Shishkin 	if (src->data->link)
10397bd1d409SAlexander Shishkin 		src->data->link(src->data);
10407bd1d409SAlexander Shishkin 
10417bd1d409SAlexander Shishkin 	return 0;
10427bd1d409SAlexander Shishkin 
10437bd1d409SAlexander Shishkin fail_free_output:
10447bd1d409SAlexander Shishkin 	stm_output_free(stm, &src->output);
10457bd1d409SAlexander Shishkin 
10467bd1d409SAlexander Shishkin fail_detach:
1047c74f7e82SAlexander Shishkin 	mutex_lock(&stm->link_mutex);
10487bd1d409SAlexander Shishkin 	spin_lock(&stm->link_lock);
10497bd1d409SAlexander Shishkin 	spin_lock(&src->link_lock);
10507bd1d409SAlexander Shishkin 
10517bd1d409SAlexander Shishkin 	rcu_assign_pointer(src->link, NULL);
10527bd1d409SAlexander Shishkin 	list_del_init(&src->link_entry);
10537bd1d409SAlexander Shishkin 
10547bd1d409SAlexander Shishkin 	spin_unlock(&src->link_lock);
10557bd1d409SAlexander Shishkin 	spin_unlock(&stm->link_lock);
1056c74f7e82SAlexander Shishkin 	mutex_unlock(&stm->link_mutex);
10577bd1d409SAlexander Shishkin 
10587bd1d409SAlexander Shishkin 	return err;
10597bd1d409SAlexander Shishkin }
10607bd1d409SAlexander Shishkin 
10617bd1d409SAlexander Shishkin /**
10627bd1d409SAlexander Shishkin  * __stm_source_link_drop() - detach stm_source from an stm device
10637bd1d409SAlexander Shishkin  * @src:	stm_source device
10647bd1d409SAlexander Shishkin  * @stm:	stm device
10657bd1d409SAlexander Shishkin  *
10667bd1d409SAlexander Shishkin  * If @stm is @src::link, disconnect them from one another and put the
10677bd1d409SAlexander Shishkin  * reference on the @stm device.
10687bd1d409SAlexander Shishkin  *
1069c74f7e82SAlexander Shishkin  * Caller must hold stm::link_mutex.
10707bd1d409SAlexander Shishkin  */
1071b4ca34aaSAlexander Shishkin static int __stm_source_link_drop(struct stm_source_device *src,
10727bd1d409SAlexander Shishkin 				  struct stm_device *stm)
10737bd1d409SAlexander Shishkin {
10740df771deSAlexander Shishkin 	struct stm_device *link;
1075b4ca34aaSAlexander Shishkin 	int ret = 0;
10760df771deSAlexander Shishkin 
1077c74f7e82SAlexander Shishkin 	lockdep_assert_held(&stm->link_mutex);
1078c74f7e82SAlexander Shishkin 
1079c74f7e82SAlexander Shishkin 	/* for stm::link_list modification, we hold both mutex and spinlock */
1080c74f7e82SAlexander Shishkin 	spin_lock(&stm->link_lock);
10817bd1d409SAlexander Shishkin 	spin_lock(&src->link_lock);
10820df771deSAlexander Shishkin 	link = srcu_dereference_check(src->link, &stm_source_srcu, 1);
1083b4ca34aaSAlexander Shishkin 
1084b4ca34aaSAlexander Shishkin 	/*
1085b4ca34aaSAlexander Shishkin 	 * The linked device may have changed since we last looked, because
1086b4ca34aaSAlexander Shishkin 	 * we weren't holding the src::link_lock back then; if this is the
1087b4ca34aaSAlexander Shishkin 	 * case, tell the caller to retry.
1088b4ca34aaSAlexander Shishkin 	 */
1089b4ca34aaSAlexander Shishkin 	if (link != stm) {
1090b4ca34aaSAlexander Shishkin 		ret = -EAGAIN;
10911810f2c4SAlexander Shishkin 		goto unlock;
1092b4ca34aaSAlexander Shishkin 	}
10937bd1d409SAlexander Shishkin 
10940df771deSAlexander Shishkin 	stm_output_free(link, &src->output);
10957bd1d409SAlexander Shishkin 	list_del_init(&src->link_entry);
10968e0469a4SAlexander Shishkin 	pm_runtime_mark_last_busy(&link->dev);
10978e0469a4SAlexander Shishkin 	pm_runtime_put_autosuspend(&link->dev);
10987bd1d409SAlexander Shishkin 	/* matches stm_find_device() from stm_source_link_store() */
10990df771deSAlexander Shishkin 	stm_put_device(link);
11007bd1d409SAlexander Shishkin 	rcu_assign_pointer(src->link, NULL);
11017bd1d409SAlexander Shishkin 
11021810f2c4SAlexander Shishkin unlock:
11037bd1d409SAlexander Shishkin 	spin_unlock(&src->link_lock);
1104c74f7e82SAlexander Shishkin 	spin_unlock(&stm->link_lock);
1105b4ca34aaSAlexander Shishkin 
1106cc842407SAlexander Shishkin 	/*
1107cc842407SAlexander Shishkin 	 * Call the unlink callbacks for both source and stm, when we know
1108cc842407SAlexander Shishkin 	 * that we have actually performed the unlinking.
1109cc842407SAlexander Shishkin 	 */
1110cc842407SAlexander Shishkin 	if (!ret) {
1111cc842407SAlexander Shishkin 		if (src->data->unlink)
1112b4ca34aaSAlexander Shishkin 			src->data->unlink(src->data);
1113b4ca34aaSAlexander Shishkin 
1114cc842407SAlexander Shishkin 		if (stm->data->unlink)
1115cc842407SAlexander Shishkin 			stm->data->unlink(stm->data, src->output.master,
1116cc842407SAlexander Shishkin 					  src->output.channel);
1117cc842407SAlexander Shishkin 	}
1118cc842407SAlexander Shishkin 
1119b4ca34aaSAlexander Shishkin 	return ret;
11207bd1d409SAlexander Shishkin }
11217bd1d409SAlexander Shishkin 
11227bd1d409SAlexander Shishkin /**
11237bd1d409SAlexander Shishkin  * stm_source_link_drop() - detach stm_source from its stm device
11247bd1d409SAlexander Shishkin  * @src:	stm_source device
11257bd1d409SAlexander Shishkin  *
11267bd1d409SAlexander Shishkin  * Unlinking means disconnecting from source's STM device; after this
11277bd1d409SAlexander Shishkin  * writes will be unsuccessful until it is linked to a new STM device.
11287bd1d409SAlexander Shishkin  *
11297bd1d409SAlexander Shishkin  * This will happen on "stm_source_link" sysfs attribute write to undo
11307bd1d409SAlexander Shishkin  * the existing link (if any), or on linked STM device's de-registration.
11317bd1d409SAlexander Shishkin  */
11327bd1d409SAlexander Shishkin static void stm_source_link_drop(struct stm_source_device *src)
11337bd1d409SAlexander Shishkin {
11347bd1d409SAlexander Shishkin 	struct stm_device *stm;
1135b4ca34aaSAlexander Shishkin 	int idx, ret;
11367bd1d409SAlexander Shishkin 
1137b4ca34aaSAlexander Shishkin retry:
11387bd1d409SAlexander Shishkin 	idx = srcu_read_lock(&stm_source_srcu);
1139b4ca34aaSAlexander Shishkin 	/*
1140b4ca34aaSAlexander Shishkin 	 * The stm device will be valid for the duration of this
1141b4ca34aaSAlexander Shishkin 	 * read section, but the link may change before we grab
1142b4ca34aaSAlexander Shishkin 	 * the src::link_lock in __stm_source_link_drop().
1143b4ca34aaSAlexander Shishkin 	 */
11447bd1d409SAlexander Shishkin 	stm = srcu_dereference(src->link, &stm_source_srcu);
11457bd1d409SAlexander Shishkin 
1146b4ca34aaSAlexander Shishkin 	ret = 0;
11477bd1d409SAlexander Shishkin 	if (stm) {
1148c74f7e82SAlexander Shishkin 		mutex_lock(&stm->link_mutex);
1149b4ca34aaSAlexander Shishkin 		ret = __stm_source_link_drop(src, stm);
1150c74f7e82SAlexander Shishkin 		mutex_unlock(&stm->link_mutex);
11517bd1d409SAlexander Shishkin 	}
11527bd1d409SAlexander Shishkin 
11537bd1d409SAlexander Shishkin 	srcu_read_unlock(&stm_source_srcu, idx);
1154b4ca34aaSAlexander Shishkin 
1155b4ca34aaSAlexander Shishkin 	/* if it did change, retry */
1156b4ca34aaSAlexander Shishkin 	if (ret == -EAGAIN)
1157b4ca34aaSAlexander Shishkin 		goto retry;
11587bd1d409SAlexander Shishkin }
11597bd1d409SAlexander Shishkin 
11607bd1d409SAlexander Shishkin static ssize_t stm_source_link_show(struct device *dev,
11617bd1d409SAlexander Shishkin 				    struct device_attribute *attr,
11627bd1d409SAlexander Shishkin 				    char *buf)
11637bd1d409SAlexander Shishkin {
11647bd1d409SAlexander Shishkin 	struct stm_source_device *src = to_stm_source_device(dev);
11657bd1d409SAlexander Shishkin 	struct stm_device *stm;
11667bd1d409SAlexander Shishkin 	int idx, ret;
11677bd1d409SAlexander Shishkin 
11687bd1d409SAlexander Shishkin 	idx = srcu_read_lock(&stm_source_srcu);
11697bd1d409SAlexander Shishkin 	stm = srcu_dereference(src->link, &stm_source_srcu);
11707bd1d409SAlexander Shishkin 	ret = sprintf(buf, "%s\n",
11717bd1d409SAlexander Shishkin 		      stm ? dev_name(&stm->dev) : "<none>");
11727bd1d409SAlexander Shishkin 	srcu_read_unlock(&stm_source_srcu, idx);
11737bd1d409SAlexander Shishkin 
11747bd1d409SAlexander Shishkin 	return ret;
11757bd1d409SAlexander Shishkin }
11767bd1d409SAlexander Shishkin 
11777bd1d409SAlexander Shishkin static ssize_t stm_source_link_store(struct device *dev,
11787bd1d409SAlexander Shishkin 				     struct device_attribute *attr,
11797bd1d409SAlexander Shishkin 				     const char *buf, size_t count)
11807bd1d409SAlexander Shishkin {
11817bd1d409SAlexander Shishkin 	struct stm_source_device *src = to_stm_source_device(dev);
11827bd1d409SAlexander Shishkin 	struct stm_device *link;
11837bd1d409SAlexander Shishkin 	int err;
11847bd1d409SAlexander Shishkin 
11857bd1d409SAlexander Shishkin 	stm_source_link_drop(src);
11867bd1d409SAlexander Shishkin 
11877bd1d409SAlexander Shishkin 	link = stm_find_device(buf);
11887bd1d409SAlexander Shishkin 	if (!link)
11897bd1d409SAlexander Shishkin 		return -EINVAL;
11907bd1d409SAlexander Shishkin 
11918e0469a4SAlexander Shishkin 	pm_runtime_get(&link->dev);
11928e0469a4SAlexander Shishkin 
11937bd1d409SAlexander Shishkin 	err = stm_source_link_add(src, link);
1194f7c81c71SAlexander Shishkin 	if (err) {
11958e0469a4SAlexander Shishkin 		pm_runtime_put_autosuspend(&link->dev);
1196f7c81c71SAlexander Shishkin 		/* matches the stm_find_device() above */
11977bd1d409SAlexander Shishkin 		stm_put_device(link);
1198f7c81c71SAlexander Shishkin 	}
11997bd1d409SAlexander Shishkin 
12007bd1d409SAlexander Shishkin 	return err ? : count;
12017bd1d409SAlexander Shishkin }
12027bd1d409SAlexander Shishkin 
12037bd1d409SAlexander Shishkin static DEVICE_ATTR_RW(stm_source_link);
12047bd1d409SAlexander Shishkin 
12057bd1d409SAlexander Shishkin static struct attribute *stm_source_attrs[] = {
12067bd1d409SAlexander Shishkin 	&dev_attr_stm_source_link.attr,
12077bd1d409SAlexander Shishkin 	NULL,
12087bd1d409SAlexander Shishkin };
12097bd1d409SAlexander Shishkin 
12107bd1d409SAlexander Shishkin ATTRIBUTE_GROUPS(stm_source);
12117bd1d409SAlexander Shishkin 
12127bd1d409SAlexander Shishkin static struct class stm_source_class = {
12137bd1d409SAlexander Shishkin 	.name		= "stm_source",
12147bd1d409SAlexander Shishkin 	.dev_groups	= stm_source_groups,
12157bd1d409SAlexander Shishkin };
12167bd1d409SAlexander Shishkin 
12177bd1d409SAlexander Shishkin static void stm_source_device_release(struct device *dev)
12187bd1d409SAlexander Shishkin {
12197bd1d409SAlexander Shishkin 	struct stm_source_device *src = to_stm_source_device(dev);
12207bd1d409SAlexander Shishkin 
12217bd1d409SAlexander Shishkin 	kfree(src);
12227bd1d409SAlexander Shishkin }
12237bd1d409SAlexander Shishkin 
12247bd1d409SAlexander Shishkin /**
12257bd1d409SAlexander Shishkin  * stm_source_register_device() - register an stm_source device
12267bd1d409SAlexander Shishkin  * @parent:	parent device
12277bd1d409SAlexander Shishkin  * @data:	device description structure
12287bd1d409SAlexander Shishkin  *
12297bd1d409SAlexander Shishkin  * This will create a device of stm_source class that can write
12307bd1d409SAlexander Shishkin  * data to an stm device once linked.
12317bd1d409SAlexander Shishkin  *
12327bd1d409SAlexander Shishkin  * Return:	0 on success, -errno otherwise.
12337bd1d409SAlexander Shishkin  */
12347bd1d409SAlexander Shishkin int stm_source_register_device(struct device *parent,
12357bd1d409SAlexander Shishkin 			       struct stm_source_data *data)
12367bd1d409SAlexander Shishkin {
12377bd1d409SAlexander Shishkin 	struct stm_source_device *src;
12387bd1d409SAlexander Shishkin 	int err;
12397bd1d409SAlexander Shishkin 
12407bd1d409SAlexander Shishkin 	if (!stm_core_up)
12417bd1d409SAlexander Shishkin 		return -EPROBE_DEFER;
12427bd1d409SAlexander Shishkin 
12437bd1d409SAlexander Shishkin 	src = kzalloc(sizeof(*src), GFP_KERNEL);
12447bd1d409SAlexander Shishkin 	if (!src)
12457bd1d409SAlexander Shishkin 		return -ENOMEM;
12467bd1d409SAlexander Shishkin 
12477bd1d409SAlexander Shishkin 	device_initialize(&src->dev);
12487bd1d409SAlexander Shishkin 	src->dev.class = &stm_source_class;
12497bd1d409SAlexander Shishkin 	src->dev.parent = parent;
12507bd1d409SAlexander Shishkin 	src->dev.release = stm_source_device_release;
12517bd1d409SAlexander Shishkin 
12527bd1d409SAlexander Shishkin 	err = kobject_set_name(&src->dev.kobj, "%s", data->name);
12537bd1d409SAlexander Shishkin 	if (err)
12547bd1d409SAlexander Shishkin 		goto err;
12557bd1d409SAlexander Shishkin 
12568e0469a4SAlexander Shishkin 	pm_runtime_no_callbacks(&src->dev);
12578e0469a4SAlexander Shishkin 	pm_runtime_forbid(&src->dev);
12588e0469a4SAlexander Shishkin 
12597bd1d409SAlexander Shishkin 	err = device_add(&src->dev);
12607bd1d409SAlexander Shishkin 	if (err)
12617bd1d409SAlexander Shishkin 		goto err;
12627bd1d409SAlexander Shishkin 
1263cde4ad83SAlexander Shishkin 	stm_output_init(&src->output);
12647bd1d409SAlexander Shishkin 	spin_lock_init(&src->link_lock);
12657bd1d409SAlexander Shishkin 	INIT_LIST_HEAD(&src->link_entry);
12667bd1d409SAlexander Shishkin 	src->data = data;
12677bd1d409SAlexander Shishkin 	data->src = src;
12687bd1d409SAlexander Shishkin 
12697bd1d409SAlexander Shishkin 	return 0;
12707bd1d409SAlexander Shishkin 
12717bd1d409SAlexander Shishkin err:
12727bd1d409SAlexander Shishkin 	put_device(&src->dev);
12737bd1d409SAlexander Shishkin 	kfree(src);
12747bd1d409SAlexander Shishkin 
12757bd1d409SAlexander Shishkin 	return err;
12767bd1d409SAlexander Shishkin }
12777bd1d409SAlexander Shishkin EXPORT_SYMBOL_GPL(stm_source_register_device);
12787bd1d409SAlexander Shishkin 
12797bd1d409SAlexander Shishkin /**
12807bd1d409SAlexander Shishkin  * stm_source_unregister_device() - unregister an stm_source device
12817bd1d409SAlexander Shishkin  * @data:	device description that was used to register the device
12827bd1d409SAlexander Shishkin  *
12837bd1d409SAlexander Shishkin  * This will remove a previously created stm_source device from the system.
12847bd1d409SAlexander Shishkin  */
12857bd1d409SAlexander Shishkin void stm_source_unregister_device(struct stm_source_data *data)
12867bd1d409SAlexander Shishkin {
12877bd1d409SAlexander Shishkin 	struct stm_source_device *src = data->src;
12887bd1d409SAlexander Shishkin 
12897bd1d409SAlexander Shishkin 	stm_source_link_drop(src);
12907bd1d409SAlexander Shishkin 
1291fd085bb1SAlexander Shishkin 	device_unregister(&src->dev);
12927bd1d409SAlexander Shishkin }
12937bd1d409SAlexander Shishkin EXPORT_SYMBOL_GPL(stm_source_unregister_device);
12947bd1d409SAlexander Shishkin 
12959dfed80dSChunyan Zhang int notrace stm_source_write(struct stm_source_data *data,
12969dfed80dSChunyan Zhang 			     unsigned int chan,
12977bd1d409SAlexander Shishkin 			     const char *buf, size_t count)
12987bd1d409SAlexander Shishkin {
12997bd1d409SAlexander Shishkin 	struct stm_source_device *src = data->src;
13007bd1d409SAlexander Shishkin 	struct stm_device *stm;
13017bd1d409SAlexander Shishkin 	int idx;
13027bd1d409SAlexander Shishkin 
13037bd1d409SAlexander Shishkin 	if (!src->output.nr_chans)
13047bd1d409SAlexander Shishkin 		return -ENODEV;
13057bd1d409SAlexander Shishkin 
13067bd1d409SAlexander Shishkin 	if (chan >= src->output.nr_chans)
13077bd1d409SAlexander Shishkin 		return -EINVAL;
13087bd1d409SAlexander Shishkin 
13097bd1d409SAlexander Shishkin 	idx = srcu_read_lock(&stm_source_srcu);
13107bd1d409SAlexander Shishkin 
13117bd1d409SAlexander Shishkin 	stm = srcu_dereference(src->link, &stm_source_srcu);
13127bd1d409SAlexander Shishkin 	if (stm)
1313f8560a9bSAlexander Shishkin 		count = stm_write(stm->data, src->output.master,
13147bd1d409SAlexander Shishkin 				  src->output.channel + chan,
13157bd1d409SAlexander Shishkin 				  buf, count);
13167bd1d409SAlexander Shishkin 	else
13177bd1d409SAlexander Shishkin 		count = -ENODEV;
13187bd1d409SAlexander Shishkin 
13197bd1d409SAlexander Shishkin 	srcu_read_unlock(&stm_source_srcu, idx);
13207bd1d409SAlexander Shishkin 
13217bd1d409SAlexander Shishkin 	return count;
13227bd1d409SAlexander Shishkin }
13237bd1d409SAlexander Shishkin EXPORT_SYMBOL_GPL(stm_source_write);
13247bd1d409SAlexander Shishkin 
13257bd1d409SAlexander Shishkin static int __init stm_core_init(void)
13267bd1d409SAlexander Shishkin {
13277bd1d409SAlexander Shishkin 	int err;
13287bd1d409SAlexander Shishkin 
13297bd1d409SAlexander Shishkin 	err = class_register(&stm_class);
13307bd1d409SAlexander Shishkin 	if (err)
13317bd1d409SAlexander Shishkin 		return err;
13327bd1d409SAlexander Shishkin 
13337bd1d409SAlexander Shishkin 	err = class_register(&stm_source_class);
13347bd1d409SAlexander Shishkin 	if (err)
13357bd1d409SAlexander Shishkin 		goto err_stm;
13367bd1d409SAlexander Shishkin 
13377bd1d409SAlexander Shishkin 	err = stp_configfs_init();
13387bd1d409SAlexander Shishkin 	if (err)
13397bd1d409SAlexander Shishkin 		goto err_src;
13407bd1d409SAlexander Shishkin 
13417bd1d409SAlexander Shishkin 	init_srcu_struct(&stm_source_srcu);
1342c7fd62bcSAlexander Shishkin 	INIT_LIST_HEAD(&stm_pdrv_head);
1343c7fd62bcSAlexander Shishkin 	mutex_init(&stm_pdrv_mutex);
13447bd1d409SAlexander Shishkin 
13457bd1d409SAlexander Shishkin 	stm_core_up++;
13467bd1d409SAlexander Shishkin 
13477bd1d409SAlexander Shishkin 	return 0;
13487bd1d409SAlexander Shishkin 
13497bd1d409SAlexander Shishkin err_src:
13507bd1d409SAlexander Shishkin 	class_unregister(&stm_source_class);
13517bd1d409SAlexander Shishkin err_stm:
13527bd1d409SAlexander Shishkin 	class_unregister(&stm_class);
13537bd1d409SAlexander Shishkin 
13547bd1d409SAlexander Shishkin 	return err;
13557bd1d409SAlexander Shishkin }
13567bd1d409SAlexander Shishkin 
13577bd1d409SAlexander Shishkin module_init(stm_core_init);
13587bd1d409SAlexander Shishkin 
13597bd1d409SAlexander Shishkin static void __exit stm_core_exit(void)
13607bd1d409SAlexander Shishkin {
13617bd1d409SAlexander Shishkin 	cleanup_srcu_struct(&stm_source_srcu);
13627bd1d409SAlexander Shishkin 	class_unregister(&stm_source_class);
13637bd1d409SAlexander Shishkin 	class_unregister(&stm_class);
13647bd1d409SAlexander Shishkin 	stp_configfs_exit();
13657bd1d409SAlexander Shishkin }
13667bd1d409SAlexander Shishkin 
13677bd1d409SAlexander Shishkin module_exit(stm_core_exit);
13687bd1d409SAlexander Shishkin 
13697bd1d409SAlexander Shishkin MODULE_LICENSE("GPL v2");
13707bd1d409SAlexander Shishkin MODULE_DESCRIPTION("System Trace Module device class");
13717bd1d409SAlexander Shishkin MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");
1372