Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 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> |
Pratik Patel | cf41862 | 2011-09-22 11:15:11 -0700 | [diff] [blame] | 15 | #include <linux/init.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/device.h> |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/miscdevice.h> |
| 23 | #include <linux/uaccess.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/delay.h> |
Pratik Patel | cf41862 | 2011-09-22 11:15:11 -0700 | [diff] [blame] | 26 | #include <linux/mutex.h> |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 27 | |
| 28 | #include "qdss.h" |
| 29 | |
| 30 | #define etb_writel(etb, val, off) __raw_writel((val), etb.base + off) |
| 31 | #define etb_readl(etb, off) __raw_readl(etb.base + off) |
| 32 | |
| 33 | #define ETB_RAM_DEPTH_REG (0x004) |
| 34 | #define ETB_STATUS_REG (0x00C) |
| 35 | #define ETB_RAM_READ_DATA_REG (0x010) |
| 36 | #define ETB_RAM_READ_POINTER (0x014) |
| 37 | #define ETB_RAM_WRITE_POINTER (0x018) |
| 38 | #define ETB_TRG (0x01C) |
| 39 | #define ETB_CTL_REG (0x020) |
| 40 | #define ETB_RWD_REG (0x024) |
| 41 | #define ETB_FFSR (0x300) |
| 42 | #define ETB_FFCR (0x304) |
| 43 | #define ETB_ITMISCOP0 (0xEE0) |
| 44 | #define ETB_ITTRFLINACK (0xEE4) |
| 45 | #define ETB_ITTRFLIN (0xEE8) |
| 46 | #define ETB_ITATBDATA0 (0xEEC) |
| 47 | #define ETB_ITATBCTR2 (0xEF0) |
| 48 | #define ETB_ITATBCTR1 (0xEF4) |
| 49 | #define ETB_ITATBCTR0 (0xEF8) |
| 50 | |
| 51 | |
| 52 | #define BYTES_PER_WORD 4 |
| 53 | #define ETB_SIZE_WORDS 4096 |
Pratik Patel | 70f5e39 | 2011-09-24 21:23:27 -0700 | [diff] [blame] | 54 | #define FRAME_SIZE_WORDS 4 |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 55 | |
| 56 | #define ETB_LOCK() \ |
| 57 | do { \ |
| 58 | mb(); \ |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 59 | etb_writel(etb, 0x0, CS_LAR); \ |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 60 | } while (0) |
| 61 | #define ETB_UNLOCK() \ |
| 62 | do { \ |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 63 | etb_writel(etb, CS_UNLOCK_MAGIC, CS_LAR); \ |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 64 | mb(); \ |
| 65 | } while (0) |
| 66 | |
| 67 | struct etb_ctx { |
| 68 | uint8_t *buf; |
| 69 | void __iomem *base; |
| 70 | bool enabled; |
| 71 | bool reading; |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 72 | struct mutex mutex; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 73 | atomic_t in_use; |
| 74 | struct device *dev; |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame^] | 75 | struct kobject *kobj; |
| 76 | uint32_t trigger_cntr; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | static struct etb_ctx etb; |
| 80 | |
| 81 | static void __etb_enable(void) |
| 82 | { |
| 83 | int i; |
| 84 | |
| 85 | ETB_UNLOCK(); |
| 86 | |
| 87 | etb_writel(etb, 0x0, ETB_RAM_WRITE_POINTER); |
| 88 | for (i = 0; i < ETB_SIZE_WORDS; i++) |
| 89 | etb_writel(etb, 0x0, ETB_RWD_REG); |
| 90 | |
| 91 | etb_writel(etb, 0x0, ETB_RAM_WRITE_POINTER); |
| 92 | etb_writel(etb, 0x0, ETB_RAM_READ_POINTER); |
| 93 | |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame^] | 94 | etb_writel(etb, etb.trigger_cntr, ETB_TRG); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 95 | etb_writel(etb, BIT(13) | BIT(0), ETB_FFCR); |
| 96 | etb_writel(etb, BIT(0), ETB_CTL_REG); |
| 97 | |
| 98 | ETB_LOCK(); |
| 99 | } |
| 100 | |
| 101 | void etb_enable(void) |
| 102 | { |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 103 | mutex_lock(&etb.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 104 | __etb_enable(); |
| 105 | etb.enabled = true; |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 106 | dev_info(etb.dev, "ETB enabled\n"); |
| 107 | mutex_unlock(&etb.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static void __etb_disable(void) |
| 111 | { |
| 112 | int count; |
| 113 | |
| 114 | ETB_UNLOCK(); |
| 115 | |
| 116 | etb_writel(etb, BIT(12) | BIT(13), ETB_FFCR); |
| 117 | etb_writel(etb, 0x0, ETB_CTL_REG); |
| 118 | |
| 119 | for (count = TIMEOUT_US; BVAL(etb_readl(etb, ETB_FFSR), 1) != 1 |
| 120 | && count > 0; count--) |
| 121 | udelay(1); |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 122 | WARN(count == 0, "timeout while disabling ETB, ETB_FFSR: %#x\n", |
| 123 | etb_readl(etb, ETB_FFSR)); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 124 | |
| 125 | ETB_LOCK(); |
| 126 | } |
| 127 | |
| 128 | void etb_disable(void) |
| 129 | { |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 130 | mutex_lock(&etb.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 131 | __etb_disable(); |
| 132 | etb.enabled = false; |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 133 | dev_info(etb.dev, "ETB disabled\n"); |
| 134 | mutex_unlock(&etb.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static void __etb_dump(void) |
| 138 | { |
| 139 | int i; |
| 140 | uint8_t *buf_ptr; |
| 141 | uint32_t read_data; |
| 142 | uint32_t read_ptr; |
| 143 | uint32_t write_ptr; |
Pratik Patel | 70f5e39 | 2011-09-24 21:23:27 -0700 | [diff] [blame] | 144 | uint32_t frame_off; |
| 145 | uint32_t frame_endoff; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 146 | |
| 147 | ETB_UNLOCK(); |
| 148 | |
| 149 | read_ptr = etb_readl(etb, ETB_RAM_READ_POINTER); |
| 150 | write_ptr = etb_readl(etb, ETB_RAM_WRITE_POINTER); |
| 151 | |
Pratik Patel | 70f5e39 | 2011-09-24 21:23:27 -0700 | [diff] [blame] | 152 | frame_off = write_ptr % FRAME_SIZE_WORDS; |
| 153 | frame_endoff = FRAME_SIZE_WORDS - frame_off; |
| 154 | if (frame_off) { |
| 155 | dev_err(etb.dev, "write_ptr: %lu not aligned to formatter " |
| 156 | "frame size\n", (unsigned long)write_ptr); |
| 157 | dev_err(etb.dev, "frameoff: %lu, frame_endoff: %lu\n", |
| 158 | (unsigned long)frame_off, (unsigned long)frame_endoff); |
| 159 | write_ptr += frame_endoff; |
| 160 | } |
| 161 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 162 | if ((etb_readl(etb, ETB_STATUS_REG) & BIT(0)) == 0) |
| 163 | etb_writel(etb, 0x0, ETB_RAM_READ_POINTER); |
| 164 | else |
| 165 | etb_writel(etb, write_ptr, ETB_RAM_READ_POINTER); |
| 166 | |
| 167 | buf_ptr = etb.buf; |
| 168 | for (i = 0; i < ETB_SIZE_WORDS; i++) { |
| 169 | read_data = etb_readl(etb, ETB_RAM_READ_DATA_REG); |
Pratik Patel | 70f5e39 | 2011-09-24 21:23:27 -0700 | [diff] [blame] | 170 | *buf_ptr++ = read_data >> 0; |
| 171 | *buf_ptr++ = read_data >> 8; |
| 172 | *buf_ptr++ = read_data >> 16; |
| 173 | *buf_ptr++ = read_data >> 24; |
| 174 | } |
| 175 | |
| 176 | if (frame_off) { |
| 177 | buf_ptr -= (frame_endoff * BYTES_PER_WORD); |
| 178 | for (i = 0; i < frame_endoff; i++) { |
| 179 | *buf_ptr++ = 0x0; |
| 180 | *buf_ptr++ = 0x0; |
| 181 | *buf_ptr++ = 0x0; |
| 182 | *buf_ptr++ = 0x0; |
| 183 | } |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | etb_writel(etb, read_ptr, ETB_RAM_READ_POINTER); |
| 187 | |
| 188 | ETB_LOCK(); |
| 189 | } |
| 190 | |
| 191 | void etb_dump(void) |
| 192 | { |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 193 | mutex_lock(&etb.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 194 | if (etb.enabled) { |
| 195 | __etb_disable(); |
| 196 | __etb_dump(); |
| 197 | __etb_enable(); |
| 198 | |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 199 | dev_info(etb.dev, "ETB dumped\n"); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 200 | } |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 201 | mutex_unlock(&etb.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static int etb_open(struct inode *inode, struct file *file) |
| 205 | { |
| 206 | if (atomic_cmpxchg(&etb.in_use, 0, 1)) |
| 207 | return -EBUSY; |
| 208 | |
| 209 | dev_dbg(etb.dev, "%s: successfully opened\n", __func__); |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | static ssize_t etb_read(struct file *file, char __user *data, |
| 214 | size_t len, loff_t *ppos) |
| 215 | { |
| 216 | if (etb.reading == false) { |
| 217 | etb_dump(); |
| 218 | etb.reading = true; |
| 219 | } |
| 220 | |
| 221 | if (*ppos + len > ETB_SIZE_WORDS * BYTES_PER_WORD) |
| 222 | len = ETB_SIZE_WORDS * BYTES_PER_WORD - *ppos; |
| 223 | |
| 224 | if (copy_to_user(data, etb.buf + *ppos, len)) { |
| 225 | dev_dbg(etb.dev, "%s: copy_to_user failed\n", __func__); |
| 226 | return -EFAULT; |
| 227 | } |
| 228 | |
| 229 | *ppos += len; |
| 230 | |
| 231 | dev_dbg(etb.dev, "%s: %d bytes copied, %d bytes left\n", |
| 232 | __func__, len, (int) (ETB_SIZE_WORDS * BYTES_PER_WORD - *ppos)); |
| 233 | |
| 234 | return len; |
| 235 | } |
| 236 | |
| 237 | static int etb_release(struct inode *inode, struct file *file) |
| 238 | { |
| 239 | etb.reading = false; |
| 240 | |
| 241 | atomic_set(&etb.in_use, 0); |
| 242 | |
| 243 | dev_dbg(etb.dev, "%s: released\n", __func__); |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static const struct file_operations etb_fops = { |
| 249 | .owner = THIS_MODULE, |
| 250 | .open = etb_open, |
| 251 | .read = etb_read, |
| 252 | .release = etb_release, |
| 253 | }; |
| 254 | |
| 255 | static struct miscdevice etb_misc = { |
| 256 | .name = "msm_etb", |
| 257 | .minor = MISC_DYNAMIC_MINOR, |
| 258 | .fops = &etb_fops, |
| 259 | }; |
| 260 | |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame^] | 261 | #define ETB_ATTR(__name) \ |
| 262 | static struct kobj_attribute __name##_attr = \ |
| 263 | __ATTR(__name, S_IRUGO | S_IWUSR, __name##_show, __name##_store) |
| 264 | |
| 265 | static ssize_t trigger_cntr_store(struct kobject *kobj, |
| 266 | struct kobj_attribute *attr, |
| 267 | const char *buf, size_t n) |
| 268 | { |
| 269 | unsigned long val; |
| 270 | |
| 271 | if (sscanf(buf, "%lx", &val) != 1) |
| 272 | return -EINVAL; |
| 273 | |
| 274 | etb.trigger_cntr = val; |
| 275 | return n; |
| 276 | } |
| 277 | static ssize_t trigger_cntr_show(struct kobject *kobj, |
| 278 | struct kobj_attribute *attr, |
| 279 | char *buf) |
| 280 | { |
| 281 | unsigned long val = etb.trigger_cntr; |
| 282 | return scnprintf(buf, PAGE_SIZE, "%#lx\n", val); |
| 283 | } |
| 284 | ETB_ATTR(trigger_cntr); |
| 285 | |
| 286 | static int __init etb_sysfs_init(void) |
| 287 | { |
| 288 | int ret; |
| 289 | |
| 290 | etb.kobj = kobject_create_and_add("etb", qdss_get_modulekobj()); |
| 291 | if (!etb.kobj) { |
| 292 | dev_err(etb.dev, "failed to create ETB sysfs kobject\n"); |
| 293 | ret = -ENOMEM; |
| 294 | goto err_create; |
| 295 | } |
| 296 | |
| 297 | ret = sysfs_create_file(etb.kobj, &trigger_cntr_attr.attr); |
| 298 | if (ret) { |
| 299 | dev_err(etb.dev, "failed to create ETB sysfs trigger_cntr" |
| 300 | " attribute\n"); |
| 301 | goto err_file; |
| 302 | } |
| 303 | |
| 304 | return 0; |
| 305 | err_file: |
| 306 | kobject_put(etb.kobj); |
| 307 | err_create: |
| 308 | return ret; |
| 309 | } |
| 310 | |
| 311 | static void etb_sysfs_exit(void) |
| 312 | { |
| 313 | sysfs_remove_file(etb.kobj, &trigger_cntr_attr.attr); |
| 314 | kobject_put(etb.kobj); |
| 315 | } |
| 316 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 317 | static int __devinit etb_probe(struct platform_device *pdev) |
| 318 | { |
| 319 | int ret; |
| 320 | struct resource *res; |
| 321 | |
| 322 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 323 | if (!res) { |
| 324 | ret = -EINVAL; |
| 325 | goto err_res; |
| 326 | } |
| 327 | |
| 328 | etb.base = ioremap_nocache(res->start, resource_size(res)); |
| 329 | if (!etb.base) { |
| 330 | ret = -EINVAL; |
| 331 | goto err_ioremap; |
| 332 | } |
| 333 | |
| 334 | etb.dev = &pdev->dev; |
| 335 | |
Pratik Patel | 9dc7022 | 2012-03-07 12:09:13 -0800 | [diff] [blame] | 336 | mutex_init(&etb.mutex); |
| 337 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 338 | ret = misc_register(&etb_misc); |
| 339 | if (ret) |
| 340 | goto err_misc; |
| 341 | |
| 342 | etb.buf = kzalloc(ETB_SIZE_WORDS * BYTES_PER_WORD, GFP_KERNEL); |
| 343 | if (!etb.buf) { |
| 344 | ret = -ENOMEM; |
| 345 | goto err_alloc; |
| 346 | } |
| 347 | |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame^] | 348 | etb_sysfs_init(); |
| 349 | |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 350 | dev_info(etb.dev, "ETB initialized\n"); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 351 | return 0; |
| 352 | |
| 353 | err_alloc: |
| 354 | misc_deregister(&etb_misc); |
| 355 | err_misc: |
Pratik Patel | 9dc7022 | 2012-03-07 12:09:13 -0800 | [diff] [blame] | 356 | mutex_destroy(&etb.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 357 | iounmap(etb.base); |
| 358 | err_ioremap: |
| 359 | err_res: |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 360 | dev_err(etb.dev, "ETB init failed\n"); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 361 | return ret; |
| 362 | } |
| 363 | |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 364 | static int etb_remove(struct platform_device *pdev) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 365 | { |
| 366 | if (etb.enabled) |
| 367 | etb_disable(); |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame^] | 368 | etb_sysfs_exit(); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 369 | kfree(etb.buf); |
| 370 | misc_deregister(&etb_misc); |
Pratik Patel | 9dc7022 | 2012-03-07 12:09:13 -0800 | [diff] [blame] | 371 | mutex_destroy(&etb.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 372 | iounmap(etb.base); |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | static struct platform_driver etb_driver = { |
| 378 | .probe = etb_probe, |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 379 | .remove = etb_remove, |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 380 | .driver = { |
| 381 | .name = "msm_etb", |
| 382 | }, |
| 383 | }; |
| 384 | |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 385 | int __init etb_init(void) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 386 | { |
| 387 | return platform_driver_register(&etb_driver); |
| 388 | } |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 389 | |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 390 | void etb_exit(void) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 391 | { |
| 392 | platform_driver_unregister(&etb_driver); |
| 393 | } |