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 DEFINE_IDA(intel_th_ida);
34 static int intel_th_match(struct device *dev, struct device_driver *driver)
36 struct intel_th_driver *thdrv = to_intel_th_driver(driver);
37 struct intel_th_device *thdev = to_intel_th_device(dev);
39 if (thdev->type == INTEL_TH_SWITCH &&
40 (!thdrv->enable || !thdrv->disable))
43 return !strcmp(thdev->name, driver->name);
46 static int intel_th_child_remove(struct device *dev, void *data)
48 device_release_driver(dev);
53 static int intel_th_probe(struct device *dev)
55 struct intel_th_driver *thdrv = to_intel_th_driver(dev->driver);
56 struct intel_th_device *thdev = to_intel_th_device(dev);
57 struct intel_th_driver *hubdrv;
58 struct intel_th_device *hub = NULL;
61 if (thdev->type == INTEL_TH_SWITCH)
64 hub = to_intel_th_device(dev->parent);
66 if (!hub || !hub->dev.driver)
69 hubdrv = to_intel_th_driver(hub->dev.driver);
71 pm_runtime_set_active(dev);
72 pm_runtime_no_callbacks(dev);
73 pm_runtime_enable(dev);
75 ret = thdrv->probe(to_intel_th_device(dev));
79 if (thdrv->attr_group) {
80 ret = sysfs_create_group(&thdev->dev.kobj, thdrv->attr_group);
85 if (thdev->type == INTEL_TH_OUTPUT &&
86 !intel_th_output_assigned(thdev))
87 /* does not talk to hardware */
88 ret = hubdrv->assign(hub, thdev);
96 pm_runtime_disable(dev);
101 static int intel_th_remove(struct device *dev)
103 struct intel_th_driver *thdrv = to_intel_th_driver(dev->driver);
104 struct intel_th_device *thdev = to_intel_th_device(dev);
105 struct intel_th_device *hub = to_intel_th_device(dev->parent);
108 if (thdev->type == INTEL_TH_SWITCH) {
109 err = device_for_each_child(dev, thdev, intel_th_child_remove);
114 if (thdrv->attr_group)
115 sysfs_remove_group(&thdev->dev.kobj, thdrv->attr_group);
117 pm_runtime_get_sync(dev);
119 thdrv->remove(thdev);
121 if (intel_th_output_assigned(thdev)) {
122 struct intel_th_driver *hubdrv =
123 to_intel_th_driver(dev->parent->driver);
126 /* does not talk to hardware */
127 hubdrv->unassign(hub, thdev);
130 pm_runtime_disable(dev);
131 pm_runtime_set_active(dev);
132 pm_runtime_enable(dev);
137 static struct bus_type intel_th_bus = {
140 .match = intel_th_match,
141 .probe = intel_th_probe,
142 .remove = intel_th_remove,
145 static void intel_th_device_free(struct intel_th_device *thdev);
147 static void intel_th_device_release(struct device *dev)
149 intel_th_device_free(to_intel_th_device(dev));
152 static struct device_type intel_th_source_device_type = {
153 .name = "intel_th_source_device",
154 .release = intel_th_device_release,
157 static struct intel_th *to_intel_th(struct intel_th_device *thdev)
160 * subdevice tree is flat: if this one is not a switch, its
163 if (thdev->type != INTEL_TH_SWITCH)
164 thdev = to_intel_th_hub(thdev);
166 if (WARN_ON_ONCE(!thdev || thdev->type != INTEL_TH_SWITCH))
169 return dev_get_drvdata(thdev->dev.parent);
172 static char *intel_th_output_devnode(struct device *dev, umode_t *mode,
173 kuid_t *uid, kgid_t *gid)
175 struct intel_th_device *thdev = to_intel_th_device(dev);
176 struct intel_th *th = to_intel_th(thdev);
180 node = kasprintf(GFP_KERNEL, "intel_th%d/%s%d", th->id,
181 thdev->name, thdev->id);
183 node = kasprintf(GFP_KERNEL, "intel_th%d/%s", th->id,
189 static ssize_t port_show(struct device *dev, struct device_attribute *attr,
192 struct intel_th_device *thdev = to_intel_th_device(dev);
194 if (thdev->output.port >= 0)
195 return scnprintf(buf, PAGE_SIZE, "%u\n", thdev->output.port);
197 return scnprintf(buf, PAGE_SIZE, "unassigned\n");
200 static DEVICE_ATTR_RO(port);
202 static int intel_th_output_activate(struct intel_th_device *thdev)
204 struct intel_th_driver *thdrv =
205 to_intel_th_driver_or_null(thdev->dev.driver);
211 if (!try_module_get(thdrv->driver.owner))
214 pm_runtime_get_sync(&thdev->dev);
217 ret = thdrv->activate(thdev);
219 intel_th_trace_enable(thdev);
222 pm_runtime_put(&thdev->dev);
227 static void intel_th_output_deactivate(struct intel_th_device *thdev)
229 struct intel_th_driver *thdrv =
230 to_intel_th_driver_or_null(thdev->dev.driver);
235 if (thdrv->deactivate)
236 thdrv->deactivate(thdev);
238 intel_th_trace_disable(thdev);
240 pm_runtime_put(&thdev->dev);
241 module_put(thdrv->driver.owner);
244 static ssize_t active_show(struct device *dev, struct device_attribute *attr,
247 struct intel_th_device *thdev = to_intel_th_device(dev);
249 return scnprintf(buf, PAGE_SIZE, "%d\n", thdev->output.active);
252 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
253 const char *buf, size_t size)
255 struct intel_th_device *thdev = to_intel_th_device(dev);
259 ret = kstrtoul(buf, 10, &val);
263 if (!!val != thdev->output.active) {
265 ret = intel_th_output_activate(thdev);
267 intel_th_output_deactivate(thdev);
270 return ret ? ret : size;
273 static DEVICE_ATTR_RW(active);
275 static struct attribute *intel_th_output_attrs[] = {
277 &dev_attr_active.attr,
281 ATTRIBUTE_GROUPS(intel_th_output);
283 static struct device_type intel_th_output_device_type = {
284 .name = "intel_th_output_device",
285 .groups = intel_th_output_groups,
286 .release = intel_th_device_release,
287 .devnode = intel_th_output_devnode,
290 static struct device_type intel_th_switch_device_type = {
291 .name = "intel_th_switch_device",
292 .release = intel_th_device_release,
295 static struct device_type *intel_th_device_type[] = {
296 [INTEL_TH_SOURCE] = &intel_th_source_device_type,
297 [INTEL_TH_OUTPUT] = &intel_th_output_device_type,
298 [INTEL_TH_SWITCH] = &intel_th_switch_device_type,
301 int intel_th_driver_register(struct intel_th_driver *thdrv)
303 if (!thdrv->probe || !thdrv->remove)
306 thdrv->driver.bus = &intel_th_bus;
308 return driver_register(&thdrv->driver);
310 EXPORT_SYMBOL_GPL(intel_th_driver_register);
312 void intel_th_driver_unregister(struct intel_th_driver *thdrv)
314 driver_unregister(&thdrv->driver);
316 EXPORT_SYMBOL_GPL(intel_th_driver_unregister);
318 static struct intel_th_device *
319 intel_th_device_alloc(struct intel_th *th, unsigned int type, const char *name,
322 struct device *parent;
323 struct intel_th_device *thdev;
325 if (type == INTEL_TH_SWITCH)
328 parent = &th->hub->dev;
330 thdev = kzalloc(sizeof(*thdev) + strlen(name) + 1, GFP_KERNEL);
337 strcpy(thdev->name, name);
338 device_initialize(&thdev->dev);
339 thdev->dev.bus = &intel_th_bus;
340 thdev->dev.type = intel_th_device_type[type];
341 thdev->dev.parent = parent;
342 thdev->dev.dma_mask = parent->dma_mask;
343 thdev->dev.dma_parms = parent->dma_parms;
344 dma_set_coherent_mask(&thdev->dev, parent->coherent_dma_mask);
346 dev_set_name(&thdev->dev, "%d-%s%d", th->id, name, id);
348 dev_set_name(&thdev->dev, "%d-%s", th->id, name);
353 static int intel_th_device_add_resources(struct intel_th_device *thdev,
354 struct resource *res, int nres)
358 r = kmemdup(res, sizeof(*res) * nres, GFP_KERNEL);
363 thdev->num_resources = nres;
368 static void intel_th_device_remove(struct intel_th_device *thdev)
370 device_del(&thdev->dev);
371 put_device(&thdev->dev);
374 static void intel_th_device_free(struct intel_th_device *thdev)
376 kfree(thdev->resource);
381 * Intel(R) Trace Hub subdevices
383 static struct intel_th_subdevice {
385 struct resource res[3];
391 } intel_th_subdevices[TH_SUBDEVICE_MAX] = {
396 .start = REG_GTH_OFFSET,
397 .end = REG_GTH_OFFSET + REG_GTH_LENGTH - 1,
398 .flags = IORESOURCE_MEM,
402 .type = INTEL_TH_SWITCH,
409 .start = REG_MSU_OFFSET,
410 .end = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
411 .flags = IORESOURCE_MEM,
414 .start = BUF_MSU_OFFSET,
415 .end = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
416 .flags = IORESOURCE_MEM,
421 .type = INTEL_TH_OUTPUT,
423 .scrpd = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC0_IS_ENABLED,
429 .start = REG_MSU_OFFSET,
430 .end = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
431 .flags = IORESOURCE_MEM,
434 .start = BUF_MSU_OFFSET,
435 .end = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
436 .flags = IORESOURCE_MEM,
441 .type = INTEL_TH_OUTPUT,
443 .scrpd = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC1_IS_ENABLED,
449 .start = REG_STH_OFFSET,
450 .end = REG_STH_OFFSET + REG_STH_LENGTH - 1,
451 .flags = IORESOURCE_MEM,
456 .flags = IORESOURCE_MEM,
461 .type = INTEL_TH_SOURCE,
467 .start = REG_PTI_OFFSET,
468 .end = REG_PTI_OFFSET + REG_PTI_LENGTH - 1,
469 .flags = IORESOURCE_MEM,
474 .type = INTEL_TH_OUTPUT,
476 .scrpd = SCRPD_PTI_IS_PRIM_DEST,
482 .start = REG_DCIH_OFFSET,
483 .end = REG_DCIH_OFFSET + REG_DCIH_LENGTH - 1,
484 .flags = IORESOURCE_MEM,
489 .type = INTEL_TH_OUTPUT,
493 #ifdef CONFIG_MODULES
494 static void __intel_th_request_hub_module(struct work_struct *work)
496 struct intel_th *th = container_of(work, struct intel_th,
497 request_module_work);
499 request_module("intel_th_%s", th->hub->name);
502 static int intel_th_request_hub_module(struct intel_th *th)
504 INIT_WORK(&th->request_module_work, __intel_th_request_hub_module);
505 schedule_work(&th->request_module_work);
510 static void intel_th_request_hub_module_flush(struct intel_th *th)
512 flush_work(&th->request_module_work);
515 static inline int intel_th_request_hub_module(struct intel_th *th)
520 static inline void intel_th_request_hub_module_flush(struct intel_th *th)
523 #endif /* CONFIG_MODULES */
525 static int intel_th_populate(struct intel_th *th, struct resource *devres,
526 unsigned int ndevres, int irq)
528 struct resource res[3];
529 unsigned int req = 0;
532 /* create devices for each intel_th_subdevice */
533 for (i = 0; i < ARRAY_SIZE(intel_th_subdevices); i++) {
534 struct intel_th_subdevice *subdev = &intel_th_subdevices[i];
535 struct intel_th_device *thdev;
538 thdev = intel_th_device_alloc(th, subdev->type, subdev->name,
545 memcpy(res, subdev->res,
546 sizeof(struct resource) * subdev->nres);
548 for (r = 0; r < subdev->nres; r++) {
549 int bar = TH_MMIO_CONFIG;
552 * Take .end == 0 to mean 'take the whole bar',
553 * .start then tells us which bar it is. Default to
556 if (!res[r].end && res[r].flags == IORESOURCE_MEM) {
559 res[r].end = resource_size(&devres[bar]) - 1;
562 if (res[r].flags & IORESOURCE_MEM) {
563 res[r].start += devres[bar].start;
564 res[r].end += devres[bar].start;
566 dev_dbg(th->dev, "%s:%d @ %pR\n",
567 subdev->name, r, &res[r]);
568 } else if (res[r].flags & IORESOURCE_IRQ) {
573 err = intel_th_device_add_resources(thdev, res, subdev->nres);
575 put_device(&thdev->dev);
579 if (subdev->type == INTEL_TH_OUTPUT) {
580 thdev->dev.devt = MKDEV(th->major, i);
581 thdev->output.type = subdev->otype;
582 thdev->output.port = -1;
583 thdev->output.scratchpad = subdev->scrpd;
586 err = device_add(&thdev->dev);
588 put_device(&thdev->dev);
592 /* need switch driver to be loaded to enumerate the rest */
593 if (subdev->type == INTEL_TH_SWITCH && !req) {
595 err = intel_th_request_hub_module(th);
600 th->thdev[i] = thdev;
606 for (i-- ; i >= 0; i--)
607 intel_th_device_remove(th->thdev[i]);
612 static int match_devt(struct device *dev, void *data)
614 dev_t devt = (dev_t)(unsigned long)data;
616 return dev->devt == devt;
619 static int intel_th_output_open(struct inode *inode, struct file *file)
621 const struct file_operations *fops;
622 struct intel_th_driver *thdrv;
626 dev = bus_find_device(&intel_th_bus, NULL,
627 (void *)(unsigned long)inode->i_rdev,
629 if (!dev || !dev->driver)
632 thdrv = to_intel_th_driver(dev->driver);
633 fops = fops_get(thdrv->fops);
637 replace_fops(file, fops);
639 file->private_data = to_intel_th_device(dev);
641 if (file->f_op->open) {
642 err = file->f_op->open(inode, file);
649 static const struct file_operations intel_th_output_fops = {
650 .open = intel_th_output_open,
651 .llseek = noop_llseek,
655 * intel_th_alloc() - allocate a new Intel TH device and its subdevices
656 * @dev: parent device
657 * @devres: parent's resources
658 * @ndevres: number of resources
662 intel_th_alloc(struct device *dev, struct resource *devres,
663 unsigned int ndevres, int irq)
668 th = kzalloc(sizeof(*th), GFP_KERNEL);
670 return ERR_PTR(-ENOMEM);
672 th->id = ida_simple_get(&intel_th_ida, 0, 0, GFP_KERNEL);
678 th->major = __register_chrdev(0, 0, TH_POSSIBLE_OUTPUTS,
679 "intel_th/output", &intel_th_output_fops);
686 dev_set_drvdata(dev, th);
688 pm_runtime_no_callbacks(dev);
690 pm_runtime_allow(dev);
692 err = intel_th_populate(th, devres, ndevres, irq);
699 pm_runtime_forbid(dev);
701 __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
705 ida_simple_remove(&intel_th_ida, th->id);
712 EXPORT_SYMBOL_GPL(intel_th_alloc);
714 void intel_th_free(struct intel_th *th)
718 intel_th_request_hub_module_flush(th);
719 for (i = 0; i < TH_SUBDEVICE_MAX; i++)
720 if (th->thdev[i] != th->hub)
721 intel_th_device_remove(th->thdev[i]);
723 intel_th_device_remove(th->hub);
725 pm_runtime_get_sync(th->dev);
726 pm_runtime_forbid(th->dev);
728 __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
731 ida_simple_remove(&intel_th_ida, th->id);
735 EXPORT_SYMBOL_GPL(intel_th_free);
738 * intel_th_trace_enable() - enable tracing for an output device
739 * @thdev: output device that requests tracing be enabled
741 int intel_th_trace_enable(struct intel_th_device *thdev)
743 struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
744 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
746 if (WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH))
749 if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
752 pm_runtime_get_sync(&thdev->dev);
753 hubdrv->enable(hub, &thdev->output);
757 EXPORT_SYMBOL_GPL(intel_th_trace_enable);
760 * intel_th_trace_disable() - disable tracing for an output device
761 * @thdev: output device that requests tracing be disabled
763 int intel_th_trace_disable(struct intel_th_device *thdev)
765 struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
766 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
768 WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH);
769 if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
772 hubdrv->disable(hub, &thdev->output);
773 pm_runtime_put(&thdev->dev);
777 EXPORT_SYMBOL_GPL(intel_th_trace_disable);
779 int intel_th_set_output(struct intel_th_device *thdev,
782 struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
783 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
785 if (!hubdrv->set_output)
788 return hubdrv->set_output(hub, master);
790 EXPORT_SYMBOL_GPL(intel_th_set_output);
792 static int __init intel_th_init(void)
794 intel_th_debug_init();
796 return bus_register(&intel_th_bus);
798 subsys_initcall(intel_th_init);
800 static void __exit intel_th_exit(void)
802 intel_th_debug_done();
804 bus_unregister(&intel_th_bus);
806 module_exit(intel_th_exit);
808 MODULE_LICENSE("GPL v2");
809 MODULE_DESCRIPTION("Intel(R) Trace Hub controller driver");
810 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");