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 | |
| 383 | static void stm_write(struct stm_data *data, unsigned int master, |
| 384 | unsigned int channel, const char *buf, size_t count) |
| 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; |
| 396 | } |
| 397 | |
| 398 | data->packet(data, master, channel, STP_PACKET_FLAG, 0, 0, &nil); |
| 399 | } |
| 400 | |
| 401 | static ssize_t stm_char_write(struct file *file, const char __user *buf, |
| 402 | size_t count, loff_t *ppos) |
| 403 | { |
| 404 | struct stm_file *stmf = file->private_data; |
| 405 | struct stm_device *stm = stmf->stm; |
| 406 | char *kbuf; |
| 407 | int err; |
| 408 | |
Alexander Shishkin | f08b182 | 2015-12-22 17:25:21 +0200 | [diff] [blame] | 409 | if (count + 1 > PAGE_SIZE) |
| 410 | count = PAGE_SIZE - 1; |
| 411 | |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 412 | /* |
| 413 | * if no m/c have been assigned to this writer up to this |
| 414 | * point, use "default" policy entry |
| 415 | */ |
| 416 | if (!stmf->output.nr_chans) { |
| 417 | err = stm_file_assign(stmf, "default", 1); |
| 418 | /* |
| 419 | * EBUSY means that somebody else just assigned this |
| 420 | * output, which is just fine for write() |
| 421 | */ |
| 422 | if (err && err != -EBUSY) |
| 423 | return err; |
| 424 | } |
| 425 | |
| 426 | kbuf = kmalloc(count + 1, GFP_KERNEL); |
| 427 | if (!kbuf) |
| 428 | return -ENOMEM; |
| 429 | |
| 430 | err = copy_from_user(kbuf, buf, count); |
| 431 | if (err) { |
| 432 | kfree(kbuf); |
| 433 | return -EFAULT; |
| 434 | } |
| 435 | |
| 436 | stm_write(stm->data, stmf->output.master, stmf->output.channel, kbuf, |
| 437 | count); |
| 438 | |
| 439 | kfree(kbuf); |
| 440 | |
| 441 | return count; |
| 442 | } |
| 443 | |
| 444 | static int stm_char_mmap(struct file *file, struct vm_area_struct *vma) |
| 445 | { |
| 446 | struct stm_file *stmf = file->private_data; |
| 447 | struct stm_device *stm = stmf->stm; |
| 448 | unsigned long size, phys; |
| 449 | |
| 450 | if (!stm->data->mmio_addr) |
| 451 | return -EOPNOTSUPP; |
| 452 | |
| 453 | if (vma->vm_pgoff) |
| 454 | return -EINVAL; |
| 455 | |
| 456 | size = vma->vm_end - vma->vm_start; |
| 457 | |
| 458 | if (stmf->output.nr_chans * stm->data->sw_mmiosz != size) |
| 459 | return -EINVAL; |
| 460 | |
| 461 | phys = stm->data->mmio_addr(stm->data, stmf->output.master, |
| 462 | stmf->output.channel, |
| 463 | stmf->output.nr_chans); |
| 464 | |
| 465 | if (!phys) |
| 466 | return -EINVAL; |
| 467 | |
| 468 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 469 | vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP; |
| 470 | vm_iomap_memory(vma, phys, size); |
| 471 | |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg) |
| 476 | { |
| 477 | struct stm_device *stm = stmf->stm; |
| 478 | struct stp_policy_id *id; |
| 479 | int ret = -EINVAL; |
| 480 | u32 size; |
| 481 | |
| 482 | if (stmf->output.nr_chans) |
| 483 | return -EBUSY; |
| 484 | |
| 485 | if (copy_from_user(&size, arg, sizeof(size))) |
| 486 | return -EFAULT; |
| 487 | |
| 488 | if (size >= PATH_MAX + sizeof(*id)) |
| 489 | return -EINVAL; |
| 490 | |
| 491 | /* |
| 492 | * size + 1 to make sure the .id string at the bottom is terminated, |
| 493 | * which is also why memdup_user() is not useful here |
| 494 | */ |
| 495 | id = kzalloc(size + 1, GFP_KERNEL); |
| 496 | if (!id) |
| 497 | return -ENOMEM; |
| 498 | |
| 499 | if (copy_from_user(id, arg, size)) { |
| 500 | ret = -EFAULT; |
| 501 | goto err_free; |
| 502 | } |
| 503 | |
| 504 | if (id->__reserved_0 || id->__reserved_1) |
| 505 | goto err_free; |
| 506 | |
| 507 | if (id->width < 1 || |
| 508 | id->width > PAGE_SIZE / stm->data->sw_mmiosz) |
| 509 | goto err_free; |
| 510 | |
| 511 | ret = stm_file_assign(stmf, id->id, id->width); |
| 512 | if (ret) |
| 513 | goto err_free; |
| 514 | |
| 515 | ret = 0; |
| 516 | |
| 517 | if (stm->data->link) |
| 518 | ret = stm->data->link(stm->data, stmf->output.master, |
| 519 | stmf->output.channel); |
| 520 | |
| 521 | if (ret) { |
| 522 | stm_output_free(stmf->stm, &stmf->output); |
| 523 | stm_put_device(stmf->stm); |
| 524 | } |
| 525 | |
| 526 | err_free: |
| 527 | kfree(id); |
| 528 | |
| 529 | return ret; |
| 530 | } |
| 531 | |
| 532 | static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg) |
| 533 | { |
| 534 | struct stp_policy_id id = { |
| 535 | .size = sizeof(id), |
| 536 | .master = stmf->output.master, |
| 537 | .channel = stmf->output.channel, |
| 538 | .width = stmf->output.nr_chans, |
| 539 | .__reserved_0 = 0, |
| 540 | .__reserved_1 = 0, |
| 541 | }; |
| 542 | |
| 543 | return copy_to_user(arg, &id, id.size) ? -EFAULT : 0; |
| 544 | } |
| 545 | |
| 546 | static long |
| 547 | stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 548 | { |
| 549 | struct stm_file *stmf = file->private_data; |
| 550 | struct stm_data *stm_data = stmf->stm->data; |
| 551 | int err = -ENOTTY; |
| 552 | u64 options; |
| 553 | |
| 554 | switch (cmd) { |
| 555 | case STP_POLICY_ID_SET: |
| 556 | err = stm_char_policy_set_ioctl(stmf, (void __user *)arg); |
| 557 | if (err) |
| 558 | return err; |
| 559 | |
| 560 | return stm_char_policy_get_ioctl(stmf, (void __user *)arg); |
| 561 | |
| 562 | case STP_POLICY_ID_GET: |
| 563 | return stm_char_policy_get_ioctl(stmf, (void __user *)arg); |
| 564 | |
| 565 | case STP_SET_OPTIONS: |
| 566 | if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64))) |
| 567 | return -EFAULT; |
| 568 | |
| 569 | if (stm_data->set_options) |
| 570 | err = stm_data->set_options(stm_data, |
| 571 | stmf->output.master, |
| 572 | stmf->output.channel, |
| 573 | stmf->output.nr_chans, |
| 574 | options); |
| 575 | |
| 576 | break; |
| 577 | default: |
| 578 | break; |
| 579 | } |
| 580 | |
| 581 | return err; |
| 582 | } |
| 583 | |
| 584 | #ifdef CONFIG_COMPAT |
| 585 | static long |
| 586 | stm_char_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 587 | { |
| 588 | return stm_char_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
| 589 | } |
| 590 | #else |
| 591 | #define stm_char_compat_ioctl NULL |
| 592 | #endif |
| 593 | |
| 594 | static const struct file_operations stm_fops = { |
| 595 | .open = stm_char_open, |
| 596 | .release = stm_char_release, |
| 597 | .write = stm_char_write, |
| 598 | .mmap = stm_char_mmap, |
| 599 | .unlocked_ioctl = stm_char_ioctl, |
| 600 | .compat_ioctl = stm_char_compat_ioctl, |
| 601 | .llseek = no_llseek, |
| 602 | }; |
| 603 | |
| 604 | static void stm_device_release(struct device *dev) |
| 605 | { |
| 606 | struct stm_device *stm = to_stm_device(dev); |
| 607 | |
| 608 | kfree(stm); |
| 609 | } |
| 610 | |
| 611 | int stm_register_device(struct device *parent, struct stm_data *stm_data, |
| 612 | struct module *owner) |
| 613 | { |
| 614 | struct stm_device *stm; |
| 615 | unsigned int nmasters; |
| 616 | int err = -ENOMEM; |
| 617 | |
| 618 | if (!stm_core_up) |
| 619 | return -EPROBE_DEFER; |
| 620 | |
| 621 | if (!stm_data->packet || !stm_data->sw_nchannels) |
| 622 | return -EINVAL; |
| 623 | |
Chunyan Zhang | 7b3bb0e | 2015-12-22 17:25:20 +0200 | [diff] [blame] | 624 | nmasters = stm_data->sw_end - stm_data->sw_start + 1; |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 625 | stm = kzalloc(sizeof(*stm) + nmasters * sizeof(void *), GFP_KERNEL); |
| 626 | if (!stm) |
| 627 | return -ENOMEM; |
| 628 | |
| 629 | stm->major = register_chrdev(0, stm_data->name, &stm_fops); |
| 630 | if (stm->major < 0) |
| 631 | goto err_free; |
| 632 | |
| 633 | device_initialize(&stm->dev); |
| 634 | stm->dev.devt = MKDEV(stm->major, 0); |
| 635 | stm->dev.class = &stm_class; |
| 636 | stm->dev.parent = parent; |
| 637 | stm->dev.release = stm_device_release; |
| 638 | |
| 639 | err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name); |
| 640 | if (err) |
| 641 | goto err_device; |
| 642 | |
| 643 | err = device_add(&stm->dev); |
| 644 | if (err) |
| 645 | goto err_device; |
| 646 | |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 647 | mutex_init(&stm->link_mutex); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 648 | spin_lock_init(&stm->link_lock); |
| 649 | INIT_LIST_HEAD(&stm->link_list); |
| 650 | |
| 651 | spin_lock_init(&stm->mc_lock); |
| 652 | mutex_init(&stm->policy_mutex); |
| 653 | stm->sw_nmasters = nmasters; |
| 654 | stm->owner = owner; |
| 655 | stm->data = stm_data; |
| 656 | stm_data->stm = stm; |
| 657 | |
| 658 | return 0; |
| 659 | |
| 660 | err_device: |
| 661 | put_device(&stm->dev); |
| 662 | err_free: |
| 663 | kfree(stm); |
| 664 | |
| 665 | return err; |
| 666 | } |
| 667 | EXPORT_SYMBOL_GPL(stm_register_device); |
| 668 | |
| 669 | static void __stm_source_link_drop(struct stm_source_device *src, |
| 670 | struct stm_device *stm); |
| 671 | |
| 672 | void stm_unregister_device(struct stm_data *stm_data) |
| 673 | { |
| 674 | struct stm_device *stm = stm_data->stm; |
| 675 | struct stm_source_device *src, *iter; |
| 676 | int i; |
| 677 | |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 678 | mutex_lock(&stm->link_mutex); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 679 | list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) { |
| 680 | __stm_source_link_drop(src, stm); |
| 681 | } |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 682 | mutex_unlock(&stm->link_mutex); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 683 | |
| 684 | synchronize_srcu(&stm_source_srcu); |
| 685 | |
| 686 | unregister_chrdev(stm->major, stm_data->name); |
| 687 | |
| 688 | mutex_lock(&stm->policy_mutex); |
| 689 | if (stm->policy) |
| 690 | stp_policy_unbind(stm->policy); |
| 691 | mutex_unlock(&stm->policy_mutex); |
| 692 | |
Chunyan Zhang | 73a3ed1 | 2016-02-15 19:11:52 +0200 | [diff] [blame] | 693 | for (i = stm->data->sw_start; i <= stm->data->sw_end; i++) |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 694 | stp_master_free(stm, i); |
| 695 | |
| 696 | device_unregister(&stm->dev); |
| 697 | stm_data->stm = NULL; |
| 698 | } |
| 699 | EXPORT_SYMBOL_GPL(stm_unregister_device); |
| 700 | |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 701 | /* |
| 702 | * stm::link_list access serialization uses a spinlock and a mutex; holding |
| 703 | * either of them guarantees that the list is stable; modification requires |
| 704 | * holding both of them. |
| 705 | * |
| 706 | * Lock ordering is as follows: |
| 707 | * stm::link_mutex |
| 708 | * stm::link_lock |
| 709 | * src::link_lock |
| 710 | */ |
| 711 | |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 712 | /** |
| 713 | * stm_source_link_add() - connect an stm_source device to an stm device |
| 714 | * @src: stm_source device |
| 715 | * @stm: stm device |
| 716 | * |
| 717 | * This function establishes a link from stm_source to an stm device so that |
| 718 | * the former can send out trace data to the latter. |
| 719 | * |
| 720 | * Return: 0 on success, -errno otherwise. |
| 721 | */ |
| 722 | static int stm_source_link_add(struct stm_source_device *src, |
| 723 | struct stm_device *stm) |
| 724 | { |
| 725 | char *id; |
| 726 | int err; |
| 727 | |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 728 | mutex_lock(&stm->link_mutex); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 729 | spin_lock(&stm->link_lock); |
| 730 | spin_lock(&src->link_lock); |
| 731 | |
| 732 | /* src->link is dereferenced under stm_source_srcu but not the list */ |
| 733 | rcu_assign_pointer(src->link, stm); |
| 734 | list_add_tail(&src->link_entry, &stm->link_list); |
| 735 | |
| 736 | spin_unlock(&src->link_lock); |
| 737 | spin_unlock(&stm->link_lock); |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 738 | mutex_unlock(&stm->link_mutex); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 739 | |
| 740 | id = kstrdup(src->data->name, GFP_KERNEL); |
| 741 | if (id) { |
| 742 | src->policy_node = |
| 743 | stp_policy_node_lookup(stm, id); |
| 744 | |
| 745 | kfree(id); |
| 746 | } |
| 747 | |
| 748 | err = stm_output_assign(stm, src->data->nr_chans, |
| 749 | src->policy_node, &src->output); |
| 750 | |
| 751 | if (src->policy_node) |
| 752 | stp_policy_node_put(src->policy_node); |
| 753 | |
| 754 | if (err) |
| 755 | goto fail_detach; |
| 756 | |
| 757 | /* this is to notify the STM device that a new link has been made */ |
| 758 | if (stm->data->link) |
| 759 | err = stm->data->link(stm->data, src->output.master, |
| 760 | src->output.channel); |
| 761 | |
| 762 | if (err) |
| 763 | goto fail_free_output; |
| 764 | |
| 765 | /* this is to let the source carry out all necessary preparations */ |
| 766 | if (src->data->link) |
| 767 | src->data->link(src->data); |
| 768 | |
| 769 | return 0; |
| 770 | |
| 771 | fail_free_output: |
| 772 | stm_output_free(stm, &src->output); |
| 773 | stm_put_device(stm); |
| 774 | |
| 775 | fail_detach: |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 776 | mutex_lock(&stm->link_mutex); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 777 | spin_lock(&stm->link_lock); |
| 778 | spin_lock(&src->link_lock); |
| 779 | |
| 780 | rcu_assign_pointer(src->link, NULL); |
| 781 | list_del_init(&src->link_entry); |
| 782 | |
| 783 | spin_unlock(&src->link_lock); |
| 784 | spin_unlock(&stm->link_lock); |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 785 | mutex_unlock(&stm->link_mutex); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 786 | |
| 787 | return err; |
| 788 | } |
| 789 | |
| 790 | /** |
| 791 | * __stm_source_link_drop() - detach stm_source from an stm device |
| 792 | * @src: stm_source device |
| 793 | * @stm: stm device |
| 794 | * |
| 795 | * If @stm is @src::link, disconnect them from one another and put the |
| 796 | * reference on the @stm device. |
| 797 | * |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 798 | * Caller must hold stm::link_mutex. |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 799 | */ |
| 800 | static void __stm_source_link_drop(struct stm_source_device *src, |
| 801 | struct stm_device *stm) |
| 802 | { |
Alexander Shishkin | 0df771d | 2015-10-06 12:47:17 +0300 | [diff] [blame] | 803 | struct stm_device *link; |
| 804 | |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 805 | lockdep_assert_held(&stm->link_mutex); |
| 806 | |
| 807 | if (src->data->unlink) |
| 808 | src->data->unlink(src->data); |
| 809 | |
| 810 | /* for stm::link_list modification, we hold both mutex and spinlock */ |
| 811 | spin_lock(&stm->link_lock); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 812 | spin_lock(&src->link_lock); |
Alexander Shishkin | 0df771d | 2015-10-06 12:47:17 +0300 | [diff] [blame] | 813 | link = srcu_dereference_check(src->link, &stm_source_srcu, 1); |
| 814 | if (WARN_ON_ONCE(link != stm)) { |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 815 | spin_unlock(&src->link_lock); |
| 816 | return; |
| 817 | } |
| 818 | |
Alexander Shishkin | 0df771d | 2015-10-06 12:47:17 +0300 | [diff] [blame] | 819 | stm_output_free(link, &src->output); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 820 | list_del_init(&src->link_entry); |
| 821 | /* matches stm_find_device() from stm_source_link_store() */ |
Alexander Shishkin | 0df771d | 2015-10-06 12:47:17 +0300 | [diff] [blame] | 822 | stm_put_device(link); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 823 | rcu_assign_pointer(src->link, NULL); |
| 824 | |
| 825 | spin_unlock(&src->link_lock); |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 826 | spin_unlock(&stm->link_lock); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | /** |
| 830 | * stm_source_link_drop() - detach stm_source from its stm device |
| 831 | * @src: stm_source device |
| 832 | * |
| 833 | * Unlinking means disconnecting from source's STM device; after this |
| 834 | * writes will be unsuccessful until it is linked to a new STM device. |
| 835 | * |
| 836 | * This will happen on "stm_source_link" sysfs attribute write to undo |
| 837 | * the existing link (if any), or on linked STM device's de-registration. |
| 838 | */ |
| 839 | static void stm_source_link_drop(struct stm_source_device *src) |
| 840 | { |
| 841 | struct stm_device *stm; |
| 842 | int idx; |
| 843 | |
| 844 | idx = srcu_read_lock(&stm_source_srcu); |
| 845 | stm = srcu_dereference(src->link, &stm_source_srcu); |
| 846 | |
| 847 | if (stm) { |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 848 | mutex_lock(&stm->link_mutex); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 849 | __stm_source_link_drop(src, stm); |
Alexander Shishkin | c74f7e8 | 2015-12-22 17:25:19 +0200 | [diff] [blame] | 850 | mutex_unlock(&stm->link_mutex); |
Alexander Shishkin | 7bd1d40 | 2015-09-22 15:47:10 +0300 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | srcu_read_unlock(&stm_source_srcu, idx); |
| 854 | } |
| 855 | |
| 856 | static ssize_t stm_source_link_show(struct device *dev, |
| 857 | struct device_attribute *attr, |
| 858 | char *buf) |
| 859 | { |
| 860 | struct stm_source_device *src = to_stm_source_device(dev); |
| 861 | struct stm_device *stm; |
| 862 | int idx, ret; |
| 863 | |
| 864 | idx = srcu_read_lock(&stm_source_srcu); |
| 865 | stm = srcu_dereference(src->link, &stm_source_srcu); |
| 866 | ret = sprintf(buf, "%s\n", |
| 867 | stm ? dev_name(&stm->dev) : "<none>"); |
| 868 | srcu_read_unlock(&stm_source_srcu, idx); |
| 869 | |
| 870 | return ret; |
| 871 | } |
| 872 | |
| 873 | static ssize_t stm_source_link_store(struct device *dev, |
| 874 | struct device_attribute *attr, |
| 875 | const char *buf, size_t count) |
| 876 | { |
| 877 | struct stm_source_device *src = to_stm_source_device(dev); |
| 878 | struct stm_device *link; |
| 879 | int err; |
| 880 | |
| 881 | stm_source_link_drop(src); |
| 882 | |
| 883 | link = stm_find_device(buf); |
| 884 | if (!link) |
| 885 | return -EINVAL; |
| 886 | |
| 887 | err = stm_source_link_add(src, link); |
| 888 | if (err) |
| 889 | stm_put_device(link); |
| 890 | |
| 891 | return err ? : count; |
| 892 | } |
| 893 | |
| 894 | static DEVICE_ATTR_RW(stm_source_link); |
| 895 | |
| 896 | static struct attribute *stm_source_attrs[] = { |
| 897 | &dev_attr_stm_source_link.attr, |
| 898 | NULL, |
| 899 | }; |
| 900 | |
| 901 | ATTRIBUTE_GROUPS(stm_source); |
| 902 | |
| 903 | static struct class stm_source_class = { |
| 904 | .name = "stm_source", |
| 905 | .dev_groups = stm_source_groups, |
| 906 | }; |
| 907 | |
| 908 | static void stm_source_device_release(struct device *dev) |
| 909 | { |
| 910 | struct stm_source_device *src = to_stm_source_device(dev); |
| 911 | |
| 912 | kfree(src); |
| 913 | } |
| 914 | |
| 915 | /** |
| 916 | * stm_source_register_device() - register an stm_source device |
| 917 | * @parent: parent device |
| 918 | * @data: device description structure |
| 919 | * |
| 920 | * This will create a device of stm_source class that can write |
| 921 | * data to an stm device once linked. |
| 922 | * |
| 923 | * Return: 0 on success, -errno otherwise. |
| 924 | */ |
| 925 | int stm_source_register_device(struct device *parent, |
| 926 | struct stm_source_data *data) |
| 927 | { |
| 928 | struct stm_source_device *src; |
| 929 | int err; |
| 930 | |
| 931 | if (!stm_core_up) |
| 932 | return -EPROBE_DEFER; |
| 933 | |
| 934 | src = kzalloc(sizeof(*src), GFP_KERNEL); |
| 935 | if (!src) |
| 936 | return -ENOMEM; |
| 937 | |
| 938 | device_initialize(&src->dev); |
| 939 | src->dev.class = &stm_source_class; |
| 940 | src->dev.parent = parent; |
| 941 | src->dev.release = stm_source_device_release; |
| 942 | |
| 943 | err = kobject_set_name(&src->dev.kobj, "%s", data->name); |
| 944 | if (err) |
| 945 | goto err; |
| 946 | |
| 947 | err = device_add(&src->dev); |
| 948 | if (err) |
| 949 | goto err; |
| 950 | |
| 951 | spin_lock_init(&src->link_lock); |
| 952 | INIT_LIST_HEAD(&src->link_entry); |
| 953 | src->data = data; |
| 954 | data->src = src; |
| 955 | |
| 956 | return 0; |
| 957 | |
| 958 | err: |
| 959 | put_device(&src->dev); |
| 960 | kfree(src); |
| 961 | |
| 962 | return err; |
| 963 | } |
| 964 | EXPORT_SYMBOL_GPL(stm_source_register_device); |
| 965 | |
| 966 | /** |
| 967 | * stm_source_unregister_device() - unregister an stm_source device |
| 968 | * @data: device description that was used to register the device |
| 969 | * |
| 970 | * This will remove a previously created stm_source device from the system. |
| 971 | */ |
| 972 | void stm_source_unregister_device(struct stm_source_data *data) |
| 973 | { |
| 974 | struct stm_source_device *src = data->src; |
| 975 | |
| 976 | stm_source_link_drop(src); |
| 977 | |
| 978 | device_destroy(&stm_source_class, src->dev.devt); |
| 979 | } |
| 980 | EXPORT_SYMBOL_GPL(stm_source_unregister_device); |
| 981 | |
| 982 | int stm_source_write(struct stm_source_data *data, unsigned int chan, |
| 983 | const char *buf, size_t count) |
| 984 | { |
| 985 | struct stm_source_device *src = data->src; |
| 986 | struct stm_device *stm; |
| 987 | int idx; |
| 988 | |
| 989 | if (!src->output.nr_chans) |
| 990 | return -ENODEV; |
| 991 | |
| 992 | if (chan >= src->output.nr_chans) |
| 993 | return -EINVAL; |
| 994 | |
| 995 | idx = srcu_read_lock(&stm_source_srcu); |
| 996 | |
| 997 | stm = srcu_dereference(src->link, &stm_source_srcu); |
| 998 | if (stm) |
| 999 | stm_write(stm->data, src->output.master, |
| 1000 | src->output.channel + chan, |
| 1001 | buf, count); |
| 1002 | else |
| 1003 | count = -ENODEV; |
| 1004 | |
| 1005 | srcu_read_unlock(&stm_source_srcu, idx); |
| 1006 | |
| 1007 | return count; |
| 1008 | } |
| 1009 | EXPORT_SYMBOL_GPL(stm_source_write); |
| 1010 | |
| 1011 | static int __init stm_core_init(void) |
| 1012 | { |
| 1013 | int err; |
| 1014 | |
| 1015 | err = class_register(&stm_class); |
| 1016 | if (err) |
| 1017 | return err; |
| 1018 | |
| 1019 | err = class_register(&stm_source_class); |
| 1020 | if (err) |
| 1021 | goto err_stm; |
| 1022 | |
| 1023 | err = stp_configfs_init(); |
| 1024 | if (err) |
| 1025 | goto err_src; |
| 1026 | |
| 1027 | init_srcu_struct(&stm_source_srcu); |
| 1028 | |
| 1029 | stm_core_up++; |
| 1030 | |
| 1031 | return 0; |
| 1032 | |
| 1033 | err_src: |
| 1034 | class_unregister(&stm_source_class); |
| 1035 | err_stm: |
| 1036 | class_unregister(&stm_class); |
| 1037 | |
| 1038 | return err; |
| 1039 | } |
| 1040 | |
| 1041 | module_init(stm_core_init); |
| 1042 | |
| 1043 | static void __exit stm_core_exit(void) |
| 1044 | { |
| 1045 | cleanup_srcu_struct(&stm_source_srcu); |
| 1046 | class_unregister(&stm_source_class); |
| 1047 | class_unregister(&stm_class); |
| 1048 | stp_configfs_exit(); |
| 1049 | } |
| 1050 | |
| 1051 | module_exit(stm_core_exit); |
| 1052 | |
| 1053 | MODULE_LICENSE("GPL v2"); |
| 1054 | MODULE_DESCRIPTION("System Trace Module device class"); |
| 1055 | MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>"); |