Pratik Patel | 7492943 | 2011-12-26 12:03:41 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/err.h> |
Pratik Patel | bf3e7744 | 2012-03-18 18:30:43 -0700 | [diff] [blame] | 21 | #include <linux/export.h> |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 22 | #include <linux/slab.h> |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 23 | #include <linux/semaphore.h> |
Pratik Patel | 27086d9 | 2012-05-17 23:21:43 -0700 | [diff] [blame] | 24 | #include <linux/clk.h> |
Pratik Patel | 1746b8f | 2012-06-02 21:11:41 -0700 | [diff] [blame] | 25 | #include <linux/coresight.h> |
Pratik Patel | 7492943 | 2011-12-26 12:03:41 -0800 | [diff] [blame] | 26 | |
Pratik Patel | 1746b8f | 2012-06-02 21:11:41 -0700 | [diff] [blame] | 27 | #include "coresight-priv.h" |
Pratik Patel | 7492943 | 2011-12-26 12:03:41 -0800 | [diff] [blame] | 28 | |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 29 | |
| 30 | #define NO_SINK (-1) |
Pratik Patel | 1403f2a | 2012-03-21 10:10:00 -0700 | [diff] [blame] | 31 | |
Pratik Patel | 7492943 | 2011-12-26 12:03:41 -0800 | [diff] [blame] | 32 | |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 33 | static int curr_sink = NO_SINK; |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 34 | static LIST_HEAD(coresight_orph_conns); |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 35 | static LIST_HEAD(coresight_devs); |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 36 | static DEFINE_SEMAPHORE(coresight_mutex); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 37 | |
| 38 | |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 39 | static int coresight_find_link_inport(struct coresight_device *csdev) |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 40 | { |
| 41 | int i; |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 42 | struct coresight_device *parent; |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 43 | struct coresight_connection *conn; |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 44 | |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 45 | parent = container_of(csdev->path_link.next, struct coresight_device, |
| 46 | path_link); |
| 47 | for (i = 0; i < parent->nr_conns; i++) { |
| 48 | conn = &parent->conns[i]; |
| 49 | if (conn->child_dev == csdev) |
| 50 | return conn->child_port; |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 51 | } |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 52 | |
| 53 | pr_err("coresight: couldn't find inport, parent: %d, child: %d\n", |
| 54 | parent->id, csdev->id); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 55 | return 0; |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static int coresight_find_link_outport(struct coresight_device *csdev) |
| 59 | { |
| 60 | int i; |
| 61 | struct coresight_device *child; |
| 62 | struct coresight_connection *conn; |
| 63 | |
| 64 | child = container_of(csdev->path_link.prev, struct coresight_device, |
| 65 | path_link); |
| 66 | for (i = 0; i < csdev->nr_conns; i++) { |
| 67 | conn = &csdev->conns[i]; |
| 68 | if (conn->child_dev == child) |
| 69 | return conn->outport; |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 70 | } |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 71 | |
| 72 | pr_err("coresight: couldn't find outport, parent: %d, child: %d\n", |
| 73 | csdev->id, child->id); |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static int coresight_enable_sink(struct coresight_device *csdev) |
| 78 | { |
| 79 | int ret; |
| 80 | |
| 81 | if (csdev->refcnt.sink_refcnt == 0) { |
| 82 | if (csdev->ops->sink_ops->enable) { |
| 83 | ret = csdev->ops->sink_ops->enable(csdev); |
| 84 | if (ret) |
| 85 | goto err; |
| 86 | csdev->enable = true; |
| 87 | } |
| 88 | } |
| 89 | csdev->refcnt.sink_refcnt++; |
| 90 | |
| 91 | return 0; |
| 92 | err: |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | static void coresight_disable_sink(struct coresight_device *csdev) |
| 97 | { |
| 98 | if (csdev->refcnt.sink_refcnt == 1) { |
| 99 | if (csdev->ops->sink_ops->disable) { |
| 100 | csdev->ops->sink_ops->disable(csdev); |
| 101 | csdev->enable = false; |
| 102 | } |
| 103 | } |
| 104 | csdev->refcnt.sink_refcnt--; |
| 105 | } |
| 106 | |
| 107 | static int coresight_enable_link(struct coresight_device *csdev) |
| 108 | { |
| 109 | int ret; |
| 110 | int link_subtype; |
| 111 | int refport, inport, outport; |
| 112 | |
| 113 | inport = coresight_find_link_inport(csdev); |
| 114 | outport = coresight_find_link_outport(csdev); |
| 115 | |
| 116 | link_subtype = csdev->subtype.link_subtype; |
| 117 | if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) |
| 118 | refport = inport; |
| 119 | else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) |
| 120 | refport = outport; |
| 121 | else |
| 122 | refport = 0; |
| 123 | |
| 124 | if (csdev->refcnt.link_refcnts[refport] == 0) { |
| 125 | if (csdev->ops->link_ops->enable) { |
| 126 | ret = csdev->ops->link_ops->enable(csdev, inport, |
| 127 | outport); |
| 128 | if (ret) |
| 129 | goto err; |
| 130 | csdev->enable = true; |
| 131 | } |
| 132 | } |
| 133 | csdev->refcnt.link_refcnts[refport]++; |
| 134 | |
| 135 | return 0; |
| 136 | err: |
| 137 | return ret; |
| 138 | } |
| 139 | |
| 140 | static void coresight_disable_link(struct coresight_device *csdev) |
| 141 | { |
| 142 | int link_subtype; |
| 143 | int refport, inport, outport; |
| 144 | |
| 145 | inport = coresight_find_link_inport(csdev); |
| 146 | outport = coresight_find_link_outport(csdev); |
| 147 | |
| 148 | link_subtype = csdev->subtype.link_subtype; |
| 149 | if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) |
| 150 | refport = inport; |
| 151 | else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) |
| 152 | refport = outport; |
| 153 | else |
| 154 | refport = 0; |
| 155 | |
| 156 | if (csdev->refcnt.link_refcnts[refport] == 1) { |
| 157 | if (csdev->ops->link_ops->disable) { |
| 158 | csdev->ops->link_ops->disable(csdev, inport, outport); |
| 159 | csdev->enable = false; |
| 160 | } |
| 161 | } |
| 162 | csdev->refcnt.link_refcnts[refport]--; |
| 163 | } |
| 164 | |
| 165 | static int coresight_enable_source(struct coresight_device *csdev) |
| 166 | { |
| 167 | int ret; |
| 168 | |
| 169 | if (csdev->refcnt.source_refcnt == 0) { |
| 170 | if (csdev->ops->source_ops->enable) { |
| 171 | ret = csdev->ops->source_ops->enable(csdev); |
| 172 | if (ret) |
| 173 | goto err; |
| 174 | csdev->enable = true; |
| 175 | } |
| 176 | } |
| 177 | csdev->refcnt.source_refcnt++; |
| 178 | |
| 179 | return 0; |
| 180 | err: |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | static void coresight_disable_source(struct coresight_device *csdev) |
| 185 | { |
| 186 | if (csdev->refcnt.source_refcnt == 1) { |
| 187 | if (csdev->ops->source_ops->disable) { |
| 188 | csdev->ops->source_ops->disable(csdev); |
| 189 | csdev->enable = false; |
| 190 | } |
| 191 | } |
| 192 | csdev->refcnt.source_refcnt--; |
| 193 | } |
| 194 | |
| 195 | static struct list_head *coresight_build_path(struct coresight_device *csdev, |
| 196 | struct list_head *path) |
| 197 | { |
| 198 | int i; |
| 199 | struct list_head *p; |
| 200 | struct coresight_connection *conn; |
| 201 | |
| 202 | if (csdev->id == curr_sink) { |
| 203 | list_add_tail(&csdev->path_link, path); |
| 204 | return path; |
| 205 | } |
| 206 | |
| 207 | for (i = 0; i < csdev->nr_conns; i++) { |
| 208 | conn = &csdev->conns[i]; |
| 209 | p = coresight_build_path(conn->child_dev, path); |
| 210 | if (p) { |
| 211 | list_add_tail(&csdev->path_link, p); |
| 212 | return p; |
| 213 | } |
| 214 | } |
| 215 | return NULL; |
| 216 | } |
| 217 | |
| 218 | static void coresight_release_path(struct list_head *path) |
| 219 | { |
| 220 | struct coresight_device *cd, *temp; |
| 221 | |
| 222 | list_for_each_entry_safe(cd, temp, path, path_link) |
| 223 | list_del(&cd->path_link); |
| 224 | } |
| 225 | |
| 226 | static int coresight_enable_path(struct list_head *path, bool incl_source) |
| 227 | { |
| 228 | int ret = 0; |
| 229 | struct coresight_device *cd; |
| 230 | |
| 231 | list_for_each_entry(cd, path, path_link) { |
| 232 | if (cd == list_first_entry(path, struct coresight_device, |
| 233 | path_link)) { |
| 234 | ret = coresight_enable_sink(cd); |
| 235 | } else if (list_is_last(&cd->path_link, path)) { |
| 236 | if (incl_source) |
| 237 | ret = coresight_enable_source(cd); |
| 238 | } else { |
| 239 | ret = coresight_enable_link(cd); |
| 240 | } |
| 241 | if (ret) |
| 242 | goto err; |
| 243 | } |
| 244 | return 0; |
| 245 | err: |
| 246 | list_for_each_entry_continue_reverse(cd, path, path_link) { |
| 247 | if (cd == list_first_entry(path, struct coresight_device, |
| 248 | path_link)) { |
| 249 | coresight_disable_sink(cd); |
| 250 | } else if (list_is_last(&cd->path_link, path)) { |
| 251 | if (incl_source) |
| 252 | coresight_disable_source(cd); |
| 253 | } else { |
| 254 | coresight_disable_link(cd); |
| 255 | } |
| 256 | } |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | static void coresight_disable_path(struct list_head *path, bool incl_source) |
| 261 | { |
| 262 | struct coresight_device *cd; |
| 263 | |
| 264 | list_for_each_entry(cd, path, path_link) { |
| 265 | if (cd == list_first_entry(path, struct coresight_device, |
| 266 | path_link)) { |
| 267 | coresight_disable_sink(cd); |
| 268 | } else if (list_is_last(&cd->path_link, path)) { |
| 269 | if (incl_source) |
| 270 | coresight_disable_source(cd); |
| 271 | } else { |
| 272 | coresight_disable_link(cd); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | static int coresight_switch_sink(struct coresight_device *csdev) |
| 278 | { |
| 279 | int ret = 0; |
| 280 | LIST_HEAD(path); |
| 281 | struct coresight_device *cd; |
| 282 | |
| 283 | if (IS_ERR_OR_NULL(csdev)) |
| 284 | return -EINVAL; |
| 285 | |
| 286 | down(&coresight_mutex); |
| 287 | if (csdev->id == curr_sink) |
| 288 | goto out; |
| 289 | |
| 290 | list_for_each_entry(cd, &coresight_devs, dev_link) { |
| 291 | if (cd->type == CORESIGHT_DEV_TYPE_SOURCE && cd->enable) { |
| 292 | coresight_build_path(cd, &path); |
| 293 | coresight_disable_path(&path, false); |
| 294 | coresight_release_path(&path); |
| 295 | } |
| 296 | } |
| 297 | curr_sink = csdev->id; |
| 298 | list_for_each_entry(cd, &coresight_devs, dev_link) { |
| 299 | if (cd->type == CORESIGHT_DEV_TYPE_SOURCE && cd->enable) { |
| 300 | coresight_build_path(cd, &path); |
| 301 | ret = coresight_enable_path(&path, false); |
| 302 | coresight_release_path(&path); |
| 303 | if (ret) |
| 304 | goto err; |
| 305 | } |
| 306 | } |
| 307 | out: |
| 308 | up(&coresight_mutex); |
| 309 | return 0; |
| 310 | err: |
| 311 | list_for_each_entry(cd, &coresight_devs, dev_link) { |
| 312 | if (cd->type == CORESIGHT_DEV_TYPE_SOURCE && cd->enable) |
| 313 | coresight_disable_source(cd); |
| 314 | } |
| 315 | pr_err("coresight: sink switch failed, sources disabled; try again\n"); |
| 316 | return ret; |
| 317 | } |
| 318 | |
| 319 | int coresight_enable(struct coresight_device *csdev) |
| 320 | { |
| 321 | int ret = 0; |
| 322 | LIST_HEAD(path); |
| 323 | |
| 324 | if (IS_ERR_OR_NULL(csdev)) |
| 325 | return -EINVAL; |
| 326 | |
| 327 | down(&coresight_mutex); |
| 328 | if (csdev->type != CORESIGHT_DEV_TYPE_SOURCE) { |
| 329 | ret = -EINVAL; |
| 330 | pr_err("coresight: wrong device type in %s\n", __func__); |
| 331 | goto out; |
| 332 | } |
| 333 | if (csdev->enable) |
| 334 | goto out; |
| 335 | |
| 336 | coresight_build_path(csdev, &path); |
| 337 | ret = coresight_enable_path(&path, true); |
| 338 | coresight_release_path(&path); |
| 339 | if (ret) |
| 340 | pr_err("coresight: enable failed\n"); |
| 341 | out: |
| 342 | up(&coresight_mutex); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 343 | return ret; |
| 344 | } |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 345 | EXPORT_SYMBOL_GPL(coresight_enable); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 346 | |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 347 | void coresight_disable(struct coresight_device *csdev) |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 348 | { |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 349 | LIST_HEAD(path); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 350 | |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 351 | if (IS_ERR_OR_NULL(csdev)) |
| 352 | return; |
| 353 | |
| 354 | down(&coresight_mutex); |
| 355 | if (csdev->type != CORESIGHT_DEV_TYPE_SOURCE) { |
| 356 | pr_err("coresight: wrong device type in %s\n", __func__); |
| 357 | goto out; |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 358 | } |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 359 | if (!csdev->enable) |
| 360 | goto out; |
| 361 | |
| 362 | coresight_build_path(csdev, &path); |
| 363 | coresight_disable_path(&path, true); |
| 364 | coresight_release_path(&path); |
| 365 | out: |
| 366 | up(&coresight_mutex); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 367 | } |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 368 | EXPORT_SYMBOL_GPL(coresight_disable); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 369 | |
Pratik Patel | cf7d045 | 2012-07-02 13:57:20 -0700 | [diff] [blame^] | 370 | void coresight_abort(void) |
| 371 | { |
| 372 | struct coresight_device *cd; |
| 373 | |
| 374 | if (down_trylock(&coresight_mutex)) { |
| 375 | pr_err("coresight: abort could not be processed\n"); |
| 376 | return; |
| 377 | } |
| 378 | if (curr_sink == NO_SINK) |
| 379 | goto out; |
| 380 | |
| 381 | list_for_each_entry(cd, &coresight_devs, dev_link) { |
| 382 | if (cd->id == curr_sink) { |
| 383 | if (cd->enable && cd->ops->sink_ops->abort) |
| 384 | cd->ops->sink_ops->abort(cd); |
| 385 | } |
| 386 | } |
| 387 | out: |
| 388 | up(&coresight_mutex); |
| 389 | } |
| 390 | EXPORT_SYMBOL_GPL(coresight_abort); |
| 391 | |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 392 | static ssize_t coresight_show_type(struct device *dev, |
| 393 | struct device_attribute *attr, char *buf) |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 394 | { |
| 395 | return snprintf(buf, PAGE_SIZE, "%s\n", dev->type->name); |
| 396 | } |
| 397 | |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 398 | static struct device_attribute coresight_dev_attrs[] = { |
| 399 | __ATTR(type, S_IRUGO, coresight_show_type, NULL), |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 400 | { }, |
| 401 | }; |
| 402 | |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 403 | struct bus_type coresight_bus_type = { |
| 404 | .name = "coresight", |
| 405 | .dev_attrs = coresight_dev_attrs, |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 406 | }; |
| 407 | |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 408 | static ssize_t coresight_show_curr_sink(struct device *dev, |
| 409 | struct device_attribute *attr, |
| 410 | char *buf) |
| 411 | { |
| 412 | struct coresight_device *csdev = to_coresight_device(dev); |
| 413 | |
| 414 | return scnprintf(buf, PAGE_SIZE, "%u\n", |
| 415 | csdev->id == curr_sink ? 1 : 0); |
| 416 | } |
| 417 | |
| 418 | static ssize_t coresight_store_curr_sink(struct device *dev, |
| 419 | struct device_attribute *attr, |
| 420 | const char *buf, size_t size) |
| 421 | { |
| 422 | int ret = 0; |
| 423 | unsigned long val; |
| 424 | struct coresight_device *csdev = to_coresight_device(dev); |
| 425 | |
| 426 | if (sscanf(buf, "%lx", &val) != 1) |
| 427 | return -EINVAL; |
| 428 | |
| 429 | if (val) |
| 430 | ret = coresight_switch_sink(csdev); |
| 431 | else |
| 432 | ret = -EINVAL; |
| 433 | |
| 434 | if (ret) |
| 435 | return ret; |
| 436 | return size; |
| 437 | } |
| 438 | static DEVICE_ATTR(curr_sink, S_IRUGO | S_IWUSR, coresight_show_curr_sink, |
| 439 | coresight_store_curr_sink); |
| 440 | |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 441 | static ssize_t coresight_show_enable(struct device *dev, |
| 442 | struct device_attribute *attr, char *buf) |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 443 | { |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 444 | struct coresight_device *csdev = to_coresight_device(dev); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 445 | |
| 446 | return scnprintf(buf, PAGE_SIZE, "%u\n", (unsigned)csdev->enable); |
| 447 | } |
| 448 | |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 449 | static ssize_t coresight_store_enable(struct device *dev, |
| 450 | struct device_attribute *attr, |
| 451 | const char *buf, size_t size) |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 452 | { |
| 453 | int ret = 0; |
| 454 | unsigned long val; |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 455 | struct coresight_device *csdev = to_coresight_device(dev); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 456 | |
| 457 | if (sscanf(buf, "%lx", &val) != 1) |
| 458 | return -EINVAL; |
| 459 | |
| 460 | if (val) |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 461 | ret = coresight_enable(csdev); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 462 | else |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 463 | coresight_disable(csdev); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 464 | |
| 465 | if (ret) |
| 466 | return ret; |
| 467 | return size; |
| 468 | } |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 469 | static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, coresight_show_enable, |
| 470 | coresight_store_enable); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 471 | |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 472 | static struct attribute *coresight_attrs_sink[] = { |
| 473 | &dev_attr_curr_sink.attr, |
| 474 | NULL, |
| 475 | }; |
| 476 | |
| 477 | static struct attribute_group coresight_attr_grp_sink = { |
| 478 | .attrs = coresight_attrs_sink, |
| 479 | }; |
| 480 | |
| 481 | static const struct attribute_group *coresight_attr_grps_sink[] = { |
| 482 | &coresight_attr_grp_sink, |
| 483 | NULL, |
| 484 | }; |
| 485 | |
| 486 | static struct attribute *coresight_attrs_source[] = { |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 487 | &dev_attr_enable.attr, |
| 488 | NULL, |
| 489 | }; |
| 490 | |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 491 | static struct attribute_group coresight_attr_grp_source = { |
| 492 | .attrs = coresight_attrs_source, |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 493 | }; |
| 494 | |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 495 | static const struct attribute_group *coresight_attr_grps_source[] = { |
| 496 | &coresight_attr_grp_source, |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 497 | NULL, |
| 498 | }; |
| 499 | |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 500 | static struct device_type coresight_dev_type[] = { |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 501 | { |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 502 | .name = "sink", |
| 503 | .groups = coresight_attr_grps_sink, |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 504 | }, |
| 505 | { |
| 506 | .name = "link", |
| 507 | }, |
| 508 | { |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 509 | .name = "linksink", |
| 510 | .groups = coresight_attr_grps_sink, |
| 511 | }, |
| 512 | { |
| 513 | .name = "source", |
| 514 | .groups = coresight_attr_grps_source, |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 515 | }, |
| 516 | }; |
| 517 | |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 518 | static void coresight_device_release(struct device *dev) |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 519 | { |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 520 | struct coresight_device *csdev = to_coresight_device(dev); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 521 | kfree(csdev); |
| 522 | } |
| 523 | |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 524 | static void coresight_fixup_orphan_conns(struct coresight_device *csdev) |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 525 | { |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 526 | struct coresight_connection *conn, *temp; |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 527 | |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 528 | list_for_each_entry_safe(conn, temp, &coresight_orph_conns, link) { |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 529 | if (conn->child_id == csdev->id) { |
| 530 | conn->child_dev = csdev; |
| 531 | list_del(&conn->link); |
| 532 | } |
| 533 | } |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 536 | static void coresight_fixup_device_conns(struct coresight_device *csdev) |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 537 | { |
| 538 | int i; |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 539 | struct coresight_device *cd; |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 540 | bool found; |
| 541 | |
| 542 | for (i = 0; i < csdev->nr_conns; i++) { |
| 543 | found = false; |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 544 | list_for_each_entry(cd, &coresight_devs, dev_link) { |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 545 | if (csdev->conns[i].child_id == cd->id) { |
| 546 | csdev->conns[i].child_dev = cd; |
| 547 | found = true; |
| 548 | break; |
| 549 | } |
| 550 | } |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 551 | if (!found) |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 552 | list_add_tail(&csdev->conns[i].link, |
| 553 | &coresight_orph_conns); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 557 | struct coresight_device *coresight_register(struct coresight_desc *desc) |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 558 | { |
| 559 | int i; |
| 560 | int ret; |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 561 | int link_subtype; |
| 562 | int nr_refcnts; |
| 563 | int *refcnts = NULL; |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 564 | struct coresight_device *csdev; |
| 565 | struct coresight_connection *conns; |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 566 | |
| 567 | csdev = kzalloc(sizeof(*csdev), GFP_KERNEL); |
| 568 | if (!csdev) { |
| 569 | ret = -ENOMEM; |
| 570 | goto err_kzalloc_csdev; |
| 571 | } |
| 572 | |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 573 | csdev->id = desc->pdata->id; |
| 574 | |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 575 | if (desc->type == CORESIGHT_DEV_TYPE_LINK || |
| 576 | desc->type == CORESIGHT_DEV_TYPE_LINKSINK) { |
| 577 | link_subtype = desc->subtype.link_subtype; |
| 578 | if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) |
| 579 | nr_refcnts = desc->pdata->nr_inports; |
| 580 | else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) |
| 581 | nr_refcnts = desc->pdata->nr_outports; |
| 582 | else |
| 583 | nr_refcnts = 1; |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 584 | |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 585 | refcnts = kzalloc(sizeof(*refcnts) * nr_refcnts, GFP_KERNEL); |
| 586 | if (!refcnts) { |
| 587 | ret = -ENOMEM; |
| 588 | goto err_kzalloc_refcnts; |
| 589 | } |
| 590 | csdev->refcnt.link_refcnts = refcnts; |
| 591 | } |
| 592 | |
| 593 | csdev->nr_conns = desc->pdata->nr_outports; |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 594 | conns = kzalloc(sizeof(*conns) * csdev->nr_conns, GFP_KERNEL); |
| 595 | if (!conns) { |
| 596 | ret = -ENOMEM; |
| 597 | goto err_kzalloc_conns; |
| 598 | } |
| 599 | for (i = 0; i < csdev->nr_conns; i++) { |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 600 | conns[i].outport = desc->pdata->outports[i]; |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 601 | conns[i].child_id = desc->pdata->child_ids[i]; |
| 602 | conns[i].child_port = desc->pdata->child_ports[i]; |
| 603 | } |
| 604 | csdev->conns = conns; |
| 605 | |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 606 | csdev->type = desc->type; |
| 607 | csdev->subtype = desc->subtype; |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 608 | csdev->ops = desc->ops; |
| 609 | csdev->owner = desc->owner; |
| 610 | |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 611 | csdev->dev.type = &coresight_dev_type[desc->type]; |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 612 | csdev->dev.groups = desc->groups; |
| 613 | csdev->dev.parent = desc->dev; |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 614 | csdev->dev.bus = &coresight_bus_type; |
| 615 | csdev->dev.release = coresight_device_release; |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 616 | dev_set_name(&csdev->dev, "%s", desc->pdata->name); |
| 617 | |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 618 | down(&coresight_mutex); |
| 619 | if (desc->pdata->default_sink) { |
| 620 | if (curr_sink == NO_SINK) { |
| 621 | curr_sink = csdev->id; |
| 622 | } else { |
| 623 | ret = -EINVAL; |
| 624 | goto err_default_sink; |
| 625 | } |
| 626 | } |
| 627 | |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 628 | coresight_fixup_device_conns(csdev); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 629 | ret = device_register(&csdev->dev); |
| 630 | if (ret) |
| 631 | goto err_dev_reg; |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 632 | coresight_fixup_orphan_conns(csdev); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 633 | |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 634 | list_add_tail(&csdev->dev_link, &coresight_devs); |
| 635 | up(&coresight_mutex); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 636 | |
| 637 | return csdev; |
| 638 | err_dev_reg: |
| 639 | put_device(&csdev->dev); |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 640 | err_default_sink: |
| 641 | up(&coresight_mutex); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 642 | kfree(conns); |
| 643 | err_kzalloc_conns: |
Pratik Patel | 0a7edd3 | 2012-06-08 09:31:55 -0700 | [diff] [blame] | 644 | kfree(refcnts); |
| 645 | err_kzalloc_refcnts: |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 646 | kfree(csdev); |
| 647 | err_kzalloc_csdev: |
| 648 | return ERR_PTR(ret); |
| 649 | } |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 650 | EXPORT_SYMBOL_GPL(coresight_register); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 651 | |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 652 | void coresight_unregister(struct coresight_device *csdev) |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 653 | { |
| 654 | if (IS_ERR_OR_NULL(csdev)) |
| 655 | return; |
| 656 | |
| 657 | if (get_device(&csdev->dev)) { |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 658 | device_unregister(&csdev->dev); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 659 | put_device(&csdev->dev); |
| 660 | } |
| 661 | } |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 662 | EXPORT_SYMBOL_GPL(coresight_unregister); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 663 | |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 664 | static int __init coresight_init(void) |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 665 | { |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 666 | return bus_register(&coresight_bus_type); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 667 | } |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 668 | subsys_initcall(coresight_init); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 669 | |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 670 | static void __exit coresight_exit(void) |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 671 | { |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 672 | bus_unregister(&coresight_bus_type); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 673 | } |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 674 | module_exit(coresight_exit); |
Pratik Patel | b84ef9d | 2012-05-24 14:01:43 -0700 | [diff] [blame] | 675 | |
| 676 | MODULE_LICENSE("GPL v2"); |