2 * Intel(R) Trace Hub driver core
4 * Copyright (C) 2014-2015 Intel Corporation.
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.
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
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/types.h>
19 #include <linux/module.h>
20 #include <linux/device.h>
21 #include <linux/sysfs.h>
22 #include <linux/kdev_t.h>
23 #include <linux/debugfs.h>
24 #include <linux/idr.h>
25 #include <linux/pci.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/dma-mapping.h>
32 static bool host_mode __read_mostly;
33 module_param(host_mode, bool, 0444);
35 static DEFINE_IDA(intel_th_ida);
37 static int intel_th_match(struct device *dev, struct device_driver *driver)
39 struct intel_th_driver *thdrv = to_intel_th_driver(driver);
40 struct intel_th_device *thdev = to_intel_th_device(dev);
42 if (thdev->type == INTEL_TH_SWITCH &&
43 (!thdrv->enable || !thdrv->disable))
46 return !strcmp(thdev->name, driver->name);
49 static int intel_th_child_remove(struct device *dev, void *data)
51 device_release_driver(dev);
56 static int intel_th_probe(struct device *dev)
58 struct intel_th_driver *thdrv = to_intel_th_driver(dev->driver);
59 struct intel_th_device *thdev = to_intel_th_device(dev);
60 struct intel_th_driver *hubdrv;
61 struct intel_th_device *hub = NULL;
64 if (thdev->type == INTEL_TH_SWITCH)
67 hub = to_intel_th_device(dev->parent);
69 if (!hub || !hub->dev.driver)
72 hubdrv = to_intel_th_driver(hub->dev.driver);
74 pm_runtime_set_active(dev);
75 pm_runtime_no_callbacks(dev);
76 pm_runtime_enable(dev);
78 ret = thdrv->probe(to_intel_th_device(dev));
82 if (thdrv->attr_group) {
83 ret = sysfs_create_group(&thdev->dev.kobj, thdrv->attr_group);
88 if (thdev->type == INTEL_TH_OUTPUT &&
89 !intel_th_output_assigned(thdev))
90 /* does not talk to hardware */
91 ret = hubdrv->assign(hub, thdev);
99 pm_runtime_disable(dev);
104 static void intel_th_device_remove(struct intel_th_device *thdev);
106 static int intel_th_remove(struct device *dev)
108 struct intel_th_driver *thdrv = to_intel_th_driver(dev->driver);
109 struct intel_th_device *thdev = to_intel_th_device(dev);
110 struct intel_th_device *hub = to_intel_th_hub(thdev);
113 if (thdev->type == INTEL_TH_SWITCH) {
114 struct intel_th *th = to_intel_th(hub);
117 /* disconnect outputs */
118 err = device_for_each_child(dev, thdev, intel_th_child_remove);
123 * Remove outputs, that is, hub's children: they are created
124 * at hub's probe time by having the hub call
125 * intel_th_output_enable() for each of them.
127 for (i = 0, lowest = -1; i < th->num_thdevs; i++) {
129 * Move the non-output devices from higher up the
130 * th->thdev[] array to lower positions to maintain
131 * a contiguous array.
133 if (th->thdev[i]->type != INTEL_TH_OUTPUT) {
135 th->thdev[lowest] = th->thdev[i];
146 intel_th_device_remove(th->thdev[i]);
151 th->num_thdevs = lowest;
154 if (thdrv->attr_group)
155 sysfs_remove_group(&thdev->dev.kobj, thdrv->attr_group);
157 pm_runtime_get_sync(dev);
159 thdrv->remove(thdev);
161 if (intel_th_output_assigned(thdev)) {
162 struct intel_th_driver *hubdrv =
163 to_intel_th_driver(dev->parent->driver);
166 /* does not talk to hardware */
167 hubdrv->unassign(hub, thdev);
170 pm_runtime_disable(dev);
171 pm_runtime_set_active(dev);
172 pm_runtime_enable(dev);
177 static struct bus_type intel_th_bus = {
179 .match = intel_th_match,
180 .probe = intel_th_probe,
181 .remove = intel_th_remove,
184 static void intel_th_device_free(struct intel_th_device *thdev);
186 static void intel_th_device_release(struct device *dev)
188 intel_th_device_free(to_intel_th_device(dev));
191 static struct device_type intel_th_source_device_type = {
192 .name = "intel_th_source_device",
193 .release = intel_th_device_release,
196 static char *intel_th_output_devnode(struct device *dev, umode_t *mode,
197 kuid_t *uid, kgid_t *gid)
199 struct intel_th_device *thdev = to_intel_th_device(dev);
200 struct intel_th *th = to_intel_th(thdev);
204 node = kasprintf(GFP_KERNEL, "intel_th%d/%s%d", th->id,
205 thdev->name, thdev->id);
207 node = kasprintf(GFP_KERNEL, "intel_th%d/%s", th->id,
213 static ssize_t port_show(struct device *dev, struct device_attribute *attr,
216 struct intel_th_device *thdev = to_intel_th_device(dev);
218 if (thdev->output.port >= 0)
219 return scnprintf(buf, PAGE_SIZE, "%u\n", thdev->output.port);
221 return scnprintf(buf, PAGE_SIZE, "unassigned\n");
224 static DEVICE_ATTR_RO(port);
226 static void intel_th_trace_prepare(struct intel_th_device *thdev)
228 struct intel_th_device *hub = to_intel_th_hub(thdev);
229 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
231 if (hub->type != INTEL_TH_SWITCH)
234 if (thdev->type != INTEL_TH_OUTPUT)
237 pm_runtime_get_sync(&thdev->dev);
238 hubdrv->prepare(hub, &thdev->output);
239 pm_runtime_put(&thdev->dev);
242 static int intel_th_output_activate(struct intel_th_device *thdev)
244 struct intel_th_driver *thdrv =
245 to_intel_th_driver_or_null(thdev->dev.driver);
246 struct intel_th *th = to_intel_th(thdev);
252 if (!try_module_get(thdrv->driver.owner))
255 pm_runtime_get_sync(&thdev->dev);
258 ret = th->activate(th);
262 intel_th_trace_prepare(thdev);
264 ret = thdrv->activate(thdev);
266 intel_th_trace_enable(thdev);
269 goto fail_deactivate;
278 pm_runtime_put(&thdev->dev);
279 module_put(thdrv->driver.owner);
284 static void intel_th_output_deactivate(struct intel_th_device *thdev)
286 struct intel_th_driver *thdrv =
287 to_intel_th_driver_or_null(thdev->dev.driver);
288 struct intel_th *th = to_intel_th(thdev);
293 if (thdrv->deactivate)
294 thdrv->deactivate(thdev);
296 intel_th_trace_disable(thdev);
301 pm_runtime_put(&thdev->dev);
302 module_put(thdrv->driver.owner);
305 static ssize_t active_show(struct device *dev, struct device_attribute *attr,
308 struct intel_th_device *thdev = to_intel_th_device(dev);
310 return scnprintf(buf, PAGE_SIZE, "%d\n", thdev->output.active);
313 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
314 const char *buf, size_t size)
316 struct intel_th_device *thdev = to_intel_th_device(dev);
320 ret = kstrtoul(buf, 10, &val);
324 if (!!val != thdev->output.active) {
326 ret = intel_th_output_activate(thdev);
328 intel_th_output_deactivate(thdev);
331 return ret ? ret : size;
334 static DEVICE_ATTR_RW(active);
336 static struct attribute *intel_th_output_attrs[] = {
338 &dev_attr_active.attr,
342 ATTRIBUTE_GROUPS(intel_th_output);
344 static struct device_type intel_th_output_device_type = {
345 .name = "intel_th_output_device",
346 .groups = intel_th_output_groups,
347 .release = intel_th_device_release,
348 .devnode = intel_th_output_devnode,
351 static struct device_type intel_th_switch_device_type = {
352 .name = "intel_th_switch_device",
353 .release = intel_th_device_release,
356 static struct device_type *intel_th_device_type[] = {
357 [INTEL_TH_SOURCE] = &intel_th_source_device_type,
358 [INTEL_TH_OUTPUT] = &intel_th_output_device_type,
359 [INTEL_TH_SWITCH] = &intel_th_switch_device_type,
362 int intel_th_driver_register(struct intel_th_driver *thdrv)
364 if (!thdrv->probe || !thdrv->remove)
367 thdrv->driver.bus = &intel_th_bus;
369 return driver_register(&thdrv->driver);
371 EXPORT_SYMBOL_GPL(intel_th_driver_register);
373 void intel_th_driver_unregister(struct intel_th_driver *thdrv)
375 driver_unregister(&thdrv->driver);
377 EXPORT_SYMBOL_GPL(intel_th_driver_unregister);
379 static struct intel_th_device *
380 intel_th_device_alloc(struct intel_th *th, unsigned int type, const char *name,
383 struct device *parent;
384 struct intel_th_device *thdev;
386 if (type == INTEL_TH_OUTPUT)
387 parent = &th->hub->dev;
391 thdev = kzalloc(sizeof(*thdev) + strlen(name) + 1, GFP_KERNEL);
398 strcpy(thdev->name, name);
399 device_initialize(&thdev->dev);
400 thdev->dev.bus = &intel_th_bus;
401 thdev->dev.type = intel_th_device_type[type];
402 thdev->dev.parent = parent;
403 thdev->dev.dma_mask = parent->dma_mask;
404 thdev->dev.dma_parms = parent->dma_parms;
405 dma_set_coherent_mask(&thdev->dev, parent->coherent_dma_mask);
407 dev_set_name(&thdev->dev, "%d-%s%d", th->id, name, id);
409 dev_set_name(&thdev->dev, "%d-%s", th->id, name);
414 static int intel_th_device_add_resources(struct intel_th_device *thdev,
415 struct resource *res, int nres)
419 r = kmemdup(res, sizeof(*res) * nres, GFP_KERNEL);
424 thdev->num_resources = nres;
429 static void intel_th_device_remove(struct intel_th_device *thdev)
431 device_del(&thdev->dev);
432 put_device(&thdev->dev);
435 static void intel_th_device_free(struct intel_th_device *thdev)
437 kfree(thdev->resource);
442 * Intel(R) Trace Hub subdevices
444 static const struct intel_th_subdevice {
446 struct resource res[3];
452 } intel_th_subdevices[] = {
457 /* Handle TSCU from GTH driver */
458 .start = REG_GTH_OFFSET,
459 .end = REG_TSCU_OFFSET + REG_TSCU_LENGTH - 1,
460 .flags = IORESOURCE_MEM,
464 .type = INTEL_TH_SWITCH,
471 .start = REG_MSU_OFFSET,
472 .end = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
473 .flags = IORESOURCE_MEM,
476 .start = BUF_MSU_OFFSET,
477 .end = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
478 .flags = IORESOURCE_MEM,
483 .type = INTEL_TH_OUTPUT,
485 .scrpd = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC0_IS_ENABLED,
491 .start = REG_MSU_OFFSET,
492 .end = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
493 .flags = IORESOURCE_MEM,
496 .start = BUF_MSU_OFFSET,
497 .end = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
498 .flags = IORESOURCE_MEM,
503 .type = INTEL_TH_OUTPUT,
505 .scrpd = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC1_IS_ENABLED,
511 .start = REG_STH_OFFSET,
512 .end = REG_STH_OFFSET + REG_STH_LENGTH - 1,
513 .flags = IORESOURCE_MEM,
518 .flags = IORESOURCE_MEM,
523 .type = INTEL_TH_SOURCE,
529 .start = REG_PTI_OFFSET,
530 .end = REG_PTI_OFFSET + REG_PTI_LENGTH - 1,
531 .flags = IORESOURCE_MEM,
536 .type = INTEL_TH_OUTPUT,
538 .scrpd = SCRPD_PTI_IS_PRIM_DEST,
544 .start = REG_PTI_OFFSET,
545 .end = REG_PTI_OFFSET + REG_PTI_LENGTH - 1,
546 .flags = IORESOURCE_MEM,
551 .type = INTEL_TH_OUTPUT,
553 .scrpd = SCRPD_PTI_IS_PRIM_DEST,
559 .start = REG_DCIH_OFFSET,
560 .end = REG_DCIH_OFFSET + REG_DCIH_LENGTH - 1,
561 .flags = IORESOURCE_MEM,
566 .type = INTEL_TH_OUTPUT,
570 #ifdef CONFIG_MODULES
571 static void __intel_th_request_hub_module(struct work_struct *work)
573 struct intel_th *th = container_of(work, struct intel_th,
574 request_module_work);
576 request_module("intel_th_%s", th->hub->name);
579 static int intel_th_request_hub_module(struct intel_th *th)
581 INIT_WORK(&th->request_module_work, __intel_th_request_hub_module);
582 schedule_work(&th->request_module_work);
587 static void intel_th_request_hub_module_flush(struct intel_th *th)
589 flush_work(&th->request_module_work);
592 static inline int intel_th_request_hub_module(struct intel_th *th)
597 static inline void intel_th_request_hub_module_flush(struct intel_th *th)
600 #endif /* CONFIG_MODULES */
602 static struct intel_th_device *
603 intel_th_subdevice_alloc(struct intel_th *th,
604 const struct intel_th_subdevice *subdev)
606 struct intel_th_device *thdev;
607 struct resource res[3];
608 unsigned int req = 0;
611 thdev = intel_th_device_alloc(th, subdev->type, subdev->name,
614 return ERR_PTR(-ENOMEM);
616 thdev->drvdata = th->drvdata;
618 memcpy(res, subdev->res,
619 sizeof(struct resource) * subdev->nres);
621 for (r = 0; r < subdev->nres; r++) {
622 struct resource *devres = th->resource;
623 int bar = TH_MMIO_CONFIG;
626 * Take .end == 0 to mean 'take the whole bar',
627 * .start then tells us which bar it is. Default to
630 if (!res[r].end && res[r].flags == IORESOURCE_MEM) {
633 res[r].end = resource_size(&devres[bar]) - 1;
636 if (res[r].flags & IORESOURCE_MEM) {
637 res[r].start += devres[bar].start;
638 res[r].end += devres[bar].start;
640 dev_dbg(th->dev, "%s:%d @ %pR\n",
641 subdev->name, r, &res[r]);
642 } else if (res[r].flags & IORESOURCE_IRQ) {
643 res[r].start = th->irq;
647 err = intel_th_device_add_resources(thdev, res, subdev->nres);
649 goto fail_put_device;
651 if (subdev->type == INTEL_TH_OUTPUT) {
652 thdev->dev.devt = MKDEV(th->major, th->num_thdevs);
653 thdev->output.type = subdev->otype;
654 thdev->output.port = -1;
655 thdev->output.scratchpad = subdev->scrpd;
656 } else if (subdev->type == INTEL_TH_SWITCH) {
657 thdev->host_mode = host_mode;
661 err = device_add(&thdev->dev);
665 /* need switch driver to be loaded to enumerate the rest */
666 if (subdev->type == INTEL_TH_SWITCH && !req) {
667 err = intel_th_request_hub_module(th);
675 kfree(thdev->resource);
678 put_device(&thdev->dev);
684 * intel_th_output_enable() - find and enable a device for a given output type
685 * @th: Intel TH instance
686 * @otype: output type
688 * Go through the unallocated output devices, find the first one whos type
689 * matches @otype and instantiate it. These devices are removed when the hub
690 * device is removed, see intel_th_remove().
692 int intel_th_output_enable(struct intel_th *th, unsigned int otype)
694 struct intel_th_device *thdev;
695 int src = 0, dst = 0;
697 for (src = 0, dst = 0; dst <= th->num_thdevs; src++, dst++) {
698 for (; src < ARRAY_SIZE(intel_th_subdevices); src++) {
699 if (intel_th_subdevices[src].type != INTEL_TH_OUTPUT)
702 if (intel_th_subdevices[src].otype != otype)
708 /* no unallocated matching subdevices */
709 if (src == ARRAY_SIZE(intel_th_subdevices))
712 for (; dst < th->num_thdevs; dst++) {
713 if (th->thdev[dst]->type != INTEL_TH_OUTPUT)
716 if (th->thdev[dst]->output.type != otype)
723 * intel_th_subdevices[src] matches our requirements and is
724 * not matched in th::thdev[]
726 if (dst == th->num_thdevs)
733 thdev = intel_th_subdevice_alloc(th, &intel_th_subdevices[src]);
735 return PTR_ERR(thdev);
737 th->thdev[th->num_thdevs++] = thdev;
741 EXPORT_SYMBOL_GPL(intel_th_output_enable);
743 static int intel_th_populate(struct intel_th *th)
747 /* create devices for each intel_th_subdevice */
748 for (src = 0; src < ARRAY_SIZE(intel_th_subdevices); src++) {
749 const struct intel_th_subdevice *subdev =
750 &intel_th_subdevices[src];
751 struct intel_th_device *thdev;
753 /* only allow SOURCE and SWITCH devices in host mode */
754 if (host_mode && subdev->type == INTEL_TH_OUTPUT)
758 * don't enable port OUTPUTs in this path; SWITCH enables them
759 * via intel_th_output_enable()
761 if (subdev->type == INTEL_TH_OUTPUT &&
762 subdev->otype != GTH_NONE)
765 thdev = intel_th_subdevice_alloc(th, subdev);
766 /* note: caller should free subdevices from th::thdev[] */
768 return PTR_ERR(thdev);
770 th->thdev[th->num_thdevs++] = thdev;
776 static int match_devt(struct device *dev, void *data)
778 dev_t devt = (dev_t)(unsigned long)data;
780 return dev->devt == devt;
783 static int intel_th_output_open(struct inode *inode, struct file *file)
785 const struct file_operations *fops;
786 struct intel_th_driver *thdrv;
790 dev = bus_find_device(&intel_th_bus, NULL,
791 (void *)(unsigned long)inode->i_rdev,
793 if (!dev || !dev->driver)
796 thdrv = to_intel_th_driver(dev->driver);
797 fops = fops_get(thdrv->fops);
801 replace_fops(file, fops);
803 file->private_data = to_intel_th_device(dev);
805 if (file->f_op->open) {
806 err = file->f_op->open(inode, file);
813 static const struct file_operations intel_th_output_fops = {
814 .open = intel_th_output_open,
815 .llseek = noop_llseek,
819 * intel_th_alloc() - allocate a new Intel TH device and its subdevices
820 * @dev: parent device
821 * @devres: parent's resources
822 * @ndevres: number of resources
826 intel_th_alloc(struct device *dev, struct intel_th_drvdata *drvdata,
827 struct resource *devres, unsigned int ndevres, int irq)
832 th = kzalloc(sizeof(*th), GFP_KERNEL);
834 return ERR_PTR(-ENOMEM);
836 th->id = ida_simple_get(&intel_th_ida, 0, 0, GFP_KERNEL);
842 th->major = __register_chrdev(0, 0, TH_POSSIBLE_OUTPUTS,
843 "intel_th/output", &intel_th_output_fops);
849 th->drvdata = drvdata;
851 th->resource = devres;
852 th->num_resources = ndevres;
855 dev_set_drvdata(dev, th);
857 pm_runtime_no_callbacks(dev);
859 pm_runtime_allow(dev);
861 err = intel_th_populate(th);
863 /* free the subdevices and undo everything */
871 ida_simple_remove(&intel_th_ida, th->id);
878 EXPORT_SYMBOL_GPL(intel_th_alloc);
880 void intel_th_free(struct intel_th *th)
884 intel_th_request_hub_module_flush(th);
886 intel_th_device_remove(th->hub);
887 for (i = 0; i < th->num_thdevs; i++) {
888 if (th->thdev[i] != th->hub)
889 intel_th_device_remove(th->thdev[i]);
895 pm_runtime_get_sync(th->dev);
896 pm_runtime_forbid(th->dev);
898 __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
901 ida_simple_remove(&intel_th_ida, th->id);
905 EXPORT_SYMBOL_GPL(intel_th_free);
908 * intel_th_trace_enable() - enable tracing for an output device
909 * @thdev: output device that requests tracing be enabled
911 int intel_th_trace_enable(struct intel_th_device *thdev)
913 struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
914 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
916 if (WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH))
919 if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
922 pm_runtime_get_sync(&thdev->dev);
923 hubdrv->enable(hub, &thdev->output);
927 EXPORT_SYMBOL_GPL(intel_th_trace_enable);
930 * intel_th_trace_disable() - disable tracing for an output device
931 * @thdev: output device that requests tracing be disabled
933 int intel_th_trace_disable(struct intel_th_device *thdev)
935 struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
936 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
938 WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH);
939 if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
942 hubdrv->disable(hub, &thdev->output);
943 pm_runtime_put(&thdev->dev);
947 EXPORT_SYMBOL_GPL(intel_th_trace_disable);
949 int intel_th_set_output(struct intel_th_device *thdev,
952 struct intel_th_device *hub = to_intel_th_hub(thdev);
953 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
955 if (!hubdrv->set_output)
958 return hubdrv->set_output(hub, master);
960 EXPORT_SYMBOL_GPL(intel_th_set_output);
962 static int __init intel_th_init(void)
964 intel_th_debug_init();
966 return bus_register(&intel_th_bus);
968 subsys_initcall(intel_th_init);
970 static void __exit intel_th_exit(void)
972 intel_th_debug_done();
974 bus_unregister(&intel_th_bus);
976 module_exit(intel_th_exit);
978 MODULE_LICENSE("GPL v2");
979 MODULE_DESCRIPTION("Intel(R) Trace Hub controller driver");
980 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");