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/dma-mapping.h>
31 static DEFINE_IDA(intel_th_ida);
33 static int intel_th_match(struct device *dev, struct device_driver *driver)
35 struct intel_th_driver *thdrv = to_intel_th_driver(driver);
36 struct intel_th_device *thdev = to_intel_th_device(dev);
38 if (thdev->type == INTEL_TH_SWITCH &&
39 (!thdrv->enable || !thdrv->disable))
42 return !strcmp(thdev->name, driver->name);
45 static int intel_th_child_remove(struct device *dev, void *data)
47 device_release_driver(dev);
52 static int intel_th_probe(struct device *dev)
54 struct intel_th_driver *thdrv = to_intel_th_driver(dev->driver);
55 struct intel_th_device *thdev = to_intel_th_device(dev);
56 struct intel_th_driver *hubdrv;
57 struct intel_th_device *hub = NULL;
60 if (thdev->type == INTEL_TH_SWITCH)
63 hub = to_intel_th_device(dev->parent);
65 if (!hub || !hub->dev.driver)
68 hubdrv = to_intel_th_driver(hub->dev.driver);
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);
83 if (thdev->type == INTEL_TH_OUTPUT &&
84 !intel_th_output_assigned(thdev))
85 ret = hubdrv->assign(hub, thdev);
90 static int intel_th_remove(struct device *dev)
92 struct intel_th_driver *thdrv = to_intel_th_driver(dev->driver);
93 struct intel_th_device *thdev = to_intel_th_device(dev);
94 struct intel_th_device *hub = to_intel_th_device(dev->parent);
97 if (thdev->type == INTEL_TH_SWITCH) {
98 err = device_for_each_child(dev, thdev, intel_th_child_remove);
103 if (thdrv->attr_group)
104 sysfs_remove_group(&thdev->dev.kobj, thdrv->attr_group);
106 thdrv->remove(thdev);
108 if (intel_th_output_assigned(thdev)) {
109 struct intel_th_driver *hubdrv =
110 to_intel_th_driver(dev->parent->driver);
113 hubdrv->unassign(hub, thdev);
119 static struct bus_type intel_th_bus = {
122 .match = intel_th_match,
123 .probe = intel_th_probe,
124 .remove = intel_th_remove,
127 static void intel_th_device_free(struct intel_th_device *thdev);
129 static void intel_th_device_release(struct device *dev)
131 intel_th_device_free(to_intel_th_device(dev));
134 static struct device_type intel_th_source_device_type = {
135 .name = "intel_th_source_device",
136 .release = intel_th_device_release,
139 static struct intel_th *to_intel_th(struct intel_th_device *thdev)
142 * subdevice tree is flat: if this one is not a switch, its
145 if (thdev->type != INTEL_TH_SWITCH)
146 thdev = to_intel_th_hub(thdev);
148 if (WARN_ON_ONCE(!thdev || thdev->type != INTEL_TH_SWITCH))
151 return dev_get_drvdata(thdev->dev.parent);
154 static char *intel_th_output_devnode(struct device *dev, umode_t *mode,
155 kuid_t *uid, kgid_t *gid)
157 struct intel_th_device *thdev = to_intel_th_device(dev);
158 struct intel_th *th = to_intel_th(thdev);
162 node = kasprintf(GFP_KERNEL, "intel_th%d/%s%d", th->id,
163 thdev->name, thdev->id);
165 node = kasprintf(GFP_KERNEL, "intel_th%d/%s", th->id,
171 static ssize_t port_show(struct device *dev, struct device_attribute *attr,
174 struct intel_th_device *thdev = to_intel_th_device(dev);
176 if (thdev->output.port >= 0)
177 return scnprintf(buf, PAGE_SIZE, "%u\n", thdev->output.port);
179 return scnprintf(buf, PAGE_SIZE, "unassigned\n");
182 static DEVICE_ATTR_RO(port);
184 static int intel_th_output_activate(struct intel_th_device *thdev)
186 struct intel_th_driver *thdrv =
187 to_intel_th_driver_or_null(thdev->dev.driver);
192 if (!try_module_get(thdrv->driver.owner))
196 return thdrv->activate(thdev);
198 intel_th_trace_enable(thdev);
203 static void intel_th_output_deactivate(struct intel_th_device *thdev)
205 struct intel_th_driver *thdrv =
206 to_intel_th_driver_or_null(thdev->dev.driver);
211 if (thdrv->deactivate)
212 thdrv->deactivate(thdev);
214 intel_th_trace_disable(thdev);
216 module_put(thdrv->driver.owner);
219 static ssize_t active_show(struct device *dev, struct device_attribute *attr,
222 struct intel_th_device *thdev = to_intel_th_device(dev);
224 return scnprintf(buf, PAGE_SIZE, "%d\n", thdev->output.active);
227 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
228 const char *buf, size_t size)
230 struct intel_th_device *thdev = to_intel_th_device(dev);
234 ret = kstrtoul(buf, 10, &val);
238 if (!!val != thdev->output.active) {
240 ret = intel_th_output_activate(thdev);
242 intel_th_output_deactivate(thdev);
245 return ret ? ret : size;
248 static DEVICE_ATTR_RW(active);
250 static struct attribute *intel_th_output_attrs[] = {
252 &dev_attr_active.attr,
256 ATTRIBUTE_GROUPS(intel_th_output);
258 static struct device_type intel_th_output_device_type = {
259 .name = "intel_th_output_device",
260 .groups = intel_th_output_groups,
261 .release = intel_th_device_release,
262 .devnode = intel_th_output_devnode,
265 static struct device_type intel_th_switch_device_type = {
266 .name = "intel_th_switch_device",
267 .release = intel_th_device_release,
270 static struct device_type *intel_th_device_type[] = {
271 [INTEL_TH_SOURCE] = &intel_th_source_device_type,
272 [INTEL_TH_OUTPUT] = &intel_th_output_device_type,
273 [INTEL_TH_SWITCH] = &intel_th_switch_device_type,
276 int intel_th_driver_register(struct intel_th_driver *thdrv)
278 if (!thdrv->probe || !thdrv->remove)
281 thdrv->driver.bus = &intel_th_bus;
283 return driver_register(&thdrv->driver);
285 EXPORT_SYMBOL_GPL(intel_th_driver_register);
287 void intel_th_driver_unregister(struct intel_th_driver *thdrv)
289 driver_unregister(&thdrv->driver);
291 EXPORT_SYMBOL_GPL(intel_th_driver_unregister);
293 static struct intel_th_device *
294 intel_th_device_alloc(struct intel_th *th, unsigned int type, const char *name,
297 struct device *parent;
298 struct intel_th_device *thdev;
300 if (type == INTEL_TH_SWITCH)
303 parent = &th->hub->dev;
305 thdev = kzalloc(sizeof(*thdev) + strlen(name) + 1, GFP_KERNEL);
312 strcpy(thdev->name, name);
313 device_initialize(&thdev->dev);
314 thdev->dev.bus = &intel_th_bus;
315 thdev->dev.type = intel_th_device_type[type];
316 thdev->dev.parent = parent;
317 thdev->dev.dma_mask = parent->dma_mask;
318 thdev->dev.dma_parms = parent->dma_parms;
319 dma_set_coherent_mask(&thdev->dev, parent->coherent_dma_mask);
321 dev_set_name(&thdev->dev, "%d-%s%d", th->id, name, id);
323 dev_set_name(&thdev->dev, "%d-%s", th->id, name);
328 static int intel_th_device_add_resources(struct intel_th_device *thdev,
329 struct resource *res, int nres)
333 r = kmemdup(res, sizeof(*res) * nres, GFP_KERNEL);
338 thdev->num_resources = nres;
343 static void intel_th_device_remove(struct intel_th_device *thdev)
345 device_del(&thdev->dev);
346 put_device(&thdev->dev);
349 static void intel_th_device_free(struct intel_th_device *thdev)
351 kfree(thdev->resource);
356 * Intel(R) Trace Hub subdevices
358 static struct intel_th_subdevice {
360 struct resource res[3];
366 } intel_th_subdevices[TH_SUBDEVICE_MAX] = {
371 .start = REG_GTH_OFFSET,
372 .end = REG_GTH_OFFSET + REG_GTH_LENGTH - 1,
373 .flags = IORESOURCE_MEM,
377 .type = INTEL_TH_SWITCH,
384 .start = REG_MSU_OFFSET,
385 .end = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
386 .flags = IORESOURCE_MEM,
389 .start = BUF_MSU_OFFSET,
390 .end = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
391 .flags = IORESOURCE_MEM,
396 .type = INTEL_TH_OUTPUT,
398 .scrpd = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC0_IS_ENABLED,
404 .start = REG_MSU_OFFSET,
405 .end = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
406 .flags = IORESOURCE_MEM,
409 .start = BUF_MSU_OFFSET,
410 .end = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
411 .flags = IORESOURCE_MEM,
416 .type = INTEL_TH_OUTPUT,
418 .scrpd = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC1_IS_ENABLED,
424 .start = REG_STH_OFFSET,
425 .end = REG_STH_OFFSET + REG_STH_LENGTH - 1,
426 .flags = IORESOURCE_MEM,
431 .flags = IORESOURCE_MEM,
436 .type = INTEL_TH_SOURCE,
442 .start = REG_PTI_OFFSET,
443 .end = REG_PTI_OFFSET + REG_PTI_LENGTH - 1,
444 .flags = IORESOURCE_MEM,
449 .type = INTEL_TH_OUTPUT,
451 .scrpd = SCRPD_PTI_IS_PRIM_DEST,
457 .start = REG_DCIH_OFFSET,
458 .end = REG_DCIH_OFFSET + REG_DCIH_LENGTH - 1,
459 .flags = IORESOURCE_MEM,
464 .type = INTEL_TH_OUTPUT,
468 static int intel_th_populate(struct intel_th *th, struct resource *devres,
469 unsigned int ndevres, int irq)
471 struct resource res[3];
472 unsigned int req = 0;
475 /* create devices for each intel_th_subdevice */
476 for (i = 0; i < ARRAY_SIZE(intel_th_subdevices); i++) {
477 struct intel_th_subdevice *subdev = &intel_th_subdevices[i];
478 struct intel_th_device *thdev;
481 thdev = intel_th_device_alloc(th, subdev->type, subdev->name,
488 memcpy(res, subdev->res,
489 sizeof(struct resource) * subdev->nres);
491 for (r = 0; r < subdev->nres; r++) {
492 int bar = TH_MMIO_CONFIG;
495 * Take .end == 0 to mean 'take the whole bar',
496 * .start then tells us which bar it is. Default to
499 if (!res[r].end && res[r].flags == IORESOURCE_MEM) {
502 res[r].end = resource_size(&devres[bar]) - 1;
505 if (res[r].flags & IORESOURCE_MEM) {
506 res[r].start += devres[bar].start;
507 res[r].end += devres[bar].start;
509 dev_dbg(th->dev, "%s:%d @ %pR\n",
510 subdev->name, r, &res[r]);
511 } else if (res[r].flags & IORESOURCE_IRQ) {
516 err = intel_th_device_add_resources(thdev, res, subdev->nres);
518 put_device(&thdev->dev);
522 if (subdev->type == INTEL_TH_OUTPUT) {
523 thdev->dev.devt = MKDEV(th->major, i);
524 thdev->output.type = subdev->otype;
525 thdev->output.port = -1;
526 thdev->output.scratchpad = subdev->scrpd;
529 err = device_add(&thdev->dev);
531 put_device(&thdev->dev);
535 /* need switch driver to be loaded to enumerate the rest */
536 if (subdev->type == INTEL_TH_SWITCH && !req) {
538 err = request_module("intel_th_%s", subdev->name);
543 th->thdev[i] = thdev;
549 for (i-- ; i >= 0; i--)
550 intel_th_device_remove(th->thdev[i]);
555 static int match_devt(struct device *dev, void *data)
557 dev_t devt = (dev_t)(unsigned long)data;
559 return dev->devt == devt;
562 static int intel_th_output_open(struct inode *inode, struct file *file)
564 const struct file_operations *fops;
565 struct intel_th_driver *thdrv;
569 dev = bus_find_device(&intel_th_bus, NULL,
570 (void *)(unsigned long)inode->i_rdev,
572 if (!dev || !dev->driver)
575 thdrv = to_intel_th_driver(dev->driver);
576 fops = fops_get(thdrv->fops);
580 replace_fops(file, fops);
582 file->private_data = to_intel_th_device(dev);
584 if (file->f_op->open) {
585 err = file->f_op->open(inode, file);
592 static const struct file_operations intel_th_output_fops = {
593 .open = intel_th_output_open,
594 .llseek = noop_llseek,
598 * intel_th_alloc() - allocate a new Intel TH device and its subdevices
599 * @dev: parent device
600 * @devres: parent's resources
601 * @ndevres: number of resources
605 intel_th_alloc(struct device *dev, struct resource *devres,
606 unsigned int ndevres, int irq)
611 th = kzalloc(sizeof(*th), GFP_KERNEL);
613 return ERR_PTR(-ENOMEM);
615 th->id = ida_simple_get(&intel_th_ida, 0, 0, GFP_KERNEL);
621 th->major = __register_chrdev(0, 0, TH_POSSIBLE_OUTPUTS,
622 "intel_th/output", &intel_th_output_fops);
629 dev_set_drvdata(dev, th);
631 err = intel_th_populate(th, devres, ndevres, irq);
638 __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
642 ida_simple_remove(&intel_th_ida, th->id);
649 EXPORT_SYMBOL_GPL(intel_th_alloc);
651 void intel_th_free(struct intel_th *th)
655 for (i = 0; i < TH_SUBDEVICE_MAX; i++)
656 if (th->thdev[i] != th->hub)
657 intel_th_device_remove(th->thdev[i]);
659 intel_th_device_remove(th->hub);
661 __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
664 ida_simple_remove(&intel_th_ida, th->id);
668 EXPORT_SYMBOL_GPL(intel_th_free);
671 * intel_th_trace_enable() - enable tracing for an output device
672 * @thdev: output device that requests tracing be enabled
674 int intel_th_trace_enable(struct intel_th_device *thdev)
676 struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
677 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
679 if (WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH))
682 if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
685 hubdrv->enable(hub, &thdev->output);
689 EXPORT_SYMBOL_GPL(intel_th_trace_enable);
692 * intel_th_trace_disable() - disable tracing for an output device
693 * @thdev: output device that requests tracing be disabled
695 int intel_th_trace_disable(struct intel_th_device *thdev)
697 struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
698 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
700 WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH);
701 if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
704 hubdrv->disable(hub, &thdev->output);
708 EXPORT_SYMBOL_GPL(intel_th_trace_disable);
710 int intel_th_set_output(struct intel_th_device *thdev,
713 struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
714 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
716 if (!hubdrv->set_output)
719 return hubdrv->set_output(hub, master);
721 EXPORT_SYMBOL_GPL(intel_th_set_output);
723 static int __init intel_th_init(void)
725 intel_th_debug_init();
727 return bus_register(&intel_th_bus);
729 subsys_initcall(intel_th_init);
731 static void __exit intel_th_exit(void)
733 intel_th_debug_done();
735 bus_unregister(&intel_th_bus);
737 module_exit(intel_th_exit);
739 MODULE_LICENSE("GPL v2");
740 MODULE_DESCRIPTION("Intel(R) Trace Hub controller driver");