blob: c6df80dc0eead063441ca1d4d9fb479ef96c3fd7 [file] [log] [blame]
Pratik Patel5ecf6a12012-04-25 18:34:59 -07001/* 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>
21#include <linux/fs.h>
22#include <linux/miscdevice.h>
23#include <linux/uaccess.h>
24#include <linux/slab.h>
Pratik Patel43e47bd2012-05-19 18:10:55 -070025#include <linux/cs.h>
Pratik Patel5ecf6a12012-04-25 18:34:59 -070026#include <asm/unaligned.h>
27#include <mach/stm.h>
28
Pratik Patel33454d22012-05-18 10:48:17 -070029#include "cs-priv.h"
Pratik Patel5ecf6a12012-04-25 18:34:59 -070030
31#define stm_writel(stm, val, off) \
32 __raw_writel((val), stm.base + off)
33#define stm_readl(stm, val, off) \
34 __raw_readl(stm.base + off)
35
36#define NR_STM_CHANNEL (32)
37#define BYTES_PER_CHANNEL (256)
38
39enum {
40 STM_PKT_TYPE_DATA = 0x98,
41 STM_PKT_TYPE_FLAG = 0xE8,
42 STM_PKT_TYPE_TRIG = 0xF8,
43};
44
45enum {
46 STM_OPTION_MARKED = 0x10,
47};
48
49#define STM_TRACE_BUF_SIZE (1024)
50
51#define OST_START_TOKEN (0x30)
52#define OST_VERSION (0x1)
53
54#define stm_channel_addr(ch) \
55 (stm.chs.base + (ch * BYTES_PER_CHANNEL))
56#define stm_channel_off(type, opts) (type & ~opts)
57
58#define STM_LOCK() \
59do { \
60 mb(); \
61 stm_writel(stm, 0x0, CS_LAR); \
62} while (0)
63#define STM_UNLOCK() \
64do { \
65 stm_writel(stm, CS_UNLOCK_MAGIC, CS_LAR); \
66 mb(); \
67} while (0)
68
69#define STMSPER (0xE00)
70#define STMSPTER (0xE20)
71#define STMTCSR (0xE80)
72#define STMSYNCR (0xE90)
73
74#ifdef CONFIG_MSM_QDSS_STM_DEFAULT_ENABLE
75static int stm_boot_enable = 1;
76#else
77static int stm_boot_enable;
78#endif
79
80module_param_named(
81 stm_boot_enable, stm_boot_enable, int, S_IRUGO
82);
83
84static int stm_boot_nr_channel;
85
86module_param_named(
87 stm_boot_nr_channel, stm_boot_nr_channel, int, S_IRUGO
88);
89
90struct channel_space {
91 void __iomem *base;
92 unsigned long *bitmap;
93};
94
95struct stm_ctx {
96 void __iomem *base;
97 bool enabled;
98 struct qdss_source *src;
99 struct device *dev;
100 struct kobject *kobj;
101 uint32_t entity;
102 struct channel_space chs;
103};
104
105static struct stm_ctx stm = {
106 .entity = OST_ENTITY_ALL,
107};
108
109
110static void __stm_enable(void)
111{
112 STM_UNLOCK();
113
114 stm_writel(stm, 0x80, STMSYNCR);
115 stm_writel(stm, 0xFFFFFFFF, STMSPTER);
116 stm_writel(stm, 0xFFFFFFFF, STMSPER);
117 stm_writel(stm, 0x30003, STMTCSR);
118
119 STM_LOCK();
120}
121
122static int stm_enable(void)
123{
124 int ret;
125
126 if (stm.enabled) {
127 dev_err(stm.dev, "STM tracing already enabled\n");
128 ret = -EINVAL;
129 goto err;
130 }
131
132 ret = qdss_enable(stm.src);
133 if (ret)
134 goto err;
135
136 __stm_enable();
137
138 stm.enabled = true;
139
140 dev_info(stm.dev, "STM tracing enabled\n");
141 return 0;
142
143err:
144 return ret;
145}
146
147static void __stm_disable(void)
148{
149 STM_UNLOCK();
150
151 stm_writel(stm, 0x30000, STMTCSR);
152 stm_writel(stm, 0x0, STMSPER);
153 stm_writel(stm, 0x0, STMSPTER);
154
155 STM_LOCK();
156}
157
158static int stm_disable(void)
159{
160 int ret;
161
162 if (!stm.enabled) {
163 dev_err(stm.dev, "STM tracing already disabled\n");
164 ret = -EINVAL;
165 goto err;
166 }
167
168 __stm_disable();
169
170 qdss_disable(stm.src);
171
172 stm.enabled = false;
173
174 dev_info(stm.dev, "STM tracing disabled\n");
175 return 0;
176
177err:
178 return ret;
179}
180
181static uint32_t stm_channel_alloc(uint32_t off)
182{
183 uint32_t ch;
184
185 do {
186 ch = find_next_zero_bit(stm.chs.bitmap, NR_STM_CHANNEL, off);
187 } while ((ch < NR_STM_CHANNEL) && test_and_set_bit(ch, stm.chs.bitmap));
188
189 return ch;
190}
191
192static void stm_channel_free(uint32_t ch)
193{
194 clear_bit(ch, stm.chs.bitmap);
195}
196
197static int stm_send(void *addr, const void *data, uint32_t size)
198{
199 uint64_t prepad = 0;
200 uint64_t postpad = 0;
201 char *pad;
202 uint8_t off, endoff;
203 uint32_t len = size;
204
205 /* only 64bit writes are supported, we rely on the compiler to
206 * generate STRD instruction for the casted 64bit assignments
207 */
208
209 off = (unsigned long)data & 0x7;
210
211 if (off) {
212 endoff = 8 - off;
213 pad = (char *)&prepad;
214 pad += off;
215
216 while (endoff && size) {
217 *pad++ = *(char *)data++;
218 endoff--;
219 size--;
220 }
221 *(volatile uint64_t __force *)addr = prepad;
222 }
223
224 /* now we are 64bit aligned */
225 while (size >= 8) {
226 *(volatile uint64_t __force *)addr = *(uint64_t *)data;
227 data += 8;
228 size -= 8;
229 }
230
231 if (size) {
232 pad = (char *)&postpad;
233
234 while (size) {
235 *pad++ = *(char *)data++;
236 size--;
237 }
238 *(volatile uint64_t __force *)addr = postpad;
239 }
240
241 return roundup(len + off, 8);
242}
243
244static int stm_trace_ost_header(unsigned long ch_addr, uint32_t options,
245 uint8_t entity_id, uint8_t proto_id,
246 const void *payload_data, uint32_t payload_size)
247{
248 void *addr;
249 uint8_t prepad_size;
250 uint64_t header;
251 char *hdr;
252
253 hdr = (char *)&header;
254
255 hdr[0] = OST_START_TOKEN;
256 hdr[1] = OST_VERSION;
257 hdr[2] = entity_id;
258 hdr[3] = proto_id;
259 prepad_size = (unsigned long)payload_data & 0x7;
260 *(uint32_t *)(hdr + 4) = (prepad_size << 24) | payload_size;
261
262 /* for 64bit writes, header is expected to be of the D32M, D32M */
263 options |= STM_OPTION_MARKED;
264 options &= ~STM_OPTION_TIMESTAMPED;
265 addr = (void *)(ch_addr | stm_channel_off(STM_PKT_TYPE_DATA, options));
266
267 return stm_send(addr, &header, sizeof(header));
268}
269
270static int stm_trace_data(unsigned long ch_addr, uint32_t options,
271 const void *data, uint32_t size)
272{
273 void *addr;
274
275 options &= ~STM_OPTION_TIMESTAMPED;
276 addr = (void *)(ch_addr | stm_channel_off(STM_PKT_TYPE_DATA, options));
277
278 return stm_send(addr, data, size);
279}
280
281static int stm_trace_ost_tail(unsigned long ch_addr, uint32_t options)
282{
283 void *addr;
284 uint64_t tail = 0x0;
285
286 addr = (void *)(ch_addr | stm_channel_off(STM_PKT_TYPE_FLAG, options));
287
288 return stm_send(addr, &tail, sizeof(tail));
289}
290
291static inline int __stm_trace(uint32_t options, uint8_t entity_id,
292 uint8_t proto_id, const void *data, uint32_t size)
293{
294 int len = 0;
295 uint32_t ch;
296 unsigned long ch_addr;
297
298 /* allocate channel and get the channel address */
299 ch = stm_channel_alloc(0);
300 ch_addr = (unsigned long)stm_channel_addr(ch);
301
302 /* send the ost header */
303 len += stm_trace_ost_header(ch_addr, options, entity_id, proto_id, data,
304 size);
305
306 /* send the payload data */
307 len += stm_trace_data(ch_addr, options, data, size);
308
309 /* send the ost tail */
310 len += stm_trace_ost_tail(ch_addr, options);
311
312 /* we are done, free the channel */
313 stm_channel_free(ch);
314
315 return len;
316}
317
318/**
319 * stm_trace - trace the binary or string data through STM
320 * @options: tracing options - guaranteed, timestamped, etc
321 * @entity_id: entity representing the trace data
322 * @proto_id: protocol id to distinguish between different binary formats
323 * @data: pointer to binary or string data buffer
324 * @size: size of data to send
325 *
326 * Packetizes the data as the payload to an OST packet and sends it over STM
327 *
328 * CONTEXT:
329 * Can be called from any context.
330 *
331 * RETURNS:
332 * number of bytes transfered over STM
333 */
334int stm_trace(uint32_t options, uint8_t entity_id, uint8_t proto_id,
335 const void *data, uint32_t size)
336{
337 /* we don't support sizes more than 24bits (0 to 23) */
338 if (!(stm.enabled && (stm.entity & entity_id) &&
339 (size < 0x1000000)))
340 return 0;
341
342 return __stm_trace(options, entity_id, proto_id, data, size);
343}
344EXPORT_SYMBOL(stm_trace);
345
346static ssize_t stm_write(struct file *file, const char __user *data,
347 size_t size, loff_t *ppos)
348{
349 char *buf;
350
351 if (!stm.enabled)
352 return -EINVAL;
353
354 if (!(stm.entity & OST_ENTITY_DEV_NODE))
355 return size;
356
357 if (size > STM_TRACE_BUF_SIZE)
358 size = STM_TRACE_BUF_SIZE;
359
360 buf = kmalloc(size, GFP_KERNEL);
361 if (!buf)
362 return -ENOMEM;
363
364 if (copy_from_user(buf, data, size)) {
365 kfree(buf);
366 dev_dbg(stm.dev, "%s: copy_from_user failed\n", __func__);
367 return -EFAULT;
368 }
369
370 __stm_trace(STM_OPTION_TIMESTAMPED, OST_ENTITY_DEV_NODE, 0, buf, size);
371
372 kfree(buf);
373
374 return size;
375}
376
377static const struct file_operations stm_fops = {
378 .owner = THIS_MODULE,
379 .write = stm_write,
380 .llseek = no_llseek,
381};
382
383static struct miscdevice stm_misc = {
384 .name = "msm_stm",
385 .minor = MISC_DYNAMIC_MINOR,
386 .fops = &stm_fops,
387};
388
389#define STM_ATTR(__name) \
390static struct kobj_attribute __name##_attr = \
391 __ATTR(__name, S_IRUGO | S_IWUSR, __name##_show, __name##_store)
392
393static ssize_t enabled_store(struct kobject *kobj,
394 struct kobj_attribute *attr,
395 const char *buf, size_t n)
396{
397 int ret = 0;
398 unsigned long val;
399
400 if (sscanf(buf, "%lx", &val) != 1)
401 return -EINVAL;
402
403 if (val)
404 ret = stm_enable();
405 else
406 ret = stm_disable();
407
408 if (ret)
409 return ret;
410 return n;
411}
412static ssize_t enabled_show(struct kobject *kobj,
413 struct kobj_attribute *attr,
414 char *buf)
415{
416 unsigned long val = stm.enabled;
417 return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
418}
419STM_ATTR(enabled);
420
421static ssize_t entity_store(struct kobject *kobj,
422 struct kobj_attribute *attr,
423 const char *buf, size_t n)
424{
425 unsigned long val;
426
427 if (sscanf(buf, "%lx", &val) != 1)
428 return -EINVAL;
429
430 stm.entity = val;
431 return n;
432}
433static ssize_t entity_show(struct kobject *kobj,
434 struct kobj_attribute *attr,
435 char *buf)
436{
437 unsigned long val = stm.entity;
438 return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
439}
440STM_ATTR(entity);
441
442static int __devinit stm_sysfs_init(void)
443{
444 int ret;
445
446 stm.kobj = kobject_create_and_add("stm", qdss_get_modulekobj());
447 if (!stm.kobj) {
448 dev_err(stm.dev, "failed to create STM sysfs kobject\n");
449 ret = -ENOMEM;
450 goto err_create;
451 }
452
453 ret = sysfs_create_file(stm.kobj, &enabled_attr.attr);
454 if (ret) {
455 dev_err(stm.dev, "failed to create STM sysfs enabled attr\n");
456 goto err_file;
457 }
458
459 if (sysfs_create_file(stm.kobj, &entity_attr.attr))
460 dev_err(stm.dev, "failed to create STM sysfs entity attr\n");
461
462 return 0;
463err_file:
464 kobject_put(stm.kobj);
465err_create:
466 return ret;
467}
468
469static void __devexit stm_sysfs_exit(void)
470{
471 sysfs_remove_file(stm.kobj, &entity_attr.attr);
472 sysfs_remove_file(stm.kobj, &enabled_attr.attr);
473 kobject_put(stm.kobj);
474}
475
476static int __devinit stm_probe(struct platform_device *pdev)
477{
478 int ret;
479 struct resource *res;
480 size_t res_size, bitmap_size;
481
482 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
483 if (!res) {
484 ret = -EINVAL;
485 goto err_res0;
486 }
487
488 stm.base = ioremap_nocache(res->start, resource_size(res));
489 if (!stm.base) {
490 ret = -EINVAL;
491 goto err_ioremap0;
492 }
493
494 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
495 if (!res) {
496 ret = -EINVAL;
497 goto err_res1;
498 }
499
500 if (stm_boot_nr_channel) {
501 res_size = min((resource_size_t)(stm_boot_nr_channel *
502 BYTES_PER_CHANNEL), resource_size(res));
503 bitmap_size = stm_boot_nr_channel * sizeof(long);
504 } else {
505 res_size = min((resource_size_t)(NR_STM_CHANNEL *
506 BYTES_PER_CHANNEL), resource_size(res));
507 bitmap_size = NR_STM_CHANNEL * sizeof(long);
508 }
509
510 stm.chs.bitmap = kzalloc(bitmap_size, GFP_KERNEL);
511 if (!stm.chs.bitmap) {
512 ret = -ENOMEM;
513 goto err_bitmap;
514 }
515
516 stm.chs.base = ioremap_nocache(res->start, res_size);
517 if (!stm.chs.base) {
518 ret = -EINVAL;
519 goto err_ioremap1;
520 }
521
522 stm.dev = &pdev->dev;
523
524 ret = misc_register(&stm_misc);
525 if (ret)
526 goto err_misc;
527
528 stm.src = qdss_get("msm_stm");
529 if (IS_ERR(stm.src)) {
530 ret = PTR_ERR(stm.src);
531 goto err_qdssget;
532 }
533
534 ret = stm_sysfs_init();
535 if (ret)
536 goto err_sysfs;
537
538 if (stm_boot_enable)
539 stm_enable();
540
541 dev_info(stm.dev, "STM initialized\n");
542 return 0;
543
544err_sysfs:
545 qdss_put(stm.src);
546err_qdssget:
547 misc_deregister(&stm_misc);
548err_misc:
549 iounmap(stm.chs.base);
550err_ioremap1:
551 kfree(stm.chs.bitmap);
552err_bitmap:
553err_res1:
554 iounmap(stm.base);
555err_ioremap0:
556err_res0:
557 dev_err(stm.dev, "STM init failed\n");
558 return ret;
559}
560
561static int __devexit stm_remove(struct platform_device *pdev)
562{
563 if (stm.enabled)
564 stm_disable();
565 stm_sysfs_exit();
566 qdss_put(stm.src);
567 misc_deregister(&stm_misc);
568 iounmap(stm.chs.base);
569 kfree(stm.chs.bitmap);
570 iounmap(stm.base);
571
572 return 0;
573}
574
Pratik Patel9eae4822012-05-14 17:34:53 -0700575static struct of_device_id stm_match[] = {
576 {.compatible = "qcom,msm-stm"},
577 {}
578};
579
Pratik Patel5ecf6a12012-04-25 18:34:59 -0700580static struct platform_driver stm_driver = {
581 .probe = stm_probe,
582 .remove = __devexit_p(stm_remove),
583 .driver = {
584 .name = "msm_stm",
Pratik Patel9eae4822012-05-14 17:34:53 -0700585 .owner = THIS_MODULE,
586 .of_match_table = stm_match,
Pratik Patel5ecf6a12012-04-25 18:34:59 -0700587 },
588};
589
590static int __init stm_init(void)
591{
592 return platform_driver_register(&stm_driver);
593}
594module_init(stm_init);
595
596static void __exit stm_exit(void)
597{
598 platform_driver_unregister(&stm_driver);
599}
600module_exit(stm_exit);
601
602MODULE_LICENSE("GPL v2");
603MODULE_DESCRIPTION("CoreSight System Trace Macrocell driver");