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 | |
Pratik Patel | b27a935 | 2012-03-17 22:37:21 -0700 | [diff] [blame] | 28 | #include "qdss-priv.h" |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 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; |
Pratik Patel | 6a32d26 | 2012-01-20 15:21:31 -0800 | [diff] [blame] | 113 | uint32_t ffcr; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 114 | |
| 115 | ETB_UNLOCK(); |
| 116 | |
Pratik Patel | 6a32d26 | 2012-01-20 15:21:31 -0800 | [diff] [blame] | 117 | ffcr = etb_readl(etb, ETB_FFCR); |
| 118 | ffcr |= (BIT(12) | BIT(6)); |
| 119 | etb_writel(etb, ffcr, ETB_FFCR); |
| 120 | |
| 121 | for (count = TIMEOUT_US; BVAL(etb_readl(etb, ETB_FFCR), 6) != 0 |
| 122 | && count > 0; count--) |
| 123 | udelay(1); |
| 124 | WARN(count == 0, "timeout while flushing ETB, ETB_FFCR: %#x\n", |
| 125 | etb_readl(etb, ETB_FFCR)); |
| 126 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 127 | etb_writel(etb, 0x0, ETB_CTL_REG); |
| 128 | |
| 129 | for (count = TIMEOUT_US; BVAL(etb_readl(etb, ETB_FFSR), 1) != 1 |
| 130 | && count > 0; count--) |
| 131 | udelay(1); |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 132 | WARN(count == 0, "timeout while disabling ETB, ETB_FFSR: %#x\n", |
| 133 | etb_readl(etb, ETB_FFSR)); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 134 | |
| 135 | ETB_LOCK(); |
| 136 | } |
| 137 | |
| 138 | void etb_disable(void) |
| 139 | { |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 140 | mutex_lock(&etb.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 141 | __etb_disable(); |
| 142 | etb.enabled = false; |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 143 | dev_info(etb.dev, "ETB disabled\n"); |
| 144 | mutex_unlock(&etb.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static void __etb_dump(void) |
| 148 | { |
| 149 | int i; |
| 150 | uint8_t *buf_ptr; |
| 151 | uint32_t read_data; |
| 152 | uint32_t read_ptr; |
| 153 | uint32_t write_ptr; |
Pratik Patel | 70f5e39 | 2011-09-24 21:23:27 -0700 | [diff] [blame] | 154 | uint32_t frame_off; |
| 155 | uint32_t frame_endoff; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 156 | |
| 157 | ETB_UNLOCK(); |
| 158 | |
| 159 | read_ptr = etb_readl(etb, ETB_RAM_READ_POINTER); |
| 160 | write_ptr = etb_readl(etb, ETB_RAM_WRITE_POINTER); |
| 161 | |
Pratik Patel | 70f5e39 | 2011-09-24 21:23:27 -0700 | [diff] [blame] | 162 | frame_off = write_ptr % FRAME_SIZE_WORDS; |
| 163 | frame_endoff = FRAME_SIZE_WORDS - frame_off; |
| 164 | if (frame_off) { |
| 165 | dev_err(etb.dev, "write_ptr: %lu not aligned to formatter " |
| 166 | "frame size\n", (unsigned long)write_ptr); |
| 167 | dev_err(etb.dev, "frameoff: %lu, frame_endoff: %lu\n", |
| 168 | (unsigned long)frame_off, (unsigned long)frame_endoff); |
| 169 | write_ptr += frame_endoff; |
| 170 | } |
| 171 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 172 | if ((etb_readl(etb, ETB_STATUS_REG) & BIT(0)) == 0) |
| 173 | etb_writel(etb, 0x0, ETB_RAM_READ_POINTER); |
| 174 | else |
| 175 | etb_writel(etb, write_ptr, ETB_RAM_READ_POINTER); |
| 176 | |
| 177 | buf_ptr = etb.buf; |
| 178 | for (i = 0; i < ETB_SIZE_WORDS; i++) { |
| 179 | read_data = etb_readl(etb, ETB_RAM_READ_DATA_REG); |
Pratik Patel | 70f5e39 | 2011-09-24 21:23:27 -0700 | [diff] [blame] | 180 | *buf_ptr++ = read_data >> 0; |
| 181 | *buf_ptr++ = read_data >> 8; |
| 182 | *buf_ptr++ = read_data >> 16; |
| 183 | *buf_ptr++ = read_data >> 24; |
| 184 | } |
| 185 | |
| 186 | if (frame_off) { |
| 187 | buf_ptr -= (frame_endoff * BYTES_PER_WORD); |
| 188 | for (i = 0; i < frame_endoff; i++) { |
| 189 | *buf_ptr++ = 0x0; |
| 190 | *buf_ptr++ = 0x0; |
| 191 | *buf_ptr++ = 0x0; |
| 192 | *buf_ptr++ = 0x0; |
| 193 | } |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | etb_writel(etb, read_ptr, ETB_RAM_READ_POINTER); |
| 197 | |
| 198 | ETB_LOCK(); |
| 199 | } |
| 200 | |
| 201 | void etb_dump(void) |
| 202 | { |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 203 | mutex_lock(&etb.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 204 | if (etb.enabled) { |
| 205 | __etb_disable(); |
| 206 | __etb_dump(); |
| 207 | __etb_enable(); |
| 208 | |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 209 | dev_info(etb.dev, "ETB dumped\n"); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 210 | } |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 211 | mutex_unlock(&etb.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static int etb_open(struct inode *inode, struct file *file) |
| 215 | { |
| 216 | if (atomic_cmpxchg(&etb.in_use, 0, 1)) |
| 217 | return -EBUSY; |
| 218 | |
| 219 | dev_dbg(etb.dev, "%s: successfully opened\n", __func__); |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | static ssize_t etb_read(struct file *file, char __user *data, |
| 224 | size_t len, loff_t *ppos) |
| 225 | { |
| 226 | if (etb.reading == false) { |
| 227 | etb_dump(); |
| 228 | etb.reading = true; |
| 229 | } |
| 230 | |
| 231 | if (*ppos + len > ETB_SIZE_WORDS * BYTES_PER_WORD) |
| 232 | len = ETB_SIZE_WORDS * BYTES_PER_WORD - *ppos; |
| 233 | |
| 234 | if (copy_to_user(data, etb.buf + *ppos, len)) { |
| 235 | dev_dbg(etb.dev, "%s: copy_to_user failed\n", __func__); |
| 236 | return -EFAULT; |
| 237 | } |
| 238 | |
| 239 | *ppos += len; |
| 240 | |
| 241 | dev_dbg(etb.dev, "%s: %d bytes copied, %d bytes left\n", |
| 242 | __func__, len, (int) (ETB_SIZE_WORDS * BYTES_PER_WORD - *ppos)); |
| 243 | |
| 244 | return len; |
| 245 | } |
| 246 | |
| 247 | static int etb_release(struct inode *inode, struct file *file) |
| 248 | { |
| 249 | etb.reading = false; |
| 250 | |
| 251 | atomic_set(&etb.in_use, 0); |
| 252 | |
| 253 | dev_dbg(etb.dev, "%s: released\n", __func__); |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static const struct file_operations etb_fops = { |
| 259 | .owner = THIS_MODULE, |
| 260 | .open = etb_open, |
| 261 | .read = etb_read, |
| 262 | .release = etb_release, |
| 263 | }; |
| 264 | |
| 265 | static struct miscdevice etb_misc = { |
| 266 | .name = "msm_etb", |
| 267 | .minor = MISC_DYNAMIC_MINOR, |
| 268 | .fops = &etb_fops, |
| 269 | }; |
| 270 | |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 271 | #define ETB_ATTR(__name) \ |
| 272 | static struct kobj_attribute __name##_attr = \ |
| 273 | __ATTR(__name, S_IRUGO | S_IWUSR, __name##_show, __name##_store) |
| 274 | |
| 275 | static ssize_t trigger_cntr_store(struct kobject *kobj, |
| 276 | struct kobj_attribute *attr, |
| 277 | const char *buf, size_t n) |
| 278 | { |
| 279 | unsigned long val; |
| 280 | |
| 281 | if (sscanf(buf, "%lx", &val) != 1) |
| 282 | return -EINVAL; |
| 283 | |
| 284 | etb.trigger_cntr = val; |
| 285 | return n; |
| 286 | } |
| 287 | static ssize_t trigger_cntr_show(struct kobject *kobj, |
| 288 | struct kobj_attribute *attr, |
| 289 | char *buf) |
| 290 | { |
| 291 | unsigned long val = etb.trigger_cntr; |
| 292 | return scnprintf(buf, PAGE_SIZE, "%#lx\n", val); |
| 293 | } |
| 294 | ETB_ATTR(trigger_cntr); |
| 295 | |
| 296 | static int __init etb_sysfs_init(void) |
| 297 | { |
| 298 | int ret; |
| 299 | |
| 300 | etb.kobj = kobject_create_and_add("etb", qdss_get_modulekobj()); |
| 301 | if (!etb.kobj) { |
| 302 | dev_err(etb.dev, "failed to create ETB sysfs kobject\n"); |
| 303 | ret = -ENOMEM; |
| 304 | goto err_create; |
| 305 | } |
| 306 | |
| 307 | ret = sysfs_create_file(etb.kobj, &trigger_cntr_attr.attr); |
| 308 | if (ret) { |
| 309 | dev_err(etb.dev, "failed to create ETB sysfs trigger_cntr" |
| 310 | " attribute\n"); |
| 311 | goto err_file; |
| 312 | } |
| 313 | |
| 314 | return 0; |
| 315 | err_file: |
| 316 | kobject_put(etb.kobj); |
| 317 | err_create: |
| 318 | return ret; |
| 319 | } |
| 320 | |
| 321 | static void etb_sysfs_exit(void) |
| 322 | { |
| 323 | sysfs_remove_file(etb.kobj, &trigger_cntr_attr.attr); |
| 324 | kobject_put(etb.kobj); |
| 325 | } |
| 326 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 327 | static int __devinit etb_probe(struct platform_device *pdev) |
| 328 | { |
| 329 | int ret; |
| 330 | struct resource *res; |
| 331 | |
| 332 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 333 | if (!res) { |
| 334 | ret = -EINVAL; |
| 335 | goto err_res; |
| 336 | } |
| 337 | |
| 338 | etb.base = ioremap_nocache(res->start, resource_size(res)); |
| 339 | if (!etb.base) { |
| 340 | ret = -EINVAL; |
| 341 | goto err_ioremap; |
| 342 | } |
| 343 | |
| 344 | etb.dev = &pdev->dev; |
| 345 | |
Pratik Patel | 9dc7022 | 2012-03-07 12:09:13 -0800 | [diff] [blame] | 346 | mutex_init(&etb.mutex); |
| 347 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 348 | ret = misc_register(&etb_misc); |
| 349 | if (ret) |
| 350 | goto err_misc; |
| 351 | |
| 352 | etb.buf = kzalloc(ETB_SIZE_WORDS * BYTES_PER_WORD, GFP_KERNEL); |
| 353 | if (!etb.buf) { |
| 354 | ret = -ENOMEM; |
| 355 | goto err_alloc; |
| 356 | } |
| 357 | |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 358 | etb_sysfs_init(); |
| 359 | |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 360 | dev_info(etb.dev, "ETB initialized\n"); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 361 | return 0; |
| 362 | |
| 363 | err_alloc: |
| 364 | misc_deregister(&etb_misc); |
| 365 | err_misc: |
Pratik Patel | 9dc7022 | 2012-03-07 12:09:13 -0800 | [diff] [blame] | 366 | mutex_destroy(&etb.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 367 | iounmap(etb.base); |
| 368 | err_ioremap: |
| 369 | err_res: |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 370 | dev_err(etb.dev, "ETB init failed\n"); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 371 | return ret; |
| 372 | } |
| 373 | |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 374 | static int etb_remove(struct platform_device *pdev) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 375 | { |
| 376 | if (etb.enabled) |
| 377 | etb_disable(); |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 378 | etb_sysfs_exit(); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 379 | kfree(etb.buf); |
| 380 | misc_deregister(&etb_misc); |
Pratik Patel | 9dc7022 | 2012-03-07 12:09:13 -0800 | [diff] [blame] | 381 | mutex_destroy(&etb.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 382 | iounmap(etb.base); |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static struct platform_driver etb_driver = { |
| 388 | .probe = etb_probe, |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 389 | .remove = etb_remove, |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 390 | .driver = { |
| 391 | .name = "msm_etb", |
| 392 | }, |
| 393 | }; |
| 394 | |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 395 | int __init etb_init(void) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 396 | { |
| 397 | return platform_driver_register(&etb_driver); |
| 398 | } |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 399 | |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 400 | void etb_exit(void) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 401 | { |
| 402 | platform_driver_unregister(&etb_driver); |
| 403 | } |