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; 247a1d75dadSZhi Jin 248a1d75dadSZhi Jin /* step over [pos..pos+i) to continue search */ 249a1d75dadSZhi Jin pos += i; 2507bd1d409SAlexander Shishkin } 2517bd1d409SAlexander Shishkin 2527bd1d409SAlexander Shishkin return -1; 2537bd1d409SAlexander Shishkin } 2547bd1d409SAlexander Shishkin 255f45f40adSLucas Tanure static int 2567bd1d409SAlexander Shishkin stm_find_master_chan(struct stm_device *stm, unsigned int width, 2577bd1d409SAlexander Shishkin unsigned int *mstart, unsigned int mend, 2587bd1d409SAlexander Shishkin unsigned int *cstart, unsigned int cend) 2597bd1d409SAlexander Shishkin { 2607bd1d409SAlexander Shishkin struct stp_master *master; 2617bd1d409SAlexander Shishkin unsigned int midx; 2627bd1d409SAlexander Shishkin int pos, err; 2637bd1d409SAlexander Shishkin 2647bd1d409SAlexander Shishkin for (midx = *mstart; midx <= mend; midx++) { 2657bd1d409SAlexander Shishkin if (!stm_master(stm, midx)) { 2667bd1d409SAlexander Shishkin err = stp_master_alloc(stm, midx); 2677bd1d409SAlexander Shishkin if (err) 2687bd1d409SAlexander Shishkin return err; 2697bd1d409SAlexander Shishkin } 2707bd1d409SAlexander Shishkin 2717bd1d409SAlexander Shishkin master = stm_master(stm, midx); 2727bd1d409SAlexander Shishkin 2737bd1d409SAlexander Shishkin if (!master->nr_free) 2747bd1d409SAlexander Shishkin continue; 2757bd1d409SAlexander Shishkin 2767bd1d409SAlexander Shishkin pos = find_free_channels(master->chan_map, *cstart, cend, 2777bd1d409SAlexander Shishkin width); 2787bd1d409SAlexander Shishkin if (pos < 0) 2797bd1d409SAlexander Shishkin continue; 2807bd1d409SAlexander Shishkin 2817bd1d409SAlexander Shishkin *mstart = midx; 2827bd1d409SAlexander Shishkin *cstart = pos; 2837bd1d409SAlexander Shishkin return 0; 2847bd1d409SAlexander Shishkin } 2857bd1d409SAlexander Shishkin 2867bd1d409SAlexander Shishkin return -ENOSPC; 2877bd1d409SAlexander Shishkin } 2887bd1d409SAlexander Shishkin 2897bd1d409SAlexander Shishkin static int stm_output_assign(struct stm_device *stm, unsigned int width, 2907bd1d409SAlexander Shishkin struct stp_policy_node *policy_node, 2917bd1d409SAlexander Shishkin struct stm_output *output) 2927bd1d409SAlexander Shishkin { 2937bd1d409SAlexander Shishkin unsigned int midx, cidx, mend, cend; 2947bd1d409SAlexander Shishkin int ret = -EINVAL; 2957bd1d409SAlexander Shishkin 2967bd1d409SAlexander Shishkin if (width > stm->data->sw_nchannels) 2977bd1d409SAlexander Shishkin return -EINVAL; 2987bd1d409SAlexander Shishkin 299cb6102bdSAlexander Shishkin /* We no longer accept policy_node==NULL here */ 300cb6102bdSAlexander Shishkin if (WARN_ON_ONCE(!policy_node)) 301cb6102bdSAlexander Shishkin return -EINVAL; 302cb6102bdSAlexander Shishkin 303cb6102bdSAlexander Shishkin /* 304cb6102bdSAlexander Shishkin * Also, the caller holds reference to policy_node, so it won't 305cb6102bdSAlexander Shishkin * disappear on us. 306cb6102bdSAlexander Shishkin */ 307cb6102bdSAlexander Shishkin stp_policy_node_get_ranges(policy_node, &midx, &mend, &cidx, &cend); 3087bd1d409SAlexander Shishkin 3097bd1d409SAlexander Shishkin spin_lock(&stm->mc_lock); 310cde4ad83SAlexander Shishkin spin_lock(&output->lock); 3117bd1d409SAlexander Shishkin /* output is already assigned -- shouldn't happen */ 3127bd1d409SAlexander Shishkin if (WARN_ON_ONCE(output->nr_chans)) 3137bd1d409SAlexander Shishkin goto unlock; 3147bd1d409SAlexander Shishkin 3157bd1d409SAlexander Shishkin ret = stm_find_master_chan(stm, width, &midx, mend, &cidx, cend); 316f45f40adSLucas Tanure if (ret < 0) 3177bd1d409SAlexander Shishkin goto unlock; 3187bd1d409SAlexander Shishkin 3197bd1d409SAlexander Shishkin output->master = midx; 3207bd1d409SAlexander Shishkin output->channel = cidx; 3217bd1d409SAlexander Shishkin output->nr_chans = width; 322c7fd62bcSAlexander Shishkin if (stm->pdrv->output_open) { 323c7fd62bcSAlexander Shishkin void *priv = stp_policy_node_priv(policy_node); 324c7fd62bcSAlexander Shishkin 325c7fd62bcSAlexander Shishkin if (WARN_ON_ONCE(!priv)) 326c7fd62bcSAlexander Shishkin goto unlock; 327c7fd62bcSAlexander Shishkin 328c7fd62bcSAlexander Shishkin /* configfs subsys mutex is held by the caller */ 329c7fd62bcSAlexander Shishkin ret = stm->pdrv->output_open(priv, output); 330c7fd62bcSAlexander Shishkin if (ret) 331c7fd62bcSAlexander Shishkin goto unlock; 332c7fd62bcSAlexander Shishkin } 333c7fd62bcSAlexander Shishkin 3347bd1d409SAlexander Shishkin stm_output_claim(stm, output); 3357bd1d409SAlexander Shishkin dev_dbg(&stm->dev, "assigned %u:%u (+%u)\n", midx, cidx, width); 3367bd1d409SAlexander Shishkin 3377bd1d409SAlexander Shishkin ret = 0; 3387bd1d409SAlexander Shishkin unlock: 339c7fd62bcSAlexander Shishkin if (ret) 340c7fd62bcSAlexander Shishkin output->nr_chans = 0; 341c7fd62bcSAlexander Shishkin 342cde4ad83SAlexander Shishkin spin_unlock(&output->lock); 3437bd1d409SAlexander Shishkin spin_unlock(&stm->mc_lock); 3447bd1d409SAlexander Shishkin 3457bd1d409SAlexander Shishkin return ret; 3467bd1d409SAlexander Shishkin } 3477bd1d409SAlexander Shishkin 3487bd1d409SAlexander Shishkin static void stm_output_free(struct stm_device *stm, struct stm_output *output) 3497bd1d409SAlexander Shishkin { 3507bd1d409SAlexander Shishkin spin_lock(&stm->mc_lock); 351cde4ad83SAlexander Shishkin spin_lock(&output->lock); 3527bd1d409SAlexander Shishkin if (output->nr_chans) 3537bd1d409SAlexander Shishkin stm_output_disclaim(stm, output); 354c7fd62bcSAlexander Shishkin if (stm->pdrv && stm->pdrv->output_close) 355c7fd62bcSAlexander Shishkin stm->pdrv->output_close(output); 356cde4ad83SAlexander Shishkin spin_unlock(&output->lock); 3577bd1d409SAlexander Shishkin spin_unlock(&stm->mc_lock); 3587bd1d409SAlexander Shishkin } 3597bd1d409SAlexander Shishkin 360cde4ad83SAlexander Shishkin static void stm_output_init(struct stm_output *output) 361cde4ad83SAlexander Shishkin { 362cde4ad83SAlexander Shishkin spin_lock_init(&output->lock); 363cde4ad83SAlexander Shishkin } 364cde4ad83SAlexander Shishkin 3657bd1d409SAlexander Shishkin static int major_match(struct device *dev, const void *data) 3667bd1d409SAlexander Shishkin { 3677bd1d409SAlexander Shishkin unsigned int major = *(unsigned int *)data; 3687bd1d409SAlexander Shishkin 3697bd1d409SAlexander Shishkin return MAJOR(dev->devt) == major; 3707bd1d409SAlexander Shishkin } 3717bd1d409SAlexander Shishkin 372c7fd62bcSAlexander Shishkin /* 373c7fd62bcSAlexander Shishkin * Framing protocol management 374c7fd62bcSAlexander Shishkin * Modules can implement STM protocol drivers and (un-)register them 375c7fd62bcSAlexander Shishkin * with the STM class framework. 376c7fd62bcSAlexander Shishkin */ 377c7fd62bcSAlexander Shishkin static struct list_head stm_pdrv_head; 378c7fd62bcSAlexander Shishkin static struct mutex stm_pdrv_mutex; 379c7fd62bcSAlexander Shishkin 380c7fd62bcSAlexander Shishkin struct stm_pdrv_entry { 381c7fd62bcSAlexander Shishkin struct list_head entry; 382c7fd62bcSAlexander Shishkin const struct stm_protocol_driver *pdrv; 383c7fd62bcSAlexander Shishkin const struct config_item_type *node_type; 384c7fd62bcSAlexander Shishkin }; 385c7fd62bcSAlexander Shishkin 386c7fd62bcSAlexander Shishkin static const struct stm_pdrv_entry * 387c7fd62bcSAlexander Shishkin __stm_lookup_protocol(const char *name) 388c7fd62bcSAlexander Shishkin { 389c7fd62bcSAlexander Shishkin struct stm_pdrv_entry *pe; 390c7fd62bcSAlexander Shishkin 391c7fd62bcSAlexander Shishkin /* 392c7fd62bcSAlexander Shishkin * If no name is given (NULL or ""), fall back to "p_basic". 393c7fd62bcSAlexander Shishkin */ 394c7fd62bcSAlexander Shishkin if (!name || !*name) 395c7fd62bcSAlexander Shishkin name = "p_basic"; 396c7fd62bcSAlexander Shishkin 397c7fd62bcSAlexander Shishkin list_for_each_entry(pe, &stm_pdrv_head, entry) { 398c7fd62bcSAlexander Shishkin if (!strcmp(name, pe->pdrv->name)) 399c7fd62bcSAlexander Shishkin return pe; 400c7fd62bcSAlexander Shishkin } 401c7fd62bcSAlexander Shishkin 402c7fd62bcSAlexander Shishkin return NULL; 403c7fd62bcSAlexander Shishkin } 404c7fd62bcSAlexander Shishkin 405c7fd62bcSAlexander Shishkin int stm_register_protocol(const struct stm_protocol_driver *pdrv) 406c7fd62bcSAlexander Shishkin { 407c7fd62bcSAlexander Shishkin struct stm_pdrv_entry *pe = NULL; 408c7fd62bcSAlexander Shishkin int ret = -ENOMEM; 409c7fd62bcSAlexander Shishkin 410c7fd62bcSAlexander Shishkin mutex_lock(&stm_pdrv_mutex); 411c7fd62bcSAlexander Shishkin 412c7fd62bcSAlexander Shishkin if (__stm_lookup_protocol(pdrv->name)) { 413c7fd62bcSAlexander Shishkin ret = -EEXIST; 414c7fd62bcSAlexander Shishkin goto unlock; 415c7fd62bcSAlexander Shishkin } 416c7fd62bcSAlexander Shishkin 417c7fd62bcSAlexander Shishkin pe = kzalloc(sizeof(*pe), GFP_KERNEL); 418c7fd62bcSAlexander Shishkin if (!pe) 419c7fd62bcSAlexander Shishkin goto unlock; 420c7fd62bcSAlexander Shishkin 421c7fd62bcSAlexander Shishkin if (pdrv->policy_attr) { 422c7fd62bcSAlexander Shishkin pe->node_type = get_policy_node_type(pdrv->policy_attr); 423c7fd62bcSAlexander Shishkin if (!pe->node_type) 424c7fd62bcSAlexander Shishkin goto unlock; 425c7fd62bcSAlexander Shishkin } 426c7fd62bcSAlexander Shishkin 427c7fd62bcSAlexander Shishkin list_add_tail(&pe->entry, &stm_pdrv_head); 428c7fd62bcSAlexander Shishkin pe->pdrv = pdrv; 429c7fd62bcSAlexander Shishkin 430c7fd62bcSAlexander Shishkin ret = 0; 431c7fd62bcSAlexander Shishkin unlock: 432c7fd62bcSAlexander Shishkin mutex_unlock(&stm_pdrv_mutex); 433c7fd62bcSAlexander Shishkin 434c7fd62bcSAlexander Shishkin if (ret) 435c7fd62bcSAlexander Shishkin kfree(pe); 436c7fd62bcSAlexander Shishkin 437c7fd62bcSAlexander Shishkin return ret; 438c7fd62bcSAlexander Shishkin } 439c7fd62bcSAlexander Shishkin EXPORT_SYMBOL_GPL(stm_register_protocol); 440c7fd62bcSAlexander Shishkin 441c7fd62bcSAlexander Shishkin void stm_unregister_protocol(const struct stm_protocol_driver *pdrv) 442c7fd62bcSAlexander Shishkin { 443c7fd62bcSAlexander Shishkin struct stm_pdrv_entry *pe, *iter; 444c7fd62bcSAlexander Shishkin 445c7fd62bcSAlexander Shishkin mutex_lock(&stm_pdrv_mutex); 446c7fd62bcSAlexander Shishkin 447c7fd62bcSAlexander Shishkin list_for_each_entry_safe(pe, iter, &stm_pdrv_head, entry) { 448c7fd62bcSAlexander Shishkin if (pe->pdrv == pdrv) { 449c7fd62bcSAlexander Shishkin list_del(&pe->entry); 450c7fd62bcSAlexander Shishkin 451c7fd62bcSAlexander Shishkin if (pe->node_type) { 452c7fd62bcSAlexander Shishkin kfree(pe->node_type->ct_attrs); 453c7fd62bcSAlexander Shishkin kfree(pe->node_type); 454c7fd62bcSAlexander Shishkin } 455c7fd62bcSAlexander Shishkin kfree(pe); 456c7fd62bcSAlexander Shishkin break; 457c7fd62bcSAlexander Shishkin } 458c7fd62bcSAlexander Shishkin } 459c7fd62bcSAlexander Shishkin 460c7fd62bcSAlexander Shishkin mutex_unlock(&stm_pdrv_mutex); 461c7fd62bcSAlexander Shishkin } 462c7fd62bcSAlexander Shishkin EXPORT_SYMBOL_GPL(stm_unregister_protocol); 463c7fd62bcSAlexander Shishkin 464c7fd62bcSAlexander Shishkin static bool stm_get_protocol(const struct stm_protocol_driver *pdrv) 465c7fd62bcSAlexander Shishkin { 466c7fd62bcSAlexander Shishkin return try_module_get(pdrv->owner); 467c7fd62bcSAlexander Shishkin } 468c7fd62bcSAlexander Shishkin 469c7fd62bcSAlexander Shishkin void stm_put_protocol(const struct stm_protocol_driver *pdrv) 470c7fd62bcSAlexander Shishkin { 471c7fd62bcSAlexander Shishkin module_put(pdrv->owner); 472c7fd62bcSAlexander Shishkin } 473c7fd62bcSAlexander Shishkin 474c7fd62bcSAlexander Shishkin int stm_lookup_protocol(const char *name, 475c7fd62bcSAlexander Shishkin const struct stm_protocol_driver **pdrv, 476c7fd62bcSAlexander Shishkin const struct config_item_type **node_type) 477c7fd62bcSAlexander Shishkin { 478c7fd62bcSAlexander Shishkin const struct stm_pdrv_entry *pe; 479c7fd62bcSAlexander Shishkin 480c7fd62bcSAlexander Shishkin mutex_lock(&stm_pdrv_mutex); 481c7fd62bcSAlexander Shishkin 482c7fd62bcSAlexander Shishkin pe = __stm_lookup_protocol(name); 483c7fd62bcSAlexander Shishkin if (pe && pe->pdrv && stm_get_protocol(pe->pdrv)) { 484c7fd62bcSAlexander Shishkin *pdrv = pe->pdrv; 485c7fd62bcSAlexander Shishkin *node_type = pe->node_type; 486c7fd62bcSAlexander Shishkin } 487c7fd62bcSAlexander Shishkin 488c7fd62bcSAlexander Shishkin mutex_unlock(&stm_pdrv_mutex); 489c7fd62bcSAlexander Shishkin 490c7fd62bcSAlexander Shishkin return pe ? 0 : -ENOENT; 491c7fd62bcSAlexander Shishkin } 492c7fd62bcSAlexander Shishkin 4937bd1d409SAlexander Shishkin static int stm_char_open(struct inode *inode, struct file *file) 4947bd1d409SAlexander Shishkin { 4957bd1d409SAlexander Shishkin struct stm_file *stmf; 4967bd1d409SAlexander Shishkin struct device *dev; 4977bd1d409SAlexander Shishkin unsigned int major = imajor(inode); 498a0ebf519SJohan Hovold int err = -ENOMEM; 4997bd1d409SAlexander Shishkin 5007bd1d409SAlexander Shishkin dev = class_find_device(&stm_class, NULL, &major, major_match); 5017bd1d409SAlexander Shishkin if (!dev) 5027bd1d409SAlexander Shishkin return -ENODEV; 5037bd1d409SAlexander Shishkin 5047bd1d409SAlexander Shishkin stmf = kzalloc(sizeof(*stmf), GFP_KERNEL); 5057bd1d409SAlexander Shishkin if (!stmf) 506a0ebf519SJohan Hovold goto err_put_device; 5077bd1d409SAlexander Shishkin 508a0ebf519SJohan Hovold err = -ENODEV; 509cde4ad83SAlexander Shishkin stm_output_init(&stmf->output); 5107bd1d409SAlexander Shishkin stmf->stm = to_stm_device(dev); 5117bd1d409SAlexander Shishkin 5127bd1d409SAlexander Shishkin if (!try_module_get(stmf->stm->owner)) 5137bd1d409SAlexander Shishkin goto err_free; 5147bd1d409SAlexander Shishkin 5157bd1d409SAlexander Shishkin file->private_data = stmf; 5167bd1d409SAlexander Shishkin 5177bd1d409SAlexander Shishkin return nonseekable_open(inode, file); 5187bd1d409SAlexander Shishkin 5197bd1d409SAlexander Shishkin err_free: 520a0ebf519SJohan Hovold kfree(stmf); 521a0ebf519SJohan Hovold err_put_device: 522f7c81c71SAlexander Shishkin /* matches class_find_device() above */ 523f7c81c71SAlexander Shishkin put_device(dev); 5247bd1d409SAlexander Shishkin 5257bd1d409SAlexander Shishkin return err; 5267bd1d409SAlexander Shishkin } 5277bd1d409SAlexander Shishkin 5287bd1d409SAlexander Shishkin static int stm_char_release(struct inode *inode, struct file *file) 5297bd1d409SAlexander Shishkin { 5307bd1d409SAlexander Shishkin struct stm_file *stmf = file->private_data; 531cc842407SAlexander Shishkin struct stm_device *stm = stmf->stm; 5327bd1d409SAlexander Shishkin 533cc842407SAlexander Shishkin if (stm->data->unlink) 534cc842407SAlexander Shishkin stm->data->unlink(stm->data, stmf->output.master, 535cc842407SAlexander Shishkin stmf->output.channel); 536cc842407SAlexander Shishkin 537cc842407SAlexander Shishkin stm_output_free(stm, &stmf->output); 538f7c81c71SAlexander Shishkin 539f7c81c71SAlexander Shishkin /* 540f7c81c71SAlexander Shishkin * matches the stm_char_open()'s 541f7c81c71SAlexander Shishkin * class_find_device() + try_module_get() 542f7c81c71SAlexander Shishkin */ 543cc842407SAlexander Shishkin stm_put_device(stm); 5447bd1d409SAlexander Shishkin kfree(stmf); 5457bd1d409SAlexander Shishkin 5467bd1d409SAlexander Shishkin return 0; 5477bd1d409SAlexander Shishkin } 5487bd1d409SAlexander Shishkin 549cb6102bdSAlexander Shishkin static int 550cb6102bdSAlexander Shishkin stm_assign_first_policy(struct stm_device *stm, struct stm_output *output, 551cb6102bdSAlexander Shishkin char **ids, unsigned int width) 5527bd1d409SAlexander Shishkin { 553cb6102bdSAlexander Shishkin struct stp_policy_node *pn; 554cb6102bdSAlexander Shishkin int err, n; 5557bd1d409SAlexander Shishkin 556cb6102bdSAlexander Shishkin /* 557cb6102bdSAlexander Shishkin * On success, stp_policy_node_lookup() will return holding the 558cb6102bdSAlexander Shishkin * configfs subsystem mutex, which is then released in 559cb6102bdSAlexander Shishkin * stp_policy_node_put(). This allows the pdrv->output_open() in 560cb6102bdSAlexander Shishkin * stm_output_assign() to serialize against the attribute accessors. 561cb6102bdSAlexander Shishkin */ 562cb6102bdSAlexander Shishkin for (n = 0, pn = NULL; ids[n] && !pn; n++) 563cb6102bdSAlexander Shishkin pn = stp_policy_node_lookup(stm, ids[n]); 5647bd1d409SAlexander Shishkin 565cb6102bdSAlexander Shishkin if (!pn) 566cb6102bdSAlexander Shishkin return -EINVAL; 5677bd1d409SAlexander Shishkin 568cb6102bdSAlexander Shishkin err = stm_output_assign(stm, width, pn, output); 5697bd1d409SAlexander Shishkin 570cb6102bdSAlexander Shishkin stp_policy_node_put(pn); 571cb6102bdSAlexander Shishkin 572cb6102bdSAlexander Shishkin return err; 5737bd1d409SAlexander Shishkin } 5747bd1d409SAlexander Shishkin 575d279a380SAlexander Shishkin /** 576d279a380SAlexander Shishkin * stm_data_write() - send the given payload as data packets 577d279a380SAlexander Shishkin * @data: stm driver's data 578d279a380SAlexander Shishkin * @m: STP master 579d279a380SAlexander Shishkin * @c: STP channel 580d279a380SAlexander Shishkin * @ts_first: timestamp the first packet 581d279a380SAlexander Shishkin * @buf: data payload buffer 582d279a380SAlexander Shishkin * @count: data payload size 583d279a380SAlexander Shishkin */ 584d279a380SAlexander Shishkin ssize_t notrace stm_data_write(struct stm_data *data, unsigned int m, 585d279a380SAlexander Shishkin unsigned int c, bool ts_first, const void *buf, 586d279a380SAlexander Shishkin size_t count) 587d279a380SAlexander Shishkin { 588d279a380SAlexander Shishkin unsigned int flags = ts_first ? STP_PACKET_TIMESTAMPED : 0; 589d279a380SAlexander Shishkin ssize_t sz; 590d279a380SAlexander Shishkin size_t pos; 591d279a380SAlexander Shishkin 592d279a380SAlexander Shishkin for (pos = 0, sz = 0; pos < count; pos += sz) { 593d279a380SAlexander Shishkin sz = min_t(unsigned int, count - pos, 8); 594d279a380SAlexander Shishkin sz = data->packet(data, m, c, STP_PACKET_DATA, flags, sz, 595d279a380SAlexander Shishkin &((u8 *)buf)[pos]); 596d279a380SAlexander Shishkin if (sz <= 0) 597d279a380SAlexander Shishkin break; 598d279a380SAlexander Shishkin 599d279a380SAlexander Shishkin if (ts_first) { 600d279a380SAlexander Shishkin flags = 0; 601d279a380SAlexander Shishkin ts_first = false; 602d279a380SAlexander Shishkin } 603d279a380SAlexander Shishkin } 604d279a380SAlexander Shishkin 605d279a380SAlexander Shishkin return sz < 0 ? sz : pos; 606d279a380SAlexander Shishkin } 607d279a380SAlexander Shishkin EXPORT_SYMBOL_GPL(stm_data_write); 608d279a380SAlexander Shishkin 60924c7bcb6SAlexander Shishkin static ssize_t notrace 61024c7bcb6SAlexander Shishkin stm_write(struct stm_device *stm, struct stm_output *output, 61124c7bcb6SAlexander Shishkin unsigned int chan, const char *buf, size_t count) 6127bd1d409SAlexander Shishkin { 61324c7bcb6SAlexander Shishkin int err; 6147bd1d409SAlexander Shishkin 61524c7bcb6SAlexander Shishkin /* stm->pdrv is serialized against policy_mutex */ 61624c7bcb6SAlexander Shishkin if (!stm->pdrv) 61724c7bcb6SAlexander Shishkin return -ENODEV; 618f8560a9bSAlexander Shishkin 61924c7bcb6SAlexander Shishkin err = stm->pdrv->write(stm->data, output, chan, buf, count); 62024c7bcb6SAlexander Shishkin if (err < 0) 62124c7bcb6SAlexander Shishkin return err; 62224c7bcb6SAlexander Shishkin 62324c7bcb6SAlexander Shishkin return err; 6247bd1d409SAlexander Shishkin } 6257bd1d409SAlexander Shishkin 6267bd1d409SAlexander Shishkin static ssize_t stm_char_write(struct file *file, const char __user *buf, 6277bd1d409SAlexander Shishkin size_t count, loff_t *ppos) 6287bd1d409SAlexander Shishkin { 6297bd1d409SAlexander Shishkin struct stm_file *stmf = file->private_data; 6307bd1d409SAlexander Shishkin struct stm_device *stm = stmf->stm; 6317bd1d409SAlexander Shishkin char *kbuf; 6327bd1d409SAlexander Shishkin int err; 6337bd1d409SAlexander Shishkin 634f08b1826SAlexander Shishkin if (count + 1 > PAGE_SIZE) 635f08b1826SAlexander Shishkin count = PAGE_SIZE - 1; 636f08b1826SAlexander Shishkin 6377bd1d409SAlexander Shishkin /* 638cb6102bdSAlexander Shishkin * If no m/c have been assigned to this writer up to this 639cb6102bdSAlexander Shishkin * point, try to use the task name and "default" policy entries. 6407bd1d409SAlexander Shishkin */ 6417bd1d409SAlexander Shishkin if (!stmf->output.nr_chans) { 642cb6102bdSAlexander Shishkin char comm[sizeof(current->comm)]; 643cb6102bdSAlexander Shishkin char *ids[] = { comm, "default", NULL }; 644cb6102bdSAlexander Shishkin 645cb6102bdSAlexander Shishkin get_task_comm(comm, current); 646cb6102bdSAlexander Shishkin 647cb6102bdSAlexander Shishkin err = stm_assign_first_policy(stmf->stm, &stmf->output, ids, 1); 6487bd1d409SAlexander Shishkin /* 6497bd1d409SAlexander Shishkin * EBUSY means that somebody else just assigned this 6507bd1d409SAlexander Shishkin * output, which is just fine for write() 6517bd1d409SAlexander Shishkin */ 652cb6102bdSAlexander Shishkin if (err) 6537bd1d409SAlexander Shishkin return err; 6547bd1d409SAlexander Shishkin } 6557bd1d409SAlexander Shishkin 6567bd1d409SAlexander Shishkin kbuf = kmalloc(count + 1, GFP_KERNEL); 6577bd1d409SAlexander Shishkin if (!kbuf) 6587bd1d409SAlexander Shishkin return -ENOMEM; 6597bd1d409SAlexander Shishkin 6607bd1d409SAlexander Shishkin err = copy_from_user(kbuf, buf, count); 6617bd1d409SAlexander Shishkin if (err) { 6627bd1d409SAlexander Shishkin kfree(kbuf); 6637bd1d409SAlexander Shishkin return -EFAULT; 6647bd1d409SAlexander Shishkin } 6657bd1d409SAlexander Shishkin 6668e0469a4SAlexander Shishkin pm_runtime_get_sync(&stm->dev); 6678e0469a4SAlexander Shishkin 66824c7bcb6SAlexander Shishkin count = stm_write(stm, &stmf->output, 0, kbuf, count); 6697bd1d409SAlexander Shishkin 6708e0469a4SAlexander Shishkin pm_runtime_mark_last_busy(&stm->dev); 6718e0469a4SAlexander Shishkin pm_runtime_put_autosuspend(&stm->dev); 6727bd1d409SAlexander Shishkin kfree(kbuf); 6737bd1d409SAlexander Shishkin 6747bd1d409SAlexander Shishkin return count; 6757bd1d409SAlexander Shishkin } 6767bd1d409SAlexander Shishkin 6778e0469a4SAlexander Shishkin static void stm_mmap_open(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_get(&stm->dev); 6838e0469a4SAlexander Shishkin } 6848e0469a4SAlexander Shishkin 6858e0469a4SAlexander Shishkin static void stm_mmap_close(struct vm_area_struct *vma) 6868e0469a4SAlexander Shishkin { 6878e0469a4SAlexander Shishkin struct stm_file *stmf = vma->vm_file->private_data; 6888e0469a4SAlexander Shishkin struct stm_device *stm = stmf->stm; 6898e0469a4SAlexander Shishkin 6908e0469a4SAlexander Shishkin pm_runtime_mark_last_busy(&stm->dev); 6918e0469a4SAlexander Shishkin pm_runtime_put_autosuspend(&stm->dev); 6928e0469a4SAlexander Shishkin } 6938e0469a4SAlexander Shishkin 6948e0469a4SAlexander Shishkin static const struct vm_operations_struct stm_mmap_vmops = { 6958e0469a4SAlexander Shishkin .open = stm_mmap_open, 6968e0469a4SAlexander Shishkin .close = stm_mmap_close, 6978e0469a4SAlexander Shishkin }; 6988e0469a4SAlexander Shishkin 6997bd1d409SAlexander Shishkin static int stm_char_mmap(struct file *file, struct vm_area_struct *vma) 7007bd1d409SAlexander Shishkin { 7017bd1d409SAlexander Shishkin struct stm_file *stmf = file->private_data; 7027bd1d409SAlexander Shishkin struct stm_device *stm = stmf->stm; 7037bd1d409SAlexander Shishkin unsigned long size, phys; 7047bd1d409SAlexander Shishkin 7057bd1d409SAlexander Shishkin if (!stm->data->mmio_addr) 7067bd1d409SAlexander Shishkin return -EOPNOTSUPP; 7077bd1d409SAlexander Shishkin 7087bd1d409SAlexander Shishkin if (vma->vm_pgoff) 7097bd1d409SAlexander Shishkin return -EINVAL; 7107bd1d409SAlexander Shishkin 7117bd1d409SAlexander Shishkin size = vma->vm_end - vma->vm_start; 7127bd1d409SAlexander Shishkin 7137bd1d409SAlexander Shishkin if (stmf->output.nr_chans * stm->data->sw_mmiosz != size) 7147bd1d409SAlexander Shishkin return -EINVAL; 7157bd1d409SAlexander Shishkin 7167bd1d409SAlexander Shishkin phys = stm->data->mmio_addr(stm->data, stmf->output.master, 7177bd1d409SAlexander Shishkin stmf->output.channel, 7187bd1d409SAlexander Shishkin stmf->output.nr_chans); 7197bd1d409SAlexander Shishkin 7207bd1d409SAlexander Shishkin if (!phys) 7217bd1d409SAlexander Shishkin return -EINVAL; 7227bd1d409SAlexander Shishkin 7238e0469a4SAlexander Shishkin pm_runtime_get_sync(&stm->dev); 7248e0469a4SAlexander Shishkin 7257bd1d409SAlexander Shishkin vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 7267bd1d409SAlexander Shishkin vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP; 7278e0469a4SAlexander Shishkin vma->vm_ops = &stm_mmap_vmops; 7287bd1d409SAlexander Shishkin vm_iomap_memory(vma, phys, size); 7297bd1d409SAlexander Shishkin 7307bd1d409SAlexander Shishkin return 0; 7317bd1d409SAlexander Shishkin } 7327bd1d409SAlexander Shishkin 7337bd1d409SAlexander Shishkin static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg) 7347bd1d409SAlexander Shishkin { 7357bd1d409SAlexander Shishkin struct stm_device *stm = stmf->stm; 7367bd1d409SAlexander Shishkin struct stp_policy_id *id; 737cb6102bdSAlexander Shishkin char *ids[] = { NULL, NULL }; 7387bd1d409SAlexander Shishkin int ret = -EINVAL; 7397bd1d409SAlexander Shishkin u32 size; 7407bd1d409SAlexander Shishkin 7417bd1d409SAlexander Shishkin if (stmf->output.nr_chans) 7427bd1d409SAlexander Shishkin return -EBUSY; 7437bd1d409SAlexander Shishkin 7447bd1d409SAlexander Shishkin if (copy_from_user(&size, arg, sizeof(size))) 7457bd1d409SAlexander Shishkin return -EFAULT; 7467bd1d409SAlexander Shishkin 74771c488f3SDan Carpenter if (size < sizeof(*id) || size >= PATH_MAX + sizeof(*id)) 7487bd1d409SAlexander Shishkin return -EINVAL; 7497bd1d409SAlexander Shishkin 7507bd1d409SAlexander Shishkin /* 7517bd1d409SAlexander Shishkin * size + 1 to make sure the .id string at the bottom is terminated, 7527bd1d409SAlexander Shishkin * which is also why memdup_user() is not useful here 7537bd1d409SAlexander Shishkin */ 7547bd1d409SAlexander Shishkin id = kzalloc(size + 1, GFP_KERNEL); 7557bd1d409SAlexander Shishkin if (!id) 7567bd1d409SAlexander Shishkin return -ENOMEM; 7577bd1d409SAlexander Shishkin 7587bd1d409SAlexander Shishkin if (copy_from_user(id, arg, size)) { 7597bd1d409SAlexander Shishkin ret = -EFAULT; 7607bd1d409SAlexander Shishkin goto err_free; 7617bd1d409SAlexander Shishkin } 7627bd1d409SAlexander Shishkin 7637bd1d409SAlexander Shishkin if (id->__reserved_0 || id->__reserved_1) 7647bd1d409SAlexander Shishkin goto err_free; 7657bd1d409SAlexander Shishkin 7667bd1d409SAlexander Shishkin if (id->width < 1 || 7677bd1d409SAlexander Shishkin id->width > PAGE_SIZE / stm->data->sw_mmiosz) 7687bd1d409SAlexander Shishkin goto err_free; 7697bd1d409SAlexander Shishkin 770cb6102bdSAlexander Shishkin ids[0] = id->id; 771cb6102bdSAlexander Shishkin ret = stm_assign_first_policy(stmf->stm, &stmf->output, ids, 772cb6102bdSAlexander Shishkin id->width); 7737bd1d409SAlexander Shishkin if (ret) 7747bd1d409SAlexander Shishkin goto err_free; 7757bd1d409SAlexander Shishkin 7767bd1d409SAlexander Shishkin if (stm->data->link) 7777bd1d409SAlexander Shishkin ret = stm->data->link(stm->data, stmf->output.master, 7787bd1d409SAlexander Shishkin stmf->output.channel); 7797bd1d409SAlexander Shishkin 780f7c81c71SAlexander Shishkin if (ret) 7817bd1d409SAlexander Shishkin stm_output_free(stmf->stm, &stmf->output); 7827bd1d409SAlexander Shishkin 7837bd1d409SAlexander Shishkin err_free: 7847bd1d409SAlexander Shishkin kfree(id); 7857bd1d409SAlexander Shishkin 7867bd1d409SAlexander Shishkin return ret; 7877bd1d409SAlexander Shishkin } 7887bd1d409SAlexander Shishkin 7897bd1d409SAlexander Shishkin static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg) 7907bd1d409SAlexander Shishkin { 7917bd1d409SAlexander Shishkin struct stp_policy_id id = { 7927bd1d409SAlexander Shishkin .size = sizeof(id), 7937bd1d409SAlexander Shishkin .master = stmf->output.master, 7947bd1d409SAlexander Shishkin .channel = stmf->output.channel, 7957bd1d409SAlexander Shishkin .width = stmf->output.nr_chans, 7967bd1d409SAlexander Shishkin .__reserved_0 = 0, 7977bd1d409SAlexander Shishkin .__reserved_1 = 0, 7987bd1d409SAlexander Shishkin }; 7997bd1d409SAlexander Shishkin 8007bd1d409SAlexander Shishkin return copy_to_user(arg, &id, id.size) ? -EFAULT : 0; 8017bd1d409SAlexander Shishkin } 8027bd1d409SAlexander Shishkin 8037bd1d409SAlexander Shishkin static long 8047bd1d409SAlexander Shishkin stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 8057bd1d409SAlexander Shishkin { 8067bd1d409SAlexander Shishkin struct stm_file *stmf = file->private_data; 8077bd1d409SAlexander Shishkin struct stm_data *stm_data = stmf->stm->data; 8087bd1d409SAlexander Shishkin int err = -ENOTTY; 8097bd1d409SAlexander Shishkin u64 options; 8107bd1d409SAlexander Shishkin 8117bd1d409SAlexander Shishkin switch (cmd) { 8127bd1d409SAlexander Shishkin case STP_POLICY_ID_SET: 8137bd1d409SAlexander Shishkin err = stm_char_policy_set_ioctl(stmf, (void __user *)arg); 8147bd1d409SAlexander Shishkin if (err) 8157bd1d409SAlexander Shishkin return err; 8167bd1d409SAlexander Shishkin 8177bd1d409SAlexander Shishkin return stm_char_policy_get_ioctl(stmf, (void __user *)arg); 8187bd1d409SAlexander Shishkin 8197bd1d409SAlexander Shishkin case STP_POLICY_ID_GET: 8207bd1d409SAlexander Shishkin return stm_char_policy_get_ioctl(stmf, (void __user *)arg); 8217bd1d409SAlexander Shishkin 8227bd1d409SAlexander Shishkin case STP_SET_OPTIONS: 8237bd1d409SAlexander Shishkin if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64))) 8247bd1d409SAlexander Shishkin return -EFAULT; 8257bd1d409SAlexander Shishkin 8267bd1d409SAlexander Shishkin if (stm_data->set_options) 8277bd1d409SAlexander Shishkin err = stm_data->set_options(stm_data, 8287bd1d409SAlexander Shishkin stmf->output.master, 8297bd1d409SAlexander Shishkin stmf->output.channel, 8307bd1d409SAlexander Shishkin stmf->output.nr_chans, 8317bd1d409SAlexander Shishkin options); 8327bd1d409SAlexander Shishkin 8337bd1d409SAlexander Shishkin break; 8347bd1d409SAlexander Shishkin default: 8357bd1d409SAlexander Shishkin break; 8367bd1d409SAlexander Shishkin } 8377bd1d409SAlexander Shishkin 8387bd1d409SAlexander Shishkin return err; 8397bd1d409SAlexander Shishkin } 8407bd1d409SAlexander Shishkin 8417bd1d409SAlexander Shishkin #ifdef CONFIG_COMPAT 8427bd1d409SAlexander Shishkin static long 8437bd1d409SAlexander Shishkin stm_char_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 8447bd1d409SAlexander Shishkin { 8457bd1d409SAlexander Shishkin return stm_char_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 8467bd1d409SAlexander Shishkin } 8477bd1d409SAlexander Shishkin #else 8487bd1d409SAlexander Shishkin #define stm_char_compat_ioctl NULL 8497bd1d409SAlexander Shishkin #endif 8507bd1d409SAlexander Shishkin 8517bd1d409SAlexander Shishkin static const struct file_operations stm_fops = { 8527bd1d409SAlexander Shishkin .open = stm_char_open, 8537bd1d409SAlexander Shishkin .release = stm_char_release, 8547bd1d409SAlexander Shishkin .write = stm_char_write, 8557bd1d409SAlexander Shishkin .mmap = stm_char_mmap, 8567bd1d409SAlexander Shishkin .unlocked_ioctl = stm_char_ioctl, 8577bd1d409SAlexander Shishkin .compat_ioctl = stm_char_compat_ioctl, 8587bd1d409SAlexander Shishkin .llseek = no_llseek, 8597bd1d409SAlexander Shishkin }; 8607bd1d409SAlexander Shishkin 8617bd1d409SAlexander Shishkin static void stm_device_release(struct device *dev) 8627bd1d409SAlexander Shishkin { 8637bd1d409SAlexander Shishkin struct stm_device *stm = to_stm_device(dev); 8647bd1d409SAlexander Shishkin 865b5e2ced9SAlexander Shishkin vfree(stm); 8667bd1d409SAlexander Shishkin } 8677bd1d409SAlexander Shishkin 8687bd1d409SAlexander Shishkin int stm_register_device(struct device *parent, struct stm_data *stm_data, 8697bd1d409SAlexander Shishkin struct module *owner) 8707bd1d409SAlexander Shishkin { 8717bd1d409SAlexander Shishkin struct stm_device *stm; 8727bd1d409SAlexander Shishkin unsigned int nmasters; 8737bd1d409SAlexander Shishkin int err = -ENOMEM; 8747bd1d409SAlexander Shishkin 8757bd1d409SAlexander Shishkin if (!stm_core_up) 8767bd1d409SAlexander Shishkin return -EPROBE_DEFER; 8777bd1d409SAlexander Shishkin 8787bd1d409SAlexander Shishkin if (!stm_data->packet || !stm_data->sw_nchannels) 8797bd1d409SAlexander Shishkin return -EINVAL; 8807bd1d409SAlexander Shishkin 8817b3bb0e7SChunyan Zhang nmasters = stm_data->sw_end - stm_data->sw_start + 1; 882b5e2ced9SAlexander Shishkin stm = vzalloc(sizeof(*stm) + nmasters * sizeof(void *)); 8837bd1d409SAlexander Shishkin if (!stm) 8847bd1d409SAlexander Shishkin return -ENOMEM; 8857bd1d409SAlexander Shishkin 8867bd1d409SAlexander Shishkin stm->major = register_chrdev(0, stm_data->name, &stm_fops); 8877bd1d409SAlexander Shishkin if (stm->major < 0) 8887bd1d409SAlexander Shishkin goto err_free; 8897bd1d409SAlexander Shishkin 8907bd1d409SAlexander Shishkin device_initialize(&stm->dev); 8917bd1d409SAlexander Shishkin stm->dev.devt = MKDEV(stm->major, 0); 8927bd1d409SAlexander Shishkin stm->dev.class = &stm_class; 8937bd1d409SAlexander Shishkin stm->dev.parent = parent; 8947bd1d409SAlexander Shishkin stm->dev.release = stm_device_release; 8957bd1d409SAlexander Shishkin 896389b6699SAlexander Shishkin mutex_init(&stm->link_mutex); 897389b6699SAlexander Shishkin spin_lock_init(&stm->link_lock); 898389b6699SAlexander Shishkin INIT_LIST_HEAD(&stm->link_list); 899389b6699SAlexander Shishkin 900389b6699SAlexander Shishkin /* initialize the object before it is accessible via sysfs */ 901389b6699SAlexander Shishkin spin_lock_init(&stm->mc_lock); 902389b6699SAlexander Shishkin mutex_init(&stm->policy_mutex); 903389b6699SAlexander Shishkin stm->sw_nmasters = nmasters; 904389b6699SAlexander Shishkin stm->owner = owner; 905389b6699SAlexander Shishkin stm->data = stm_data; 906389b6699SAlexander Shishkin stm_data->stm = stm; 907389b6699SAlexander Shishkin 9087bd1d409SAlexander Shishkin err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name); 9097bd1d409SAlexander Shishkin if (err) 9107bd1d409SAlexander Shishkin goto err_device; 9117bd1d409SAlexander Shishkin 9127bd1d409SAlexander Shishkin err = device_add(&stm->dev); 9137bd1d409SAlexander Shishkin if (err) 9147bd1d409SAlexander Shishkin goto err_device; 9157bd1d409SAlexander Shishkin 9168e0469a4SAlexander Shishkin /* 9178e0469a4SAlexander Shishkin * Use delayed autosuspend to avoid bouncing back and forth 9188e0469a4SAlexander Shishkin * on recurring character device writes, with the initial 9198e0469a4SAlexander Shishkin * delay time of 2 seconds. 9208e0469a4SAlexander Shishkin */ 9218e0469a4SAlexander Shishkin pm_runtime_no_callbacks(&stm->dev); 9228e0469a4SAlexander Shishkin pm_runtime_use_autosuspend(&stm->dev); 9238e0469a4SAlexander Shishkin pm_runtime_set_autosuspend_delay(&stm->dev, 2000); 9248e0469a4SAlexander Shishkin pm_runtime_set_suspended(&stm->dev); 9258e0469a4SAlexander Shishkin pm_runtime_enable(&stm->dev); 9268e0469a4SAlexander Shishkin 9277bd1d409SAlexander Shishkin return 0; 9287bd1d409SAlexander Shishkin 9297bd1d409SAlexander Shishkin err_device: 930cbe4a61dSAlexander Shishkin unregister_chrdev(stm->major, stm_data->name); 931cbe4a61dSAlexander Shishkin 932f7c81c71SAlexander Shishkin /* matches device_initialize() above */ 9337bd1d409SAlexander Shishkin put_device(&stm->dev); 9347bd1d409SAlexander Shishkin err_free: 935b5e2ced9SAlexander Shishkin vfree(stm); 9367bd1d409SAlexander Shishkin 9377bd1d409SAlexander Shishkin return err; 9387bd1d409SAlexander Shishkin } 9397bd1d409SAlexander Shishkin EXPORT_SYMBOL_GPL(stm_register_device); 9407bd1d409SAlexander Shishkin 941b4ca34aaSAlexander Shishkin static int __stm_source_link_drop(struct stm_source_device *src, 9427bd1d409SAlexander Shishkin struct stm_device *stm); 9437bd1d409SAlexander Shishkin 9447bd1d409SAlexander Shishkin void stm_unregister_device(struct stm_data *stm_data) 9457bd1d409SAlexander Shishkin { 9467bd1d409SAlexander Shishkin struct stm_device *stm = stm_data->stm; 9477bd1d409SAlexander Shishkin struct stm_source_device *src, *iter; 948b4ca34aaSAlexander Shishkin int i, ret; 9497bd1d409SAlexander Shishkin 9508e0469a4SAlexander Shishkin pm_runtime_dont_use_autosuspend(&stm->dev); 9518e0469a4SAlexander Shishkin pm_runtime_disable(&stm->dev); 9528e0469a4SAlexander Shishkin 953c74f7e82SAlexander Shishkin mutex_lock(&stm->link_mutex); 9547bd1d409SAlexander Shishkin list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) { 955b4ca34aaSAlexander Shishkin ret = __stm_source_link_drop(src, stm); 956b4ca34aaSAlexander Shishkin /* 957b4ca34aaSAlexander Shishkin * src <-> stm link must not change under the same 958b4ca34aaSAlexander Shishkin * stm::link_mutex, so complain loudly if it has; 959b4ca34aaSAlexander Shishkin * also in this situation ret!=0 means this src is 960b4ca34aaSAlexander Shishkin * not connected to this stm and it should be otherwise 961b4ca34aaSAlexander Shishkin * safe to proceed with the tear-down of stm. 962b4ca34aaSAlexander Shishkin */ 963b4ca34aaSAlexander Shishkin WARN_ON_ONCE(ret); 9647bd1d409SAlexander Shishkin } 965c74f7e82SAlexander Shishkin mutex_unlock(&stm->link_mutex); 9667bd1d409SAlexander Shishkin 9677bd1d409SAlexander Shishkin synchronize_srcu(&stm_source_srcu); 9687bd1d409SAlexander Shishkin 9697bd1d409SAlexander Shishkin unregister_chrdev(stm->major, stm_data->name); 9707bd1d409SAlexander Shishkin 9717bd1d409SAlexander Shishkin mutex_lock(&stm->policy_mutex); 9727bd1d409SAlexander Shishkin if (stm->policy) 9737bd1d409SAlexander Shishkin stp_policy_unbind(stm->policy); 9747bd1d409SAlexander Shishkin mutex_unlock(&stm->policy_mutex); 9757bd1d409SAlexander Shishkin 97673a3ed19SChunyan Zhang for (i = stm->data->sw_start; i <= stm->data->sw_end; i++) 9777bd1d409SAlexander Shishkin stp_master_free(stm, i); 9787bd1d409SAlexander Shishkin 9797bd1d409SAlexander Shishkin device_unregister(&stm->dev); 9807bd1d409SAlexander Shishkin stm_data->stm = NULL; 9817bd1d409SAlexander Shishkin } 9827bd1d409SAlexander Shishkin EXPORT_SYMBOL_GPL(stm_unregister_device); 9837bd1d409SAlexander Shishkin 984c74f7e82SAlexander Shishkin /* 985c74f7e82SAlexander Shishkin * stm::link_list access serialization uses a spinlock and a mutex; holding 986c74f7e82SAlexander Shishkin * either of them guarantees that the list is stable; modification requires 987c74f7e82SAlexander Shishkin * holding both of them. 988c74f7e82SAlexander Shishkin * 989c74f7e82SAlexander Shishkin * Lock ordering is as follows: 990c74f7e82SAlexander Shishkin * stm::link_mutex 991c74f7e82SAlexander Shishkin * stm::link_lock 992c74f7e82SAlexander Shishkin * src::link_lock 993c74f7e82SAlexander Shishkin */ 994c74f7e82SAlexander Shishkin 9957bd1d409SAlexander Shishkin /** 9967bd1d409SAlexander Shishkin * stm_source_link_add() - connect an stm_source device to an stm device 9977bd1d409SAlexander Shishkin * @src: stm_source device 9987bd1d409SAlexander Shishkin * @stm: stm device 9997bd1d409SAlexander Shishkin * 10007bd1d409SAlexander Shishkin * This function establishes a link from stm_source to an stm device so that 10017bd1d409SAlexander Shishkin * the former can send out trace data to the latter. 10027bd1d409SAlexander Shishkin * 10037bd1d409SAlexander Shishkin * Return: 0 on success, -errno otherwise. 10047bd1d409SAlexander Shishkin */ 10057bd1d409SAlexander Shishkin static int stm_source_link_add(struct stm_source_device *src, 10067bd1d409SAlexander Shishkin struct stm_device *stm) 10077bd1d409SAlexander Shishkin { 1008cb6102bdSAlexander Shishkin char *ids[] = { NULL, "default", NULL }; 1009cb6102bdSAlexander Shishkin int err = -ENOMEM; 10107bd1d409SAlexander Shishkin 1011c74f7e82SAlexander Shishkin mutex_lock(&stm->link_mutex); 10127bd1d409SAlexander Shishkin spin_lock(&stm->link_lock); 10137bd1d409SAlexander Shishkin spin_lock(&src->link_lock); 10147bd1d409SAlexander Shishkin 10157bd1d409SAlexander Shishkin /* src->link is dereferenced under stm_source_srcu but not the list */ 10167bd1d409SAlexander Shishkin rcu_assign_pointer(src->link, stm); 10177bd1d409SAlexander Shishkin list_add_tail(&src->link_entry, &stm->link_list); 10187bd1d409SAlexander Shishkin 10197bd1d409SAlexander Shishkin spin_unlock(&src->link_lock); 10207bd1d409SAlexander Shishkin spin_unlock(&stm->link_lock); 1021c74f7e82SAlexander Shishkin mutex_unlock(&stm->link_mutex); 10227bd1d409SAlexander Shishkin 1023cb6102bdSAlexander Shishkin ids[0] = kstrdup(src->data->name, GFP_KERNEL); 1024cb6102bdSAlexander Shishkin if (!ids[0]) 1025cb6102bdSAlexander Shishkin goto fail_detach; 10267bd1d409SAlexander Shishkin 1027cb6102bdSAlexander Shishkin err = stm_assign_first_policy(stm, &src->output, ids, 1028cb6102bdSAlexander Shishkin src->data->nr_chans); 1029cb6102bdSAlexander Shishkin kfree(ids[0]); 10307bd1d409SAlexander Shishkin 10317bd1d409SAlexander Shishkin if (err) 10327bd1d409SAlexander Shishkin goto fail_detach; 10337bd1d409SAlexander Shishkin 10347bd1d409SAlexander Shishkin /* this is to notify the STM device that a new link has been made */ 10357bd1d409SAlexander Shishkin if (stm->data->link) 10367bd1d409SAlexander Shishkin err = stm->data->link(stm->data, src->output.master, 10377bd1d409SAlexander Shishkin src->output.channel); 10387bd1d409SAlexander Shishkin 10397bd1d409SAlexander Shishkin if (err) 10407bd1d409SAlexander Shishkin goto fail_free_output; 10417bd1d409SAlexander Shishkin 10427bd1d409SAlexander Shishkin /* this is to let the source carry out all necessary preparations */ 10437bd1d409SAlexander Shishkin if (src->data->link) 10447bd1d409SAlexander Shishkin src->data->link(src->data); 10457bd1d409SAlexander Shishkin 10467bd1d409SAlexander Shishkin return 0; 10477bd1d409SAlexander Shishkin 10487bd1d409SAlexander Shishkin fail_free_output: 10497bd1d409SAlexander Shishkin stm_output_free(stm, &src->output); 10507bd1d409SAlexander Shishkin 10517bd1d409SAlexander Shishkin fail_detach: 1052c74f7e82SAlexander Shishkin mutex_lock(&stm->link_mutex); 10537bd1d409SAlexander Shishkin spin_lock(&stm->link_lock); 10547bd1d409SAlexander Shishkin spin_lock(&src->link_lock); 10557bd1d409SAlexander Shishkin 10567bd1d409SAlexander Shishkin rcu_assign_pointer(src->link, NULL); 10577bd1d409SAlexander Shishkin list_del_init(&src->link_entry); 10587bd1d409SAlexander Shishkin 10597bd1d409SAlexander Shishkin spin_unlock(&src->link_lock); 10607bd1d409SAlexander Shishkin spin_unlock(&stm->link_lock); 1061c74f7e82SAlexander Shishkin mutex_unlock(&stm->link_mutex); 10627bd1d409SAlexander Shishkin 10637bd1d409SAlexander Shishkin return err; 10647bd1d409SAlexander Shishkin } 10657bd1d409SAlexander Shishkin 10667bd1d409SAlexander Shishkin /** 10677bd1d409SAlexander Shishkin * __stm_source_link_drop() - detach stm_source from an stm device 10687bd1d409SAlexander Shishkin * @src: stm_source device 10697bd1d409SAlexander Shishkin * @stm: stm device 10707bd1d409SAlexander Shishkin * 10717bd1d409SAlexander Shishkin * If @stm is @src::link, disconnect them from one another and put the 10727bd1d409SAlexander Shishkin * reference on the @stm device. 10737bd1d409SAlexander Shishkin * 1074c74f7e82SAlexander Shishkin * Caller must hold stm::link_mutex. 10757bd1d409SAlexander Shishkin */ 1076b4ca34aaSAlexander Shishkin static int __stm_source_link_drop(struct stm_source_device *src, 10777bd1d409SAlexander Shishkin struct stm_device *stm) 10787bd1d409SAlexander Shishkin { 10790df771deSAlexander Shishkin struct stm_device *link; 1080b4ca34aaSAlexander Shishkin int ret = 0; 10810df771deSAlexander Shishkin 1082c74f7e82SAlexander Shishkin lockdep_assert_held(&stm->link_mutex); 1083c74f7e82SAlexander Shishkin 1084c74f7e82SAlexander Shishkin /* for stm::link_list modification, we hold both mutex and spinlock */ 1085c74f7e82SAlexander Shishkin spin_lock(&stm->link_lock); 10867bd1d409SAlexander Shishkin spin_lock(&src->link_lock); 10870df771deSAlexander Shishkin link = srcu_dereference_check(src->link, &stm_source_srcu, 1); 1088b4ca34aaSAlexander Shishkin 1089b4ca34aaSAlexander Shishkin /* 1090b4ca34aaSAlexander Shishkin * The linked device may have changed since we last looked, because 1091b4ca34aaSAlexander Shishkin * we weren't holding the src::link_lock back then; if this is the 1092b4ca34aaSAlexander Shishkin * case, tell the caller to retry. 1093b4ca34aaSAlexander Shishkin */ 1094b4ca34aaSAlexander Shishkin if (link != stm) { 1095b4ca34aaSAlexander Shishkin ret = -EAGAIN; 10961810f2c4SAlexander Shishkin goto unlock; 1097b4ca34aaSAlexander Shishkin } 10987bd1d409SAlexander Shishkin 10990df771deSAlexander Shishkin stm_output_free(link, &src->output); 11007bd1d409SAlexander Shishkin list_del_init(&src->link_entry); 11018e0469a4SAlexander Shishkin pm_runtime_mark_last_busy(&link->dev); 11028e0469a4SAlexander Shishkin pm_runtime_put_autosuspend(&link->dev); 11037bd1d409SAlexander Shishkin /* matches stm_find_device() from stm_source_link_store() */ 11040df771deSAlexander Shishkin stm_put_device(link); 11057bd1d409SAlexander Shishkin rcu_assign_pointer(src->link, NULL); 11067bd1d409SAlexander Shishkin 11071810f2c4SAlexander Shishkin unlock: 11087bd1d409SAlexander Shishkin spin_unlock(&src->link_lock); 1109c74f7e82SAlexander Shishkin spin_unlock(&stm->link_lock); 1110b4ca34aaSAlexander Shishkin 1111cc842407SAlexander Shishkin /* 1112cc842407SAlexander Shishkin * Call the unlink callbacks for both source and stm, when we know 1113cc842407SAlexander Shishkin * that we have actually performed the unlinking. 1114cc842407SAlexander Shishkin */ 1115cc842407SAlexander Shishkin if (!ret) { 1116cc842407SAlexander Shishkin if (src->data->unlink) 1117b4ca34aaSAlexander Shishkin src->data->unlink(src->data); 1118b4ca34aaSAlexander Shishkin 1119cc842407SAlexander Shishkin if (stm->data->unlink) 1120cc842407SAlexander Shishkin stm->data->unlink(stm->data, src->output.master, 1121cc842407SAlexander Shishkin src->output.channel); 1122cc842407SAlexander Shishkin } 1123cc842407SAlexander Shishkin 1124b4ca34aaSAlexander Shishkin return ret; 11257bd1d409SAlexander Shishkin } 11267bd1d409SAlexander Shishkin 11277bd1d409SAlexander Shishkin /** 11287bd1d409SAlexander Shishkin * stm_source_link_drop() - detach stm_source from its stm device 11297bd1d409SAlexander Shishkin * @src: stm_source device 11307bd1d409SAlexander Shishkin * 11317bd1d409SAlexander Shishkin * Unlinking means disconnecting from source's STM device; after this 11327bd1d409SAlexander Shishkin * writes will be unsuccessful until it is linked to a new STM device. 11337bd1d409SAlexander Shishkin * 11347bd1d409SAlexander Shishkin * This will happen on "stm_source_link" sysfs attribute write to undo 11357bd1d409SAlexander Shishkin * the existing link (if any), or on linked STM device's de-registration. 11367bd1d409SAlexander Shishkin */ 11377bd1d409SAlexander Shishkin static void stm_source_link_drop(struct stm_source_device *src) 11387bd1d409SAlexander Shishkin { 11397bd1d409SAlexander Shishkin struct stm_device *stm; 1140b4ca34aaSAlexander Shishkin int idx, ret; 11417bd1d409SAlexander Shishkin 1142b4ca34aaSAlexander Shishkin retry: 11437bd1d409SAlexander Shishkin idx = srcu_read_lock(&stm_source_srcu); 1144b4ca34aaSAlexander Shishkin /* 1145b4ca34aaSAlexander Shishkin * The stm device will be valid for the duration of this 1146b4ca34aaSAlexander Shishkin * read section, but the link may change before we grab 1147b4ca34aaSAlexander Shishkin * the src::link_lock in __stm_source_link_drop(). 1148b4ca34aaSAlexander Shishkin */ 11497bd1d409SAlexander Shishkin stm = srcu_dereference(src->link, &stm_source_srcu); 11507bd1d409SAlexander Shishkin 1151b4ca34aaSAlexander Shishkin ret = 0; 11527bd1d409SAlexander Shishkin if (stm) { 1153c74f7e82SAlexander Shishkin mutex_lock(&stm->link_mutex); 1154b4ca34aaSAlexander Shishkin ret = __stm_source_link_drop(src, stm); 1155c74f7e82SAlexander Shishkin mutex_unlock(&stm->link_mutex); 11567bd1d409SAlexander Shishkin } 11577bd1d409SAlexander Shishkin 11587bd1d409SAlexander Shishkin srcu_read_unlock(&stm_source_srcu, idx); 1159b4ca34aaSAlexander Shishkin 1160b4ca34aaSAlexander Shishkin /* if it did change, retry */ 1161b4ca34aaSAlexander Shishkin if (ret == -EAGAIN) 1162b4ca34aaSAlexander Shishkin goto retry; 11637bd1d409SAlexander Shishkin } 11647bd1d409SAlexander Shishkin 11657bd1d409SAlexander Shishkin static ssize_t stm_source_link_show(struct device *dev, 11667bd1d409SAlexander Shishkin struct device_attribute *attr, 11677bd1d409SAlexander Shishkin char *buf) 11687bd1d409SAlexander Shishkin { 11697bd1d409SAlexander Shishkin struct stm_source_device *src = to_stm_source_device(dev); 11707bd1d409SAlexander Shishkin struct stm_device *stm; 11717bd1d409SAlexander Shishkin int idx, ret; 11727bd1d409SAlexander Shishkin 11737bd1d409SAlexander Shishkin idx = srcu_read_lock(&stm_source_srcu); 11747bd1d409SAlexander Shishkin stm = srcu_dereference(src->link, &stm_source_srcu); 11757bd1d409SAlexander Shishkin ret = sprintf(buf, "%s\n", 11767bd1d409SAlexander Shishkin stm ? dev_name(&stm->dev) : "<none>"); 11777bd1d409SAlexander Shishkin srcu_read_unlock(&stm_source_srcu, idx); 11787bd1d409SAlexander Shishkin 11797bd1d409SAlexander Shishkin return ret; 11807bd1d409SAlexander Shishkin } 11817bd1d409SAlexander Shishkin 11827bd1d409SAlexander Shishkin static ssize_t stm_source_link_store(struct device *dev, 11837bd1d409SAlexander Shishkin struct device_attribute *attr, 11847bd1d409SAlexander Shishkin const char *buf, size_t count) 11857bd1d409SAlexander Shishkin { 11867bd1d409SAlexander Shishkin struct stm_source_device *src = to_stm_source_device(dev); 11877bd1d409SAlexander Shishkin struct stm_device *link; 11887bd1d409SAlexander Shishkin int err; 11897bd1d409SAlexander Shishkin 11907bd1d409SAlexander Shishkin stm_source_link_drop(src); 11917bd1d409SAlexander Shishkin 11927bd1d409SAlexander Shishkin link = stm_find_device(buf); 11937bd1d409SAlexander Shishkin if (!link) 11947bd1d409SAlexander Shishkin return -EINVAL; 11957bd1d409SAlexander Shishkin 11968e0469a4SAlexander Shishkin pm_runtime_get(&link->dev); 11978e0469a4SAlexander Shishkin 11987bd1d409SAlexander Shishkin err = stm_source_link_add(src, link); 1199f7c81c71SAlexander Shishkin if (err) { 12008e0469a4SAlexander Shishkin pm_runtime_put_autosuspend(&link->dev); 1201f7c81c71SAlexander Shishkin /* matches the stm_find_device() above */ 12027bd1d409SAlexander Shishkin stm_put_device(link); 1203f7c81c71SAlexander Shishkin } 12047bd1d409SAlexander Shishkin 12057bd1d409SAlexander Shishkin return err ? : count; 12067bd1d409SAlexander Shishkin } 12077bd1d409SAlexander Shishkin 12087bd1d409SAlexander Shishkin static DEVICE_ATTR_RW(stm_source_link); 12097bd1d409SAlexander Shishkin 12107bd1d409SAlexander Shishkin static struct attribute *stm_source_attrs[] = { 12117bd1d409SAlexander Shishkin &dev_attr_stm_source_link.attr, 12127bd1d409SAlexander Shishkin NULL, 12137bd1d409SAlexander Shishkin }; 12147bd1d409SAlexander Shishkin 12157bd1d409SAlexander Shishkin ATTRIBUTE_GROUPS(stm_source); 12167bd1d409SAlexander Shishkin 12177bd1d409SAlexander Shishkin static struct class stm_source_class = { 12187bd1d409SAlexander Shishkin .name = "stm_source", 12197bd1d409SAlexander Shishkin .dev_groups = stm_source_groups, 12207bd1d409SAlexander Shishkin }; 12217bd1d409SAlexander Shishkin 12227bd1d409SAlexander Shishkin static void stm_source_device_release(struct device *dev) 12237bd1d409SAlexander Shishkin { 12247bd1d409SAlexander Shishkin struct stm_source_device *src = to_stm_source_device(dev); 12257bd1d409SAlexander Shishkin 12267bd1d409SAlexander Shishkin kfree(src); 12277bd1d409SAlexander Shishkin } 12287bd1d409SAlexander Shishkin 12297bd1d409SAlexander Shishkin /** 12307bd1d409SAlexander Shishkin * stm_source_register_device() - register an stm_source device 12317bd1d409SAlexander Shishkin * @parent: parent device 12327bd1d409SAlexander Shishkin * @data: device description structure 12337bd1d409SAlexander Shishkin * 12347bd1d409SAlexander Shishkin * This will create a device of stm_source class that can write 12357bd1d409SAlexander Shishkin * data to an stm device once linked. 12367bd1d409SAlexander Shishkin * 12377bd1d409SAlexander Shishkin * Return: 0 on success, -errno otherwise. 12387bd1d409SAlexander Shishkin */ 12397bd1d409SAlexander Shishkin int stm_source_register_device(struct device *parent, 12407bd1d409SAlexander Shishkin struct stm_source_data *data) 12417bd1d409SAlexander Shishkin { 12427bd1d409SAlexander Shishkin struct stm_source_device *src; 12437bd1d409SAlexander Shishkin int err; 12447bd1d409SAlexander Shishkin 12457bd1d409SAlexander Shishkin if (!stm_core_up) 12467bd1d409SAlexander Shishkin return -EPROBE_DEFER; 12477bd1d409SAlexander Shishkin 12487bd1d409SAlexander Shishkin src = kzalloc(sizeof(*src), GFP_KERNEL); 12497bd1d409SAlexander Shishkin if (!src) 12507bd1d409SAlexander Shishkin return -ENOMEM; 12517bd1d409SAlexander Shishkin 12527bd1d409SAlexander Shishkin device_initialize(&src->dev); 12537bd1d409SAlexander Shishkin src->dev.class = &stm_source_class; 12547bd1d409SAlexander Shishkin src->dev.parent = parent; 12557bd1d409SAlexander Shishkin src->dev.release = stm_source_device_release; 12567bd1d409SAlexander Shishkin 12577bd1d409SAlexander Shishkin err = kobject_set_name(&src->dev.kobj, "%s", data->name); 12587bd1d409SAlexander Shishkin if (err) 12597bd1d409SAlexander Shishkin goto err; 12607bd1d409SAlexander Shishkin 12618e0469a4SAlexander Shishkin pm_runtime_no_callbacks(&src->dev); 12628e0469a4SAlexander Shishkin pm_runtime_forbid(&src->dev); 12638e0469a4SAlexander Shishkin 12647bd1d409SAlexander Shishkin err = device_add(&src->dev); 12657bd1d409SAlexander Shishkin if (err) 12667bd1d409SAlexander Shishkin goto err; 12677bd1d409SAlexander Shishkin 1268cde4ad83SAlexander Shishkin stm_output_init(&src->output); 12697bd1d409SAlexander Shishkin spin_lock_init(&src->link_lock); 12707bd1d409SAlexander Shishkin INIT_LIST_HEAD(&src->link_entry); 12717bd1d409SAlexander Shishkin src->data = data; 12727bd1d409SAlexander Shishkin data->src = src; 12737bd1d409SAlexander Shishkin 12747bd1d409SAlexander Shishkin return 0; 12757bd1d409SAlexander Shishkin 12767bd1d409SAlexander Shishkin err: 12777bd1d409SAlexander Shishkin put_device(&src->dev); 12787bd1d409SAlexander Shishkin kfree(src); 12797bd1d409SAlexander Shishkin 12807bd1d409SAlexander Shishkin return err; 12817bd1d409SAlexander Shishkin } 12827bd1d409SAlexander Shishkin EXPORT_SYMBOL_GPL(stm_source_register_device); 12837bd1d409SAlexander Shishkin 12847bd1d409SAlexander Shishkin /** 12857bd1d409SAlexander Shishkin * stm_source_unregister_device() - unregister an stm_source device 12867bd1d409SAlexander Shishkin * @data: device description that was used to register the device 12877bd1d409SAlexander Shishkin * 12887bd1d409SAlexander Shishkin * This will remove a previously created stm_source device from the system. 12897bd1d409SAlexander Shishkin */ 12907bd1d409SAlexander Shishkin void stm_source_unregister_device(struct stm_source_data *data) 12917bd1d409SAlexander Shishkin { 12927bd1d409SAlexander Shishkin struct stm_source_device *src = data->src; 12937bd1d409SAlexander Shishkin 12947bd1d409SAlexander Shishkin stm_source_link_drop(src); 12957bd1d409SAlexander Shishkin 1296fd085bb1SAlexander Shishkin device_unregister(&src->dev); 12977bd1d409SAlexander Shishkin } 12987bd1d409SAlexander Shishkin EXPORT_SYMBOL_GPL(stm_source_unregister_device); 12997bd1d409SAlexander Shishkin 13009dfed80dSChunyan Zhang int notrace stm_source_write(struct stm_source_data *data, 13019dfed80dSChunyan Zhang unsigned int chan, 13027bd1d409SAlexander Shishkin const char *buf, size_t count) 13037bd1d409SAlexander Shishkin { 13047bd1d409SAlexander Shishkin struct stm_source_device *src = data->src; 13057bd1d409SAlexander Shishkin struct stm_device *stm; 13067bd1d409SAlexander Shishkin int idx; 13077bd1d409SAlexander Shishkin 13087bd1d409SAlexander Shishkin if (!src->output.nr_chans) 13097bd1d409SAlexander Shishkin return -ENODEV; 13107bd1d409SAlexander Shishkin 13117bd1d409SAlexander Shishkin if (chan >= src->output.nr_chans) 13127bd1d409SAlexander Shishkin return -EINVAL; 13137bd1d409SAlexander Shishkin 13147bd1d409SAlexander Shishkin idx = srcu_read_lock(&stm_source_srcu); 13157bd1d409SAlexander Shishkin 13167bd1d409SAlexander Shishkin stm = srcu_dereference(src->link, &stm_source_srcu); 13177bd1d409SAlexander Shishkin if (stm) 131824c7bcb6SAlexander Shishkin count = stm_write(stm, &src->output, chan, buf, count); 13197bd1d409SAlexander Shishkin else 13207bd1d409SAlexander Shishkin count = -ENODEV; 13217bd1d409SAlexander Shishkin 13227bd1d409SAlexander Shishkin srcu_read_unlock(&stm_source_srcu, idx); 13237bd1d409SAlexander Shishkin 13247bd1d409SAlexander Shishkin return count; 13257bd1d409SAlexander Shishkin } 13267bd1d409SAlexander Shishkin EXPORT_SYMBOL_GPL(stm_source_write); 13277bd1d409SAlexander Shishkin 13287bd1d409SAlexander Shishkin static int __init stm_core_init(void) 13297bd1d409SAlexander Shishkin { 13307bd1d409SAlexander Shishkin int err; 13317bd1d409SAlexander Shishkin 13327bd1d409SAlexander Shishkin err = class_register(&stm_class); 13337bd1d409SAlexander Shishkin if (err) 13347bd1d409SAlexander Shishkin return err; 13357bd1d409SAlexander Shishkin 13367bd1d409SAlexander Shishkin err = class_register(&stm_source_class); 13377bd1d409SAlexander Shishkin if (err) 13387bd1d409SAlexander Shishkin goto err_stm; 13397bd1d409SAlexander Shishkin 13407bd1d409SAlexander Shishkin err = stp_configfs_init(); 13417bd1d409SAlexander Shishkin if (err) 13427bd1d409SAlexander Shishkin goto err_src; 13437bd1d409SAlexander Shishkin 13447bd1d409SAlexander Shishkin init_srcu_struct(&stm_source_srcu); 1345c7fd62bcSAlexander Shishkin INIT_LIST_HEAD(&stm_pdrv_head); 1346c7fd62bcSAlexander Shishkin mutex_init(&stm_pdrv_mutex); 13477bd1d409SAlexander Shishkin 134824c7bcb6SAlexander Shishkin /* 134924c7bcb6SAlexander Shishkin * So as to not confuse existing users with a requirement 135024c7bcb6SAlexander Shishkin * to load yet another module, do it here. 135124c7bcb6SAlexander Shishkin */ 135224c7bcb6SAlexander Shishkin if (IS_ENABLED(CONFIG_STM_PROTO_BASIC)) 135324c7bcb6SAlexander Shishkin (void)request_module_nowait("stm_p_basic"); 13547bd1d409SAlexander Shishkin stm_core_up++; 13557bd1d409SAlexander Shishkin 13567bd1d409SAlexander Shishkin return 0; 13577bd1d409SAlexander Shishkin 13587bd1d409SAlexander Shishkin err_src: 13597bd1d409SAlexander Shishkin class_unregister(&stm_source_class); 13607bd1d409SAlexander Shishkin err_stm: 13617bd1d409SAlexander Shishkin class_unregister(&stm_class); 13627bd1d409SAlexander Shishkin 13637bd1d409SAlexander Shishkin return err; 13647bd1d409SAlexander Shishkin } 13657bd1d409SAlexander Shishkin 13667bd1d409SAlexander Shishkin module_init(stm_core_init); 13677bd1d409SAlexander Shishkin 13687bd1d409SAlexander Shishkin static void __exit stm_core_exit(void) 13697bd1d409SAlexander Shishkin { 13707bd1d409SAlexander Shishkin cleanup_srcu_struct(&stm_source_srcu); 13717bd1d409SAlexander Shishkin class_unregister(&stm_source_class); 13727bd1d409SAlexander Shishkin class_unregister(&stm_class); 13737bd1d409SAlexander Shishkin stp_configfs_exit(); 13747bd1d409SAlexander Shishkin } 13757bd1d409SAlexander Shishkin 13767bd1d409SAlexander Shishkin module_exit(stm_core_exit); 13777bd1d409SAlexander Shishkin 13787bd1d409SAlexander Shishkin MODULE_LICENSE("GPL v2"); 13797bd1d409SAlexander Shishkin MODULE_DESCRIPTION("System Trace Module device class"); 13807bd1d409SAlexander Shishkin MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>"); 1381