Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 1 | /* |
| 2 | * System Trace Module (STM) infrastructure |
| 3 | * Copyright (c) 2014, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * STM class implements generic infrastructure for System Trace Module devices |
| 15 | * as defined in MIPI STPv2 specification. |
| 16 | */ |
| 17 | |
Alexander Shishkin | 8e0469a | 2016-06-28 11:35:02 +0300 | [diff] [blame] | 18 | #include <linux/pm_runtime.h> |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 19 | #include <linux/uaccess.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/device.h> |
| 23 | #include <linux/compat.h> |
| 24 | #include <linux/kdev_t.h> |
| 25 | #include <linux/srcu.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/stm.h> |
| 28 | #include <linux/fs.h> |
| 29 | #include <linux/mm.h> |
Greg Kroah-Hartman | 6ba7b04 | 2018-05-26 08:49:24 +0200 | [diff] [blame] | 30 | #include <linux/vmalloc.h> |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 31 | #include "stm.h" |
| 32 | |
| 33 | #include <uapi/linux/stm.h> |
| 34 | |
| 35 | static unsigned int stm_core_up; |
| 36 | |
| 37 | /* |
| 38 | * The SRCU here makes sure that STM device doesn't disappear from under a |
| 39 | * stm_source_write() caller, which may want to have as little overhead as |
| 40 | * possible. |
| 41 | */ |
| 42 | static struct srcu_struct stm_source_srcu; |
| 43 | |
| 44 | static ssize_t masters_show(struct device *dev, |
| 45 | struct device_attribute *attr, |
| 46 | char *buf) |
| 47 | { |
| 48 | struct stm_device *stm = to_stm_device(dev); |
| 49 | int ret; |
| 50 | |
| 51 | ret = sprintf(buf, "%u %u\n", stm->data->sw_start, stm->data->sw_end); |
| 52 | |
| 53 | return ret; |
| 54 | } |
| 55 | |
| 56 | static DEVICE_ATTR_RO(masters); |
| 57 | |
| 58 | static ssize_t channels_show(struct device *dev, |
| 59 | struct device_attribute *attr, |
| 60 | char *buf) |
| 61 | { |
| 62 | struct stm_device *stm = to_stm_device(dev); |
| 63 | int ret; |
| 64 | |
| 65 | ret = sprintf(buf, "%u\n", stm->data->sw_nchannels); |
| 66 | |
| 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | static DEVICE_ATTR_RO(channels); |
| 71 | |
Alexander Shishkin | 8e996a2 | 2016-05-03 11:33:37 -0600 | [diff] [blame] | 72 | static ssize_t hw_override_show(struct device *dev, |
| 73 | struct device_attribute *attr, |
| 74 | char *buf) |
| 75 | { |
| 76 | struct stm_device *stm = to_stm_device(dev); |
| 77 | int ret; |
| 78 | |
| 79 | ret = sprintf(buf, "%u\n", stm->data->hw_override); |
| 80 | |
| 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | static DEVICE_ATTR_RO(hw_override); |
| 85 | |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 86 | static struct attribute *stm_attrs[] = { |
| 87 | &dev_attr_masters.attr, |
| 88 | &dev_attr_channels.attr, |
Alexander Shishkin | 8e996a2 | 2016-05-03 11:33:37 -0600 | [diff] [blame] | 89 | &dev_attr_hw_override.attr, |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 90 | NULL, |
| 91 | }; |
| 92 | |
| 93 | ATTRIBUTE_GROUPS(stm); |
| 94 | |
| 95 | static struct class stm_class = { |
| 96 | .name = "stm", |
| 97 | .dev_groups = stm_groups, |
| 98 | }; |
| 99 | |
| 100 | static int stm_dev_match(struct device *dev, const void *data) |
| 101 | { |
| 102 | const char *name = data; |
| 103 | |
| 104 | return sysfs_streq(name, dev_name(dev)); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * stm_find_device() - find stm device by name |
| 109 | * @buf: character buffer containing the name |
| 110 | * |
| 111 | * This is called when either policy gets assigned to an stm device or an |
| 112 | * stm_source device gets linked to an stm device. |
| 113 | * |
| 114 | * This grabs device's reference (get_device()) and module reference, both |
| 115 | * of which the calling path needs to make sure to drop with stm_put_device(). |
| 116 | * |
| 117 | * Return: stm device pointer or null if lookup failed. |
| 118 | */ |
| 119 | struct stm_device *stm_find_device(const char *buf) |
| 120 | { |
| 121 | struct stm_device *stm; |
| 122 | struct device *dev; |
| 123 | |
| 124 | if (!stm_core_up) |
| 125 | return NULL; |
| 126 | |
| 127 | dev = class_find_device(&stm_class, NULL, buf, stm_dev_match); |
| 128 | if (!dev) |
| 129 | return NULL; |
| 130 | |
| 131 | stm = to_stm_device(dev); |
| 132 | if (!try_module_get(stm->owner)) { |
Alexander Shishkin | f7c81c7 | 2016-02-15 19:12:07 +0200 | [diff] [blame] | 133 | /* matches class_find_device() above */ |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 134 | put_device(dev); |
| 135 | return NULL; |
| 136 | } |
| 137 | |
| 138 | return stm; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * stm_put_device() - drop references on the stm device |
| 143 | * @stm: stm device, previously acquired by stm_find_device() |
| 144 | * |
| 145 | * This drops the module reference and device reference taken by |
Alexander Shishkin | f7c81c7 | 2016-02-15 19:12:07 +0200 | [diff] [blame] | 146 | * stm_find_device() or stm_char_open(). |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 147 | */ |
| 148 | void stm_put_device(struct stm_device *stm) |
| 149 | { |
| 150 | module_put(stm->owner); |
| 151 | put_device(&stm->dev); |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Internally we only care about software-writable masters here, that is the |
| 156 | * ones in the range [stm_data->sw_start..stm_data..sw_end], however we need |
| 157 | * original master numbers to be visible externally, since they are the ones |
| 158 | * that will appear in the STP stream. Thus, the internal bookkeeping uses |
| 159 | * $master - stm_data->sw_start to reference master descriptors and such. |
| 160 | */ |
| 161 | |
| 162 | #define __stm_master(_s, _m) \ |
| 163 | ((_s)->masters[(_m) - (_s)->data->sw_start]) |
| 164 | |
| 165 | static inline struct stp_master * |
| 166 | stm_master(struct stm_device *stm, unsigned int idx) |
| 167 | { |
| 168 | if (idx < stm->data->sw_start || idx > stm->data->sw_end) |
| 169 | return NULL; |
| 170 | |
| 171 | return __stm_master(stm, idx); |
| 172 | } |
| 173 | |
| 174 | static int stp_master_alloc(struct stm_device *stm, unsigned int idx) |
| 175 | { |
| 176 | struct stp_master *master; |
| 177 | size_t size; |
| 178 | |
| 179 | size = ALIGN(stm->data->sw_nchannels, 8) / 8; |
| 180 | size += sizeof(struct stp_master); |
| 181 | master = kzalloc(size, GFP_ATOMIC); |
| 182 | if (!master) |
| 183 | return -ENOMEM; |
| 184 | |
| 185 | master->nr_free = stm->data->sw_nchannels; |
| 186 | __stm_master(stm, idx) = master; |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static void stp_master_free(struct stm_device *stm, unsigned int idx) |
| 192 | { |
| 193 | struct stp_master *master = stm_master(stm, idx); |
| 194 | |
| 195 | if (!master) |
| 196 | return; |
| 197 | |
| 198 | __stm_master(stm, idx) = NULL; |
| 199 | kfree(master); |
| 200 | } |
| 201 | |
| 202 | static void stm_output_claim(struct stm_device *stm, struct stm_output *output) |
| 203 | { |
| 204 | struct stp_master *master = stm_master(stm, output->master); |
| 205 | |
Alexander Shishkin | cde4ad8 | 2016-02-15 19:12:06 +0200 | [diff] [blame] | 206 | lockdep_assert_held(&stm->mc_lock); |
| 207 | lockdep_assert_held(&output->lock); |
| 208 | |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 209 | if (WARN_ON_ONCE(master->nr_free < output->nr_chans)) |
| 210 | return; |
| 211 | |
| 212 | bitmap_allocate_region(&master->chan_map[0], output->channel, |
| 213 | ilog2(output->nr_chans)); |
| 214 | |
| 215 | master->nr_free -= output->nr_chans; |
| 216 | } |
| 217 | |
| 218 | static void |
| 219 | stm_output_disclaim(struct stm_device *stm, struct stm_output *output) |
| 220 | { |
| 221 | struct stp_master *master = stm_master(stm, output->master); |
| 222 | |
Alexander Shishkin | cde4ad8 | 2016-02-15 19:12:06 +0200 | [diff] [blame] | 223 | lockdep_assert_held(&stm->mc_lock); |
| 224 | lockdep_assert_held(&output->lock); |
| 225 | |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 226 | bitmap_release_region(&master->chan_map[0], output->channel, |
| 227 | ilog2(output->nr_chans)); |
| 228 | |
| 229 | output->nr_chans = 0; |
| 230 | master->nr_free += output->nr_chans; |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * This is like bitmap_find_free_region(), except it can ignore @start bits |
| 235 | * at the beginning. |
| 236 | */ |
| 237 | static int find_free_channels(unsigned long *bitmap, unsigned int start, |
| 238 | unsigned int end, unsigned int width) |
| 239 | { |
| 240 | unsigned int pos; |
| 241 | int i; |
| 242 | |
| 243 | for (pos = start; pos < end + 1; pos = ALIGN(pos, width)) { |
| 244 | pos = find_next_zero_bit(bitmap, end + 1, pos); |
| 245 | if (pos + width > end + 1) |
| 246 | break; |
| 247 | |
| 248 | if (pos & (width - 1)) |
| 249 | continue; |
| 250 | |
| 251 | for (i = 1; i < width && !test_bit(pos + i, bitmap); i++) |
| 252 | ; |
| 253 | if (i == width) |
| 254 | return pos; |
| 255 | } |
| 256 | |
| 257 | return -1; |
| 258 | } |
| 259 | |
Lucas Tanure | f45f40a | 2016-02-15 19:11:51 +0200 | [diff] [blame] | 260 | static int |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 261 | stm_find_master_chan(struct stm_device *stm, unsigned int width, |
| 262 | unsigned int *mstart, unsigned int mend, |
| 263 | unsigned int *cstart, unsigned int cend) |
| 264 | { |
| 265 | struct stp_master *master; |
| 266 | unsigned int midx; |
| 267 | int pos, err; |
| 268 | |
| 269 | for (midx = *mstart; midx <= mend; midx++) { |
| 270 | if (!stm_master(stm, midx)) { |
| 271 | err = stp_master_alloc(stm, midx); |
| 272 | if (err) |
| 273 | return err; |
| 274 | } |
| 275 | |
| 276 | master = stm_master(stm, midx); |
| 277 | |
| 278 | if (!master->nr_free) |
| 279 | continue; |
| 280 | |
| 281 | pos = find_free_channels(master->chan_map, *cstart, cend, |
| 282 | width); |
| 283 | if (pos < 0) |
| 284 | continue; |
| 285 | |
| 286 | *mstart = midx; |
| 287 | *cstart = pos; |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | return -ENOSPC; |
| 292 | } |
| 293 | |
| 294 | static int stm_output_assign(struct stm_device *stm, unsigned int width, |
| 295 | struct stp_policy_node *policy_node, |
| 296 | struct stm_output *output) |
| 297 | { |
| 298 | unsigned int midx, cidx, mend, cend; |
| 299 | int ret = -EINVAL; |
| 300 | |
| 301 | if (width > stm->data->sw_nchannels) |
| 302 | return -EINVAL; |
| 303 | |
| 304 | if (policy_node) { |
| 305 | stp_policy_node_get_ranges(policy_node, |
| 306 | &midx, &mend, &cidx, &cend); |
| 307 | } else { |
| 308 | midx = stm->data->sw_start; |
| 309 | cidx = 0; |
| 310 | mend = stm->data->sw_end; |
| 311 | cend = stm->data->sw_nchannels - 1; |
| 312 | } |
| 313 | |
| 314 | spin_lock(&stm->mc_lock); |
Alexander Shishkin | cde4ad8 | 2016-02-15 19:12:06 +0200 | [diff] [blame] | 315 | spin_lock(&output->lock); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 316 | /* output is already assigned -- shouldn't happen */ |
| 317 | if (WARN_ON_ONCE(output->nr_chans)) |
| 318 | goto unlock; |
| 319 | |
| 320 | ret = stm_find_master_chan(stm, width, &midx, mend, &cidx, cend); |
Lucas Tanure | f45f40a | 2016-02-15 19:11:51 +0200 | [diff] [blame] | 321 | if (ret < 0) |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 322 | goto unlock; |
| 323 | |
| 324 | output->master = midx; |
| 325 | output->channel = cidx; |
| 326 | output->nr_chans = width; |
| 327 | stm_output_claim(stm, output); |
| 328 | dev_dbg(&stm->dev, "assigned %u:%u (+%u)\n", midx, cidx, width); |
| 329 | |
| 330 | ret = 0; |
| 331 | unlock: |
Alexander Shishkin | cde4ad8 | 2016-02-15 19:12:06 +0200 | [diff] [blame] | 332 | spin_unlock(&output->lock); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 333 | spin_unlock(&stm->mc_lock); |
| 334 | |
| 335 | return ret; |
| 336 | } |
| 337 | |
| 338 | static void stm_output_free(struct stm_device *stm, struct stm_output *output) |
| 339 | { |
| 340 | spin_lock(&stm->mc_lock); |
Alexander Shishkin | cde4ad8 | 2016-02-15 19:12:06 +0200 | [diff] [blame] | 341 | spin_lock(&output->lock); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 342 | if (output->nr_chans) |
| 343 | stm_output_disclaim(stm, output); |
Alexander Shishkin | cde4ad8 | 2016-02-15 19:12:06 +0200 | [diff] [blame] | 344 | spin_unlock(&output->lock); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 345 | spin_unlock(&stm->mc_lock); |
| 346 | } |
| 347 | |
Alexander Shishkin | cde4ad8 | 2016-02-15 19:12:06 +0200 | [diff] [blame] | 348 | static void stm_output_init(struct stm_output *output) |
| 349 | { |
| 350 | spin_lock_init(&output->lock); |
| 351 | } |
| 352 | |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 353 | static int major_match(struct device *dev, const void *data) |
| 354 | { |
| 355 | unsigned int major = *(unsigned int *)data; |
| 356 | |
| 357 | return MAJOR(dev->devt) == major; |
| 358 | } |
| 359 | |
| 360 | static int stm_char_open(struct inode *inode, struct file *file) |
| 361 | { |
| 362 | struct stm_file *stmf; |
| 363 | struct device *dev; |
| 364 | unsigned int major = imajor(inode); |
Johan Hovold | 2d29143 | 2016-11-18 14:17:31 +0200 | [diff] [blame] | 365 | int err = -ENOMEM; |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 366 | |
| 367 | dev = class_find_device(&stm_class, NULL, &major, major_match); |
| 368 | if (!dev) |
| 369 | return -ENODEV; |
| 370 | |
| 371 | stmf = kzalloc(sizeof(*stmf), GFP_KERNEL); |
| 372 | if (!stmf) |
Johan Hovold | 2d29143 | 2016-11-18 14:17:31 +0200 | [diff] [blame] | 373 | goto err_put_device; |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 374 | |
Johan Hovold | 2d29143 | 2016-11-18 14:17:31 +0200 | [diff] [blame] | 375 | err = -ENODEV; |
Alexander Shishkin | cde4ad8 | 2016-02-15 19:12:06 +0200 | [diff] [blame] | 376 | stm_output_init(&stmf->output); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 377 | stmf->stm = to_stm_device(dev); |
| 378 | |
| 379 | if (!try_module_get(stmf->stm->owner)) |
| 380 | goto err_free; |
| 381 | |
| 382 | file->private_data = stmf; |
| 383 | |
| 384 | return nonseekable_open(inode, file); |
| 385 | |
| 386 | err_free: |
Johan Hovold | 2d29143 | 2016-11-18 14:17:31 +0200 | [diff] [blame] | 387 | kfree(stmf); |
| 388 | err_put_device: |
Alexander Shishkin | f7c81c7 | 2016-02-15 19:12:07 +0200 | [diff] [blame] | 389 | /* matches class_find_device() above */ |
| 390 | put_device(dev); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 391 | |
| 392 | return err; |
| 393 | } |
| 394 | |
| 395 | static int stm_char_release(struct inode *inode, struct file *file) |
| 396 | { |
| 397 | struct stm_file *stmf = file->private_data; |
Alexander Shishkin | cc84240 | 2016-02-15 19:12:09 +0200 | [diff] [blame] | 398 | struct stm_device *stm = stmf->stm; |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 399 | |
Alexander Shishkin | cc84240 | 2016-02-15 19:12:09 +0200 | [diff] [blame] | 400 | if (stm->data->unlink) |
| 401 | stm->data->unlink(stm->data, stmf->output.master, |
| 402 | stmf->output.channel); |
| 403 | |
| 404 | stm_output_free(stm, &stmf->output); |
Alexander Shishkin | f7c81c7 | 2016-02-15 19:12:07 +0200 | [diff] [blame] | 405 | |
| 406 | /* |
| 407 | * matches the stm_char_open()'s |
| 408 | * class_find_device() + try_module_get() |
| 409 | */ |
Alexander Shishkin | cc84240 | 2016-02-15 19:12:09 +0200 | [diff] [blame] | 410 | stm_put_device(stm); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 411 | kfree(stmf); |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | static int stm_file_assign(struct stm_file *stmf, char *id, unsigned int width) |
| 417 | { |
| 418 | struct stm_device *stm = stmf->stm; |
| 419 | int ret; |
| 420 | |
| 421 | stmf->policy_node = stp_policy_node_lookup(stm, id); |
| 422 | |
| 423 | ret = stm_output_assign(stm, width, stmf->policy_node, &stmf->output); |
| 424 | |
| 425 | if (stmf->policy_node) |
| 426 | stp_policy_node_put(stmf->policy_node); |
| 427 | |
| 428 | return ret; |
| 429 | } |
| 430 | |
Alexander Shishkin | f8560a9 | 2016-02-15 19:12:01 +0200 | [diff] [blame] | 431 | static ssize_t stm_write(struct stm_data *data, unsigned int master, |
| 432 | unsigned int channel, const char *buf, size_t count) |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 433 | { |
| 434 | unsigned int flags = STP_PACKET_TIMESTAMPED; |
| 435 | const unsigned char *p = buf, nil = 0; |
| 436 | size_t pos; |
| 437 | ssize_t sz; |
| 438 | |
| 439 | for (pos = 0, p = buf; count > pos; pos += sz, p += sz) { |
| 440 | sz = min_t(unsigned int, count - pos, 8); |
| 441 | sz = data->packet(data, master, channel, STP_PACKET_DATA, flags, |
| 442 | sz, p); |
| 443 | flags = 0; |
Alexander Shishkin | f8560a9 | 2016-02-15 19:12:01 +0200 | [diff] [blame] | 444 | |
| 445 | if (sz < 0) |
| 446 | break; |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | data->packet(data, master, channel, STP_PACKET_FLAG, 0, 0, &nil); |
Alexander Shishkin | f8560a9 | 2016-02-15 19:12:01 +0200 | [diff] [blame] | 450 | |
| 451 | return pos; |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | static ssize_t stm_char_write(struct file *file, const char __user *buf, |
| 455 | size_t count, loff_t *ppos) |
| 456 | { |
| 457 | struct stm_file *stmf = file->private_data; |
| 458 | struct stm_device *stm = stmf->stm; |
| 459 | char *kbuf; |
| 460 | int err; |
| 461 | |
Alexander Shishkin | f08b182 | 2015-12-22 17:25:21 +0200 | [diff] [blame] | 462 | if (count + 1 > PAGE_SIZE) |
| 463 | count = PAGE_SIZE - 1; |
| 464 | |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 465 | /* |
| 466 | * if no m/c have been assigned to this writer up to this |
| 467 | * point, use "default" policy entry |
| 468 | */ |
| 469 | if (!stmf->output.nr_chans) { |
| 470 | err = stm_file_assign(stmf, "default", 1); |
| 471 | /* |
| 472 | * EBUSY means that somebody else just assigned this |
| 473 | * output, which is just fine for write() |
| 474 | */ |
| 475 | if (err && err != -EBUSY) |
| 476 | return err; |
| 477 | } |
| 478 | |
| 479 | kbuf = kmalloc(count + 1, GFP_KERNEL); |
| 480 | if (!kbuf) |
| 481 | return -ENOMEM; |
| 482 | |
| 483 | err = copy_from_user(kbuf, buf, count); |
| 484 | if (err) { |
| 485 | kfree(kbuf); |
| 486 | return -EFAULT; |
| 487 | } |
| 488 | |
Alexander Shishkin | 8e0469a | 2016-06-28 11:35:02 +0300 | [diff] [blame] | 489 | pm_runtime_get_sync(&stm->dev); |
| 490 | |
Alexander Shishkin | f8560a9 | 2016-02-15 19:12:01 +0200 | [diff] [blame] | 491 | count = stm_write(stm->data, stmf->output.master, stmf->output.channel, |
| 492 | kbuf, count); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 493 | |
Alexander Shishkin | 8e0469a | 2016-06-28 11:35:02 +0300 | [diff] [blame] | 494 | pm_runtime_mark_last_busy(&stm->dev); |
| 495 | pm_runtime_put_autosuspend(&stm->dev); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 496 | kfree(kbuf); |
| 497 | |
| 498 | return count; |
| 499 | } |
| 500 | |
Alexander Shishkin | 8e0469a | 2016-06-28 11:35:02 +0300 | [diff] [blame] | 501 | static void stm_mmap_open(struct vm_area_struct *vma) |
| 502 | { |
| 503 | struct stm_file *stmf = vma->vm_file->private_data; |
| 504 | struct stm_device *stm = stmf->stm; |
| 505 | |
| 506 | pm_runtime_get(&stm->dev); |
| 507 | } |
| 508 | |
| 509 | static void stm_mmap_close(struct vm_area_struct *vma) |
| 510 | { |
| 511 | struct stm_file *stmf = vma->vm_file->private_data; |
| 512 | struct stm_device *stm = stmf->stm; |
| 513 | |
| 514 | pm_runtime_mark_last_busy(&stm->dev); |
| 515 | pm_runtime_put_autosuspend(&stm->dev); |
| 516 | } |
| 517 | |
| 518 | static const struct vm_operations_struct stm_mmap_vmops = { |
| 519 | .open = stm_mmap_open, |
| 520 | .close = stm_mmap_close, |
| 521 | }; |
| 522 | |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 523 | static int stm_char_mmap(struct file *file, struct vm_area_struct *vma) |
| 524 | { |
| 525 | struct stm_file *stmf = file->private_data; |
| 526 | struct stm_device *stm = stmf->stm; |
| 527 | unsigned long size, phys; |
| 528 | |
| 529 | if (!stm->data->mmio_addr) |
| 530 | return -EOPNOTSUPP; |
| 531 | |
| 532 | if (vma->vm_pgoff) |
| 533 | return -EINVAL; |
| 534 | |
| 535 | size = vma->vm_end - vma->vm_start; |
| 536 | |
| 537 | if (stmf->output.nr_chans * stm->data->sw_mmiosz != size) |
| 538 | return -EINVAL; |
| 539 | |
| 540 | phys = stm->data->mmio_addr(stm->data, stmf->output.master, |
| 541 | stmf->output.channel, |
| 542 | stmf->output.nr_chans); |
| 543 | |
| 544 | if (!phys) |
| 545 | return -EINVAL; |
| 546 | |
Alexander Shishkin | 8e0469a | 2016-06-28 11:35:02 +0300 | [diff] [blame] | 547 | pm_runtime_get_sync(&stm->dev); |
| 548 | |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 549 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 550 | vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP; |
Alexander Shishkin | 8e0469a | 2016-06-28 11:35:02 +0300 | [diff] [blame] | 551 | vma->vm_ops = &stm_mmap_vmops; |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 552 | vm_iomap_memory(vma, phys, size); |
| 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg) |
| 558 | { |
| 559 | struct stm_device *stm = stmf->stm; |
| 560 | struct stp_policy_id *id; |
| 561 | int ret = -EINVAL; |
| 562 | u32 size; |
| 563 | |
| 564 | if (stmf->output.nr_chans) |
| 565 | return -EBUSY; |
| 566 | |
| 567 | if (copy_from_user(&size, arg, sizeof(size))) |
| 568 | return -EFAULT; |
| 569 | |
| 570 | if (size >= PATH_MAX + sizeof(*id)) |
| 571 | return -EINVAL; |
| 572 | |
| 573 | /* |
| 574 | * size + 1 to make sure the .id string at the bottom is terminated, |
| 575 | * which is also why memdup_user() is not useful here |
| 576 | */ |
| 577 | id = kzalloc(size + 1, GFP_KERNEL); |
| 578 | if (!id) |
| 579 | return -ENOMEM; |
| 580 | |
| 581 | if (copy_from_user(id, arg, size)) { |
| 582 | ret = -EFAULT; |
| 583 | goto err_free; |
| 584 | } |
| 585 | |
| 586 | if (id->__reserved_0 || id->__reserved_1) |
| 587 | goto err_free; |
| 588 | |
| 589 | if (id->width < 1 || |
| 590 | id->width > PAGE_SIZE / stm->data->sw_mmiosz) |
| 591 | goto err_free; |
| 592 | |
| 593 | ret = stm_file_assign(stmf, id->id, id->width); |
| 594 | if (ret) |
| 595 | goto err_free; |
| 596 | |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 597 | if (stm->data->link) |
| 598 | ret = stm->data->link(stm->data, stmf->output.master, |
| 599 | stmf->output.channel); |
| 600 | |
Alexander Shishkin | f7c81c7 | 2016-02-15 19:12:07 +0200 | [diff] [blame] | 601 | if (ret) |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 602 | stm_output_free(stmf->stm, &stmf->output); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 603 | |
| 604 | err_free: |
| 605 | kfree(id); |
| 606 | |
| 607 | return ret; |
| 608 | } |
| 609 | |
| 610 | static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg) |
| 611 | { |
| 612 | struct stp_policy_id id = { |
| 613 | .size = sizeof(id), |
| 614 | .master = stmf->output.master, |
| 615 | .channel = stmf->output.channel, |
| 616 | .width = stmf->output.nr_chans, |
| 617 | .__reserved_0 = 0, |
| 618 | .__reserved_1 = 0, |
| 619 | }; |
| 620 | |
| 621 | return copy_to_user(arg, &id, id.size) ? -EFAULT : 0; |
| 622 | } |
| 623 | |
| 624 | static long |
| 625 | stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 626 | { |
| 627 | struct stm_file *stmf = file->private_data; |
| 628 | struct stm_data *stm_data = stmf->stm->data; |
| 629 | int err = -ENOTTY; |
| 630 | u64 options; |
| 631 | |
| 632 | switch (cmd) { |
| 633 | case STP_POLICY_ID_SET: |
| 634 | err = stm_char_policy_set_ioctl(stmf, (void __user *)arg); |
| 635 | if (err) |
| 636 | return err; |
| 637 | |
| 638 | return stm_char_policy_get_ioctl(stmf, (void __user *)arg); |
| 639 | |
| 640 | case STP_POLICY_ID_GET: |
| 641 | return stm_char_policy_get_ioctl(stmf, (void __user *)arg); |
| 642 | |
| 643 | case STP_SET_OPTIONS: |
| 644 | if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64))) |
| 645 | return -EFAULT; |
| 646 | |
| 647 | if (stm_data->set_options) |
| 648 | err = stm_data->set_options(stm_data, |
| 649 | stmf->output.master, |
| 650 | stmf->output.channel, |
| 651 | stmf->output.nr_chans, |
| 652 | options); |
| 653 | |
| 654 | break; |
| 655 | default: |
| 656 | break; |
| 657 | } |
| 658 | |
| 659 | return err; |
| 660 | } |
| 661 | |
| 662 | #ifdef CONFIG_COMPAT |
| 663 | static long |
| 664 | stm_char_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 665 | { |
| 666 | return stm_char_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
| 667 | } |
| 668 | #else |
| 669 | #define stm_char_compat_ioctl NULL |
| 670 | #endif |
| 671 | |
| 672 | static const struct file_operations stm_fops = { |
| 673 | .open = stm_char_open, |
| 674 | .release = stm_char_release, |
| 675 | .write = stm_char_write, |
| 676 | .mmap = stm_char_mmap, |
| 677 | .unlocked_ioctl = stm_char_ioctl, |
| 678 | .compat_ioctl = stm_char_compat_ioctl, |
| 679 | .llseek = no_llseek, |
| 680 | }; |
| 681 | |
| 682 | static void stm_device_release(struct device *dev) |
| 683 | { |
| 684 | struct stm_device *stm = to_stm_device(dev); |
| 685 | |
Alexander Shishkin | 9943470 | 2018-05-24 11:27:26 +0300 | [diff] [blame] | 686 | vfree(stm); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | int stm_register_device(struct device *parent, struct stm_data *stm_data, |
| 690 | struct module *owner) |
| 691 | { |
| 692 | struct stm_device *stm; |
| 693 | unsigned int nmasters; |
| 694 | int err = -ENOMEM; |
| 695 | |
| 696 | if (!stm_core_up) |
| 697 | return -EPROBE_DEFER; |
| 698 | |
| 699 | if (!stm_data->packet || !stm_data->sw_nchannels) |
| 700 | return -EINVAL; |
| 701 | |
Chunyan Zhang | 7b3bb0e | 2015-12-22 17:25:20 +0200 | [diff] [blame] | 702 | nmasters = stm_data->sw_end - stm_data->sw_start + 1; |
Alexander Shishkin | 9943470 | 2018-05-24 11:27:26 +0300 | [diff] [blame] | 703 | stm = vzalloc(sizeof(*stm) + nmasters * sizeof(void *)); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 704 | if (!stm) |
| 705 | return -ENOMEM; |
| 706 | |
| 707 | stm->major = register_chrdev(0, stm_data->name, &stm_fops); |
| 708 | if (stm->major < 0) |
| 709 | goto err_free; |
| 710 | |
| 711 | device_initialize(&stm->dev); |
| 712 | stm->dev.devt = MKDEV(stm->major, 0); |
| 713 | stm->dev.class = &stm_class; |
| 714 | stm->dev.parent = parent; |
| 715 | stm->dev.release = stm_device_release; |
| 716 | |
Alexander Shishkin | 389b669 | 2016-03-04 16:48:14 +0200 | [diff] [blame] | 717 | mutex_init(&stm->link_mutex); |
| 718 | spin_lock_init(&stm->link_lock); |
| 719 | INIT_LIST_HEAD(&stm->link_list); |
| 720 | |
| 721 | /* initialize the object before it is accessible via sysfs */ |
| 722 | spin_lock_init(&stm->mc_lock); |
| 723 | mutex_init(&stm->policy_mutex); |
| 724 | stm->sw_nmasters = nmasters; |
| 725 | stm->owner = owner; |
| 726 | stm->data = stm_data; |
| 727 | stm_data->stm = stm; |
| 728 | |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 729 | err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name); |
| 730 | if (err) |
| 731 | goto err_device; |
| 732 | |
| 733 | err = device_add(&stm->dev); |
| 734 | if (err) |
| 735 | goto err_device; |
| 736 | |
Alexander Shishkin | 8e0469a | 2016-06-28 11:35:02 +0300 | [diff] [blame] | 737 | /* |
| 738 | * Use delayed autosuspend to avoid bouncing back and forth |
| 739 | * on recurring character device writes, with the initial |
| 740 | * delay time of 2 seconds. |
| 741 | */ |
| 742 | pm_runtime_no_callbacks(&stm->dev); |
| 743 | pm_runtime_use_autosuspend(&stm->dev); |
| 744 | pm_runtime_set_autosuspend_delay(&stm->dev, 2000); |
| 745 | pm_runtime_set_suspended(&stm->dev); |
| 746 | pm_runtime_enable(&stm->dev); |
| 747 | |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 748 | return 0; |
| 749 | |
| 750 | err_device: |
Alexander Shishkin | cbe4a61 | 2016-03-04 16:36:10 +0200 | [diff] [blame] | 751 | unregister_chrdev(stm->major, stm_data->name); |
| 752 | |
Alexander Shishkin | f7c81c7 | 2016-02-15 19:12:07 +0200 | [diff] [blame] | 753 | /* matches device_initialize() above */ |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 754 | put_device(&stm->dev); |
| 755 | err_free: |
Alexander Shishkin | 9943470 | 2018-05-24 11:27:26 +0300 | [diff] [blame] | 756 | vfree(stm); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 757 | |
| 758 | return err; |
| 759 | } |
| 760 | EXPORT_SYMBOL_GPL(stm_register_device); |
| 761 | |
Alexander Shishkin | b4ca34a | 2016-02-15 19:12:08 +0200 | [diff] [blame] | 762 | static int __stm_source_link_drop(struct stm_source_device *src, |
| 763 | struct stm_device *stm); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 764 | |
| 765 | void stm_unregister_device(struct stm_data *stm_data) |
| 766 | { |
| 767 | struct stm_device *stm = stm_data->stm; |
| 768 | struct stm_source_device *src, *iter; |
Alexander Shishkin | b4ca34a | 2016-02-15 19:12:08 +0200 | [diff] [blame] | 769 | int i, ret; |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 770 | |
Alexander Shishkin | 8e0469a | 2016-06-28 11:35:02 +0300 | [diff] [blame] | 771 | pm_runtime_dont_use_autosuspend(&stm->dev); |
| 772 | pm_runtime_disable(&stm->dev); |
| 773 | |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 774 | mutex_lock(&stm->link_mutex); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 775 | list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) { |
Alexander Shishkin | b4ca34a | 2016-02-15 19:12:08 +0200 | [diff] [blame] | 776 | ret = __stm_source_link_drop(src, stm); |
| 777 | /* |
| 778 | * src <-> stm link must not change under the same |
| 779 | * stm::link_mutex, so complain loudly if it has; |
| 780 | * also in this situation ret!=0 means this src is |
| 781 | * not connected to this stm and it should be otherwise |
| 782 | * safe to proceed with the tear-down of stm. |
| 783 | */ |
| 784 | WARN_ON_ONCE(ret); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 785 | } |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 786 | mutex_unlock(&stm->link_mutex); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 787 | |
| 788 | synchronize_srcu(&stm_source_srcu); |
| 789 | |
| 790 | unregister_chrdev(stm->major, stm_data->name); |
| 791 | |
| 792 | mutex_lock(&stm->policy_mutex); |
| 793 | if (stm->policy) |
| 794 | stp_policy_unbind(stm->policy); |
| 795 | mutex_unlock(&stm->policy_mutex); |
| 796 | |
Chunyan Zhang | 73a3ed1 | 2016-02-15 19:11:52 +0200 | [diff] [blame] | 797 | for (i = stm->data->sw_start; i <= stm->data->sw_end; i++) |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 798 | stp_master_free(stm, i); |
| 799 | |
| 800 | device_unregister(&stm->dev); |
| 801 | stm_data->stm = NULL; |
| 802 | } |
| 803 | EXPORT_SYMBOL_GPL(stm_unregister_device); |
| 804 | |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 805 | /* |
| 806 | * stm::link_list access serialization uses a spinlock and a mutex; holding |
| 807 | * either of them guarantees that the list is stable; modification requires |
| 808 | * holding both of them. |
| 809 | * |
| 810 | * Lock ordering is as follows: |
| 811 | * stm::link_mutex |
| 812 | * stm::link_lock |
| 813 | * src::link_lock |
| 814 | */ |
| 815 | |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 816 | /** |
| 817 | * stm_source_link_add() - connect an stm_source device to an stm device |
| 818 | * @src: stm_source device |
| 819 | * @stm: stm device |
| 820 | * |
| 821 | * This function establishes a link from stm_source to an stm device so that |
| 822 | * the former can send out trace data to the latter. |
| 823 | * |
| 824 | * Return: 0 on success, -errno otherwise. |
| 825 | */ |
| 826 | static int stm_source_link_add(struct stm_source_device *src, |
| 827 | struct stm_device *stm) |
| 828 | { |
| 829 | char *id; |
| 830 | int err; |
| 831 | |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 832 | mutex_lock(&stm->link_mutex); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 833 | spin_lock(&stm->link_lock); |
| 834 | spin_lock(&src->link_lock); |
| 835 | |
| 836 | /* src->link is dereferenced under stm_source_srcu but not the list */ |
| 837 | rcu_assign_pointer(src->link, stm); |
| 838 | list_add_tail(&src->link_entry, &stm->link_list); |
| 839 | |
| 840 | spin_unlock(&src->link_lock); |
| 841 | spin_unlock(&stm->link_lock); |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 842 | mutex_unlock(&stm->link_mutex); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 843 | |
| 844 | id = kstrdup(src->data->name, GFP_KERNEL); |
| 845 | if (id) { |
| 846 | src->policy_node = |
| 847 | stp_policy_node_lookup(stm, id); |
| 848 | |
| 849 | kfree(id); |
| 850 | } |
| 851 | |
| 852 | err = stm_output_assign(stm, src->data->nr_chans, |
| 853 | src->policy_node, &src->output); |
| 854 | |
| 855 | if (src->policy_node) |
| 856 | stp_policy_node_put(src->policy_node); |
| 857 | |
| 858 | if (err) |
| 859 | goto fail_detach; |
| 860 | |
| 861 | /* this is to notify the STM device that a new link has been made */ |
| 862 | if (stm->data->link) |
| 863 | err = stm->data->link(stm->data, src->output.master, |
| 864 | src->output.channel); |
| 865 | |
| 866 | if (err) |
| 867 | goto fail_free_output; |
| 868 | |
| 869 | /* this is to let the source carry out all necessary preparations */ |
| 870 | if (src->data->link) |
| 871 | src->data->link(src->data); |
| 872 | |
| 873 | return 0; |
| 874 | |
| 875 | fail_free_output: |
| 876 | stm_output_free(stm, &src->output); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 877 | |
| 878 | fail_detach: |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 879 | mutex_lock(&stm->link_mutex); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 880 | spin_lock(&stm->link_lock); |
| 881 | spin_lock(&src->link_lock); |
| 882 | |
| 883 | rcu_assign_pointer(src->link, NULL); |
| 884 | list_del_init(&src->link_entry); |
| 885 | |
| 886 | spin_unlock(&src->link_lock); |
| 887 | spin_unlock(&stm->link_lock); |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 888 | mutex_unlock(&stm->link_mutex); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 889 | |
| 890 | return err; |
| 891 | } |
| 892 | |
| 893 | /** |
| 894 | * __stm_source_link_drop() - detach stm_source from an stm device |
| 895 | * @src: stm_source device |
| 896 | * @stm: stm device |
| 897 | * |
| 898 | * If @stm is @src::link, disconnect them from one another and put the |
| 899 | * reference on the @stm device. |
| 900 | * |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 901 | * Caller must hold stm::link_mutex. |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 902 | */ |
Alexander Shishkin | b4ca34a | 2016-02-15 19:12:08 +0200 | [diff] [blame] | 903 | static int __stm_source_link_drop(struct stm_source_device *src, |
| 904 | struct stm_device *stm) |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 905 | { |
Alexander Shishkin | 0df771d | 2015-10-06 12:47:17 +0300 | [diff] [blame] | 906 | struct stm_device *link; |
Alexander Shishkin | b4ca34a | 2016-02-15 19:12:08 +0200 | [diff] [blame] | 907 | int ret = 0; |
Alexander Shishkin | 0df771d | 2015-10-06 12:47:17 +0300 | [diff] [blame] | 908 | |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 909 | lockdep_assert_held(&stm->link_mutex); |
| 910 | |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 911 | /* for stm::link_list modification, we hold both mutex and spinlock */ |
| 912 | spin_lock(&stm->link_lock); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 913 | spin_lock(&src->link_lock); |
Alexander Shishkin | 0df771d | 2015-10-06 12:47:17 +0300 | [diff] [blame] | 914 | link = srcu_dereference_check(src->link, &stm_source_srcu, 1); |
Alexander Shishkin | b4ca34a | 2016-02-15 19:12:08 +0200 | [diff] [blame] | 915 | |
| 916 | /* |
| 917 | * The linked device may have changed since we last looked, because |
| 918 | * we weren't holding the src::link_lock back then; if this is the |
| 919 | * case, tell the caller to retry. |
| 920 | */ |
| 921 | if (link != stm) { |
| 922 | ret = -EAGAIN; |
Alexander Shishkin | 1810f2c | 2016-02-15 19:12:05 +0200 | [diff] [blame] | 923 | goto unlock; |
Alexander Shishkin | b4ca34a | 2016-02-15 19:12:08 +0200 | [diff] [blame] | 924 | } |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 925 | |
Alexander Shishkin | 0df771d | 2015-10-06 12:47:17 +0300 | [diff] [blame] | 926 | stm_output_free(link, &src->output); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 927 | list_del_init(&src->link_entry); |
Alexander Shishkin | 8e0469a | 2016-06-28 11:35:02 +0300 | [diff] [blame] | 928 | pm_runtime_mark_last_busy(&link->dev); |
| 929 | pm_runtime_put_autosuspend(&link->dev); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 930 | /* matches stm_find_device() from stm_source_link_store() */ |
Alexander Shishkin | 0df771d | 2015-10-06 12:47:17 +0300 | [diff] [blame] | 931 | stm_put_device(link); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 932 | rcu_assign_pointer(src->link, NULL); |
| 933 | |
Alexander Shishkin | 1810f2c | 2016-02-15 19:12:05 +0200 | [diff] [blame] | 934 | unlock: |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 935 | spin_unlock(&src->link_lock); |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 936 | spin_unlock(&stm->link_lock); |
Alexander Shishkin | b4ca34a | 2016-02-15 19:12:08 +0200 | [diff] [blame] | 937 | |
Alexander Shishkin | cc84240 | 2016-02-15 19:12:09 +0200 | [diff] [blame] | 938 | /* |
| 939 | * Call the unlink callbacks for both source and stm, when we know |
| 940 | * that we have actually performed the unlinking. |
| 941 | */ |
| 942 | if (!ret) { |
| 943 | if (src->data->unlink) |
| 944 | src->data->unlink(src->data); |
| 945 | |
| 946 | if (stm->data->unlink) |
| 947 | stm->data->unlink(stm->data, src->output.master, |
| 948 | src->output.channel); |
| 949 | } |
Alexander Shishkin | b4ca34a | 2016-02-15 19:12:08 +0200 | [diff] [blame] | 950 | |
| 951 | return ret; |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | /** |
| 955 | * stm_source_link_drop() - detach stm_source from its stm device |
| 956 | * @src: stm_source device |
| 957 | * |
| 958 | * Unlinking means disconnecting from source's STM device; after this |
| 959 | * writes will be unsuccessful until it is linked to a new STM device. |
| 960 | * |
| 961 | * This will happen on "stm_source_link" sysfs attribute write to undo |
| 962 | * the existing link (if any), or on linked STM device's de-registration. |
| 963 | */ |
| 964 | static void stm_source_link_drop(struct stm_source_device *src) |
| 965 | { |
| 966 | struct stm_device *stm; |
Alexander Shishkin | b4ca34a | 2016-02-15 19:12:08 +0200 | [diff] [blame] | 967 | int idx, ret; |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 968 | |
Alexander Shishkin | b4ca34a | 2016-02-15 19:12:08 +0200 | [diff] [blame] | 969 | retry: |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 970 | idx = srcu_read_lock(&stm_source_srcu); |
Alexander Shishkin | b4ca34a | 2016-02-15 19:12:08 +0200 | [diff] [blame] | 971 | /* |
| 972 | * The stm device will be valid for the duration of this |
| 973 | * read section, but the link may change before we grab |
| 974 | * the src::link_lock in __stm_source_link_drop(). |
| 975 | */ |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 976 | stm = srcu_dereference(src->link, &stm_source_srcu); |
| 977 | |
Alexander Shishkin | b4ca34a | 2016-02-15 19:12:08 +0200 | [diff] [blame] | 978 | ret = 0; |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 979 | if (stm) { |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 980 | mutex_lock(&stm->link_mutex); |
Alexander Shishkin | b4ca34a | 2016-02-15 19:12:08 +0200 | [diff] [blame] | 981 | ret = __stm_source_link_drop(src, stm); |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 982 | mutex_unlock(&stm->link_mutex); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | srcu_read_unlock(&stm_source_srcu, idx); |
Alexander Shishkin | b4ca34a | 2016-02-15 19:12:08 +0200 | [diff] [blame] | 986 | |
| 987 | /* if it did change, retry */ |
| 988 | if (ret == -EAGAIN) |
| 989 | goto retry; |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | static ssize_t stm_source_link_show(struct device *dev, |
| 993 | struct device_attribute *attr, |
| 994 | char *buf) |
| 995 | { |
| 996 | struct stm_source_device *src = to_stm_source_device(dev); |
| 997 | struct stm_device *stm; |
| 998 | int idx, ret; |
| 999 | |
| 1000 | idx = srcu_read_lock(&stm_source_srcu); |
| 1001 | stm = srcu_dereference(src->link, &stm_source_srcu); |
| 1002 | ret = sprintf(buf, "%s\n", |
| 1003 | stm ? dev_name(&stm->dev) : "<none>"); |
| 1004 | srcu_read_unlock(&stm_source_srcu, idx); |
| 1005 | |
| 1006 | return ret; |
| 1007 | } |
| 1008 | |
| 1009 | static ssize_t stm_source_link_store(struct device *dev, |
| 1010 | struct device_attribute *attr, |
| 1011 | const char *buf, size_t count) |
| 1012 | { |
| 1013 | struct stm_source_device *src = to_stm_source_device(dev); |
| 1014 | struct stm_device *link; |
| 1015 | int err; |
| 1016 | |
| 1017 | stm_source_link_drop(src); |
| 1018 | |
| 1019 | link = stm_find_device(buf); |
| 1020 | if (!link) |
| 1021 | return -EINVAL; |
| 1022 | |
Alexander Shishkin | 8e0469a | 2016-06-28 11:35:02 +0300 | [diff] [blame] | 1023 | pm_runtime_get(&link->dev); |
| 1024 | |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 1025 | err = stm_source_link_add(src, link); |
Alexander Shishkin | f7c81c7 | 2016-02-15 19:12:07 +0200 | [diff] [blame] | 1026 | if (err) { |
Alexander Shishkin | 8e0469a | 2016-06-28 11:35:02 +0300 | [diff] [blame] | 1027 | pm_runtime_put_autosuspend(&link->dev); |
Alexander Shishkin | f7c81c7 | 2016-02-15 19:12:07 +0200 | [diff] [blame] | 1028 | /* matches the stm_find_device() above */ |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 1029 | stm_put_device(link); |
Alexander Shishkin | f7c81c7 | 2016-02-15 19:12:07 +0200 | [diff] [blame] | 1030 | } |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 1031 | |
| 1032 | return err ? : count; |
| 1033 | } |
| 1034 | |
| 1035 | static DEVICE_ATTR_RW(stm_source_link); |
| 1036 | |
| 1037 | static struct attribute *stm_source_attrs[] = { |
| 1038 | &dev_attr_stm_source_link.attr, |
| 1039 | NULL, |
| 1040 | }; |
| 1041 | |
| 1042 | ATTRIBUTE_GROUPS(stm_source); |
| 1043 | |
| 1044 | static struct class stm_source_class = { |
| 1045 | .name = "stm_source", |
| 1046 | .dev_groups = stm_source_groups, |
| 1047 | }; |
| 1048 | |
| 1049 | static void stm_source_device_release(struct device *dev) |
| 1050 | { |
| 1051 | struct stm_source_device *src = to_stm_source_device(dev); |
| 1052 | |
| 1053 | kfree(src); |
| 1054 | } |
| 1055 | |
| 1056 | /** |
| 1057 | * stm_source_register_device() - register an stm_source device |
| 1058 | * @parent: parent device |
| 1059 | * @data: device description structure |
| 1060 | * |
| 1061 | * This will create a device of stm_source class that can write |
| 1062 | * data to an stm device once linked. |
| 1063 | * |
| 1064 | * Return: 0 on success, -errno otherwise. |
| 1065 | */ |
| 1066 | int stm_source_register_device(struct device *parent, |
| 1067 | struct stm_source_data *data) |
| 1068 | { |
| 1069 | struct stm_source_device *src; |
| 1070 | int err; |
| 1071 | |
| 1072 | if (!stm_core_up) |
| 1073 | return -EPROBE_DEFER; |
| 1074 | |
| 1075 | src = kzalloc(sizeof(*src), GFP_KERNEL); |
| 1076 | if (!src) |
| 1077 | return -ENOMEM; |
| 1078 | |
| 1079 | device_initialize(&src->dev); |
| 1080 | src->dev.class = &stm_source_class; |
| 1081 | src->dev.parent = parent; |
| 1082 | src->dev.release = stm_source_device_release; |
| 1083 | |
| 1084 | err = kobject_set_name(&src->dev.kobj, "%s", data->name); |
| 1085 | if (err) |
| 1086 | goto err; |
| 1087 | |
Alexander Shishkin | 8e0469a | 2016-06-28 11:35:02 +0300 | [diff] [blame] | 1088 | pm_runtime_no_callbacks(&src->dev); |
| 1089 | pm_runtime_forbid(&src->dev); |
| 1090 | |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 1091 | err = device_add(&src->dev); |
| 1092 | if (err) |
| 1093 | goto err; |
| 1094 | |
Alexander Shishkin | cde4ad8 | 2016-02-15 19:12:06 +0200 | [diff] [blame] | 1095 | stm_output_init(&src->output); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 1096 | spin_lock_init(&src->link_lock); |
| 1097 | INIT_LIST_HEAD(&src->link_entry); |
| 1098 | src->data = data; |
| 1099 | data->src = src; |
| 1100 | |
| 1101 | return 0; |
| 1102 | |
| 1103 | err: |
| 1104 | put_device(&src->dev); |
| 1105 | kfree(src); |
| 1106 | |
| 1107 | return err; |
| 1108 | } |
| 1109 | EXPORT_SYMBOL_GPL(stm_source_register_device); |
| 1110 | |
| 1111 | /** |
| 1112 | * stm_source_unregister_device() - unregister an stm_source device |
| 1113 | * @data: device description that was used to register the device |
| 1114 | * |
| 1115 | * This will remove a previously created stm_source device from the system. |
| 1116 | */ |
| 1117 | void stm_source_unregister_device(struct stm_source_data *data) |
| 1118 | { |
| 1119 | struct stm_source_device *src = data->src; |
| 1120 | |
| 1121 | stm_source_link_drop(src); |
| 1122 | |
Alexander Shishkin | 3ff8bc8 | 2017-09-19 18:47:40 +0300 | [diff] [blame] | 1123 | device_unregister(&src->dev); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 1124 | } |
| 1125 | EXPORT_SYMBOL_GPL(stm_source_unregister_device); |
| 1126 | |
| 1127 | int stm_source_write(struct stm_source_data *data, unsigned int chan, |
| 1128 | const char *buf, size_t count) |
| 1129 | { |
| 1130 | struct stm_source_device *src = data->src; |
| 1131 | struct stm_device *stm; |
| 1132 | int idx; |
| 1133 | |
| 1134 | if (!src->output.nr_chans) |
| 1135 | return -ENODEV; |
| 1136 | |
| 1137 | if (chan >= src->output.nr_chans) |
| 1138 | return -EINVAL; |
| 1139 | |
| 1140 | idx = srcu_read_lock(&stm_source_srcu); |
| 1141 | |
| 1142 | stm = srcu_dereference(src->link, &stm_source_srcu); |
| 1143 | if (stm) |
Alexander Shishkin | f8560a9 | 2016-02-15 19:12:01 +0200 | [diff] [blame] | 1144 | count = stm_write(stm->data, src->output.master, |
| 1145 | src->output.channel + chan, |
| 1146 | buf, count); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 1147 | else |
| 1148 | count = -ENODEV; |
| 1149 | |
| 1150 | srcu_read_unlock(&stm_source_srcu, idx); |
| 1151 | |
| 1152 | return count; |
| 1153 | } |
| 1154 | EXPORT_SYMBOL_GPL(stm_source_write); |
| 1155 | |
| 1156 | static int __init stm_core_init(void) |
| 1157 | { |
| 1158 | int err; |
| 1159 | |
| 1160 | err = class_register(&stm_class); |
| 1161 | if (err) |
| 1162 | return err; |
| 1163 | |
| 1164 | err = class_register(&stm_source_class); |
| 1165 | if (err) |
| 1166 | goto err_stm; |
| 1167 | |
| 1168 | err = stp_configfs_init(); |
| 1169 | if (err) |
| 1170 | goto err_src; |
| 1171 | |
| 1172 | init_srcu_struct(&stm_source_srcu); |
| 1173 | |
| 1174 | stm_core_up++; |
| 1175 | |
| 1176 | return 0; |
| 1177 | |
| 1178 | err_src: |
| 1179 | class_unregister(&stm_source_class); |
| 1180 | err_stm: |
| 1181 | class_unregister(&stm_class); |
| 1182 | |
| 1183 | return err; |
| 1184 | } |
| 1185 | |
| 1186 | module_init(stm_core_init); |
| 1187 | |
| 1188 | static void __exit stm_core_exit(void) |
| 1189 | { |
| 1190 | cleanup_srcu_struct(&stm_source_srcu); |
| 1191 | class_unregister(&stm_source_class); |
| 1192 | class_unregister(&stm_class); |
| 1193 | stp_configfs_exit(); |
| 1194 | } |
| 1195 | |
| 1196 | module_exit(stm_core_exit); |
| 1197 | |
| 1198 | MODULE_LICENSE("GPL v2"); |
| 1199 | MODULE_DESCRIPTION("System Trace Module device class"); |
| 1200 | MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>"); |