xref: /openbmc/linux/drivers/hwtracing/stm/core.c (revision 370c4804)
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 
masters_show(struct device * dev,struct device_attribute * attr,char * buf)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 
channels_show(struct device * dev,struct device_attribute * attr,char * buf)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 
hw_override_show(struct device * dev,struct device_attribute * attr,char * buf)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 /**
937bd1d409SAlexander Shishkin  * stm_find_device() - find stm device by name
947bd1d409SAlexander Shishkin  * @buf:	character buffer containing the name
957bd1d409SAlexander Shishkin  *
967bd1d409SAlexander Shishkin  * This is called when either policy gets assigned to an stm device or an
977bd1d409SAlexander Shishkin  * stm_source device gets linked to an stm device.
987bd1d409SAlexander Shishkin  *
997bd1d409SAlexander Shishkin  * This grabs device's reference (get_device()) and module reference, both
1007bd1d409SAlexander Shishkin  * of which the calling path needs to make sure to drop with stm_put_device().
1017bd1d409SAlexander Shishkin  *
1027bd1d409SAlexander Shishkin  * Return:	stm device pointer or null if lookup failed.
1037bd1d409SAlexander Shishkin  */
stm_find_device(const char * buf)1047bd1d409SAlexander Shishkin struct stm_device *stm_find_device(const char *buf)
1057bd1d409SAlexander Shishkin {
1067bd1d409SAlexander Shishkin 	struct stm_device *stm;
1077bd1d409SAlexander Shishkin 	struct device *dev;
1087bd1d409SAlexander Shishkin 
1097bd1d409SAlexander Shishkin 	if (!stm_core_up)
1107bd1d409SAlexander Shishkin 		return NULL;
1117bd1d409SAlexander Shishkin 
1126cda08a2SSuzuki K Poulose 	dev = class_find_device_by_name(&stm_class, buf);
1137bd1d409SAlexander Shishkin 	if (!dev)
1147bd1d409SAlexander Shishkin 		return NULL;
1157bd1d409SAlexander Shishkin 
1167bd1d409SAlexander Shishkin 	stm = to_stm_device(dev);
1177bd1d409SAlexander Shishkin 	if (!try_module_get(stm->owner)) {
118f7c81c71SAlexander Shishkin 		/* matches class_find_device() above */
1197bd1d409SAlexander Shishkin 		put_device(dev);
1207bd1d409SAlexander Shishkin 		return NULL;
1217bd1d409SAlexander Shishkin 	}
1227bd1d409SAlexander Shishkin 
1237bd1d409SAlexander Shishkin 	return stm;
1247bd1d409SAlexander Shishkin }
1257bd1d409SAlexander Shishkin 
1267bd1d409SAlexander Shishkin /**
1277bd1d409SAlexander Shishkin  * stm_put_device() - drop references on the stm device
1287bd1d409SAlexander Shishkin  * @stm:	stm device, previously acquired by stm_find_device()
1297bd1d409SAlexander Shishkin  *
1307bd1d409SAlexander Shishkin  * This drops the module reference and device reference taken by
131f7c81c71SAlexander Shishkin  * stm_find_device() or stm_char_open().
1327bd1d409SAlexander Shishkin  */
stm_put_device(struct stm_device * stm)1337bd1d409SAlexander Shishkin void stm_put_device(struct stm_device *stm)
1347bd1d409SAlexander Shishkin {
1357bd1d409SAlexander Shishkin 	module_put(stm->owner);
1367bd1d409SAlexander Shishkin 	put_device(&stm->dev);
1377bd1d409SAlexander Shishkin }
1387bd1d409SAlexander Shishkin 
1397bd1d409SAlexander Shishkin /*
1407bd1d409SAlexander Shishkin  * Internally we only care about software-writable masters here, that is the
1417bd1d409SAlexander Shishkin  * ones in the range [stm_data->sw_start..stm_data..sw_end], however we need
1427bd1d409SAlexander Shishkin  * original master numbers to be visible externally, since they are the ones
1437bd1d409SAlexander Shishkin  * that will appear in the STP stream. Thus, the internal bookkeeping uses
1447bd1d409SAlexander Shishkin  * $master - stm_data->sw_start to reference master descriptors and such.
1457bd1d409SAlexander Shishkin  */
1467bd1d409SAlexander Shishkin 
1477bd1d409SAlexander Shishkin #define __stm_master(_s, _m)				\
1487bd1d409SAlexander Shishkin 	((_s)->masters[(_m) - (_s)->data->sw_start])
1497bd1d409SAlexander Shishkin 
1507bd1d409SAlexander Shishkin static inline struct stp_master *
stm_master(struct stm_device * stm,unsigned int idx)1517bd1d409SAlexander Shishkin stm_master(struct stm_device *stm, unsigned int idx)
1527bd1d409SAlexander Shishkin {
1537bd1d409SAlexander Shishkin 	if (idx < stm->data->sw_start || idx > stm->data->sw_end)
1547bd1d409SAlexander Shishkin 		return NULL;
1557bd1d409SAlexander Shishkin 
1567bd1d409SAlexander Shishkin 	return __stm_master(stm, idx);
1577bd1d409SAlexander Shishkin }
1587bd1d409SAlexander Shishkin 
stp_master_alloc(struct stm_device * stm,unsigned int idx)1597bd1d409SAlexander Shishkin static int stp_master_alloc(struct stm_device *stm, unsigned int idx)
1607bd1d409SAlexander Shishkin {
1617bd1d409SAlexander Shishkin 	struct stp_master *master;
1627bd1d409SAlexander Shishkin 
16351e0f227SAlexander Shishkin 	master = kzalloc(struct_size(master, chan_map,
16451e0f227SAlexander Shishkin 				     BITS_TO_LONGS(stm->data->sw_nchannels)),
16551e0f227SAlexander Shishkin 			 GFP_ATOMIC);
1667bd1d409SAlexander Shishkin 	if (!master)
1677bd1d409SAlexander Shishkin 		return -ENOMEM;
1687bd1d409SAlexander Shishkin 
1697bd1d409SAlexander Shishkin 	master->nr_free = stm->data->sw_nchannels;
1707bd1d409SAlexander Shishkin 	__stm_master(stm, idx) = master;
1717bd1d409SAlexander Shishkin 
1727bd1d409SAlexander Shishkin 	return 0;
1737bd1d409SAlexander Shishkin }
1747bd1d409SAlexander Shishkin 
stp_master_free(struct stm_device * stm,unsigned int idx)1757bd1d409SAlexander Shishkin static void stp_master_free(struct stm_device *stm, unsigned int idx)
1767bd1d409SAlexander Shishkin {
1777bd1d409SAlexander Shishkin 	struct stp_master *master = stm_master(stm, idx);
1787bd1d409SAlexander Shishkin 
1797bd1d409SAlexander Shishkin 	if (!master)
1807bd1d409SAlexander Shishkin 		return;
1817bd1d409SAlexander Shishkin 
1827bd1d409SAlexander Shishkin 	__stm_master(stm, idx) = NULL;
1837bd1d409SAlexander Shishkin 	kfree(master);
1847bd1d409SAlexander Shishkin }
1857bd1d409SAlexander Shishkin 
stm_output_claim(struct stm_device * stm,struct stm_output * output)1867bd1d409SAlexander Shishkin static void stm_output_claim(struct stm_device *stm, struct stm_output *output)
1877bd1d409SAlexander Shishkin {
1887bd1d409SAlexander Shishkin 	struct stp_master *master = stm_master(stm, output->master);
1897bd1d409SAlexander Shishkin 
190cde4ad83SAlexander Shishkin 	lockdep_assert_held(&stm->mc_lock);
191cde4ad83SAlexander Shishkin 	lockdep_assert_held(&output->lock);
192cde4ad83SAlexander Shishkin 
1937bd1d409SAlexander Shishkin 	if (WARN_ON_ONCE(master->nr_free < output->nr_chans))
1947bd1d409SAlexander Shishkin 		return;
1957bd1d409SAlexander Shishkin 
1967bd1d409SAlexander Shishkin 	bitmap_allocate_region(&master->chan_map[0], output->channel,
1977bd1d409SAlexander Shishkin 			       ilog2(output->nr_chans));
1987bd1d409SAlexander Shishkin 
1997bd1d409SAlexander Shishkin 	master->nr_free -= output->nr_chans;
2007bd1d409SAlexander Shishkin }
2017bd1d409SAlexander Shishkin 
2027bd1d409SAlexander Shishkin static void
stm_output_disclaim(struct stm_device * stm,struct stm_output * output)2037bd1d409SAlexander Shishkin stm_output_disclaim(struct stm_device *stm, struct stm_output *output)
2047bd1d409SAlexander Shishkin {
2057bd1d409SAlexander Shishkin 	struct stp_master *master = stm_master(stm, output->master);
2067bd1d409SAlexander Shishkin 
207cde4ad83SAlexander Shishkin 	lockdep_assert_held(&stm->mc_lock);
208cde4ad83SAlexander Shishkin 	lockdep_assert_held(&output->lock);
209cde4ad83SAlexander Shishkin 
2107bd1d409SAlexander Shishkin 	bitmap_release_region(&master->chan_map[0], output->channel,
2117bd1d409SAlexander Shishkin 			      ilog2(output->nr_chans));
2127bd1d409SAlexander Shishkin 
2137bd1d409SAlexander Shishkin 	master->nr_free += output->nr_chans;
214ee496da4STingwei Zhang 	output->nr_chans = 0;
2157bd1d409SAlexander Shishkin }
2167bd1d409SAlexander Shishkin 
2177bd1d409SAlexander Shishkin /*
2187bd1d409SAlexander Shishkin  * This is like bitmap_find_free_region(), except it can ignore @start bits
2197bd1d409SAlexander Shishkin  * at the beginning.
2207bd1d409SAlexander Shishkin  */
find_free_channels(unsigned long * bitmap,unsigned int start,unsigned int end,unsigned int width)2217bd1d409SAlexander Shishkin static int find_free_channels(unsigned long *bitmap, unsigned int start,
2227bd1d409SAlexander Shishkin 			      unsigned int end, unsigned int width)
2237bd1d409SAlexander Shishkin {
2247bd1d409SAlexander Shishkin 	unsigned int pos;
2257bd1d409SAlexander Shishkin 	int i;
2267bd1d409SAlexander Shishkin 
2277bd1d409SAlexander Shishkin 	for (pos = start; pos < end + 1; pos = ALIGN(pos, width)) {
2287bd1d409SAlexander Shishkin 		pos = find_next_zero_bit(bitmap, end + 1, pos);
2297bd1d409SAlexander Shishkin 		if (pos + width > end + 1)
2307bd1d409SAlexander Shishkin 			break;
2317bd1d409SAlexander Shishkin 
2327bd1d409SAlexander Shishkin 		if (pos & (width - 1))
2337bd1d409SAlexander Shishkin 			continue;
2347bd1d409SAlexander Shishkin 
2357bd1d409SAlexander Shishkin 		for (i = 1; i < width && !test_bit(pos + i, bitmap); i++)
2367bd1d409SAlexander Shishkin 			;
2377bd1d409SAlexander Shishkin 		if (i == width)
2387bd1d409SAlexander Shishkin 			return pos;
239a1d75dadSZhi Jin 
240a1d75dadSZhi Jin 		/* step over [pos..pos+i) to continue search */
241a1d75dadSZhi Jin 		pos += i;
2427bd1d409SAlexander Shishkin 	}
2437bd1d409SAlexander Shishkin 
2447bd1d409SAlexander Shishkin 	return -1;
2457bd1d409SAlexander Shishkin }
2467bd1d409SAlexander Shishkin 
247f45f40adSLucas Tanure static int
stm_find_master_chan(struct stm_device * stm,unsigned int width,unsigned int * mstart,unsigned int mend,unsigned int * cstart,unsigned int cend)2487bd1d409SAlexander Shishkin stm_find_master_chan(struct stm_device *stm, unsigned int width,
2497bd1d409SAlexander Shishkin 		     unsigned int *mstart, unsigned int mend,
2507bd1d409SAlexander Shishkin 		     unsigned int *cstart, unsigned int cend)
2517bd1d409SAlexander Shishkin {
2527bd1d409SAlexander Shishkin 	struct stp_master *master;
2537bd1d409SAlexander Shishkin 	unsigned int midx;
2547bd1d409SAlexander Shishkin 	int pos, err;
2557bd1d409SAlexander Shishkin 
2567bd1d409SAlexander Shishkin 	for (midx = *mstart; midx <= mend; midx++) {
2577bd1d409SAlexander Shishkin 		if (!stm_master(stm, midx)) {
2587bd1d409SAlexander Shishkin 			err = stp_master_alloc(stm, midx);
2597bd1d409SAlexander Shishkin 			if (err)
2607bd1d409SAlexander Shishkin 				return err;
2617bd1d409SAlexander Shishkin 		}
2627bd1d409SAlexander Shishkin 
2637bd1d409SAlexander Shishkin 		master = stm_master(stm, midx);
2647bd1d409SAlexander Shishkin 
2657bd1d409SAlexander Shishkin 		if (!master->nr_free)
2667bd1d409SAlexander Shishkin 			continue;
2677bd1d409SAlexander Shishkin 
2687bd1d409SAlexander Shishkin 		pos = find_free_channels(master->chan_map, *cstart, cend,
2697bd1d409SAlexander Shishkin 					 width);
2707bd1d409SAlexander Shishkin 		if (pos < 0)
2717bd1d409SAlexander Shishkin 			continue;
2727bd1d409SAlexander Shishkin 
2737bd1d409SAlexander Shishkin 		*mstart = midx;
2747bd1d409SAlexander Shishkin 		*cstart = pos;
2757bd1d409SAlexander Shishkin 		return 0;
2767bd1d409SAlexander Shishkin 	}
2777bd1d409SAlexander Shishkin 
2787bd1d409SAlexander Shishkin 	return -ENOSPC;
2797bd1d409SAlexander Shishkin }
2807bd1d409SAlexander Shishkin 
stm_output_assign(struct stm_device * stm,unsigned int width,struct stp_policy_node * policy_node,struct stm_output * output)2817bd1d409SAlexander Shishkin static int stm_output_assign(struct stm_device *stm, unsigned int width,
2827bd1d409SAlexander Shishkin 			     struct stp_policy_node *policy_node,
2837bd1d409SAlexander Shishkin 			     struct stm_output *output)
2847bd1d409SAlexander Shishkin {
2857bd1d409SAlexander Shishkin 	unsigned int midx, cidx, mend, cend;
2867bd1d409SAlexander Shishkin 	int ret = -EINVAL;
2877bd1d409SAlexander Shishkin 
2887bd1d409SAlexander Shishkin 	if (width > stm->data->sw_nchannels)
2897bd1d409SAlexander Shishkin 		return -EINVAL;
2907bd1d409SAlexander Shishkin 
291cb6102bdSAlexander Shishkin 	/* We no longer accept policy_node==NULL here */
292cb6102bdSAlexander Shishkin 	if (WARN_ON_ONCE(!policy_node))
293cb6102bdSAlexander Shishkin 		return -EINVAL;
294cb6102bdSAlexander Shishkin 
295cb6102bdSAlexander Shishkin 	/*
296cb6102bdSAlexander Shishkin 	 * Also, the caller holds reference to policy_node, so it won't
297cb6102bdSAlexander Shishkin 	 * disappear on us.
298cb6102bdSAlexander Shishkin 	 */
299cb6102bdSAlexander Shishkin 	stp_policy_node_get_ranges(policy_node, &midx, &mend, &cidx, &cend);
3007bd1d409SAlexander Shishkin 
3017bd1d409SAlexander Shishkin 	spin_lock(&stm->mc_lock);
302cde4ad83SAlexander Shishkin 	spin_lock(&output->lock);
3037bd1d409SAlexander Shishkin 	/* output is already assigned -- shouldn't happen */
3047bd1d409SAlexander Shishkin 	if (WARN_ON_ONCE(output->nr_chans))
3057bd1d409SAlexander Shishkin 		goto unlock;
3067bd1d409SAlexander Shishkin 
3077bd1d409SAlexander Shishkin 	ret = stm_find_master_chan(stm, width, &midx, mend, &cidx, cend);
308f45f40adSLucas Tanure 	if (ret < 0)
3097bd1d409SAlexander Shishkin 		goto unlock;
3107bd1d409SAlexander Shishkin 
3117bd1d409SAlexander Shishkin 	output->master = midx;
3127bd1d409SAlexander Shishkin 	output->channel = cidx;
3137bd1d409SAlexander Shishkin 	output->nr_chans = width;
314c7fd62bcSAlexander Shishkin 	if (stm->pdrv->output_open) {
315c7fd62bcSAlexander Shishkin 		void *priv = stp_policy_node_priv(policy_node);
316c7fd62bcSAlexander Shishkin 
317c7fd62bcSAlexander Shishkin 		if (WARN_ON_ONCE(!priv))
318c7fd62bcSAlexander Shishkin 			goto unlock;
319c7fd62bcSAlexander Shishkin 
320c7fd62bcSAlexander Shishkin 		/* configfs subsys mutex is held by the caller */
321c7fd62bcSAlexander Shishkin 		ret = stm->pdrv->output_open(priv, output);
322c7fd62bcSAlexander Shishkin 		if (ret)
323c7fd62bcSAlexander Shishkin 			goto unlock;
324c7fd62bcSAlexander Shishkin 	}
325c7fd62bcSAlexander Shishkin 
3267bd1d409SAlexander Shishkin 	stm_output_claim(stm, output);
3277bd1d409SAlexander Shishkin 	dev_dbg(&stm->dev, "assigned %u:%u (+%u)\n", midx, cidx, width);
3287bd1d409SAlexander Shishkin 
3297bd1d409SAlexander Shishkin 	ret = 0;
3307bd1d409SAlexander Shishkin unlock:
331c7fd62bcSAlexander Shishkin 	if (ret)
332c7fd62bcSAlexander Shishkin 		output->nr_chans = 0;
333c7fd62bcSAlexander Shishkin 
334cde4ad83SAlexander Shishkin 	spin_unlock(&output->lock);
3357bd1d409SAlexander Shishkin 	spin_unlock(&stm->mc_lock);
3367bd1d409SAlexander Shishkin 
3377bd1d409SAlexander Shishkin 	return ret;
3387bd1d409SAlexander Shishkin }
3397bd1d409SAlexander Shishkin 
stm_output_free(struct stm_device * stm,struct stm_output * output)3407bd1d409SAlexander Shishkin static void stm_output_free(struct stm_device *stm, struct stm_output *output)
3417bd1d409SAlexander Shishkin {
3427bd1d409SAlexander Shishkin 	spin_lock(&stm->mc_lock);
343cde4ad83SAlexander Shishkin 	spin_lock(&output->lock);
3447bd1d409SAlexander Shishkin 	if (output->nr_chans)
3457bd1d409SAlexander Shishkin 		stm_output_disclaim(stm, output);
346c7fd62bcSAlexander Shishkin 	if (stm->pdrv && stm->pdrv->output_close)
347c7fd62bcSAlexander Shishkin 		stm->pdrv->output_close(output);
348cde4ad83SAlexander Shishkin 	spin_unlock(&output->lock);
3497bd1d409SAlexander Shishkin 	spin_unlock(&stm->mc_lock);
3507bd1d409SAlexander Shishkin }
3517bd1d409SAlexander Shishkin 
stm_output_init(struct stm_output * output)352cde4ad83SAlexander Shishkin static void stm_output_init(struct stm_output *output)
353cde4ad83SAlexander Shishkin {
354cde4ad83SAlexander Shishkin 	spin_lock_init(&output->lock);
355cde4ad83SAlexander Shishkin }
356cde4ad83SAlexander Shishkin 
major_match(struct device * dev,const void * data)3577bd1d409SAlexander Shishkin static int major_match(struct device *dev, const void *data)
3587bd1d409SAlexander Shishkin {
3597bd1d409SAlexander Shishkin 	unsigned int major = *(unsigned int *)data;
3607bd1d409SAlexander Shishkin 
3617bd1d409SAlexander Shishkin 	return MAJOR(dev->devt) == major;
3627bd1d409SAlexander Shishkin }
3637bd1d409SAlexander Shishkin 
364c7fd62bcSAlexander Shishkin /*
365c7fd62bcSAlexander Shishkin  * Framing protocol management
366c7fd62bcSAlexander Shishkin  * Modules can implement STM protocol drivers and (un-)register them
367c7fd62bcSAlexander Shishkin  * with the STM class framework.
368c7fd62bcSAlexander Shishkin  */
369c7fd62bcSAlexander Shishkin static struct list_head stm_pdrv_head;
370c7fd62bcSAlexander Shishkin static struct mutex stm_pdrv_mutex;
371c7fd62bcSAlexander Shishkin 
372c7fd62bcSAlexander Shishkin struct stm_pdrv_entry {
373c7fd62bcSAlexander Shishkin 	struct list_head			entry;
374c7fd62bcSAlexander Shishkin 	const struct stm_protocol_driver	*pdrv;
375c7fd62bcSAlexander Shishkin 	const struct config_item_type		*node_type;
376c7fd62bcSAlexander Shishkin };
377c7fd62bcSAlexander Shishkin 
378c7fd62bcSAlexander Shishkin static const struct stm_pdrv_entry *
__stm_lookup_protocol(const char * name)379c7fd62bcSAlexander Shishkin __stm_lookup_protocol(const char *name)
380c7fd62bcSAlexander Shishkin {
381c7fd62bcSAlexander Shishkin 	struct stm_pdrv_entry *pe;
382c7fd62bcSAlexander Shishkin 
383c7fd62bcSAlexander Shishkin 	/*
384c7fd62bcSAlexander Shishkin 	 * If no name is given (NULL or ""), fall back to "p_basic".
385c7fd62bcSAlexander Shishkin 	 */
386c7fd62bcSAlexander Shishkin 	if (!name || !*name)
387c7fd62bcSAlexander Shishkin 		name = "p_basic";
388c7fd62bcSAlexander Shishkin 
389c7fd62bcSAlexander Shishkin 	list_for_each_entry(pe, &stm_pdrv_head, entry) {
390c7fd62bcSAlexander Shishkin 		if (!strcmp(name, pe->pdrv->name))
391c7fd62bcSAlexander Shishkin 			return pe;
392c7fd62bcSAlexander Shishkin 	}
393c7fd62bcSAlexander Shishkin 
394c7fd62bcSAlexander Shishkin 	return NULL;
395c7fd62bcSAlexander Shishkin }
396c7fd62bcSAlexander Shishkin 
stm_register_protocol(const struct stm_protocol_driver * pdrv)397c7fd62bcSAlexander Shishkin int stm_register_protocol(const struct stm_protocol_driver *pdrv)
398c7fd62bcSAlexander Shishkin {
399c7fd62bcSAlexander Shishkin 	struct stm_pdrv_entry *pe = NULL;
400c7fd62bcSAlexander Shishkin 	int ret = -ENOMEM;
401c7fd62bcSAlexander Shishkin 
402c7fd62bcSAlexander Shishkin 	mutex_lock(&stm_pdrv_mutex);
403c7fd62bcSAlexander Shishkin 
404c7fd62bcSAlexander Shishkin 	if (__stm_lookup_protocol(pdrv->name)) {
405c7fd62bcSAlexander Shishkin 		ret = -EEXIST;
406c7fd62bcSAlexander Shishkin 		goto unlock;
407c7fd62bcSAlexander Shishkin 	}
408c7fd62bcSAlexander Shishkin 
409c7fd62bcSAlexander Shishkin 	pe = kzalloc(sizeof(*pe), GFP_KERNEL);
410c7fd62bcSAlexander Shishkin 	if (!pe)
411c7fd62bcSAlexander Shishkin 		goto unlock;
412c7fd62bcSAlexander Shishkin 
413c7fd62bcSAlexander Shishkin 	if (pdrv->policy_attr) {
414c7fd62bcSAlexander Shishkin 		pe->node_type = get_policy_node_type(pdrv->policy_attr);
415c7fd62bcSAlexander Shishkin 		if (!pe->node_type)
416c7fd62bcSAlexander Shishkin 			goto unlock;
417c7fd62bcSAlexander Shishkin 	}
418c7fd62bcSAlexander Shishkin 
419c7fd62bcSAlexander Shishkin 	list_add_tail(&pe->entry, &stm_pdrv_head);
420c7fd62bcSAlexander Shishkin 	pe->pdrv = pdrv;
421c7fd62bcSAlexander Shishkin 
422c7fd62bcSAlexander Shishkin 	ret = 0;
423c7fd62bcSAlexander Shishkin unlock:
424c7fd62bcSAlexander Shishkin 	mutex_unlock(&stm_pdrv_mutex);
425c7fd62bcSAlexander Shishkin 
426c7fd62bcSAlexander Shishkin 	if (ret)
427c7fd62bcSAlexander Shishkin 		kfree(pe);
428c7fd62bcSAlexander Shishkin 
429c7fd62bcSAlexander Shishkin 	return ret;
430c7fd62bcSAlexander Shishkin }
431c7fd62bcSAlexander Shishkin EXPORT_SYMBOL_GPL(stm_register_protocol);
432c7fd62bcSAlexander Shishkin 
stm_unregister_protocol(const struct stm_protocol_driver * pdrv)433c7fd62bcSAlexander Shishkin void stm_unregister_protocol(const struct stm_protocol_driver *pdrv)
434c7fd62bcSAlexander Shishkin {
435c7fd62bcSAlexander Shishkin 	struct stm_pdrv_entry *pe, *iter;
436c7fd62bcSAlexander Shishkin 
437c7fd62bcSAlexander Shishkin 	mutex_lock(&stm_pdrv_mutex);
438c7fd62bcSAlexander Shishkin 
439c7fd62bcSAlexander Shishkin 	list_for_each_entry_safe(pe, iter, &stm_pdrv_head, entry) {
440c7fd62bcSAlexander Shishkin 		if (pe->pdrv == pdrv) {
441c7fd62bcSAlexander Shishkin 			list_del(&pe->entry);
442c7fd62bcSAlexander Shishkin 
443c7fd62bcSAlexander Shishkin 			if (pe->node_type) {
444c7fd62bcSAlexander Shishkin 				kfree(pe->node_type->ct_attrs);
445c7fd62bcSAlexander Shishkin 				kfree(pe->node_type);
446c7fd62bcSAlexander Shishkin 			}
447c7fd62bcSAlexander Shishkin 			kfree(pe);
448c7fd62bcSAlexander Shishkin 			break;
449c7fd62bcSAlexander Shishkin 		}
450c7fd62bcSAlexander Shishkin 	}
451c7fd62bcSAlexander Shishkin 
452c7fd62bcSAlexander Shishkin 	mutex_unlock(&stm_pdrv_mutex);
453c7fd62bcSAlexander Shishkin }
454c7fd62bcSAlexander Shishkin EXPORT_SYMBOL_GPL(stm_unregister_protocol);
455c7fd62bcSAlexander Shishkin 
stm_get_protocol(const struct stm_protocol_driver * pdrv)456c7fd62bcSAlexander Shishkin static bool stm_get_protocol(const struct stm_protocol_driver *pdrv)
457c7fd62bcSAlexander Shishkin {
458c7fd62bcSAlexander Shishkin 	return try_module_get(pdrv->owner);
459c7fd62bcSAlexander Shishkin }
460c7fd62bcSAlexander Shishkin 
stm_put_protocol(const struct stm_protocol_driver * pdrv)461c7fd62bcSAlexander Shishkin void stm_put_protocol(const struct stm_protocol_driver *pdrv)
462c7fd62bcSAlexander Shishkin {
463c7fd62bcSAlexander Shishkin 	module_put(pdrv->owner);
464c7fd62bcSAlexander Shishkin }
465c7fd62bcSAlexander Shishkin 
stm_lookup_protocol(const char * name,const struct stm_protocol_driver ** pdrv,const struct config_item_type ** node_type)466c7fd62bcSAlexander Shishkin int stm_lookup_protocol(const char *name,
467c7fd62bcSAlexander Shishkin 			const struct stm_protocol_driver **pdrv,
468c7fd62bcSAlexander Shishkin 			const struct config_item_type **node_type)
469c7fd62bcSAlexander Shishkin {
470c7fd62bcSAlexander Shishkin 	const struct stm_pdrv_entry *pe;
471c7fd62bcSAlexander Shishkin 
472c7fd62bcSAlexander Shishkin 	mutex_lock(&stm_pdrv_mutex);
473c7fd62bcSAlexander Shishkin 
474c7fd62bcSAlexander Shishkin 	pe = __stm_lookup_protocol(name);
475c7fd62bcSAlexander Shishkin 	if (pe && pe->pdrv && stm_get_protocol(pe->pdrv)) {
476c7fd62bcSAlexander Shishkin 		*pdrv = pe->pdrv;
477c7fd62bcSAlexander Shishkin 		*node_type = pe->node_type;
478c7fd62bcSAlexander Shishkin 	}
479c7fd62bcSAlexander Shishkin 
480c7fd62bcSAlexander Shishkin 	mutex_unlock(&stm_pdrv_mutex);
481c7fd62bcSAlexander Shishkin 
482c7fd62bcSAlexander Shishkin 	return pe ? 0 : -ENOENT;
483c7fd62bcSAlexander Shishkin }
484c7fd62bcSAlexander Shishkin 
stm_char_open(struct inode * inode,struct file * file)4857bd1d409SAlexander Shishkin static int stm_char_open(struct inode *inode, struct file *file)
4867bd1d409SAlexander Shishkin {
4877bd1d409SAlexander Shishkin 	struct stm_file *stmf;
4887bd1d409SAlexander Shishkin 	struct device *dev;
4897bd1d409SAlexander Shishkin 	unsigned int major = imajor(inode);
490a0ebf519SJohan Hovold 	int err = -ENOMEM;
4917bd1d409SAlexander Shishkin 
4927bd1d409SAlexander Shishkin 	dev = class_find_device(&stm_class, NULL, &major, major_match);
4937bd1d409SAlexander Shishkin 	if (!dev)
4947bd1d409SAlexander Shishkin 		return -ENODEV;
4957bd1d409SAlexander Shishkin 
4967bd1d409SAlexander Shishkin 	stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
4977bd1d409SAlexander Shishkin 	if (!stmf)
498a0ebf519SJohan Hovold 		goto err_put_device;
4997bd1d409SAlexander Shishkin 
500a0ebf519SJohan Hovold 	err = -ENODEV;
501cde4ad83SAlexander Shishkin 	stm_output_init(&stmf->output);
5027bd1d409SAlexander Shishkin 	stmf->stm = to_stm_device(dev);
5037bd1d409SAlexander Shishkin 
5047bd1d409SAlexander Shishkin 	if (!try_module_get(stmf->stm->owner))
5057bd1d409SAlexander Shishkin 		goto err_free;
5067bd1d409SAlexander Shishkin 
5077bd1d409SAlexander Shishkin 	file->private_data = stmf;
5087bd1d409SAlexander Shishkin 
5097bd1d409SAlexander Shishkin 	return nonseekable_open(inode, file);
5107bd1d409SAlexander Shishkin 
5117bd1d409SAlexander Shishkin err_free:
512a0ebf519SJohan Hovold 	kfree(stmf);
513a0ebf519SJohan Hovold err_put_device:
514f7c81c71SAlexander Shishkin 	/* matches class_find_device() above */
515f7c81c71SAlexander Shishkin 	put_device(dev);
5167bd1d409SAlexander Shishkin 
5177bd1d409SAlexander Shishkin 	return err;
5187bd1d409SAlexander Shishkin }
5197bd1d409SAlexander Shishkin 
stm_char_release(struct inode * inode,struct file * file)5207bd1d409SAlexander Shishkin static int stm_char_release(struct inode *inode, struct file *file)
5217bd1d409SAlexander Shishkin {
5227bd1d409SAlexander Shishkin 	struct stm_file *stmf = file->private_data;
523cc842407SAlexander Shishkin 	struct stm_device *stm = stmf->stm;
5247bd1d409SAlexander Shishkin 
525cc842407SAlexander Shishkin 	if (stm->data->unlink)
526cc842407SAlexander Shishkin 		stm->data->unlink(stm->data, stmf->output.master,
527cc842407SAlexander Shishkin 				  stmf->output.channel);
528cc842407SAlexander Shishkin 
529cc842407SAlexander Shishkin 	stm_output_free(stm, &stmf->output);
530f7c81c71SAlexander Shishkin 
531f7c81c71SAlexander Shishkin 	/*
532f7c81c71SAlexander Shishkin 	 * matches the stm_char_open()'s
533f7c81c71SAlexander Shishkin 	 * class_find_device() + try_module_get()
534f7c81c71SAlexander Shishkin 	 */
535cc842407SAlexander Shishkin 	stm_put_device(stm);
5367bd1d409SAlexander Shishkin 	kfree(stmf);
5377bd1d409SAlexander Shishkin 
5387bd1d409SAlexander Shishkin 	return 0;
5397bd1d409SAlexander Shishkin }
5407bd1d409SAlexander Shishkin 
541cb6102bdSAlexander Shishkin static int
stm_assign_first_policy(struct stm_device * stm,struct stm_output * output,char ** ids,unsigned int width)542cb6102bdSAlexander Shishkin stm_assign_first_policy(struct stm_device *stm, struct stm_output *output,
543cb6102bdSAlexander Shishkin 			char **ids, unsigned int width)
5447bd1d409SAlexander Shishkin {
545cb6102bdSAlexander Shishkin 	struct stp_policy_node *pn;
546cb6102bdSAlexander Shishkin 	int err, n;
5477bd1d409SAlexander Shishkin 
548cb6102bdSAlexander Shishkin 	/*
549cb6102bdSAlexander Shishkin 	 * On success, stp_policy_node_lookup() will return holding the
550cb6102bdSAlexander Shishkin 	 * configfs subsystem mutex, which is then released in
551cb6102bdSAlexander Shishkin 	 * stp_policy_node_put(). This allows the pdrv->output_open() in
552cb6102bdSAlexander Shishkin 	 * stm_output_assign() to serialize against the attribute accessors.
553cb6102bdSAlexander Shishkin 	 */
554cb6102bdSAlexander Shishkin 	for (n = 0, pn = NULL; ids[n] && !pn; n++)
555cb6102bdSAlexander Shishkin 		pn = stp_policy_node_lookup(stm, ids[n]);
5567bd1d409SAlexander Shishkin 
557cb6102bdSAlexander Shishkin 	if (!pn)
558cb6102bdSAlexander Shishkin 		return -EINVAL;
5597bd1d409SAlexander Shishkin 
560cb6102bdSAlexander Shishkin 	err = stm_output_assign(stm, width, pn, output);
5617bd1d409SAlexander Shishkin 
562cb6102bdSAlexander Shishkin 	stp_policy_node_put(pn);
563cb6102bdSAlexander Shishkin 
564cb6102bdSAlexander Shishkin 	return err;
5657bd1d409SAlexander Shishkin }
5667bd1d409SAlexander Shishkin 
567d279a380SAlexander Shishkin /**
568d279a380SAlexander Shishkin  * stm_data_write() - send the given payload as data packets
569d279a380SAlexander Shishkin  * @data:	stm driver's data
570d279a380SAlexander Shishkin  * @m:		STP master
571d279a380SAlexander Shishkin  * @c:		STP channel
572d279a380SAlexander Shishkin  * @ts_first:	timestamp the first packet
573d279a380SAlexander Shishkin  * @buf:	data payload buffer
574d279a380SAlexander Shishkin  * @count:	data payload size
575d279a380SAlexander Shishkin  */
stm_data_write(struct stm_data * data,unsigned int m,unsigned int c,bool ts_first,const void * buf,size_t count)576d279a380SAlexander Shishkin ssize_t notrace stm_data_write(struct stm_data *data, unsigned int m,
577d279a380SAlexander Shishkin 			       unsigned int c, bool ts_first, const void *buf,
578d279a380SAlexander Shishkin 			       size_t count)
579d279a380SAlexander Shishkin {
580d279a380SAlexander Shishkin 	unsigned int flags = ts_first ? STP_PACKET_TIMESTAMPED : 0;
581d279a380SAlexander Shishkin 	ssize_t sz;
582d279a380SAlexander Shishkin 	size_t pos;
583d279a380SAlexander Shishkin 
584d279a380SAlexander Shishkin 	for (pos = 0, sz = 0; pos < count; pos += sz) {
585d279a380SAlexander Shishkin 		sz = min_t(unsigned int, count - pos, 8);
586d279a380SAlexander Shishkin 		sz = data->packet(data, m, c, STP_PACKET_DATA, flags, sz,
587d279a380SAlexander Shishkin 				  &((u8 *)buf)[pos]);
588d279a380SAlexander Shishkin 		if (sz <= 0)
589d279a380SAlexander Shishkin 			break;
590d279a380SAlexander Shishkin 
591d279a380SAlexander Shishkin 		if (ts_first) {
592d279a380SAlexander Shishkin 			flags = 0;
593d279a380SAlexander Shishkin 			ts_first = false;
594d279a380SAlexander Shishkin 		}
595d279a380SAlexander Shishkin 	}
596d279a380SAlexander Shishkin 
597d279a380SAlexander Shishkin 	return sz < 0 ? sz : pos;
598d279a380SAlexander Shishkin }
599d279a380SAlexander Shishkin EXPORT_SYMBOL_GPL(stm_data_write);
600d279a380SAlexander Shishkin 
60124c7bcb6SAlexander Shishkin static ssize_t notrace
stm_write(struct stm_device * stm,struct stm_output * output,unsigned int chan,const char * buf,size_t count)60224c7bcb6SAlexander Shishkin stm_write(struct stm_device *stm, struct stm_output *output,
60324c7bcb6SAlexander Shishkin 	  unsigned int chan, const char *buf, size_t count)
6047bd1d409SAlexander Shishkin {
60524c7bcb6SAlexander Shishkin 	int err;
6067bd1d409SAlexander Shishkin 
60724c7bcb6SAlexander Shishkin 	/* stm->pdrv is serialized against policy_mutex */
60824c7bcb6SAlexander Shishkin 	if (!stm->pdrv)
60924c7bcb6SAlexander Shishkin 		return -ENODEV;
610f8560a9bSAlexander Shishkin 
61124c7bcb6SAlexander Shishkin 	err = stm->pdrv->write(stm->data, output, chan, buf, count);
61224c7bcb6SAlexander Shishkin 	if (err < 0)
61324c7bcb6SAlexander Shishkin 		return err;
61424c7bcb6SAlexander Shishkin 
61524c7bcb6SAlexander Shishkin 	return err;
6167bd1d409SAlexander Shishkin }
6177bd1d409SAlexander Shishkin 
stm_char_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)6187bd1d409SAlexander Shishkin static ssize_t stm_char_write(struct file *file, const char __user *buf,
6197bd1d409SAlexander Shishkin 			      size_t count, loff_t *ppos)
6207bd1d409SAlexander Shishkin {
6217bd1d409SAlexander Shishkin 	struct stm_file *stmf = file->private_data;
6227bd1d409SAlexander Shishkin 	struct stm_device *stm = stmf->stm;
6237bd1d409SAlexander Shishkin 	char *kbuf;
6247bd1d409SAlexander Shishkin 	int err;
6257bd1d409SAlexander Shishkin 
626f08b1826SAlexander Shishkin 	if (count + 1 > PAGE_SIZE)
627f08b1826SAlexander Shishkin 		count = PAGE_SIZE - 1;
628f08b1826SAlexander Shishkin 
6297bd1d409SAlexander Shishkin 	/*
630cb6102bdSAlexander Shishkin 	 * If no m/c have been assigned to this writer up to this
631cb6102bdSAlexander Shishkin 	 * point, try to use the task name and "default" policy entries.
6327bd1d409SAlexander Shishkin 	 */
6337bd1d409SAlexander Shishkin 	if (!stmf->output.nr_chans) {
634cb6102bdSAlexander Shishkin 		char comm[sizeof(current->comm)];
635cb6102bdSAlexander Shishkin 		char *ids[] = { comm, "default", NULL };
636cb6102bdSAlexander Shishkin 
637cb6102bdSAlexander Shishkin 		get_task_comm(comm, current);
638cb6102bdSAlexander Shishkin 
639cb6102bdSAlexander Shishkin 		err = stm_assign_first_policy(stmf->stm, &stmf->output, ids, 1);
6407bd1d409SAlexander Shishkin 		/*
6417bd1d409SAlexander Shishkin 		 * EBUSY means that somebody else just assigned this
6427bd1d409SAlexander Shishkin 		 * output, which is just fine for write()
6437bd1d409SAlexander Shishkin 		 */
644cb6102bdSAlexander Shishkin 		if (err)
6457bd1d409SAlexander Shishkin 			return err;
6467bd1d409SAlexander Shishkin 	}
6477bd1d409SAlexander Shishkin 
6487bd1d409SAlexander Shishkin 	kbuf = kmalloc(count + 1, GFP_KERNEL);
6497bd1d409SAlexander Shishkin 	if (!kbuf)
6507bd1d409SAlexander Shishkin 		return -ENOMEM;
6517bd1d409SAlexander Shishkin 
6527bd1d409SAlexander Shishkin 	err = copy_from_user(kbuf, buf, count);
6537bd1d409SAlexander Shishkin 	if (err) {
6547bd1d409SAlexander Shishkin 		kfree(kbuf);
6557bd1d409SAlexander Shishkin 		return -EFAULT;
6567bd1d409SAlexander Shishkin 	}
6577bd1d409SAlexander Shishkin 
6588e0469a4SAlexander Shishkin 	pm_runtime_get_sync(&stm->dev);
6598e0469a4SAlexander Shishkin 
66024c7bcb6SAlexander Shishkin 	count = stm_write(stm, &stmf->output, 0, kbuf, count);
6617bd1d409SAlexander Shishkin 
6628e0469a4SAlexander Shishkin 	pm_runtime_mark_last_busy(&stm->dev);
6638e0469a4SAlexander Shishkin 	pm_runtime_put_autosuspend(&stm->dev);
6647bd1d409SAlexander Shishkin 	kfree(kbuf);
6657bd1d409SAlexander Shishkin 
6667bd1d409SAlexander Shishkin 	return count;
6677bd1d409SAlexander Shishkin }
6687bd1d409SAlexander Shishkin 
stm_mmap_open(struct vm_area_struct * vma)6698e0469a4SAlexander Shishkin static void stm_mmap_open(struct vm_area_struct *vma)
6708e0469a4SAlexander Shishkin {
6718e0469a4SAlexander Shishkin 	struct stm_file *stmf = vma->vm_file->private_data;
6728e0469a4SAlexander Shishkin 	struct stm_device *stm = stmf->stm;
6738e0469a4SAlexander Shishkin 
6748e0469a4SAlexander Shishkin 	pm_runtime_get(&stm->dev);
6758e0469a4SAlexander Shishkin }
6768e0469a4SAlexander Shishkin 
stm_mmap_close(struct vm_area_struct * vma)6778e0469a4SAlexander Shishkin static void stm_mmap_close(struct vm_area_struct *vma)
6788e0469a4SAlexander Shishkin {
6798e0469a4SAlexander Shishkin 	struct stm_file *stmf = vma->vm_file->private_data;
6808e0469a4SAlexander Shishkin 	struct stm_device *stm = stmf->stm;
6818e0469a4SAlexander Shishkin 
6828e0469a4SAlexander Shishkin 	pm_runtime_mark_last_busy(&stm->dev);
6838e0469a4SAlexander Shishkin 	pm_runtime_put_autosuspend(&stm->dev);
6848e0469a4SAlexander Shishkin }
6858e0469a4SAlexander Shishkin 
6868e0469a4SAlexander Shishkin static const struct vm_operations_struct stm_mmap_vmops = {
6878e0469a4SAlexander Shishkin 	.open	= stm_mmap_open,
6888e0469a4SAlexander Shishkin 	.close	= stm_mmap_close,
6898e0469a4SAlexander Shishkin };
6908e0469a4SAlexander Shishkin 
stm_char_mmap(struct file * file,struct vm_area_struct * vma)6917bd1d409SAlexander Shishkin static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)
6927bd1d409SAlexander Shishkin {
6937bd1d409SAlexander Shishkin 	struct stm_file *stmf = file->private_data;
6947bd1d409SAlexander Shishkin 	struct stm_device *stm = stmf->stm;
6957bd1d409SAlexander Shishkin 	unsigned long size, phys;
6967bd1d409SAlexander Shishkin 
6977bd1d409SAlexander Shishkin 	if (!stm->data->mmio_addr)
6987bd1d409SAlexander Shishkin 		return -EOPNOTSUPP;
6997bd1d409SAlexander Shishkin 
7007bd1d409SAlexander Shishkin 	if (vma->vm_pgoff)
7017bd1d409SAlexander Shishkin 		return -EINVAL;
7027bd1d409SAlexander Shishkin 
7037bd1d409SAlexander Shishkin 	size = vma->vm_end - vma->vm_start;
7047bd1d409SAlexander Shishkin 
7057bd1d409SAlexander Shishkin 	if (stmf->output.nr_chans * stm->data->sw_mmiosz != size)
7067bd1d409SAlexander Shishkin 		return -EINVAL;
7077bd1d409SAlexander Shishkin 
7087bd1d409SAlexander Shishkin 	phys = stm->data->mmio_addr(stm->data, stmf->output.master,
7097bd1d409SAlexander Shishkin 				    stmf->output.channel,
7107bd1d409SAlexander Shishkin 				    stmf->output.nr_chans);
7117bd1d409SAlexander Shishkin 
7127bd1d409SAlexander Shishkin 	if (!phys)
7137bd1d409SAlexander Shishkin 		return -EINVAL;
7147bd1d409SAlexander Shishkin 
7158e0469a4SAlexander Shishkin 	pm_runtime_get_sync(&stm->dev);
7168e0469a4SAlexander Shishkin 
7177bd1d409SAlexander Shishkin 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
7181c71222eSSuren Baghdasaryan 	vm_flags_set(vma, VM_IO | VM_DONTEXPAND | VM_DONTDUMP);
7198e0469a4SAlexander Shishkin 	vma->vm_ops = &stm_mmap_vmops;
7207bd1d409SAlexander Shishkin 	vm_iomap_memory(vma, phys, size);
7217bd1d409SAlexander Shishkin 
7227bd1d409SAlexander Shishkin 	return 0;
7237bd1d409SAlexander Shishkin }
7247bd1d409SAlexander Shishkin 
stm_char_policy_set_ioctl(struct stm_file * stmf,void __user * arg)7257bd1d409SAlexander Shishkin static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg)
7267bd1d409SAlexander Shishkin {
7277bd1d409SAlexander Shishkin 	struct stm_device *stm = stmf->stm;
7287bd1d409SAlexander Shishkin 	struct stp_policy_id *id;
729cb6102bdSAlexander Shishkin 	char *ids[] = { NULL, NULL };
730bf7cbaaeSAlexander Shishkin 	int ret = -EINVAL, wlimit = 1;
7317bd1d409SAlexander Shishkin 	u32 size;
7327bd1d409SAlexander Shishkin 
7337bd1d409SAlexander Shishkin 	if (stmf->output.nr_chans)
7347bd1d409SAlexander Shishkin 		return -EBUSY;
7357bd1d409SAlexander Shishkin 
7367bd1d409SAlexander Shishkin 	if (copy_from_user(&size, arg, sizeof(size)))
7377bd1d409SAlexander Shishkin 		return -EFAULT;
7387bd1d409SAlexander Shishkin 
73971c488f3SDan Carpenter 	if (size < sizeof(*id) || size >= PATH_MAX + sizeof(*id))
7407bd1d409SAlexander Shishkin 		return -EINVAL;
7417bd1d409SAlexander Shishkin 
7427bd1d409SAlexander Shishkin 	/*
7437bd1d409SAlexander Shishkin 	 * size + 1 to make sure the .id string at the bottom is terminated,
7447bd1d409SAlexander Shishkin 	 * which is also why memdup_user() is not useful here
7457bd1d409SAlexander Shishkin 	 */
7467bd1d409SAlexander Shishkin 	id = kzalloc(size + 1, GFP_KERNEL);
7477bd1d409SAlexander Shishkin 	if (!id)
7487bd1d409SAlexander Shishkin 		return -ENOMEM;
7497bd1d409SAlexander Shishkin 
7507bd1d409SAlexander Shishkin 	if (copy_from_user(id, arg, size)) {
7517bd1d409SAlexander Shishkin 		ret = -EFAULT;
7527bd1d409SAlexander Shishkin 		goto err_free;
7537bd1d409SAlexander Shishkin 	}
7547bd1d409SAlexander Shishkin 
7557bd1d409SAlexander Shishkin 	if (id->__reserved_0 || id->__reserved_1)
7567bd1d409SAlexander Shishkin 		goto err_free;
7577bd1d409SAlexander Shishkin 
758bf7cbaaeSAlexander Shishkin 	if (stm->data->sw_mmiosz)
759bf7cbaaeSAlexander Shishkin 		wlimit = PAGE_SIZE / stm->data->sw_mmiosz;
760bf7cbaaeSAlexander Shishkin 
761bf7cbaaeSAlexander Shishkin 	if (id->width < 1 || id->width > wlimit)
7627bd1d409SAlexander Shishkin 		goto err_free;
7637bd1d409SAlexander Shishkin 
764cb6102bdSAlexander Shishkin 	ids[0] = id->id;
765cb6102bdSAlexander Shishkin 	ret = stm_assign_first_policy(stmf->stm, &stmf->output, ids,
766cb6102bdSAlexander Shishkin 				      id->width);
7677bd1d409SAlexander Shishkin 	if (ret)
7687bd1d409SAlexander Shishkin 		goto err_free;
7697bd1d409SAlexander Shishkin 
7707bd1d409SAlexander Shishkin 	if (stm->data->link)
7717bd1d409SAlexander Shishkin 		ret = stm->data->link(stm->data, stmf->output.master,
7727bd1d409SAlexander Shishkin 				      stmf->output.channel);
7737bd1d409SAlexander Shishkin 
774f7c81c71SAlexander Shishkin 	if (ret)
7757bd1d409SAlexander Shishkin 		stm_output_free(stmf->stm, &stmf->output);
7767bd1d409SAlexander Shishkin 
7777bd1d409SAlexander Shishkin err_free:
7787bd1d409SAlexander Shishkin 	kfree(id);
7797bd1d409SAlexander Shishkin 
7807bd1d409SAlexander Shishkin 	return ret;
7817bd1d409SAlexander Shishkin }
7827bd1d409SAlexander Shishkin 
stm_char_policy_get_ioctl(struct stm_file * stmf,void __user * arg)7837bd1d409SAlexander Shishkin static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg)
7847bd1d409SAlexander Shishkin {
7857bd1d409SAlexander Shishkin 	struct stp_policy_id id = {
7867bd1d409SAlexander Shishkin 		.size		= sizeof(id),
7877bd1d409SAlexander Shishkin 		.master		= stmf->output.master,
7887bd1d409SAlexander Shishkin 		.channel	= stmf->output.channel,
7897bd1d409SAlexander Shishkin 		.width		= stmf->output.nr_chans,
7907bd1d409SAlexander Shishkin 		.__reserved_0	= 0,
7917bd1d409SAlexander Shishkin 		.__reserved_1	= 0,
7927bd1d409SAlexander Shishkin 	};
7937bd1d409SAlexander Shishkin 
7947bd1d409SAlexander Shishkin 	return copy_to_user(arg, &id, id.size) ? -EFAULT : 0;
7957bd1d409SAlexander Shishkin }
7967bd1d409SAlexander Shishkin 
7977bd1d409SAlexander Shishkin static long
stm_char_ioctl(struct file * file,unsigned int cmd,unsigned long arg)7987bd1d409SAlexander Shishkin stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
7997bd1d409SAlexander Shishkin {
8007bd1d409SAlexander Shishkin 	struct stm_file *stmf = file->private_data;
8017bd1d409SAlexander Shishkin 	struct stm_data *stm_data = stmf->stm->data;
8027bd1d409SAlexander Shishkin 	int err = -ENOTTY;
8037bd1d409SAlexander Shishkin 	u64 options;
8047bd1d409SAlexander Shishkin 
8057bd1d409SAlexander Shishkin 	switch (cmd) {
8067bd1d409SAlexander Shishkin 	case STP_POLICY_ID_SET:
8077bd1d409SAlexander Shishkin 		err = stm_char_policy_set_ioctl(stmf, (void __user *)arg);
8087bd1d409SAlexander Shishkin 		if (err)
8097bd1d409SAlexander Shishkin 			return err;
8107bd1d409SAlexander Shishkin 
8117bd1d409SAlexander Shishkin 		return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
8127bd1d409SAlexander Shishkin 
8137bd1d409SAlexander Shishkin 	case STP_POLICY_ID_GET:
8147bd1d409SAlexander Shishkin 		return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
8157bd1d409SAlexander Shishkin 
8167bd1d409SAlexander Shishkin 	case STP_SET_OPTIONS:
8177bd1d409SAlexander Shishkin 		if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64)))
8187bd1d409SAlexander Shishkin 			return -EFAULT;
8197bd1d409SAlexander Shishkin 
8207bd1d409SAlexander Shishkin 		if (stm_data->set_options)
8217bd1d409SAlexander Shishkin 			err = stm_data->set_options(stm_data,
8227bd1d409SAlexander Shishkin 						    stmf->output.master,
8237bd1d409SAlexander Shishkin 						    stmf->output.channel,
8247bd1d409SAlexander Shishkin 						    stmf->output.nr_chans,
8257bd1d409SAlexander Shishkin 						    options);
8267bd1d409SAlexander Shishkin 
8277bd1d409SAlexander Shishkin 		break;
8287bd1d409SAlexander Shishkin 	default:
8297bd1d409SAlexander Shishkin 		break;
8307bd1d409SAlexander Shishkin 	}
8317bd1d409SAlexander Shishkin 
8327bd1d409SAlexander Shishkin 	return err;
8337bd1d409SAlexander Shishkin }
8347bd1d409SAlexander Shishkin 
8357bd1d409SAlexander Shishkin static const struct file_operations stm_fops = {
8367bd1d409SAlexander Shishkin 	.open		= stm_char_open,
8377bd1d409SAlexander Shishkin 	.release	= stm_char_release,
8387bd1d409SAlexander Shishkin 	.write		= stm_char_write,
8397bd1d409SAlexander Shishkin 	.mmap		= stm_char_mmap,
8407bd1d409SAlexander Shishkin 	.unlocked_ioctl	= stm_char_ioctl,
841407e9ef7SArnd Bergmann 	.compat_ioctl	= compat_ptr_ioctl,
8427bd1d409SAlexander Shishkin 	.llseek		= no_llseek,
8437bd1d409SAlexander Shishkin };
8447bd1d409SAlexander Shishkin 
stm_device_release(struct device * dev)8457bd1d409SAlexander Shishkin static void stm_device_release(struct device *dev)
8467bd1d409SAlexander Shishkin {
8477bd1d409SAlexander Shishkin 	struct stm_device *stm = to_stm_device(dev);
8487bd1d409SAlexander Shishkin 
849b5e2ced9SAlexander Shishkin 	vfree(stm);
8507bd1d409SAlexander Shishkin }
8517bd1d409SAlexander Shishkin 
stm_register_device(struct device * parent,struct stm_data * stm_data,struct module * owner)8527bd1d409SAlexander Shishkin int stm_register_device(struct device *parent, struct stm_data *stm_data,
8537bd1d409SAlexander Shishkin 			struct module *owner)
8547bd1d409SAlexander Shishkin {
8557bd1d409SAlexander Shishkin 	struct stm_device *stm;
8567bd1d409SAlexander Shishkin 	unsigned int nmasters;
8577bd1d409SAlexander Shishkin 	int err = -ENOMEM;
8587bd1d409SAlexander Shishkin 
8597bd1d409SAlexander Shishkin 	if (!stm_core_up)
8607bd1d409SAlexander Shishkin 		return -EPROBE_DEFER;
8617bd1d409SAlexander Shishkin 
8627bd1d409SAlexander Shishkin 	if (!stm_data->packet || !stm_data->sw_nchannels)
8637bd1d409SAlexander Shishkin 		return -EINVAL;
8647bd1d409SAlexander Shishkin 
8657b3bb0e7SChunyan Zhang 	nmasters = stm_data->sw_end - stm_data->sw_start + 1;
866b5e2ced9SAlexander Shishkin 	stm = vzalloc(sizeof(*stm) + nmasters * sizeof(void *));
8677bd1d409SAlexander Shishkin 	if (!stm)
8687bd1d409SAlexander Shishkin 		return -ENOMEM;
8697bd1d409SAlexander Shishkin 
8707bd1d409SAlexander Shishkin 	stm->major = register_chrdev(0, stm_data->name, &stm_fops);
871370c4804SDan Carpenter 	if (stm->major < 0) {
872370c4804SDan Carpenter 		err = stm->major;
873370c4804SDan Carpenter 		vfree(stm);
874370c4804SDan Carpenter 		return err;
875370c4804SDan Carpenter 	}
8767bd1d409SAlexander Shishkin 
8777bd1d409SAlexander Shishkin 	device_initialize(&stm->dev);
8787bd1d409SAlexander Shishkin 	stm->dev.devt = MKDEV(stm->major, 0);
8797bd1d409SAlexander Shishkin 	stm->dev.class = &stm_class;
8807bd1d409SAlexander Shishkin 	stm->dev.parent = parent;
8817bd1d409SAlexander Shishkin 	stm->dev.release = stm_device_release;
8827bd1d409SAlexander Shishkin 
883389b6699SAlexander Shishkin 	mutex_init(&stm->link_mutex);
884389b6699SAlexander Shishkin 	spin_lock_init(&stm->link_lock);
885389b6699SAlexander Shishkin 	INIT_LIST_HEAD(&stm->link_list);
886389b6699SAlexander Shishkin 
887389b6699SAlexander Shishkin 	/* initialize the object before it is accessible via sysfs */
888389b6699SAlexander Shishkin 	spin_lock_init(&stm->mc_lock);
889389b6699SAlexander Shishkin 	mutex_init(&stm->policy_mutex);
890389b6699SAlexander Shishkin 	stm->sw_nmasters = nmasters;
891389b6699SAlexander Shishkin 	stm->owner = owner;
892389b6699SAlexander Shishkin 	stm->data = stm_data;
893389b6699SAlexander Shishkin 	stm_data->stm = stm;
894389b6699SAlexander Shishkin 
8957bd1d409SAlexander Shishkin 	err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name);
8967bd1d409SAlexander Shishkin 	if (err)
8977bd1d409SAlexander Shishkin 		goto err_device;
8987bd1d409SAlexander Shishkin 
8997bd1d409SAlexander Shishkin 	err = device_add(&stm->dev);
9007bd1d409SAlexander Shishkin 	if (err)
9017bd1d409SAlexander Shishkin 		goto err_device;
9027bd1d409SAlexander Shishkin 
9038e0469a4SAlexander Shishkin 	/*
9048e0469a4SAlexander Shishkin 	 * Use delayed autosuspend to avoid bouncing back and forth
9058e0469a4SAlexander Shishkin 	 * on recurring character device writes, with the initial
9068e0469a4SAlexander Shishkin 	 * delay time of 2 seconds.
9078e0469a4SAlexander Shishkin 	 */
9088e0469a4SAlexander Shishkin 	pm_runtime_no_callbacks(&stm->dev);
9098e0469a4SAlexander Shishkin 	pm_runtime_use_autosuspend(&stm->dev);
9108e0469a4SAlexander Shishkin 	pm_runtime_set_autosuspend_delay(&stm->dev, 2000);
9118e0469a4SAlexander Shishkin 	pm_runtime_set_suspended(&stm->dev);
9128e0469a4SAlexander Shishkin 	pm_runtime_enable(&stm->dev);
9138e0469a4SAlexander Shishkin 
9147bd1d409SAlexander Shishkin 	return 0;
9157bd1d409SAlexander Shishkin 
9167bd1d409SAlexander Shishkin err_device:
917cbe4a61dSAlexander Shishkin 	unregister_chrdev(stm->major, stm_data->name);
918cbe4a61dSAlexander Shishkin 
919370c4804SDan Carpenter 	/* calls stm_device_release() */
9207bd1d409SAlexander Shishkin 	put_device(&stm->dev);
9217bd1d409SAlexander Shishkin 
9227bd1d409SAlexander Shishkin 	return err;
9237bd1d409SAlexander Shishkin }
9247bd1d409SAlexander Shishkin EXPORT_SYMBOL_GPL(stm_register_device);
9257bd1d409SAlexander Shishkin 
926b4ca34aaSAlexander Shishkin static int __stm_source_link_drop(struct stm_source_device *src,
9277bd1d409SAlexander Shishkin 				  struct stm_device *stm);
9287bd1d409SAlexander Shishkin 
stm_unregister_device(struct stm_data * stm_data)9297bd1d409SAlexander Shishkin void stm_unregister_device(struct stm_data *stm_data)
9307bd1d409SAlexander Shishkin {
9317bd1d409SAlexander Shishkin 	struct stm_device *stm = stm_data->stm;
9327bd1d409SAlexander Shishkin 	struct stm_source_device *src, *iter;
933b4ca34aaSAlexander Shishkin 	int i, ret;
9347bd1d409SAlexander Shishkin 
9358e0469a4SAlexander Shishkin 	pm_runtime_dont_use_autosuspend(&stm->dev);
9368e0469a4SAlexander Shishkin 	pm_runtime_disable(&stm->dev);
9378e0469a4SAlexander Shishkin 
938c74f7e82SAlexander Shishkin 	mutex_lock(&stm->link_mutex);
9397bd1d409SAlexander Shishkin 	list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) {
940b4ca34aaSAlexander Shishkin 		ret = __stm_source_link_drop(src, stm);
941b4ca34aaSAlexander Shishkin 		/*
942b4ca34aaSAlexander Shishkin 		 * src <-> stm link must not change under the same
943b4ca34aaSAlexander Shishkin 		 * stm::link_mutex, so complain loudly if it has;
944b4ca34aaSAlexander Shishkin 		 * also in this situation ret!=0 means this src is
945b4ca34aaSAlexander Shishkin 		 * not connected to this stm and it should be otherwise
946b4ca34aaSAlexander Shishkin 		 * safe to proceed with the tear-down of stm.
947b4ca34aaSAlexander Shishkin 		 */
948b4ca34aaSAlexander Shishkin 		WARN_ON_ONCE(ret);
9497bd1d409SAlexander Shishkin 	}
950c74f7e82SAlexander Shishkin 	mutex_unlock(&stm->link_mutex);
9517bd1d409SAlexander Shishkin 
9527bd1d409SAlexander Shishkin 	synchronize_srcu(&stm_source_srcu);
9537bd1d409SAlexander Shishkin 
9547bd1d409SAlexander Shishkin 	unregister_chrdev(stm->major, stm_data->name);
9557bd1d409SAlexander Shishkin 
9567bd1d409SAlexander Shishkin 	mutex_lock(&stm->policy_mutex);
9577bd1d409SAlexander Shishkin 	if (stm->policy)
9587bd1d409SAlexander Shishkin 		stp_policy_unbind(stm->policy);
9597bd1d409SAlexander Shishkin 	mutex_unlock(&stm->policy_mutex);
9607bd1d409SAlexander Shishkin 
96173a3ed19SChunyan Zhang 	for (i = stm->data->sw_start; i <= stm->data->sw_end; i++)
9627bd1d409SAlexander Shishkin 		stp_master_free(stm, i);
9637bd1d409SAlexander Shishkin 
9647bd1d409SAlexander Shishkin 	device_unregister(&stm->dev);
9657bd1d409SAlexander Shishkin 	stm_data->stm = NULL;
9667bd1d409SAlexander Shishkin }
9677bd1d409SAlexander Shishkin EXPORT_SYMBOL_GPL(stm_unregister_device);
9687bd1d409SAlexander Shishkin 
969c74f7e82SAlexander Shishkin /*
970c74f7e82SAlexander Shishkin  * stm::link_list access serialization uses a spinlock and a mutex; holding
971c74f7e82SAlexander Shishkin  * either of them guarantees that the list is stable; modification requires
972c74f7e82SAlexander Shishkin  * holding both of them.
973c74f7e82SAlexander Shishkin  *
974c74f7e82SAlexander Shishkin  * Lock ordering is as follows:
975c74f7e82SAlexander Shishkin  *   stm::link_mutex
976c74f7e82SAlexander Shishkin  *     stm::link_lock
977c74f7e82SAlexander Shishkin  *       src::link_lock
978c74f7e82SAlexander Shishkin  */
979c74f7e82SAlexander Shishkin 
9807bd1d409SAlexander Shishkin /**
9817bd1d409SAlexander Shishkin  * stm_source_link_add() - connect an stm_source device to an stm device
9827bd1d409SAlexander Shishkin  * @src:	stm_source device
9837bd1d409SAlexander Shishkin  * @stm:	stm device
9847bd1d409SAlexander Shishkin  *
9857bd1d409SAlexander Shishkin  * This function establishes a link from stm_source to an stm device so that
9867bd1d409SAlexander Shishkin  * the former can send out trace data to the latter.
9877bd1d409SAlexander Shishkin  *
9887bd1d409SAlexander Shishkin  * Return:	0 on success, -errno otherwise.
9897bd1d409SAlexander Shishkin  */
stm_source_link_add(struct stm_source_device * src,struct stm_device * stm)9907bd1d409SAlexander Shishkin static int stm_source_link_add(struct stm_source_device *src,
9917bd1d409SAlexander Shishkin 			       struct stm_device *stm)
9927bd1d409SAlexander Shishkin {
993cb6102bdSAlexander Shishkin 	char *ids[] = { NULL, "default", NULL };
994cb6102bdSAlexander Shishkin 	int err = -ENOMEM;
9957bd1d409SAlexander Shishkin 
996c74f7e82SAlexander Shishkin 	mutex_lock(&stm->link_mutex);
9977bd1d409SAlexander Shishkin 	spin_lock(&stm->link_lock);
9987bd1d409SAlexander Shishkin 	spin_lock(&src->link_lock);
9997bd1d409SAlexander Shishkin 
10007bd1d409SAlexander Shishkin 	/* src->link is dereferenced under stm_source_srcu but not the list */
10017bd1d409SAlexander Shishkin 	rcu_assign_pointer(src->link, stm);
10027bd1d409SAlexander Shishkin 	list_add_tail(&src->link_entry, &stm->link_list);
10037bd1d409SAlexander Shishkin 
10047bd1d409SAlexander Shishkin 	spin_unlock(&src->link_lock);
10057bd1d409SAlexander Shishkin 	spin_unlock(&stm->link_lock);
1006c74f7e82SAlexander Shishkin 	mutex_unlock(&stm->link_mutex);
10077bd1d409SAlexander Shishkin 
1008cb6102bdSAlexander Shishkin 	ids[0] = kstrdup(src->data->name, GFP_KERNEL);
1009cb6102bdSAlexander Shishkin 	if (!ids[0])
1010cb6102bdSAlexander Shishkin 		goto fail_detach;
10117bd1d409SAlexander Shishkin 
1012cb6102bdSAlexander Shishkin 	err = stm_assign_first_policy(stm, &src->output, ids,
1013cb6102bdSAlexander Shishkin 				      src->data->nr_chans);
1014cb6102bdSAlexander Shishkin 	kfree(ids[0]);
10157bd1d409SAlexander Shishkin 
10167bd1d409SAlexander Shishkin 	if (err)
10177bd1d409SAlexander Shishkin 		goto fail_detach;
10187bd1d409SAlexander Shishkin 
10197bd1d409SAlexander Shishkin 	/* this is to notify the STM device that a new link has been made */
10207bd1d409SAlexander Shishkin 	if (stm->data->link)
10217bd1d409SAlexander Shishkin 		err = stm->data->link(stm->data, src->output.master,
10227bd1d409SAlexander Shishkin 				      src->output.channel);
10237bd1d409SAlexander Shishkin 
10247bd1d409SAlexander Shishkin 	if (err)
10257bd1d409SAlexander Shishkin 		goto fail_free_output;
10267bd1d409SAlexander Shishkin 
10277bd1d409SAlexander Shishkin 	/* this is to let the source carry out all necessary preparations */
10287bd1d409SAlexander Shishkin 	if (src->data->link)
10297bd1d409SAlexander Shishkin 		src->data->link(src->data);
10307bd1d409SAlexander Shishkin 
10317bd1d409SAlexander Shishkin 	return 0;
10327bd1d409SAlexander Shishkin 
10337bd1d409SAlexander Shishkin fail_free_output:
10347bd1d409SAlexander Shishkin 	stm_output_free(stm, &src->output);
10357bd1d409SAlexander Shishkin 
10367bd1d409SAlexander Shishkin fail_detach:
1037c74f7e82SAlexander Shishkin 	mutex_lock(&stm->link_mutex);
10387bd1d409SAlexander Shishkin 	spin_lock(&stm->link_lock);
10397bd1d409SAlexander Shishkin 	spin_lock(&src->link_lock);
10407bd1d409SAlexander Shishkin 
10417bd1d409SAlexander Shishkin 	rcu_assign_pointer(src->link, NULL);
10427bd1d409SAlexander Shishkin 	list_del_init(&src->link_entry);
10437bd1d409SAlexander Shishkin 
10447bd1d409SAlexander Shishkin 	spin_unlock(&src->link_lock);
10457bd1d409SAlexander Shishkin 	spin_unlock(&stm->link_lock);
1046c74f7e82SAlexander Shishkin 	mutex_unlock(&stm->link_mutex);
10477bd1d409SAlexander Shishkin 
10487bd1d409SAlexander Shishkin 	return err;
10497bd1d409SAlexander Shishkin }
10507bd1d409SAlexander Shishkin 
10517bd1d409SAlexander Shishkin /**
10527bd1d409SAlexander Shishkin  * __stm_source_link_drop() - detach stm_source from an stm device
10537bd1d409SAlexander Shishkin  * @src:	stm_source device
10547bd1d409SAlexander Shishkin  * @stm:	stm device
10557bd1d409SAlexander Shishkin  *
10567bd1d409SAlexander Shishkin  * If @stm is @src::link, disconnect them from one another and put the
10577bd1d409SAlexander Shishkin  * reference on the @stm device.
10587bd1d409SAlexander Shishkin  *
1059c74f7e82SAlexander Shishkin  * Caller must hold stm::link_mutex.
10607bd1d409SAlexander Shishkin  */
__stm_source_link_drop(struct stm_source_device * src,struct stm_device * stm)1061b4ca34aaSAlexander Shishkin static int __stm_source_link_drop(struct stm_source_device *src,
10627bd1d409SAlexander Shishkin 				  struct stm_device *stm)
10637bd1d409SAlexander Shishkin {
10640df771deSAlexander Shishkin 	struct stm_device *link;
1065b4ca34aaSAlexander Shishkin 	int ret = 0;
10660df771deSAlexander Shishkin 
1067c74f7e82SAlexander Shishkin 	lockdep_assert_held(&stm->link_mutex);
1068c74f7e82SAlexander Shishkin 
1069c74f7e82SAlexander Shishkin 	/* for stm::link_list modification, we hold both mutex and spinlock */
1070c74f7e82SAlexander Shishkin 	spin_lock(&stm->link_lock);
10717bd1d409SAlexander Shishkin 	spin_lock(&src->link_lock);
10720df771deSAlexander Shishkin 	link = srcu_dereference_check(src->link, &stm_source_srcu, 1);
1073b4ca34aaSAlexander Shishkin 
1074b4ca34aaSAlexander Shishkin 	/*
1075b4ca34aaSAlexander Shishkin 	 * The linked device may have changed since we last looked, because
1076b4ca34aaSAlexander Shishkin 	 * we weren't holding the src::link_lock back then; if this is the
1077b4ca34aaSAlexander Shishkin 	 * case, tell the caller to retry.
1078b4ca34aaSAlexander Shishkin 	 */
1079b4ca34aaSAlexander Shishkin 	if (link != stm) {
1080b4ca34aaSAlexander Shishkin 		ret = -EAGAIN;
10811810f2c4SAlexander Shishkin 		goto unlock;
1082b4ca34aaSAlexander Shishkin 	}
10837bd1d409SAlexander Shishkin 
10840df771deSAlexander Shishkin 	stm_output_free(link, &src->output);
10857bd1d409SAlexander Shishkin 	list_del_init(&src->link_entry);
10868e0469a4SAlexander Shishkin 	pm_runtime_mark_last_busy(&link->dev);
10878e0469a4SAlexander Shishkin 	pm_runtime_put_autosuspend(&link->dev);
10887bd1d409SAlexander Shishkin 	/* matches stm_find_device() from stm_source_link_store() */
10890df771deSAlexander Shishkin 	stm_put_device(link);
10907bd1d409SAlexander Shishkin 	rcu_assign_pointer(src->link, NULL);
10917bd1d409SAlexander Shishkin 
10921810f2c4SAlexander Shishkin unlock:
10937bd1d409SAlexander Shishkin 	spin_unlock(&src->link_lock);
1094c74f7e82SAlexander Shishkin 	spin_unlock(&stm->link_lock);
1095b4ca34aaSAlexander Shishkin 
1096cc842407SAlexander Shishkin 	/*
1097cc842407SAlexander Shishkin 	 * Call the unlink callbacks for both source and stm, when we know
1098cc842407SAlexander Shishkin 	 * that we have actually performed the unlinking.
1099cc842407SAlexander Shishkin 	 */
1100cc842407SAlexander Shishkin 	if (!ret) {
1101cc842407SAlexander Shishkin 		if (src->data->unlink)
1102b4ca34aaSAlexander Shishkin 			src->data->unlink(src->data);
1103b4ca34aaSAlexander Shishkin 
1104cc842407SAlexander Shishkin 		if (stm->data->unlink)
1105cc842407SAlexander Shishkin 			stm->data->unlink(stm->data, src->output.master,
1106cc842407SAlexander Shishkin 					  src->output.channel);
1107cc842407SAlexander Shishkin 	}
1108cc842407SAlexander Shishkin 
1109b4ca34aaSAlexander Shishkin 	return ret;
11107bd1d409SAlexander Shishkin }
11117bd1d409SAlexander Shishkin 
11127bd1d409SAlexander Shishkin /**
11137bd1d409SAlexander Shishkin  * stm_source_link_drop() - detach stm_source from its stm device
11147bd1d409SAlexander Shishkin  * @src:	stm_source device
11157bd1d409SAlexander Shishkin  *
11167bd1d409SAlexander Shishkin  * Unlinking means disconnecting from source's STM device; after this
11177bd1d409SAlexander Shishkin  * writes will be unsuccessful until it is linked to a new STM device.
11187bd1d409SAlexander Shishkin  *
11197bd1d409SAlexander Shishkin  * This will happen on "stm_source_link" sysfs attribute write to undo
11207bd1d409SAlexander Shishkin  * the existing link (if any), or on linked STM device's de-registration.
11217bd1d409SAlexander Shishkin  */
stm_source_link_drop(struct stm_source_device * src)11227bd1d409SAlexander Shishkin static void stm_source_link_drop(struct stm_source_device *src)
11237bd1d409SAlexander Shishkin {
11247bd1d409SAlexander Shishkin 	struct stm_device *stm;
1125b4ca34aaSAlexander Shishkin 	int idx, ret;
11267bd1d409SAlexander Shishkin 
1127b4ca34aaSAlexander Shishkin retry:
11287bd1d409SAlexander Shishkin 	idx = srcu_read_lock(&stm_source_srcu);
1129b4ca34aaSAlexander Shishkin 	/*
1130b4ca34aaSAlexander Shishkin 	 * The stm device will be valid for the duration of this
1131b4ca34aaSAlexander Shishkin 	 * read section, but the link may change before we grab
1132b4ca34aaSAlexander Shishkin 	 * the src::link_lock in __stm_source_link_drop().
1133b4ca34aaSAlexander Shishkin 	 */
11347bd1d409SAlexander Shishkin 	stm = srcu_dereference(src->link, &stm_source_srcu);
11357bd1d409SAlexander Shishkin 
1136b4ca34aaSAlexander Shishkin 	ret = 0;
11377bd1d409SAlexander Shishkin 	if (stm) {
1138c74f7e82SAlexander Shishkin 		mutex_lock(&stm->link_mutex);
1139b4ca34aaSAlexander Shishkin 		ret = __stm_source_link_drop(src, stm);
1140c74f7e82SAlexander Shishkin 		mutex_unlock(&stm->link_mutex);
11417bd1d409SAlexander Shishkin 	}
11427bd1d409SAlexander Shishkin 
11437bd1d409SAlexander Shishkin 	srcu_read_unlock(&stm_source_srcu, idx);
1144b4ca34aaSAlexander Shishkin 
1145b4ca34aaSAlexander Shishkin 	/* if it did change, retry */
1146b4ca34aaSAlexander Shishkin 	if (ret == -EAGAIN)
1147b4ca34aaSAlexander Shishkin 		goto retry;
11487bd1d409SAlexander Shishkin }
11497bd1d409SAlexander Shishkin 
stm_source_link_show(struct device * dev,struct device_attribute * attr,char * buf)11507bd1d409SAlexander Shishkin static ssize_t stm_source_link_show(struct device *dev,
11517bd1d409SAlexander Shishkin 				    struct device_attribute *attr,
11527bd1d409SAlexander Shishkin 				    char *buf)
11537bd1d409SAlexander Shishkin {
11547bd1d409SAlexander Shishkin 	struct stm_source_device *src = to_stm_source_device(dev);
11557bd1d409SAlexander Shishkin 	struct stm_device *stm;
11567bd1d409SAlexander Shishkin 	int idx, ret;
11577bd1d409SAlexander Shishkin 
11587bd1d409SAlexander Shishkin 	idx = srcu_read_lock(&stm_source_srcu);
11597bd1d409SAlexander Shishkin 	stm = srcu_dereference(src->link, &stm_source_srcu);
11607bd1d409SAlexander Shishkin 	ret = sprintf(buf, "%s\n",
11617bd1d409SAlexander Shishkin 		      stm ? dev_name(&stm->dev) : "<none>");
11627bd1d409SAlexander Shishkin 	srcu_read_unlock(&stm_source_srcu, idx);
11637bd1d409SAlexander Shishkin 
11647bd1d409SAlexander Shishkin 	return ret;
11657bd1d409SAlexander Shishkin }
11667bd1d409SAlexander Shishkin 
stm_source_link_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)11677bd1d409SAlexander Shishkin static ssize_t stm_source_link_store(struct device *dev,
11687bd1d409SAlexander Shishkin 				     struct device_attribute *attr,
11697bd1d409SAlexander Shishkin 				     const char *buf, size_t count)
11707bd1d409SAlexander Shishkin {
11717bd1d409SAlexander Shishkin 	struct stm_source_device *src = to_stm_source_device(dev);
11727bd1d409SAlexander Shishkin 	struct stm_device *link;
11737bd1d409SAlexander Shishkin 	int err;
11747bd1d409SAlexander Shishkin 
11757bd1d409SAlexander Shishkin 	stm_source_link_drop(src);
11767bd1d409SAlexander Shishkin 
11777bd1d409SAlexander Shishkin 	link = stm_find_device(buf);
11787bd1d409SAlexander Shishkin 	if (!link)
11797bd1d409SAlexander Shishkin 		return -EINVAL;
11807bd1d409SAlexander Shishkin 
11818e0469a4SAlexander Shishkin 	pm_runtime_get(&link->dev);
11828e0469a4SAlexander Shishkin 
11837bd1d409SAlexander Shishkin 	err = stm_source_link_add(src, link);
1184f7c81c71SAlexander Shishkin 	if (err) {
11858e0469a4SAlexander Shishkin 		pm_runtime_put_autosuspend(&link->dev);
1186f7c81c71SAlexander Shishkin 		/* matches the stm_find_device() above */
11877bd1d409SAlexander Shishkin 		stm_put_device(link);
1188f7c81c71SAlexander Shishkin 	}
11897bd1d409SAlexander Shishkin 
11907bd1d409SAlexander Shishkin 	return err ? : count;
11917bd1d409SAlexander Shishkin }
11927bd1d409SAlexander Shishkin 
11937bd1d409SAlexander Shishkin static DEVICE_ATTR_RW(stm_source_link);
11947bd1d409SAlexander Shishkin 
11957bd1d409SAlexander Shishkin static struct attribute *stm_source_attrs[] = {
11967bd1d409SAlexander Shishkin 	&dev_attr_stm_source_link.attr,
11977bd1d409SAlexander Shishkin 	NULL,
11987bd1d409SAlexander Shishkin };
11997bd1d409SAlexander Shishkin 
12007bd1d409SAlexander Shishkin ATTRIBUTE_GROUPS(stm_source);
12017bd1d409SAlexander Shishkin 
12027bd1d409SAlexander Shishkin static struct class stm_source_class = {
12037bd1d409SAlexander Shishkin 	.name		= "stm_source",
12047bd1d409SAlexander Shishkin 	.dev_groups	= stm_source_groups,
12057bd1d409SAlexander Shishkin };
12067bd1d409SAlexander Shishkin 
stm_source_device_release(struct device * dev)12077bd1d409SAlexander Shishkin static void stm_source_device_release(struct device *dev)
12087bd1d409SAlexander Shishkin {
12097bd1d409SAlexander Shishkin 	struct stm_source_device *src = to_stm_source_device(dev);
12107bd1d409SAlexander Shishkin 
12117bd1d409SAlexander Shishkin 	kfree(src);
12127bd1d409SAlexander Shishkin }
12137bd1d409SAlexander Shishkin 
12147bd1d409SAlexander Shishkin /**
12157bd1d409SAlexander Shishkin  * stm_source_register_device() - register an stm_source device
12167bd1d409SAlexander Shishkin  * @parent:	parent device
12177bd1d409SAlexander Shishkin  * @data:	device description structure
12187bd1d409SAlexander Shishkin  *
12197bd1d409SAlexander Shishkin  * This will create a device of stm_source class that can write
12207bd1d409SAlexander Shishkin  * data to an stm device once linked.
12217bd1d409SAlexander Shishkin  *
12227bd1d409SAlexander Shishkin  * Return:	0 on success, -errno otherwise.
12237bd1d409SAlexander Shishkin  */
stm_source_register_device(struct device * parent,struct stm_source_data * data)12247bd1d409SAlexander Shishkin int stm_source_register_device(struct device *parent,
12257bd1d409SAlexander Shishkin 			       struct stm_source_data *data)
12267bd1d409SAlexander Shishkin {
12277bd1d409SAlexander Shishkin 	struct stm_source_device *src;
12287bd1d409SAlexander Shishkin 	int err;
12297bd1d409SAlexander Shishkin 
12307bd1d409SAlexander Shishkin 	if (!stm_core_up)
12317bd1d409SAlexander Shishkin 		return -EPROBE_DEFER;
12327bd1d409SAlexander Shishkin 
12337bd1d409SAlexander Shishkin 	src = kzalloc(sizeof(*src), GFP_KERNEL);
12347bd1d409SAlexander Shishkin 	if (!src)
12357bd1d409SAlexander Shishkin 		return -ENOMEM;
12367bd1d409SAlexander Shishkin 
12377bd1d409SAlexander Shishkin 	device_initialize(&src->dev);
12387bd1d409SAlexander Shishkin 	src->dev.class = &stm_source_class;
12397bd1d409SAlexander Shishkin 	src->dev.parent = parent;
12407bd1d409SAlexander Shishkin 	src->dev.release = stm_source_device_release;
12417bd1d409SAlexander Shishkin 
12427bd1d409SAlexander Shishkin 	err = kobject_set_name(&src->dev.kobj, "%s", data->name);
12437bd1d409SAlexander Shishkin 	if (err)
12447bd1d409SAlexander Shishkin 		goto err;
12457bd1d409SAlexander Shishkin 
12468e0469a4SAlexander Shishkin 	pm_runtime_no_callbacks(&src->dev);
12478e0469a4SAlexander Shishkin 	pm_runtime_forbid(&src->dev);
12488e0469a4SAlexander Shishkin 
12497bd1d409SAlexander Shishkin 	err = device_add(&src->dev);
12507bd1d409SAlexander Shishkin 	if (err)
12517bd1d409SAlexander Shishkin 		goto err;
12527bd1d409SAlexander Shishkin 
1253cde4ad83SAlexander Shishkin 	stm_output_init(&src->output);
12547bd1d409SAlexander Shishkin 	spin_lock_init(&src->link_lock);
12557bd1d409SAlexander Shishkin 	INIT_LIST_HEAD(&src->link_entry);
12567bd1d409SAlexander Shishkin 	src->data = data;
12577bd1d409SAlexander Shishkin 	data->src = src;
12587bd1d409SAlexander Shishkin 
12597bd1d409SAlexander Shishkin 	return 0;
12607bd1d409SAlexander Shishkin 
12617bd1d409SAlexander Shishkin err:
12627bd1d409SAlexander Shishkin 	put_device(&src->dev);
12637bd1d409SAlexander Shishkin 
12647bd1d409SAlexander Shishkin 	return err;
12657bd1d409SAlexander Shishkin }
12667bd1d409SAlexander Shishkin EXPORT_SYMBOL_GPL(stm_source_register_device);
12677bd1d409SAlexander Shishkin 
12687bd1d409SAlexander Shishkin /**
12697bd1d409SAlexander Shishkin  * stm_source_unregister_device() - unregister an stm_source device
12707bd1d409SAlexander Shishkin  * @data:	device description that was used to register the device
12717bd1d409SAlexander Shishkin  *
12727bd1d409SAlexander Shishkin  * This will remove a previously created stm_source device from the system.
12737bd1d409SAlexander Shishkin  */
stm_source_unregister_device(struct stm_source_data * data)12747bd1d409SAlexander Shishkin void stm_source_unregister_device(struct stm_source_data *data)
12757bd1d409SAlexander Shishkin {
12767bd1d409SAlexander Shishkin 	struct stm_source_device *src = data->src;
12777bd1d409SAlexander Shishkin 
12787bd1d409SAlexander Shishkin 	stm_source_link_drop(src);
12797bd1d409SAlexander Shishkin 
1280fd085bb1SAlexander Shishkin 	device_unregister(&src->dev);
12817bd1d409SAlexander Shishkin }
12827bd1d409SAlexander Shishkin EXPORT_SYMBOL_GPL(stm_source_unregister_device);
12837bd1d409SAlexander Shishkin 
stm_source_write(struct stm_source_data * data,unsigned int chan,const char * buf,size_t count)12849dfed80dSChunyan Zhang int notrace stm_source_write(struct stm_source_data *data,
12859dfed80dSChunyan Zhang 			     unsigned int chan,
12867bd1d409SAlexander Shishkin 			     const char *buf, size_t count)
12877bd1d409SAlexander Shishkin {
12887bd1d409SAlexander Shishkin 	struct stm_source_device *src = data->src;
12897bd1d409SAlexander Shishkin 	struct stm_device *stm;
12907bd1d409SAlexander Shishkin 	int idx;
12917bd1d409SAlexander Shishkin 
12927bd1d409SAlexander Shishkin 	if (!src->output.nr_chans)
12937bd1d409SAlexander Shishkin 		return -ENODEV;
12947bd1d409SAlexander Shishkin 
12957bd1d409SAlexander Shishkin 	if (chan >= src->output.nr_chans)
12967bd1d409SAlexander Shishkin 		return -EINVAL;
12977bd1d409SAlexander Shishkin 
12987bd1d409SAlexander Shishkin 	idx = srcu_read_lock(&stm_source_srcu);
12997bd1d409SAlexander Shishkin 
13007bd1d409SAlexander Shishkin 	stm = srcu_dereference(src->link, &stm_source_srcu);
13017bd1d409SAlexander Shishkin 	if (stm)
130224c7bcb6SAlexander Shishkin 		count = stm_write(stm, &src->output, chan, buf, count);
13037bd1d409SAlexander Shishkin 	else
13047bd1d409SAlexander Shishkin 		count = -ENODEV;
13057bd1d409SAlexander Shishkin 
13067bd1d409SAlexander Shishkin 	srcu_read_unlock(&stm_source_srcu, idx);
13077bd1d409SAlexander Shishkin 
13087bd1d409SAlexander Shishkin 	return count;
13097bd1d409SAlexander Shishkin }
13107bd1d409SAlexander Shishkin EXPORT_SYMBOL_GPL(stm_source_write);
13117bd1d409SAlexander Shishkin 
stm_core_init(void)13127bd1d409SAlexander Shishkin static int __init stm_core_init(void)
13137bd1d409SAlexander Shishkin {
13147bd1d409SAlexander Shishkin 	int err;
13157bd1d409SAlexander Shishkin 
13167bd1d409SAlexander Shishkin 	err = class_register(&stm_class);
13177bd1d409SAlexander Shishkin 	if (err)
13187bd1d409SAlexander Shishkin 		return err;
13197bd1d409SAlexander Shishkin 
13207bd1d409SAlexander Shishkin 	err = class_register(&stm_source_class);
13217bd1d409SAlexander Shishkin 	if (err)
13227bd1d409SAlexander Shishkin 		goto err_stm;
13237bd1d409SAlexander Shishkin 
13247bd1d409SAlexander Shishkin 	err = stp_configfs_init();
13257bd1d409SAlexander Shishkin 	if (err)
13267bd1d409SAlexander Shishkin 		goto err_src;
13277bd1d409SAlexander Shishkin 
13287bd1d409SAlexander Shishkin 	init_srcu_struct(&stm_source_srcu);
1329c7fd62bcSAlexander Shishkin 	INIT_LIST_HEAD(&stm_pdrv_head);
1330c7fd62bcSAlexander Shishkin 	mutex_init(&stm_pdrv_mutex);
13317bd1d409SAlexander Shishkin 
133224c7bcb6SAlexander Shishkin 	/*
133324c7bcb6SAlexander Shishkin 	 * So as to not confuse existing users with a requirement
133424c7bcb6SAlexander Shishkin 	 * to load yet another module, do it here.
133524c7bcb6SAlexander Shishkin 	 */
133624c7bcb6SAlexander Shishkin 	if (IS_ENABLED(CONFIG_STM_PROTO_BASIC))
133724c7bcb6SAlexander Shishkin 		(void)request_module_nowait("stm_p_basic");
13387bd1d409SAlexander Shishkin 	stm_core_up++;
13397bd1d409SAlexander Shishkin 
13407bd1d409SAlexander Shishkin 	return 0;
13417bd1d409SAlexander Shishkin 
13427bd1d409SAlexander Shishkin err_src:
13437bd1d409SAlexander Shishkin 	class_unregister(&stm_source_class);
13447bd1d409SAlexander Shishkin err_stm:
13457bd1d409SAlexander Shishkin 	class_unregister(&stm_class);
13467bd1d409SAlexander Shishkin 
13477bd1d409SAlexander Shishkin 	return err;
13487bd1d409SAlexander Shishkin }
13497bd1d409SAlexander Shishkin 
13507bd1d409SAlexander Shishkin module_init(stm_core_init);
13517bd1d409SAlexander Shishkin 
stm_core_exit(void)13527bd1d409SAlexander Shishkin static void __exit stm_core_exit(void)
13537bd1d409SAlexander Shishkin {
13547bd1d409SAlexander Shishkin 	cleanup_srcu_struct(&stm_source_srcu);
13557bd1d409SAlexander Shishkin 	class_unregister(&stm_source_class);
13567bd1d409SAlexander Shishkin 	class_unregister(&stm_class);
13577bd1d409SAlexander Shishkin 	stp_configfs_exit();
13587bd1d409SAlexander Shishkin }
13597bd1d409SAlexander Shishkin 
13607bd1d409SAlexander Shishkin module_exit(stm_core_exit);
13617bd1d409SAlexander Shishkin 
13627bd1d409SAlexander Shishkin MODULE_LICENSE("GPL v2");
13637bd1d409SAlexander Shishkin MODULE_DESCRIPTION("System Trace Module device class");
13647bd1d409SAlexander Shishkin MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");
1365