1 // SPDX-License-Identifier: GPL-2.0
3 * Intel(R) Trace Hub driver core
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>
13 #include <linux/sysfs.h>
14 #include <linux/kdev_t.h>
15 #include <linux/debugfs.h>
16 #include <linux/idr.h>
17 #include <linux/pci.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/dma-mapping.h>
24 static bool host_mode __read_mostly;
25 module_param(host_mode, bool, 0444);
27 static DEFINE_IDA(intel_th_ida);
29 static int intel_th_match(struct device *dev, struct device_driver *driver)
31 struct intel_th_driver *thdrv = to_intel_th_driver(driver);
32 struct intel_th_device *thdev = to_intel_th_device(dev);
34 if (thdev->type == INTEL_TH_SWITCH &&
35 (!thdrv->enable || !thdrv->disable))
38 return !strcmp(thdev->name, driver->name);
41 static int intel_th_child_remove(struct device *dev, void *data)
43 device_release_driver(dev);
48 static int intel_th_probe(struct device *dev)
50 struct intel_th_driver *thdrv = to_intel_th_driver(dev->driver);
51 struct intel_th_device *thdev = to_intel_th_device(dev);
52 struct intel_th_driver *hubdrv;
53 struct intel_th_device *hub = NULL;
56 if (thdev->type == INTEL_TH_SWITCH)
59 hub = to_intel_th_device(dev->parent);
61 if (!hub || !hub->dev.driver)
64 hubdrv = to_intel_th_driver(hub->dev.driver);
66 pm_runtime_set_active(dev);
67 pm_runtime_no_callbacks(dev);
68 pm_runtime_enable(dev);
70 ret = thdrv->probe(to_intel_th_device(dev));
74 if (thdrv->attr_group) {
75 ret = sysfs_create_group(&thdev->dev.kobj, thdrv->attr_group);
80 if (thdev->type == INTEL_TH_OUTPUT &&
81 !intel_th_output_assigned(thdev))
82 /* does not talk to hardware */
83 ret = hubdrv->assign(hub, thdev);
91 pm_runtime_disable(dev);
96 static void intel_th_device_remove(struct intel_th_device *thdev);
98 static int intel_th_remove(struct device *dev)
100 struct intel_th_driver *thdrv = to_intel_th_driver(dev->driver);
101 struct intel_th_device *thdev = to_intel_th_device(dev);
102 struct intel_th_device *hub = to_intel_th_hub(thdev);
105 if (thdev->type == INTEL_TH_SWITCH) {
106 struct intel_th *th = to_intel_th(hub);
109 /* disconnect outputs */
110 err = device_for_each_child(dev, thdev, intel_th_child_remove);
115 * Remove outputs, that is, hub's children: they are created
116 * at hub's probe time by having the hub call
117 * intel_th_output_enable() for each of them.
119 for (i = 0, lowest = -1; i < th->num_thdevs; i++) {
121 * Move the non-output devices from higher up the
122 * th->thdev[] array to lower positions to maintain
123 * a contiguous array.
125 if (th->thdev[i]->type != INTEL_TH_OUTPUT) {
127 th->thdev[lowest] = th->thdev[i];
138 intel_th_device_remove(th->thdev[i]);
143 th->num_thdevs = lowest;
146 if (thdrv->attr_group)
147 sysfs_remove_group(&thdev->dev.kobj, thdrv->attr_group);
149 pm_runtime_get_sync(dev);
151 thdrv->remove(thdev);
153 if (intel_th_output_assigned(thdev)) {
154 struct intel_th_driver *hubdrv =
155 to_intel_th_driver(dev->parent->driver);
158 /* does not talk to hardware */
159 hubdrv->unassign(hub, thdev);
162 pm_runtime_disable(dev);
163 pm_runtime_set_active(dev);
164 pm_runtime_enable(dev);
169 static struct bus_type intel_th_bus = {
171 .match = intel_th_match,
172 .probe = intel_th_probe,
173 .remove = intel_th_remove,
176 static void intel_th_device_free(struct intel_th_device *thdev);
178 static void intel_th_device_release(struct device *dev)
180 intel_th_device_free(to_intel_th_device(dev));
183 static struct device_type intel_th_source_device_type = {
184 .name = "intel_th_source_device",
185 .release = intel_th_device_release,
188 static char *intel_th_output_devnode(struct device *dev, umode_t *mode,
189 kuid_t *uid, kgid_t *gid)
191 struct intel_th_device *thdev = to_intel_th_device(dev);
192 struct intel_th *th = to_intel_th(thdev);
196 node = kasprintf(GFP_KERNEL, "intel_th%d/%s%d", th->id,
197 thdev->name, thdev->id);
199 node = kasprintf(GFP_KERNEL, "intel_th%d/%s", th->id,
205 static ssize_t port_show(struct device *dev, struct device_attribute *attr,
208 struct intel_th_device *thdev = to_intel_th_device(dev);
210 if (thdev->output.port >= 0)
211 return scnprintf(buf, PAGE_SIZE, "%u\n", thdev->output.port);
213 return scnprintf(buf, PAGE_SIZE, "unassigned\n");
216 static DEVICE_ATTR_RO(port);
218 static void intel_th_trace_prepare(struct intel_th_device *thdev)
220 struct intel_th_device *hub = to_intel_th_hub(thdev);
221 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
223 if (hub->type != INTEL_TH_SWITCH)
226 if (thdev->type != INTEL_TH_OUTPUT)
229 pm_runtime_get_sync(&thdev->dev);
230 hubdrv->prepare(hub, &thdev->output);
231 pm_runtime_put(&thdev->dev);
234 static int intel_th_output_activate(struct intel_th_device *thdev)
236 struct intel_th_driver *thdrv =
237 to_intel_th_driver_or_null(thdev->dev.driver);
238 struct intel_th *th = to_intel_th(thdev);
244 if (!try_module_get(thdrv->driver.owner))
247 pm_runtime_get_sync(&thdev->dev);
250 ret = th->activate(th);
254 intel_th_trace_prepare(thdev);
256 ret = thdrv->activate(thdev);
258 intel_th_trace_enable(thdev);
261 goto fail_deactivate;
270 pm_runtime_put(&thdev->dev);
271 module_put(thdrv->driver.owner);
276 static void intel_th_output_deactivate(struct intel_th_device *thdev)
278 struct intel_th_driver *thdrv =
279 to_intel_th_driver_or_null(thdev->dev.driver);
280 struct intel_th *th = to_intel_th(thdev);
285 if (thdrv->deactivate)
286 thdrv->deactivate(thdev);
288 intel_th_trace_disable(thdev);
293 pm_runtime_put(&thdev->dev);
294 module_put(thdrv->driver.owner);
297 static ssize_t active_show(struct device *dev, struct device_attribute *attr,
300 struct intel_th_device *thdev = to_intel_th_device(dev);
302 return scnprintf(buf, PAGE_SIZE, "%d\n", thdev->output.active);
305 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
306 const char *buf, size_t size)
308 struct intel_th_device *thdev = to_intel_th_device(dev);
312 ret = kstrtoul(buf, 10, &val);
316 if (!!val != thdev->output.active) {
318 ret = intel_th_output_activate(thdev);
320 intel_th_output_deactivate(thdev);
323 return ret ? ret : size;
326 static DEVICE_ATTR_RW(active);
328 static struct attribute *intel_th_output_attrs[] = {
330 &dev_attr_active.attr,
334 ATTRIBUTE_GROUPS(intel_th_output);
336 static struct device_type intel_th_output_device_type = {
337 .name = "intel_th_output_device",
338 .groups = intel_th_output_groups,
339 .release = intel_th_device_release,
340 .devnode = intel_th_output_devnode,
343 static struct device_type intel_th_switch_device_type = {
344 .name = "intel_th_switch_device",
345 .release = intel_th_device_release,
348 static struct device_type *intel_th_device_type[] = {
349 [INTEL_TH_SOURCE] = &intel_th_source_device_type,
350 [INTEL_TH_OUTPUT] = &intel_th_output_device_type,
351 [INTEL_TH_SWITCH] = &intel_th_switch_device_type,
354 int intel_th_driver_register(struct intel_th_driver *thdrv)
356 if (!thdrv->probe || !thdrv->remove)
359 thdrv->driver.bus = &intel_th_bus;
361 return driver_register(&thdrv->driver);
363 EXPORT_SYMBOL_GPL(intel_th_driver_register);
365 void intel_th_driver_unregister(struct intel_th_driver *thdrv)
367 driver_unregister(&thdrv->driver);
369 EXPORT_SYMBOL_GPL(intel_th_driver_unregister);
371 static struct intel_th_device *
372 intel_th_device_alloc(struct intel_th *th, unsigned int type, const char *name,
375 struct device *parent;
376 struct intel_th_device *thdev;
378 if (type == INTEL_TH_OUTPUT)
379 parent = &th->hub->dev;
383 thdev = kzalloc(sizeof(*thdev) + strlen(name) + 1, GFP_KERNEL);
390 strcpy(thdev->name, name);
391 device_initialize(&thdev->dev);
392 thdev->dev.bus = &intel_th_bus;
393 thdev->dev.type = intel_th_device_type[type];
394 thdev->dev.parent = parent;
395 thdev->dev.dma_mask = parent->dma_mask;
396 thdev->dev.dma_parms = parent->dma_parms;
397 dma_set_coherent_mask(&thdev->dev, parent->coherent_dma_mask);
399 dev_set_name(&thdev->dev, "%d-%s%d", th->id, name, id);
401 dev_set_name(&thdev->dev, "%d-%s", th->id, name);
406 static int intel_th_device_add_resources(struct intel_th_device *thdev,
407 struct resource *res, int nres)
411 r = kmemdup(res, sizeof(*res) * nres, GFP_KERNEL);
416 thdev->num_resources = nres;
421 static void intel_th_device_remove(struct intel_th_device *thdev)
423 device_del(&thdev->dev);
424 put_device(&thdev->dev);
427 static void intel_th_device_free(struct intel_th_device *thdev)
429 kfree(thdev->resource);
434 * Intel(R) Trace Hub subdevices
436 static const struct intel_th_subdevice {
438 struct resource res[3];
444 } intel_th_subdevices[] = {
449 /* Handle TSCU from GTH driver */
450 .start = REG_GTH_OFFSET,
451 .end = REG_TSCU_OFFSET + REG_TSCU_LENGTH - 1,
452 .flags = IORESOURCE_MEM,
456 .type = INTEL_TH_SWITCH,
463 .start = REG_MSU_OFFSET,
464 .end = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
465 .flags = IORESOURCE_MEM,
468 .start = BUF_MSU_OFFSET,
469 .end = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
470 .flags = IORESOURCE_MEM,
475 .type = INTEL_TH_OUTPUT,
477 .scrpd = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC0_IS_ENABLED,
483 .start = REG_MSU_OFFSET,
484 .end = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
485 .flags = IORESOURCE_MEM,
488 .start = BUF_MSU_OFFSET,
489 .end = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
490 .flags = IORESOURCE_MEM,
495 .type = INTEL_TH_OUTPUT,
497 .scrpd = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC1_IS_ENABLED,
503 .start = REG_STH_OFFSET,
504 .end = REG_STH_OFFSET + REG_STH_LENGTH - 1,
505 .flags = IORESOURCE_MEM,
508 .start = 1, /* use resource[1] */
510 .flags = IORESOURCE_MEM,
515 .type = INTEL_TH_SOURCE,
521 .start = REG_PTI_OFFSET,
522 .end = REG_PTI_OFFSET + REG_PTI_LENGTH - 1,
523 .flags = IORESOURCE_MEM,
528 .type = INTEL_TH_OUTPUT,
530 .scrpd = SCRPD_PTI_IS_PRIM_DEST,
536 .start = REG_PTI_OFFSET,
537 .end = REG_PTI_OFFSET + REG_PTI_LENGTH - 1,
538 .flags = IORESOURCE_MEM,
543 .type = INTEL_TH_OUTPUT,
545 .scrpd = SCRPD_PTI_IS_PRIM_DEST,
551 .start = REG_DCIH_OFFSET,
552 .end = REG_DCIH_OFFSET + REG_DCIH_LENGTH - 1,
553 .flags = IORESOURCE_MEM,
558 .type = INTEL_TH_OUTPUT,
562 #ifdef CONFIG_MODULES
563 static void __intel_th_request_hub_module(struct work_struct *work)
565 struct intel_th *th = container_of(work, struct intel_th,
566 request_module_work);
568 request_module("intel_th_%s", th->hub->name);
571 static int intel_th_request_hub_module(struct intel_th *th)
573 INIT_WORK(&th->request_module_work, __intel_th_request_hub_module);
574 schedule_work(&th->request_module_work);
579 static void intel_th_request_hub_module_flush(struct intel_th *th)
581 flush_work(&th->request_module_work);
584 static inline int intel_th_request_hub_module(struct intel_th *th)
589 static inline void intel_th_request_hub_module_flush(struct intel_th *th)
592 #endif /* CONFIG_MODULES */
594 static struct intel_th_device *
595 intel_th_subdevice_alloc(struct intel_th *th,
596 const struct intel_th_subdevice *subdev)
598 struct intel_th_device *thdev;
599 struct resource res[3];
600 unsigned int req = 0;
601 bool is64bit = false;
604 thdev = intel_th_device_alloc(th, subdev->type, subdev->name,
607 return ERR_PTR(-ENOMEM);
609 thdev->drvdata = th->drvdata;
611 for (r = 0; r < th->num_resources; r++)
612 if (th->resource[r].flags & IORESOURCE_MEM_64) {
617 memcpy(res, subdev->res,
618 sizeof(struct resource) * subdev->nres);
620 for (r = 0; r < subdev->nres; r++) {
621 struct resource *devres = th->resource;
622 int bar = 0; /* cut subdevices' MMIO from resource[0] */
625 * Take .end == 0 to mean 'take the whole bar',
626 * .start then tells us which bar it is. Default to
629 if (!res[r].end && res[r].flags == IORESOURCE_MEM) {
634 res[r].end = resource_size(&devres[bar]) - 1;
637 if (res[r].flags & IORESOURCE_MEM) {
638 res[r].start += devres[bar].start;
639 res[r].end += devres[bar].start;
641 dev_dbg(th->dev, "%s:%d @ %pR\n",
642 subdev->name, r, &res[r]);
643 } else if (res[r].flags & IORESOURCE_IRQ) {
644 res[r].start = th->irq;
648 err = intel_th_device_add_resources(thdev, res, subdev->nres);
650 goto fail_put_device;
652 if (subdev->type == INTEL_TH_OUTPUT) {
653 thdev->dev.devt = MKDEV(th->major, th->num_thdevs);
654 thdev->output.type = subdev->otype;
655 thdev->output.port = -1;
656 thdev->output.scratchpad = subdev->scrpd;
657 } else if (subdev->type == INTEL_TH_SWITCH) {
659 INTEL_TH_CAP(th, host_mode_only) ? true : host_mode;
663 err = device_add(&thdev->dev);
667 /* need switch driver to be loaded to enumerate the rest */
668 if (subdev->type == INTEL_TH_SWITCH && !req) {
669 err = intel_th_request_hub_module(th);
677 kfree(thdev->resource);
680 put_device(&thdev->dev);
686 * intel_th_output_enable() - find and enable a device for a given output type
687 * @th: Intel TH instance
688 * @otype: output type
690 * Go through the unallocated output devices, find the first one whos type
691 * matches @otype and instantiate it. These devices are removed when the hub
692 * device is removed, see intel_th_remove().
694 int intel_th_output_enable(struct intel_th *th, unsigned int otype)
696 struct intel_th_device *thdev;
697 int src = 0, dst = 0;
699 for (src = 0, dst = 0; dst <= th->num_thdevs; src++, dst++) {
700 for (; src < ARRAY_SIZE(intel_th_subdevices); src++) {
701 if (intel_th_subdevices[src].type != INTEL_TH_OUTPUT)
704 if (intel_th_subdevices[src].otype != otype)
710 /* no unallocated matching subdevices */
711 if (src == ARRAY_SIZE(intel_th_subdevices))
714 for (; dst < th->num_thdevs; dst++) {
715 if (th->thdev[dst]->type != INTEL_TH_OUTPUT)
718 if (th->thdev[dst]->output.type != otype)
725 * intel_th_subdevices[src] matches our requirements and is
726 * not matched in th::thdev[]
728 if (dst == th->num_thdevs)
735 thdev = intel_th_subdevice_alloc(th, &intel_th_subdevices[src]);
737 return PTR_ERR(thdev);
739 th->thdev[th->num_thdevs++] = thdev;
743 EXPORT_SYMBOL_GPL(intel_th_output_enable);
745 static int intel_th_populate(struct intel_th *th)
749 /* create devices for each intel_th_subdevice */
750 for (src = 0; src < ARRAY_SIZE(intel_th_subdevices); src++) {
751 const struct intel_th_subdevice *subdev =
752 &intel_th_subdevices[src];
753 struct intel_th_device *thdev;
755 /* only allow SOURCE and SWITCH devices in host mode */
756 if ((INTEL_TH_CAP(th, host_mode_only) || host_mode) &&
757 subdev->type == INTEL_TH_OUTPUT)
761 * don't enable port OUTPUTs in this path; SWITCH enables them
762 * via intel_th_output_enable()
764 if (subdev->type == INTEL_TH_OUTPUT &&
765 subdev->otype != GTH_NONE)
768 thdev = intel_th_subdevice_alloc(th, subdev);
769 /* note: caller should free subdevices from th::thdev[] */
771 return PTR_ERR(thdev);
773 th->thdev[th->num_thdevs++] = thdev;
779 static int match_devt(struct device *dev, void *data)
781 dev_t devt = (dev_t)(unsigned long)data;
783 return dev->devt == devt;
786 static int intel_th_output_open(struct inode *inode, struct file *file)
788 const struct file_operations *fops;
789 struct intel_th_driver *thdrv;
793 dev = bus_find_device(&intel_th_bus, NULL,
794 (void *)(unsigned long)inode->i_rdev,
796 if (!dev || !dev->driver)
799 thdrv = to_intel_th_driver(dev->driver);
800 fops = fops_get(thdrv->fops);
804 replace_fops(file, fops);
806 file->private_data = to_intel_th_device(dev);
808 if (file->f_op->open) {
809 err = file->f_op->open(inode, file);
816 static const struct file_operations intel_th_output_fops = {
817 .open = intel_th_output_open,
818 .llseek = noop_llseek,
822 * intel_th_alloc() - allocate a new Intel TH device and its subdevices
823 * @dev: parent device
824 * @devres: parent's resources
825 * @ndevres: number of resources
829 intel_th_alloc(struct device *dev, struct intel_th_drvdata *drvdata,
830 struct resource *devres, unsigned int ndevres, int irq)
836 for (r = 0; r < ndevres; r++)
837 if (devres[r].flags & IORESOURCE_IRQ) {
838 irq = devres[r].start;
842 th = kzalloc(sizeof(*th), GFP_KERNEL);
844 return ERR_PTR(-ENOMEM);
846 th->id = ida_simple_get(&intel_th_ida, 0, 0, GFP_KERNEL);
852 th->major = __register_chrdev(0, 0, TH_POSSIBLE_OUTPUTS,
853 "intel_th/output", &intel_th_output_fops);
859 th->drvdata = drvdata;
861 th->resource = devres;
862 th->num_resources = ndevres;
865 dev_set_drvdata(dev, th);
867 pm_runtime_no_callbacks(dev);
869 pm_runtime_allow(dev);
871 err = intel_th_populate(th);
873 /* free the subdevices and undo everything */
881 ida_simple_remove(&intel_th_ida, th->id);
888 EXPORT_SYMBOL_GPL(intel_th_alloc);
890 void intel_th_free(struct intel_th *th)
894 intel_th_request_hub_module_flush(th);
896 intel_th_device_remove(th->hub);
897 for (i = 0; i < th->num_thdevs; i++) {
898 if (th->thdev[i] != th->hub)
899 intel_th_device_remove(th->thdev[i]);
905 pm_runtime_get_sync(th->dev);
906 pm_runtime_forbid(th->dev);
908 __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
911 ida_simple_remove(&intel_th_ida, th->id);
915 EXPORT_SYMBOL_GPL(intel_th_free);
918 * intel_th_trace_enable() - enable tracing for an output device
919 * @thdev: output device that requests tracing be enabled
921 int intel_th_trace_enable(struct intel_th_device *thdev)
923 struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
924 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
926 if (WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH))
929 if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
932 pm_runtime_get_sync(&thdev->dev);
933 hubdrv->enable(hub, &thdev->output);
937 EXPORT_SYMBOL_GPL(intel_th_trace_enable);
940 * intel_th_trace_disable() - disable tracing for an output device
941 * @thdev: output device that requests tracing be disabled
943 int intel_th_trace_disable(struct intel_th_device *thdev)
945 struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
946 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
948 WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH);
949 if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
952 hubdrv->disable(hub, &thdev->output);
953 pm_runtime_put(&thdev->dev);
957 EXPORT_SYMBOL_GPL(intel_th_trace_disable);
959 int intel_th_set_output(struct intel_th_device *thdev,
962 struct intel_th_device *hub = to_intel_th_hub(thdev);
963 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
966 /* In host mode, this is up to the external debugger, do nothing. */
971 * hub is instantiated together with the source device that
972 * calls here, so guaranteed to be present.
974 hubdrv = to_intel_th_driver(hub->dev.driver);
975 if (!hubdrv || !try_module_get(hubdrv->driver.owner))
978 if (!hubdrv->set_output) {
983 ret = hubdrv->set_output(hub, master);
986 module_put(hubdrv->driver.owner);
989 EXPORT_SYMBOL_GPL(intel_th_set_output);
991 static int __init intel_th_init(void)
993 intel_th_debug_init();
995 return bus_register(&intel_th_bus);
997 subsys_initcall(intel_th_init);
999 static void __exit intel_th_exit(void)
1001 intel_th_debug_done();
1003 bus_unregister(&intel_th_bus);
1005 module_exit(intel_th_exit);
1007 MODULE_LICENSE("GPL v2");
1008 MODULE_DESCRIPTION("Intel(R) Trace Hub controller driver");
1009 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");