blob: e4c9811c1f400ceabb8b73f7bd17f117299feb9a [file] [log] [blame]
Alexander Shishkinb27a6a32015-09-22 15:47:16 +03001/*
2 * Intel(R) Trace Hub Global Trace Hub
3 *
4 * Copyright (C) 2014-2015 Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 */
15
16#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
18#include <linux/types.h>
19#include <linux/module.h>
20#include <linux/device.h>
21#include <linux/io.h>
22#include <linux/mm.h>
23#include <linux/slab.h>
24#include <linux/bitmap.h>
25
26#include "intel_th.h"
27#include "gth.h"
28
29struct gth_device;
30
31/**
32 * struct gth_output - GTH view on an output port
33 * @gth: backlink to the GTH device
34 * @output: link to output device's output descriptor
35 * @index: output port number
36 * @port_type: one of GTH_* port type values
37 * @master: bitmap of masters configured for this output
38 */
39struct gth_output {
40 struct gth_device *gth;
41 struct intel_th_output *output;
42 unsigned int index;
43 unsigned int port_type;
44 DECLARE_BITMAP(master, TH_CONFIGURABLE_MASTERS + 1);
45};
46
47/**
48 * struct gth_device - GTH device
49 * @dev: driver core's device
50 * @base: register window base address
51 * @output_group: attributes describing output ports
52 * @master_group: attributes describing master assignments
53 * @output: output ports
54 * @master: master/output port assignments
55 * @gth_lock: serializes accesses to GTH bits
56 */
57struct gth_device {
58 struct device *dev;
59 void __iomem *base;
60
61 struct attribute_group output_group;
62 struct attribute_group master_group;
63 struct gth_output output[TH_POSSIBLE_OUTPUTS];
64 signed char master[TH_CONFIGURABLE_MASTERS + 1];
65 spinlock_t gth_lock;
66};
67
68static void gth_output_set(struct gth_device *gth, int port,
69 unsigned int config)
70{
71 unsigned long reg = port & 4 ? REG_GTH_GTHOPT1 : REG_GTH_GTHOPT0;
72 u32 val;
73 int shift = (port & 3) * 8;
74
75 val = ioread32(gth->base + reg);
76 val &= ~(0xff << shift);
77 val |= config << shift;
78 iowrite32(val, gth->base + reg);
79}
80
81static unsigned int gth_output_get(struct gth_device *gth, int port)
82{
83 unsigned long reg = port & 4 ? REG_GTH_GTHOPT1 : REG_GTH_GTHOPT0;
84 u32 val;
85 int shift = (port & 3) * 8;
86
87 val = ioread32(gth->base + reg);
88 val &= 0xff << shift;
89 val >>= shift;
90
91 return val;
92}
93
94static void gth_smcfreq_set(struct gth_device *gth, int port,
95 unsigned int freq)
96{
97 unsigned long reg = REG_GTH_SMCR0 + ((port / 2) * 4);
98 int shift = (port & 1) * 16;
99 u32 val;
100
101 val = ioread32(gth->base + reg);
102 val &= ~(0xffff << shift);
103 val |= freq << shift;
104 iowrite32(val, gth->base + reg);
105}
106
107static unsigned int gth_smcfreq_get(struct gth_device *gth, int port)
108{
109 unsigned long reg = REG_GTH_SMCR0 + ((port / 2) * 4);
110 int shift = (port & 1) * 16;
111 u32 val;
112
113 val = ioread32(gth->base + reg);
114 val &= 0xffff << shift;
115 val >>= shift;
116
117 return val;
118}
119
120/*
121 * "masters" attribute group
122 */
123
124struct master_attribute {
125 struct device_attribute attr;
126 struct gth_device *gth;
127 unsigned int master;
128};
129
130static void
131gth_master_set(struct gth_device *gth, unsigned int master, int port)
132{
133 unsigned int reg = REG_GTH_SWDEST0 + ((master >> 1) & ~3u);
134 unsigned int shift = (master & 0x7) * 4;
135 u32 val;
136
137 if (master >= 256) {
138 reg = REG_GTH_GSWTDEST;
139 shift = 0;
140 }
141
142 val = ioread32(gth->base + reg);
143 val &= ~(0xf << shift);
144 if (port >= 0)
145 val |= (0x8 | port) << shift;
146 iowrite32(val, gth->base + reg);
147}
148
Alexander Shishkinb27a6a32015-09-22 15:47:16 +0300149static ssize_t master_attr_show(struct device *dev,
150 struct device_attribute *attr,
151 char *buf)
152{
153 struct master_attribute *ma =
154 container_of(attr, struct master_attribute, attr);
155 struct gth_device *gth = ma->gth;
156 size_t count;
157 int port;
158
159 spin_lock(&gth->gth_lock);
160 port = gth->master[ma->master];
161 spin_unlock(&gth->gth_lock);
162
163 if (port >= 0)
164 count = snprintf(buf, PAGE_SIZE, "%x\n", port);
165 else
166 count = snprintf(buf, PAGE_SIZE, "disabled\n");
167
168 return count;
169}
170
171static ssize_t master_attr_store(struct device *dev,
172 struct device_attribute *attr,
173 const char *buf, size_t count)
174{
175 struct master_attribute *ma =
176 container_of(attr, struct master_attribute, attr);
177 struct gth_device *gth = ma->gth;
178 int old_port, port;
179
180 if (kstrtoint(buf, 10, &port) < 0)
181 return -EINVAL;
182
183 if (port >= TH_POSSIBLE_OUTPUTS || port < -1)
184 return -EINVAL;
185
186 spin_lock(&gth->gth_lock);
187
188 /* disconnect from the previous output port, if any */
189 old_port = gth->master[ma->master];
190 if (old_port >= 0) {
191 gth->master[ma->master] = -1;
192 clear_bit(ma->master, gth->output[old_port].master);
193 if (gth->output[old_port].output->active)
194 gth_master_set(gth, ma->master, -1);
195 }
196
197 /* connect to the new output port, if any */
198 if (port >= 0) {
199 /* check if there's a driver for this port */
200 if (!gth->output[port].output) {
201 count = -ENODEV;
202 goto unlock;
203 }
204
205 set_bit(ma->master, gth->output[port].master);
206
207 /* if the port is active, program this setting */
208 if (gth->output[port].output->active)
209 gth_master_set(gth, ma->master, port);
210 }
211
212 gth->master[ma->master] = port;
213
214unlock:
215 spin_unlock(&gth->gth_lock);
216
217 return count;
218}
219
220struct output_attribute {
221 struct device_attribute attr;
222 struct gth_device *gth;
223 unsigned int port;
224 unsigned int parm;
225};
226
227#define OUTPUT_PARM(_name, _mask, _r, _w, _what) \
228 [TH_OUTPUT_PARM(_name)] = { .name = __stringify(_name), \
229 .get = gth_ ## _what ## _get, \
230 .set = gth_ ## _what ## _set, \
231 .mask = (_mask), \
232 .readable = (_r), \
233 .writable = (_w) }
234
235static const struct output_parm {
236 const char *name;
237 unsigned int (*get)(struct gth_device *gth, int port);
238 void (*set)(struct gth_device *gth, int port,
239 unsigned int val);
240 unsigned int mask;
241 unsigned int readable : 1,
242 writable : 1;
243} output_parms[] = {
244 OUTPUT_PARM(port, 0x7, 1, 0, output),
245 OUTPUT_PARM(null, BIT(3), 1, 1, output),
246 OUTPUT_PARM(drop, BIT(4), 1, 1, output),
247 OUTPUT_PARM(reset, BIT(5), 1, 0, output),
248 OUTPUT_PARM(flush, BIT(7), 0, 1, output),
249 OUTPUT_PARM(smcfreq, 0xffff, 1, 1, smcfreq),
250};
251
252static void
253gth_output_parm_set(struct gth_device *gth, int port, unsigned int parm,
254 unsigned int val)
255{
256 unsigned int config = output_parms[parm].get(gth, port);
257 unsigned int mask = output_parms[parm].mask;
258 unsigned int shift = __ffs(mask);
259
260 config &= ~mask;
261 config |= (val << shift) & mask;
262 output_parms[parm].set(gth, port, config);
263}
264
265static unsigned int
266gth_output_parm_get(struct gth_device *gth, int port, unsigned int parm)
267{
268 unsigned int config = output_parms[parm].get(gth, port);
269 unsigned int mask = output_parms[parm].mask;
270 unsigned int shift = __ffs(mask);
271
272 config &= mask;
273 config >>= shift;
274 return config;
275}
276
277/*
278 * Reset outputs and sources
279 */
280static int intel_th_gth_reset(struct gth_device *gth)
281{
282 u32 scratchpad;
283 int port, i;
284
285 scratchpad = ioread32(gth->base + REG_GTH_SCRPD0);
286 if (scratchpad & SCRPD_DEBUGGER_IN_USE)
287 return -EBUSY;
288
289 /* output ports */
290 for (port = 0; port < 8; port++) {
291 if (gth_output_parm_get(gth, port, TH_OUTPUT_PARM(port)) ==
292 GTH_NONE)
293 continue;
294
295 gth_output_set(gth, port, 0);
296 gth_smcfreq_set(gth, port, 16);
297 }
298 /* disable overrides */
299 iowrite32(0, gth->base + REG_GTH_DESTOVR);
300
301 /* masters swdest_0~31 and gswdest */
302 for (i = 0; i < 33; i++)
303 iowrite32(0, gth->base + REG_GTH_SWDEST0 + i * 4);
304
305 /* sources */
306 iowrite32(0, gth->base + REG_GTH_SCR);
307 iowrite32(0xfc, gth->base + REG_GTH_SCR2);
308
309 return 0;
310}
311
312/*
313 * "outputs" attribute group
314 */
315
316static ssize_t output_attr_show(struct device *dev,
317 struct device_attribute *attr,
318 char *buf)
319{
320 struct output_attribute *oa =
321 container_of(attr, struct output_attribute, attr);
322 struct gth_device *gth = oa->gth;
323 size_t count;
324
325 spin_lock(&gth->gth_lock);
326 count = snprintf(buf, PAGE_SIZE, "%x\n",
327 gth_output_parm_get(gth, oa->port, oa->parm));
328 spin_unlock(&gth->gth_lock);
329
330 return count;
331}
332
333static ssize_t output_attr_store(struct device *dev,
334 struct device_attribute *attr,
335 const char *buf, size_t count)
336{
337 struct output_attribute *oa =
338 container_of(attr, struct output_attribute, attr);
339 struct gth_device *gth = oa->gth;
340 unsigned int config;
341
342 if (kstrtouint(buf, 16, &config) < 0)
343 return -EINVAL;
344
345 spin_lock(&gth->gth_lock);
346 gth_output_parm_set(gth, oa->port, oa->parm, config);
347 spin_unlock(&gth->gth_lock);
348
349 return count;
350}
351
352static int intel_th_master_attributes(struct gth_device *gth)
353{
354 struct master_attribute *master_attrs;
355 struct attribute **attrs;
356 int i, nattrs = TH_CONFIGURABLE_MASTERS + 2;
357
358 attrs = devm_kcalloc(gth->dev, nattrs, sizeof(void *), GFP_KERNEL);
359 if (!attrs)
360 return -ENOMEM;
361
362 master_attrs = devm_kcalloc(gth->dev, nattrs,
363 sizeof(struct master_attribute),
364 GFP_KERNEL);
365 if (!master_attrs)
366 return -ENOMEM;
367
368 for (i = 0; i < TH_CONFIGURABLE_MASTERS + 1; i++) {
369 char *name;
370
371 name = devm_kasprintf(gth->dev, GFP_KERNEL, "%d%s", i,
372 i == TH_CONFIGURABLE_MASTERS ? "+" : "");
373 if (!name)
374 return -ENOMEM;
375
376 master_attrs[i].attr.attr.name = name;
377 master_attrs[i].attr.attr.mode = S_IRUGO | S_IWUSR;
378 master_attrs[i].attr.show = master_attr_show;
379 master_attrs[i].attr.store = master_attr_store;
380
381 sysfs_attr_init(&master_attrs[i].attr.attr);
382 attrs[i] = &master_attrs[i].attr.attr;
383
384 master_attrs[i].gth = gth;
385 master_attrs[i].master = i;
386 }
387
388 gth->master_group.name = "masters";
389 gth->master_group.attrs = attrs;
390
391 return sysfs_create_group(&gth->dev->kobj, &gth->master_group);
392}
393
394static int intel_th_output_attributes(struct gth_device *gth)
395{
396 struct output_attribute *out_attrs;
397 struct attribute **attrs;
398 int i, j, nouts = TH_POSSIBLE_OUTPUTS;
399 int nparms = ARRAY_SIZE(output_parms);
400 int nattrs = nouts * nparms + 1;
401
402 attrs = devm_kcalloc(gth->dev, nattrs, sizeof(void *), GFP_KERNEL);
403 if (!attrs)
404 return -ENOMEM;
405
406 out_attrs = devm_kcalloc(gth->dev, nattrs,
407 sizeof(struct output_attribute),
408 GFP_KERNEL);
409 if (!out_attrs)
410 return -ENOMEM;
411
412 for (i = 0; i < nouts; i++) {
413 for (j = 0; j < nparms; j++) {
414 unsigned int idx = i * nparms + j;
415 char *name;
416
417 name = devm_kasprintf(gth->dev, GFP_KERNEL, "%d_%s", i,
418 output_parms[j].name);
419 if (!name)
420 return -ENOMEM;
421
422 out_attrs[idx].attr.attr.name = name;
423
424 if (output_parms[j].readable) {
425 out_attrs[idx].attr.attr.mode |= S_IRUGO;
426 out_attrs[idx].attr.show = output_attr_show;
427 }
428
429 if (output_parms[j].writable) {
430 out_attrs[idx].attr.attr.mode |= S_IWUSR;
431 out_attrs[idx].attr.store = output_attr_store;
432 }
433
434 sysfs_attr_init(&out_attrs[idx].attr.attr);
435 attrs[idx] = &out_attrs[idx].attr.attr;
436
437 out_attrs[idx].gth = gth;
438 out_attrs[idx].port = i;
439 out_attrs[idx].parm = j;
440 }
441 }
442
443 gth->output_group.name = "outputs";
444 gth->output_group.attrs = attrs;
445
446 return sysfs_create_group(&gth->dev->kobj, &gth->output_group);
447}
448
449/**
450 * intel_th_gth_disable() - enable tracing to an output device
451 * @thdev: GTH device
452 * @output: output device's descriptor
453 *
454 * This will deconfigure all masters set to output to this device,
455 * disable tracing using force storeEn off signal and wait for the
456 * "pipeline empty" bit for corresponding output port.
457 */
458static void intel_th_gth_disable(struct intel_th_device *thdev,
459 struct intel_th_output *output)
460{
461 struct gth_device *gth = dev_get_drvdata(&thdev->dev);
462 unsigned long count;
463 int master;
464 u32 reg;
465
466 spin_lock(&gth->gth_lock);
467 output->active = false;
468
469 for_each_set_bit(master, gth->output[output->port].master,
470 TH_CONFIGURABLE_MASTERS) {
471 gth_master_set(gth, master, -1);
472 }
473 spin_unlock(&gth->gth_lock);
474
475 iowrite32(0, gth->base + REG_GTH_SCR);
476 iowrite32(0xfd, gth->base + REG_GTH_SCR2);
477
478 /* wait on pipeline empty for the given port */
479 for (reg = 0, count = GTH_PLE_WAITLOOP_DEPTH;
480 count && !(reg & BIT(output->port)); count--) {
481 reg = ioread32(gth->base + REG_GTH_STAT);
482 cpu_relax();
483 }
484
485 /* clear force capture done for next captures */
486 iowrite32(0xfc, gth->base + REG_GTH_SCR2);
487
488 if (!count)
489 dev_dbg(&thdev->dev, "timeout waiting for GTH[%d] PLE\n",
490 output->port);
491}
492
493/**
494 * intel_th_gth_enable() - enable tracing to an output device
495 * @thdev: GTH device
496 * @output: output device's descriptor
497 *
498 * This will configure all masters set to output to this device and
499 * enable tracing using force storeEn signal.
500 */
501static void intel_th_gth_enable(struct intel_th_device *thdev,
502 struct intel_th_output *output)
503{
504 struct gth_device *gth = dev_get_drvdata(&thdev->dev);
505 u32 scr = 0xfc0000;
506 int master;
507
508 spin_lock(&gth->gth_lock);
509 for_each_set_bit(master, gth->output[output->port].master,
510 TH_CONFIGURABLE_MASTERS + 1) {
511 gth_master_set(gth, master, output->port);
512 }
513
514 if (output->multiblock)
515 scr |= 0xff;
516
517 output->active = true;
518 spin_unlock(&gth->gth_lock);
519
520 iowrite32(scr, gth->base + REG_GTH_SCR);
521 iowrite32(0, gth->base + REG_GTH_SCR2);
522}
523
524/**
525 * intel_th_gth_assign() - assign output device to a GTH output port
526 * @thdev: GTH device
527 * @othdev: output device
528 *
529 * This will match a given output device parameters against present
530 * output ports on the GTH and fill out relevant bits in output device's
531 * descriptor.
532 *
533 * Return: 0 on success, -errno on error.
534 */
535static int intel_th_gth_assign(struct intel_th_device *thdev,
536 struct intel_th_device *othdev)
537{
538 struct gth_device *gth = dev_get_drvdata(&thdev->dev);
539 int i, id;
540
541 if (othdev->type != INTEL_TH_OUTPUT)
542 return -EINVAL;
543
544 for (i = 0, id = 0; i < TH_POSSIBLE_OUTPUTS; i++) {
545 if (gth->output[i].port_type != othdev->output.type)
546 continue;
547
548 if (othdev->id == -1 || othdev->id == id)
549 goto found;
550
551 id++;
552 }
553
554 return -ENOENT;
555
556found:
557 spin_lock(&gth->gth_lock);
558 othdev->output.port = i;
559 othdev->output.active = false;
560 gth->output[i].output = &othdev->output;
561 spin_unlock(&gth->gth_lock);
562
563 return 0;
564}
565
566/**
567 * intel_th_gth_unassign() - deassociate an output device from its output port
568 * @thdev: GTH device
569 * @othdev: output device
570 */
571static void intel_th_gth_unassign(struct intel_th_device *thdev,
572 struct intel_th_device *othdev)
573{
574 struct gth_device *gth = dev_get_drvdata(&thdev->dev);
575 int port = othdev->output.port;
576
577 spin_lock(&gth->gth_lock);
578 othdev->output.port = -1;
579 othdev->output.active = false;
580 gth->output[port].output = NULL;
581 spin_unlock(&gth->gth_lock);
582}
583
584static int
585intel_th_gth_set_output(struct intel_th_device *thdev, unsigned int master)
586{
587 struct gth_device *gth = dev_get_drvdata(&thdev->dev);
588 int port = 0; /* FIXME: make default output configurable */
589
590 /*
591 * everything above TH_CONFIGURABLE_MASTERS is controlled by the
592 * same register
593 */
594 if (master > TH_CONFIGURABLE_MASTERS)
595 master = TH_CONFIGURABLE_MASTERS;
596
597 spin_lock(&gth->gth_lock);
598 if (gth->master[master] == -1) {
599 set_bit(master, gth->output[port].master);
600 gth->master[master] = port;
601 }
602 spin_unlock(&gth->gth_lock);
603
604 return 0;
605}
606
607static int intel_th_gth_probe(struct intel_th_device *thdev)
608{
609 struct device *dev = &thdev->dev;
610 struct gth_device *gth;
611 struct resource *res;
612 void __iomem *base;
613 int i, ret;
614
615 res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
616 if (!res)
617 return -ENODEV;
618
619 base = devm_ioremap(dev, res->start, resource_size(res));
Dan Carpenter73061da2015-10-16 17:09:13 +0300620 if (!base)
621 return -ENOMEM;
Alexander Shishkinb27a6a32015-09-22 15:47:16 +0300622
623 gth = devm_kzalloc(dev, sizeof(*gth), GFP_KERNEL);
624 if (!gth)
625 return -ENOMEM;
626
627 gth->dev = dev;
628 gth->base = base;
629 spin_lock_init(&gth->gth_lock);
630
631 ret = intel_th_gth_reset(gth);
632 if (ret)
633 return ret;
634
635 for (i = 0; i < TH_CONFIGURABLE_MASTERS + 1; i++)
636 gth->master[i] = -1;
637
638 for (i = 0; i < TH_POSSIBLE_OUTPUTS; i++) {
639 gth->output[i].gth = gth;
640 gth->output[i].index = i;
641 gth->output[i].port_type =
642 gth_output_parm_get(gth, i, TH_OUTPUT_PARM(port));
643 }
644
645 if (intel_th_output_attributes(gth) ||
646 intel_th_master_attributes(gth)) {
647 pr_warn("Can't initialize sysfs attributes\n");
648
649 if (gth->output_group.attrs)
650 sysfs_remove_group(&gth->dev->kobj, &gth->output_group);
651 return -ENOMEM;
652 }
653
654 dev_set_drvdata(dev, gth);
655
656 return 0;
657}
658
659static void intel_th_gth_remove(struct intel_th_device *thdev)
660{
661 struct gth_device *gth = dev_get_drvdata(&thdev->dev);
662
663 sysfs_remove_group(&gth->dev->kobj, &gth->output_group);
664 sysfs_remove_group(&gth->dev->kobj, &gth->master_group);
665}
666
667static struct intel_th_driver intel_th_gth_driver = {
668 .probe = intel_th_gth_probe,
669 .remove = intel_th_gth_remove,
670 .assign = intel_th_gth_assign,
671 .unassign = intel_th_gth_unassign,
672 .set_output = intel_th_gth_set_output,
673 .enable = intel_th_gth_enable,
674 .disable = intel_th_gth_disable,
675 .driver = {
676 .name = "gth",
677 .owner = THIS_MODULE,
678 },
679};
680
681module_driver(intel_th_gth_driver,
682 intel_th_driver_register,
683 intel_th_driver_unregister);
684
685MODULE_ALIAS("intel_th_switch");
686MODULE_LICENSE("GPL v2");
687MODULE_DESCRIPTION("Intel(R) Trace Hub Global Trace Hub driver");
688MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");