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);
223 module_put(thdrv->driver.owner);
229 static void intel_th_output_deactivate(struct intel_th_device *thdev)
231 struct intel_th_driver *thdrv =
232 to_intel_th_driver_or_null(thdev->dev.driver);
237 if (thdrv->deactivate)
238 thdrv->deactivate(thdev);
240 intel_th_trace_disable(thdev);
242 pm_runtime_put(&thdev->dev);
243 module_put(thdrv->driver.owner);
246 static ssize_t active_show(struct device *dev, struct device_attribute *attr,
249 struct intel_th_device *thdev = to_intel_th_device(dev);
251 return scnprintf(buf, PAGE_SIZE, "%d\n", thdev->output.active);
254 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
255 const char *buf, size_t size)
257 struct intel_th_device *thdev = to_intel_th_device(dev);
261 ret = kstrtoul(buf, 10, &val);
265 if (!!val != thdev->output.active) {
267 ret = intel_th_output_activate(thdev);
269 intel_th_output_deactivate(thdev);
272 return ret ? ret : size;
275 static DEVICE_ATTR_RW(active);
277 static struct attribute *intel_th_output_attrs[] = {
279 &dev_attr_active.attr,
283 ATTRIBUTE_GROUPS(intel_th_output);
285 static struct device_type intel_th_output_device_type = {
286 .name = "intel_th_output_device",
287 .groups = intel_th_output_groups,
288 .release = intel_th_device_release,
289 .devnode = intel_th_output_devnode,
292 static struct device_type intel_th_switch_device_type = {
293 .name = "intel_th_switch_device",
294 .release = intel_th_device_release,
297 static struct device_type *intel_th_device_type[] = {
298 [INTEL_TH_SOURCE] = &intel_th_source_device_type,
299 [INTEL_TH_OUTPUT] = &intel_th_output_device_type,
300 [INTEL_TH_SWITCH] = &intel_th_switch_device_type,
303 int intel_th_driver_register(struct intel_th_driver *thdrv)
305 if (!thdrv->probe || !thdrv->remove)
308 thdrv->driver.bus = &intel_th_bus;
310 return driver_register(&thdrv->driver);
312 EXPORT_SYMBOL_GPL(intel_th_driver_register);
314 void intel_th_driver_unregister(struct intel_th_driver *thdrv)
316 driver_unregister(&thdrv->driver);
318 EXPORT_SYMBOL_GPL(intel_th_driver_unregister);
320 static struct intel_th_device *
321 intel_th_device_alloc(struct intel_th *th, unsigned int type, const char *name,
324 struct device *parent;
325 struct intel_th_device *thdev;
327 if (type == INTEL_TH_SWITCH)
330 parent = &th->hub->dev;
332 thdev = kzalloc(sizeof(*thdev) + strlen(name) + 1, GFP_KERNEL);
339 strcpy(thdev->name, name);
340 device_initialize(&thdev->dev);
341 thdev->dev.bus = &intel_th_bus;
342 thdev->dev.type = intel_th_device_type[type];
343 thdev->dev.parent = parent;
344 thdev->dev.dma_mask = parent->dma_mask;
345 thdev->dev.dma_parms = parent->dma_parms;
346 dma_set_coherent_mask(&thdev->dev, parent->coherent_dma_mask);
348 dev_set_name(&thdev->dev, "%d-%s%d", th->id, name, id);
350 dev_set_name(&thdev->dev, "%d-%s", th->id, name);
355 static int intel_th_device_add_resources(struct intel_th_device *thdev,
356 struct resource *res, int nres)
360 r = kmemdup(res, sizeof(*res) * nres, GFP_KERNEL);
365 thdev->num_resources = nres;
370 static void intel_th_device_remove(struct intel_th_device *thdev)
372 device_del(&thdev->dev);
373 put_device(&thdev->dev);
376 static void intel_th_device_free(struct intel_th_device *thdev)
378 kfree(thdev->resource);
383 * Intel(R) Trace Hub subdevices
385 static struct intel_th_subdevice {
387 struct resource res[3];
393 } intel_th_subdevices[TH_SUBDEVICE_MAX] = {
398 .start = REG_GTH_OFFSET,
399 .end = REG_GTH_OFFSET + REG_GTH_LENGTH - 1,
400 .flags = IORESOURCE_MEM,
404 .type = INTEL_TH_SWITCH,
411 .start = REG_MSU_OFFSET,
412 .end = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
413 .flags = IORESOURCE_MEM,
416 .start = BUF_MSU_OFFSET,
417 .end = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
418 .flags = IORESOURCE_MEM,
423 .type = INTEL_TH_OUTPUT,
425 .scrpd = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC0_IS_ENABLED,
431 .start = REG_MSU_OFFSET,
432 .end = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
433 .flags = IORESOURCE_MEM,
436 .start = BUF_MSU_OFFSET,
437 .end = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
438 .flags = IORESOURCE_MEM,
443 .type = INTEL_TH_OUTPUT,
445 .scrpd = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC1_IS_ENABLED,
451 .start = REG_STH_OFFSET,
452 .end = REG_STH_OFFSET + REG_STH_LENGTH - 1,
453 .flags = IORESOURCE_MEM,
458 .flags = IORESOURCE_MEM,
463 .type = INTEL_TH_SOURCE,
469 .start = REG_PTI_OFFSET,
470 .end = REG_PTI_OFFSET + REG_PTI_LENGTH - 1,
471 .flags = IORESOURCE_MEM,
476 .type = INTEL_TH_OUTPUT,
478 .scrpd = SCRPD_PTI_IS_PRIM_DEST,
484 .start = REG_DCIH_OFFSET,
485 .end = REG_DCIH_OFFSET + REG_DCIH_LENGTH - 1,
486 .flags = IORESOURCE_MEM,
491 .type = INTEL_TH_OUTPUT,
495 #ifdef CONFIG_MODULES
496 static void __intel_th_request_hub_module(struct work_struct *work)
498 struct intel_th *th = container_of(work, struct intel_th,
499 request_module_work);
501 request_module("intel_th_%s", th->hub->name);
504 static int intel_th_request_hub_module(struct intel_th *th)
506 INIT_WORK(&th->request_module_work, __intel_th_request_hub_module);
507 schedule_work(&th->request_module_work);
512 static void intel_th_request_hub_module_flush(struct intel_th *th)
514 flush_work(&th->request_module_work);
517 static inline int intel_th_request_hub_module(struct intel_th *th)
522 static inline void intel_th_request_hub_module_flush(struct intel_th *th)
525 #endif /* CONFIG_MODULES */
527 static int intel_th_populate(struct intel_th *th, struct resource *devres,
528 unsigned int ndevres, int irq)
530 struct resource res[3];
531 unsigned int req = 0;
534 /* create devices for each intel_th_subdevice */
535 for (i = 0; i < ARRAY_SIZE(intel_th_subdevices); i++) {
536 struct intel_th_subdevice *subdev = &intel_th_subdevices[i];
537 struct intel_th_device *thdev;
540 thdev = intel_th_device_alloc(th, subdev->type, subdev->name,
547 memcpy(res, subdev->res,
548 sizeof(struct resource) * subdev->nres);
550 for (r = 0; r < subdev->nres; r++) {
551 int bar = TH_MMIO_CONFIG;
554 * Take .end == 0 to mean 'take the whole bar',
555 * .start then tells us which bar it is. Default to
558 if (!res[r].end && res[r].flags == IORESOURCE_MEM) {
561 res[r].end = resource_size(&devres[bar]) - 1;
564 if (res[r].flags & IORESOURCE_MEM) {
565 res[r].start += devres[bar].start;
566 res[r].end += devres[bar].start;
568 dev_dbg(th->dev, "%s:%d @ %pR\n",
569 subdev->name, r, &res[r]);
570 } else if (res[r].flags & IORESOURCE_IRQ) {
575 err = intel_th_device_add_resources(thdev, res, subdev->nres);
577 put_device(&thdev->dev);
581 if (subdev->type == INTEL_TH_OUTPUT) {
582 thdev->dev.devt = MKDEV(th->major, i);
583 thdev->output.type = subdev->otype;
584 thdev->output.port = -1;
585 thdev->output.scratchpad = subdev->scrpd;
588 err = device_add(&thdev->dev);
590 put_device(&thdev->dev);
594 /* need switch driver to be loaded to enumerate the rest */
595 if (subdev->type == INTEL_TH_SWITCH && !req) {
597 err = intel_th_request_hub_module(th);
602 th->thdev[i] = thdev;
608 for (i-- ; i >= 0; i--)
609 intel_th_device_remove(th->thdev[i]);
614 static int match_devt(struct device *dev, void *data)
616 dev_t devt = (dev_t)(unsigned long)data;
618 return dev->devt == devt;
621 static int intel_th_output_open(struct inode *inode, struct file *file)
623 const struct file_operations *fops;
624 struct intel_th_driver *thdrv;
628 dev = bus_find_device(&intel_th_bus, NULL,
629 (void *)(unsigned long)inode->i_rdev,
631 if (!dev || !dev->driver)
634 thdrv = to_intel_th_driver(dev->driver);
635 fops = fops_get(thdrv->fops);
639 replace_fops(file, fops);
641 file->private_data = to_intel_th_device(dev);
643 if (file->f_op->open) {
644 err = file->f_op->open(inode, file);
651 static const struct file_operations intel_th_output_fops = {
652 .open = intel_th_output_open,
653 .llseek = noop_llseek,
657 * intel_th_alloc() - allocate a new Intel TH device and its subdevices
658 * @dev: parent device
659 * @devres: parent's resources
660 * @ndevres: number of resources
664 intel_th_alloc(struct device *dev, struct resource *devres,
665 unsigned int ndevres, int irq)
670 th = kzalloc(sizeof(*th), GFP_KERNEL);
672 return ERR_PTR(-ENOMEM);
674 th->id = ida_simple_get(&intel_th_ida, 0, 0, GFP_KERNEL);
680 th->major = __register_chrdev(0, 0, TH_POSSIBLE_OUTPUTS,
681 "intel_th/output", &intel_th_output_fops);
688 dev_set_drvdata(dev, th);
690 pm_runtime_no_callbacks(dev);
692 pm_runtime_allow(dev);
694 err = intel_th_populate(th, devres, ndevres, irq);
701 pm_runtime_forbid(dev);
703 __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
707 ida_simple_remove(&intel_th_ida, th->id);
714 EXPORT_SYMBOL_GPL(intel_th_alloc);
716 void intel_th_free(struct intel_th *th)
720 intel_th_request_hub_module_flush(th);
721 for (i = 0; i < TH_SUBDEVICE_MAX; i++)
722 if (th->thdev[i] != th->hub)
723 intel_th_device_remove(th->thdev[i]);
725 intel_th_device_remove(th->hub);
727 pm_runtime_get_sync(th->dev);
728 pm_runtime_forbid(th->dev);
730 __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
733 ida_simple_remove(&intel_th_ida, th->id);
737 EXPORT_SYMBOL_GPL(intel_th_free);
740 * intel_th_trace_enable() - enable tracing for an output device
741 * @thdev: output device that requests tracing be enabled
743 int intel_th_trace_enable(struct intel_th_device *thdev)
745 struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
746 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
748 if (WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH))
751 if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
754 pm_runtime_get_sync(&thdev->dev);
755 hubdrv->enable(hub, &thdev->output);
759 EXPORT_SYMBOL_GPL(intel_th_trace_enable);
762 * intel_th_trace_disable() - disable tracing for an output device
763 * @thdev: output device that requests tracing be disabled
765 int intel_th_trace_disable(struct intel_th_device *thdev)
767 struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
768 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
770 WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH);
771 if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
774 hubdrv->disable(hub, &thdev->output);
775 pm_runtime_put(&thdev->dev);
779 EXPORT_SYMBOL_GPL(intel_th_trace_disable);
781 int intel_th_set_output(struct intel_th_device *thdev,
784 struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
785 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
787 if (!hubdrv->set_output)
790 return hubdrv->set_output(hub, master);
792 EXPORT_SYMBOL_GPL(intel_th_set_output);
794 static int __init intel_th_init(void)
796 intel_th_debug_init();
798 return bus_register(&intel_th_bus);
800 subsys_initcall(intel_th_init);
802 static void __exit intel_th_exit(void)
804 intel_th_debug_done();
806 bus_unregister(&intel_th_bus);
808 module_exit(intel_th_exit);
810 MODULE_LICENSE("GPL v2");
811 MODULE_DESCRIPTION("Intel(R) Trace Hub controller driver");
812 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");