1 // SPDX-License-Identifier: GPL-2.0
3 * Intel(R) Trace Hub Global Trace Hub
5 * Copyright (C) 2014-2015 Intel Corporation.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/types.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
15 #include <linux/slab.h>
16 #include <linux/bitmap.h>
17 #include <linux/pm_runtime.h>
25 * struct gth_output - GTH view on an output port
26 * @gth: backlink to the GTH device
27 * @output: link to output device's output descriptor
28 * @index: output port number
29 * @port_type: one of GTH_* port type values
30 * @master: bitmap of masters configured for this output
33 struct gth_device *gth;
34 struct intel_th_output *output;
36 unsigned int port_type;
37 DECLARE_BITMAP(master, TH_CONFIGURABLE_MASTERS + 1);
41 * struct gth_device - GTH device
42 * @dev: driver core's device
43 * @base: register window base address
44 * @output_group: attributes describing output ports
45 * @master_group: attributes describing master assignments
46 * @output: output ports
47 * @master: master/output port assignments
48 * @gth_lock: serializes accesses to GTH bits
54 struct attribute_group output_group;
55 struct attribute_group master_group;
56 struct gth_output output[TH_POSSIBLE_OUTPUTS];
57 signed char master[TH_CONFIGURABLE_MASTERS + 1];
61 static void gth_output_set(struct gth_device *gth, int port,
64 unsigned long reg = port & 4 ? REG_GTH_GTHOPT1 : REG_GTH_GTHOPT0;
66 int shift = (port & 3) * 8;
68 val = ioread32(gth->base + reg);
69 val &= ~(0xff << shift);
70 val |= config << shift;
71 iowrite32(val, gth->base + reg);
74 static unsigned int gth_output_get(struct gth_device *gth, int port)
76 unsigned long reg = port & 4 ? REG_GTH_GTHOPT1 : REG_GTH_GTHOPT0;
78 int shift = (port & 3) * 8;
80 val = ioread32(gth->base + reg);
87 static void gth_smcfreq_set(struct gth_device *gth, int port,
90 unsigned long reg = REG_GTH_SMCR0 + ((port / 2) * 4);
91 int shift = (port & 1) * 16;
94 val = ioread32(gth->base + reg);
95 val &= ~(0xffff << shift);
97 iowrite32(val, gth->base + reg);
100 static unsigned int gth_smcfreq_get(struct gth_device *gth, int port)
102 unsigned long reg = REG_GTH_SMCR0 + ((port / 2) * 4);
103 int shift = (port & 1) * 16;
106 val = ioread32(gth->base + reg);
107 val &= 0xffff << shift;
114 * "masters" attribute group
117 struct master_attribute {
118 struct device_attribute attr;
119 struct gth_device *gth;
124 gth_master_set(struct gth_device *gth, unsigned int master, int port)
126 unsigned int reg = REG_GTH_SWDEST0 + ((master >> 1) & ~3u);
127 unsigned int shift = (master & 0x7) * 4;
131 reg = REG_GTH_GSWTDEST;
135 val = ioread32(gth->base + reg);
136 val &= ~(0xf << shift);
138 val |= (0x8 | port) << shift;
139 iowrite32(val, gth->base + reg);
142 static ssize_t master_attr_show(struct device *dev,
143 struct device_attribute *attr,
146 struct master_attribute *ma =
147 container_of(attr, struct master_attribute, attr);
148 struct gth_device *gth = ma->gth;
152 spin_lock(>h->gth_lock);
153 port = gth->master[ma->master];
154 spin_unlock(>h->gth_lock);
157 count = snprintf(buf, PAGE_SIZE, "%x\n", port);
159 count = snprintf(buf, PAGE_SIZE, "disabled\n");
164 static ssize_t master_attr_store(struct device *dev,
165 struct device_attribute *attr,
166 const char *buf, size_t count)
168 struct master_attribute *ma =
169 container_of(attr, struct master_attribute, attr);
170 struct gth_device *gth = ma->gth;
173 if (kstrtoint(buf, 10, &port) < 0)
176 if (port >= TH_POSSIBLE_OUTPUTS || port < -1)
179 spin_lock(>h->gth_lock);
181 /* disconnect from the previous output port, if any */
182 old_port = gth->master[ma->master];
184 gth->master[ma->master] = -1;
185 clear_bit(ma->master, gth->output[old_port].master);
188 * if the port is active, program this setting,
189 * implies that runtime PM is on
191 if (gth->output[old_port].output->active)
192 gth_master_set(gth, ma->master, -1);
195 /* connect to the new output port, if any */
197 /* check if there's a driver for this port */
198 if (!gth->output[port].output) {
203 set_bit(ma->master, gth->output[port].master);
205 /* if the port is active, program this setting, see above */
206 if (gth->output[port].output->active)
207 gth_master_set(gth, ma->master, port);
210 gth->master[ma->master] = port;
213 spin_unlock(>h->gth_lock);
218 struct output_attribute {
219 struct device_attribute attr;
220 struct gth_device *gth;
225 #define OUTPUT_PARM(_name, _mask, _r, _w, _what) \
226 [TH_OUTPUT_PARM(_name)] = { .name = __stringify(_name), \
227 .get = gth_ ## _what ## _get, \
228 .set = gth_ ## _what ## _set, \
233 static const struct output_parm {
235 unsigned int (*get)(struct gth_device *gth, int port);
236 void (*set)(struct gth_device *gth, int port,
239 unsigned int readable : 1,
242 OUTPUT_PARM(port, 0x7, 1, 0, output),
243 OUTPUT_PARM(null, BIT(3), 1, 1, output),
244 OUTPUT_PARM(drop, BIT(4), 1, 1, output),
245 OUTPUT_PARM(reset, BIT(5), 1, 0, output),
246 OUTPUT_PARM(flush, BIT(7), 0, 1, output),
247 OUTPUT_PARM(smcfreq, 0xffff, 1, 1, smcfreq),
251 gth_output_parm_set(struct gth_device *gth, int port, unsigned int parm,
254 unsigned int config = output_parms[parm].get(gth, port);
255 unsigned int mask = output_parms[parm].mask;
256 unsigned int shift = __ffs(mask);
259 config |= (val << shift) & mask;
260 output_parms[parm].set(gth, port, config);
264 gth_output_parm_get(struct gth_device *gth, int port, unsigned int parm)
266 unsigned int config = output_parms[parm].get(gth, port);
267 unsigned int mask = output_parms[parm].mask;
268 unsigned int shift = __ffs(mask);
276 * Reset outputs and sources
278 static int intel_th_gth_reset(struct gth_device *gth)
283 reg = ioread32(gth->base + REG_GTH_SCRPD0);
284 if (reg & SCRPD_DEBUGGER_IN_USE)
287 /* Always save/restore STH and TU registers in S0ix entry/exit */
288 reg |= SCRPD_STH_IS_ENABLED | SCRPD_TRIGGER_IS_ENABLED;
289 iowrite32(reg, gth->base + REG_GTH_SCRPD0);
292 for (port = 0; port < 8; port++) {
293 if (gth_output_parm_get(gth, port, TH_OUTPUT_PARM(port)) ==
297 gth_output_set(gth, port, 0);
298 gth_smcfreq_set(gth, port, 16);
300 /* disable overrides */
301 iowrite32(0, gth->base + REG_GTH_DESTOVR);
303 /* masters swdest_0~31 and gswdest */
304 for (i = 0; i < 33; i++)
305 iowrite32(0, gth->base + REG_GTH_SWDEST0 + i * 4);
308 iowrite32(0, gth->base + REG_GTH_SCR);
309 iowrite32(0xfc, gth->base + REG_GTH_SCR2);
315 * "outputs" attribute group
318 static ssize_t output_attr_show(struct device *dev,
319 struct device_attribute *attr,
322 struct output_attribute *oa =
323 container_of(attr, struct output_attribute, attr);
324 struct gth_device *gth = oa->gth;
327 pm_runtime_get_sync(dev);
329 spin_lock(>h->gth_lock);
330 count = snprintf(buf, PAGE_SIZE, "%x\n",
331 gth_output_parm_get(gth, oa->port, oa->parm));
332 spin_unlock(>h->gth_lock);
339 static ssize_t output_attr_store(struct device *dev,
340 struct device_attribute *attr,
341 const char *buf, size_t count)
343 struct output_attribute *oa =
344 container_of(attr, struct output_attribute, attr);
345 struct gth_device *gth = oa->gth;
348 if (kstrtouint(buf, 16, &config) < 0)
351 pm_runtime_get_sync(dev);
353 spin_lock(>h->gth_lock);
354 gth_output_parm_set(gth, oa->port, oa->parm, config);
355 spin_unlock(>h->gth_lock);
362 static int intel_th_master_attributes(struct gth_device *gth)
364 struct master_attribute *master_attrs;
365 struct attribute **attrs;
366 int i, nattrs = TH_CONFIGURABLE_MASTERS + 2;
368 attrs = devm_kcalloc(gth->dev, nattrs, sizeof(void *), GFP_KERNEL);
372 master_attrs = devm_kcalloc(gth->dev, nattrs,
373 sizeof(struct master_attribute),
378 for (i = 0; i < TH_CONFIGURABLE_MASTERS + 1; i++) {
381 name = devm_kasprintf(gth->dev, GFP_KERNEL, "%d%s", i,
382 i == TH_CONFIGURABLE_MASTERS ? "+" : "");
386 master_attrs[i].attr.attr.name = name;
387 master_attrs[i].attr.attr.mode = S_IRUGO | S_IWUSR;
388 master_attrs[i].attr.show = master_attr_show;
389 master_attrs[i].attr.store = master_attr_store;
391 sysfs_attr_init(&master_attrs[i].attr.attr);
392 attrs[i] = &master_attrs[i].attr.attr;
394 master_attrs[i].gth = gth;
395 master_attrs[i].master = i;
398 gth->master_group.name = "masters";
399 gth->master_group.attrs = attrs;
401 return sysfs_create_group(>h->dev->kobj, >h->master_group);
404 static int intel_th_output_attributes(struct gth_device *gth)
406 struct output_attribute *out_attrs;
407 struct attribute **attrs;
408 int i, j, nouts = TH_POSSIBLE_OUTPUTS;
409 int nparms = ARRAY_SIZE(output_parms);
410 int nattrs = nouts * nparms + 1;
412 attrs = devm_kcalloc(gth->dev, nattrs, sizeof(void *), GFP_KERNEL);
416 out_attrs = devm_kcalloc(gth->dev, nattrs,
417 sizeof(struct output_attribute),
422 for (i = 0; i < nouts; i++) {
423 for (j = 0; j < nparms; j++) {
424 unsigned int idx = i * nparms + j;
427 name = devm_kasprintf(gth->dev, GFP_KERNEL, "%d_%s", i,
428 output_parms[j].name);
432 out_attrs[idx].attr.attr.name = name;
434 if (output_parms[j].readable) {
435 out_attrs[idx].attr.attr.mode |= S_IRUGO;
436 out_attrs[idx].attr.show = output_attr_show;
439 if (output_parms[j].writable) {
440 out_attrs[idx].attr.attr.mode |= S_IWUSR;
441 out_attrs[idx].attr.store = output_attr_store;
444 sysfs_attr_init(&out_attrs[idx].attr.attr);
445 attrs[idx] = &out_attrs[idx].attr.attr;
447 out_attrs[idx].gth = gth;
448 out_attrs[idx].port = i;
449 out_attrs[idx].parm = j;
453 gth->output_group.name = "outputs";
454 gth->output_group.attrs = attrs;
456 return sysfs_create_group(>h->dev->kobj, >h->output_group);
460 * intel_th_gth_disable() - disable tracing to an output device
462 * @output: output device's descriptor
464 * This will deconfigure all masters set to output to this device,
465 * disable tracing using force storeEn off signal and wait for the
466 * "pipeline empty" bit for corresponding output port.
468 static void intel_th_gth_disable(struct intel_th_device *thdev,
469 struct intel_th_output *output)
471 struct gth_device *gth = dev_get_drvdata(&thdev->dev);
476 spin_lock(>h->gth_lock);
477 output->active = false;
479 for_each_set_bit(master, gth->output[output->port].master,
480 TH_CONFIGURABLE_MASTERS) {
481 gth_master_set(gth, master, -1);
483 spin_unlock(>h->gth_lock);
485 iowrite32(0, gth->base + REG_GTH_SCR);
486 iowrite32(0xfd, gth->base + REG_GTH_SCR2);
488 /* wait on pipeline empty for the given port */
489 for (reg = 0, count = GTH_PLE_WAITLOOP_DEPTH;
490 count && !(reg & BIT(output->port)); count--) {
491 reg = ioread32(gth->base + REG_GTH_STAT);
495 /* clear force capture done for next captures */
496 iowrite32(0xfc, gth->base + REG_GTH_SCR2);
499 dev_dbg(&thdev->dev, "timeout waiting for GTH[%d] PLE\n",
502 reg = ioread32(gth->base + REG_GTH_SCRPD0);
503 reg &= ~output->scratchpad;
504 iowrite32(reg, gth->base + REG_GTH_SCRPD0);
507 static void gth_tscu_resync(struct gth_device *gth)
511 reg = ioread32(gth->base + REG_TSCU_TSUCTRL);
512 reg &= ~TSUCTRL_CTCRESYNC;
513 iowrite32(reg, gth->base + REG_TSCU_TSUCTRL);
517 * intel_th_gth_enable() - enable tracing to an output device
519 * @output: output device's descriptor
521 * This will configure all masters set to output to this device and
522 * enable tracing using force storeEn signal.
524 static void intel_th_gth_enable(struct intel_th_device *thdev,
525 struct intel_th_output *output)
527 struct gth_device *gth = dev_get_drvdata(&thdev->dev);
528 struct intel_th *th = to_intel_th(thdev);
529 u32 scr = 0xfc0000, scrpd;
532 spin_lock(>h->gth_lock);
533 for_each_set_bit(master, gth->output[output->port].master,
534 TH_CONFIGURABLE_MASTERS + 1) {
535 gth_master_set(gth, master, output->port);
538 if (output->multiblock)
541 output->active = true;
542 spin_unlock(>h->gth_lock);
544 if (INTEL_TH_CAP(th, tscu_enable))
545 gth_tscu_resync(gth);
547 scrpd = ioread32(gth->base + REG_GTH_SCRPD0);
548 scrpd |= output->scratchpad;
549 iowrite32(scrpd, gth->base + REG_GTH_SCRPD0);
551 iowrite32(scr, gth->base + REG_GTH_SCR);
552 iowrite32(0, gth->base + REG_GTH_SCR2);
556 * intel_th_gth_assign() - assign output device to a GTH output port
558 * @othdev: output device
560 * This will match a given output device parameters against present
561 * output ports on the GTH and fill out relevant bits in output device's
564 * Return: 0 on success, -errno on error.
566 static int intel_th_gth_assign(struct intel_th_device *thdev,
567 struct intel_th_device *othdev)
569 struct gth_device *gth = dev_get_drvdata(&thdev->dev);
572 if (thdev->host_mode)
575 if (othdev->type != INTEL_TH_OUTPUT)
578 for (i = 0, id = 0; i < TH_POSSIBLE_OUTPUTS; i++) {
579 if (gth->output[i].port_type != othdev->output.type)
582 if (othdev->id == -1 || othdev->id == id)
591 spin_lock(>h->gth_lock);
592 othdev->output.port = i;
593 othdev->output.active = false;
594 gth->output[i].output = &othdev->output;
595 spin_unlock(>h->gth_lock);
601 * intel_th_gth_unassign() - deassociate an output device from its output port
603 * @othdev: output device
605 static void intel_th_gth_unassign(struct intel_th_device *thdev,
606 struct intel_th_device *othdev)
608 struct gth_device *gth = dev_get_drvdata(&thdev->dev);
609 int port = othdev->output.port;
611 if (thdev->host_mode)
614 spin_lock(>h->gth_lock);
615 othdev->output.port = -1;
616 othdev->output.active = false;
617 gth->output[port].output = NULL;
618 spin_unlock(>h->gth_lock);
622 intel_th_gth_set_output(struct intel_th_device *thdev, unsigned int master)
624 struct gth_device *gth = dev_get_drvdata(&thdev->dev);
625 int port = 0; /* FIXME: make default output configurable */
628 * everything above TH_CONFIGURABLE_MASTERS is controlled by the
631 if (master > TH_CONFIGURABLE_MASTERS)
632 master = TH_CONFIGURABLE_MASTERS;
634 spin_lock(>h->gth_lock);
635 if (gth->master[master] == -1) {
636 set_bit(master, gth->output[port].master);
637 gth->master[master] = port;
639 spin_unlock(>h->gth_lock);
644 static int intel_th_gth_probe(struct intel_th_device *thdev)
646 struct device *dev = &thdev->dev;
647 struct intel_th *th = dev_get_drvdata(dev->parent);
648 struct gth_device *gth;
649 struct resource *res;
653 res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
657 base = devm_ioremap(dev, res->start, resource_size(res));
661 gth = devm_kzalloc(dev, sizeof(*gth), GFP_KERNEL);
667 spin_lock_init(>h->gth_lock);
669 dev_set_drvdata(dev, gth);
672 * Host mode can be signalled via SW means or via SCRPD_DEBUGGER_IN_USE
673 * bit. Either way, don't reset HW in this case, and don't export any
674 * capture configuration attributes. Also, refuse to assign output
675 * drivers to ports, see intel_th_gth_assign().
677 if (thdev->host_mode)
680 ret = intel_th_gth_reset(gth);
685 thdev->host_mode = true;
690 for (i = 0; i < TH_CONFIGURABLE_MASTERS + 1; i++)
693 for (i = 0; i < TH_POSSIBLE_OUTPUTS; i++) {
694 gth->output[i].gth = gth;
695 gth->output[i].index = i;
696 gth->output[i].port_type =
697 gth_output_parm_get(gth, i, TH_OUTPUT_PARM(port));
698 if (gth->output[i].port_type == GTH_NONE)
701 ret = intel_th_output_enable(th, gth->output[i].port_type);
702 /* -ENODEV is ok, we just won't have that device enumerated */
703 if (ret && ret != -ENODEV)
707 if (intel_th_output_attributes(gth) ||
708 intel_th_master_attributes(gth)) {
709 pr_warn("Can't initialize sysfs attributes\n");
711 if (gth->output_group.attrs)
712 sysfs_remove_group(>h->dev->kobj, >h->output_group);
719 static void intel_th_gth_remove(struct intel_th_device *thdev)
721 struct gth_device *gth = dev_get_drvdata(&thdev->dev);
723 sysfs_remove_group(>h->dev->kobj, >h->output_group);
724 sysfs_remove_group(>h->dev->kobj, >h->master_group);
727 static struct intel_th_driver intel_th_gth_driver = {
728 .probe = intel_th_gth_probe,
729 .remove = intel_th_gth_remove,
730 .assign = intel_th_gth_assign,
731 .unassign = intel_th_gth_unassign,
732 .set_output = intel_th_gth_set_output,
733 .enable = intel_th_gth_enable,
734 .disable = intel_th_gth_disable,
737 .owner = THIS_MODULE,
741 module_driver(intel_th_gth_driver,
742 intel_th_driver_register,
743 intel_th_driver_unregister);
745 MODULE_ALIAS("intel_th_switch");
746 MODULE_LICENSE("GPL v2");
747 MODULE_DESCRIPTION("Intel(R) Trace Hub Global Trace Hub driver");