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