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 = to_intel_th_driver(thdev->dev.driver);
189 return thdrv->activate(thdev);
191 intel_th_trace_enable(thdev);
196 static void intel_th_output_deactivate(struct intel_th_device *thdev)
198 struct intel_th_driver *thdrv = to_intel_th_driver(thdev->dev.driver);
200 if (thdrv->deactivate)
201 thdrv->deactivate(thdev);
203 intel_th_trace_disable(thdev);
206 static ssize_t active_show(struct device *dev, struct device_attribute *attr,
209 struct intel_th_device *thdev = to_intel_th_device(dev);
211 return scnprintf(buf, PAGE_SIZE, "%d\n", thdev->output.active);
214 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
215 const char *buf, size_t size)
217 struct intel_th_device *thdev = to_intel_th_device(dev);
221 ret = kstrtoul(buf, 10, &val);
225 if (!!val != thdev->output.active) {
227 ret = intel_th_output_activate(thdev);
229 intel_th_output_deactivate(thdev);
232 return ret ? ret : size;
235 static DEVICE_ATTR_RW(active);
237 static struct attribute *intel_th_output_attrs[] = {
239 &dev_attr_active.attr,
243 ATTRIBUTE_GROUPS(intel_th_output);
245 static struct device_type intel_th_output_device_type = {
246 .name = "intel_th_output_device",
247 .groups = intel_th_output_groups,
248 .release = intel_th_device_release,
249 .devnode = intel_th_output_devnode,
252 static struct device_type intel_th_switch_device_type = {
253 .name = "intel_th_switch_device",
254 .release = intel_th_device_release,
257 static struct device_type *intel_th_device_type[] = {
258 [INTEL_TH_SOURCE] = &intel_th_source_device_type,
259 [INTEL_TH_OUTPUT] = &intel_th_output_device_type,
260 [INTEL_TH_SWITCH] = &intel_th_switch_device_type,
263 int intel_th_driver_register(struct intel_th_driver *thdrv)
265 if (!thdrv->probe || !thdrv->remove)
268 thdrv->driver.bus = &intel_th_bus;
270 return driver_register(&thdrv->driver);
272 EXPORT_SYMBOL_GPL(intel_th_driver_register);
274 void intel_th_driver_unregister(struct intel_th_driver *thdrv)
276 driver_unregister(&thdrv->driver);
278 EXPORT_SYMBOL_GPL(intel_th_driver_unregister);
280 static struct intel_th_device *
281 intel_th_device_alloc(struct intel_th *th, unsigned int type, const char *name,
284 struct device *parent;
285 struct intel_th_device *thdev;
287 if (type == INTEL_TH_SWITCH)
290 parent = &th->hub->dev;
292 thdev = kzalloc(sizeof(*thdev) + strlen(name) + 1, GFP_KERNEL);
299 strcpy(thdev->name, name);
300 device_initialize(&thdev->dev);
301 thdev->dev.bus = &intel_th_bus;
302 thdev->dev.type = intel_th_device_type[type];
303 thdev->dev.parent = parent;
304 thdev->dev.dma_mask = parent->dma_mask;
305 thdev->dev.dma_parms = parent->dma_parms;
306 dma_set_coherent_mask(&thdev->dev, parent->coherent_dma_mask);
308 dev_set_name(&thdev->dev, "%d-%s%d", th->id, name, id);
310 dev_set_name(&thdev->dev, "%d-%s", th->id, name);
315 static int intel_th_device_add_resources(struct intel_th_device *thdev,
316 struct resource *res, int nres)
320 r = kmemdup(res, sizeof(*res) * nres, GFP_KERNEL);
325 thdev->num_resources = nres;
330 static void intel_th_device_remove(struct intel_th_device *thdev)
332 device_del(&thdev->dev);
333 put_device(&thdev->dev);
336 static void intel_th_device_free(struct intel_th_device *thdev)
338 kfree(thdev->resource);
343 * Intel(R) Trace Hub subdevices
345 static struct intel_th_subdevice {
347 struct resource res[3];
353 } intel_th_subdevices[TH_SUBDEVICE_MAX] = {
358 .start = REG_GTH_OFFSET,
359 .end = REG_GTH_OFFSET + REG_GTH_LENGTH - 1,
360 .flags = IORESOURCE_MEM,
364 .type = INTEL_TH_SWITCH,
371 .start = REG_MSU_OFFSET,
372 .end = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
373 .flags = IORESOURCE_MEM,
376 .start = BUF_MSU_OFFSET,
377 .end = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
378 .flags = IORESOURCE_MEM,
383 .type = INTEL_TH_OUTPUT,
385 .scrpd = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC0_IS_ENABLED,
391 .start = REG_MSU_OFFSET,
392 .end = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
393 .flags = IORESOURCE_MEM,
396 .start = BUF_MSU_OFFSET,
397 .end = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
398 .flags = IORESOURCE_MEM,
403 .type = INTEL_TH_OUTPUT,
405 .scrpd = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC1_IS_ENABLED,
411 .start = REG_STH_OFFSET,
412 .end = REG_STH_OFFSET + REG_STH_LENGTH - 1,
413 .flags = IORESOURCE_MEM,
418 .flags = IORESOURCE_MEM,
423 .type = INTEL_TH_SOURCE,
429 .start = REG_PTI_OFFSET,
430 .end = REG_PTI_OFFSET + REG_PTI_LENGTH - 1,
431 .flags = IORESOURCE_MEM,
436 .type = INTEL_TH_OUTPUT,
438 .scrpd = SCRPD_PTI_IS_PRIM_DEST,
444 .start = REG_DCIH_OFFSET,
445 .end = REG_DCIH_OFFSET + REG_DCIH_LENGTH - 1,
446 .flags = IORESOURCE_MEM,
451 .type = INTEL_TH_OUTPUT,
455 static int intel_th_populate(struct intel_th *th, struct resource *devres,
456 unsigned int ndevres, int irq)
458 struct resource res[3];
459 unsigned int req = 0;
462 /* create devices for each intel_th_subdevice */
463 for (i = 0; i < ARRAY_SIZE(intel_th_subdevices); i++) {
464 struct intel_th_subdevice *subdev = &intel_th_subdevices[i];
465 struct intel_th_device *thdev;
468 thdev = intel_th_device_alloc(th, subdev->type, subdev->name,
475 memcpy(res, subdev->res,
476 sizeof(struct resource) * subdev->nres);
478 for (r = 0; r < subdev->nres; r++) {
479 int bar = TH_MMIO_CONFIG;
482 * Take .end == 0 to mean 'take the whole bar',
483 * .start then tells us which bar it is. Default to
486 if (!res[r].end && res[r].flags == IORESOURCE_MEM) {
489 res[r].end = resource_size(&devres[bar]) - 1;
492 if (res[r].flags & IORESOURCE_MEM) {
493 res[r].start += devres[bar].start;
494 res[r].end += devres[bar].start;
496 dev_dbg(th->dev, "%s:%d @ %pR\n",
497 subdev->name, r, &res[r]);
498 } else if (res[r].flags & IORESOURCE_IRQ) {
503 err = intel_th_device_add_resources(thdev, res, subdev->nres);
505 put_device(&thdev->dev);
509 if (subdev->type == INTEL_TH_OUTPUT) {
510 thdev->dev.devt = MKDEV(th->major, i);
511 thdev->output.type = subdev->otype;
512 thdev->output.port = -1;
513 thdev->output.scratchpad = subdev->scrpd;
516 err = device_add(&thdev->dev);
518 put_device(&thdev->dev);
522 /* need switch driver to be loaded to enumerate the rest */
523 if (subdev->type == INTEL_TH_SWITCH && !req) {
525 err = request_module("intel_th_%s", subdev->name);
530 th->thdev[i] = thdev;
536 for (i-- ; i >= 0; i--)
537 intel_th_device_remove(th->thdev[i]);
542 static int match_devt(struct device *dev, void *data)
544 dev_t devt = (dev_t)(unsigned long)data;
546 return dev->devt == devt;
549 static int intel_th_output_open(struct inode *inode, struct file *file)
551 const struct file_operations *fops;
552 struct intel_th_driver *thdrv;
556 dev = bus_find_device(&intel_th_bus, NULL,
557 (void *)(unsigned long)inode->i_rdev,
559 if (!dev || !dev->driver)
562 thdrv = to_intel_th_driver(dev->driver);
563 fops = fops_get(thdrv->fops);
567 replace_fops(file, fops);
569 file->private_data = to_intel_th_device(dev);
571 if (file->f_op->open) {
572 err = file->f_op->open(inode, file);
579 static const struct file_operations intel_th_output_fops = {
580 .open = intel_th_output_open,
581 .llseek = noop_llseek,
585 * intel_th_alloc() - allocate a new Intel TH device and its subdevices
586 * @dev: parent device
587 * @devres: parent's resources
588 * @ndevres: number of resources
592 intel_th_alloc(struct device *dev, struct resource *devres,
593 unsigned int ndevres, int irq)
598 th = kzalloc(sizeof(*th), GFP_KERNEL);
600 return ERR_PTR(-ENOMEM);
602 th->id = ida_simple_get(&intel_th_ida, 0, 0, GFP_KERNEL);
608 th->major = __register_chrdev(0, 0, TH_POSSIBLE_OUTPUTS,
609 "intel_th/output", &intel_th_output_fops);
616 dev_set_drvdata(dev, th);
618 err = intel_th_populate(th, devres, ndevres, irq);
625 __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
629 ida_simple_remove(&intel_th_ida, th->id);
636 EXPORT_SYMBOL_GPL(intel_th_alloc);
638 void intel_th_free(struct intel_th *th)
642 for (i = 0; i < TH_SUBDEVICE_MAX; i++)
643 if (th->thdev[i] != th->hub)
644 intel_th_device_remove(th->thdev[i]);
646 intel_th_device_remove(th->hub);
648 __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
651 ida_simple_remove(&intel_th_ida, th->id);
655 EXPORT_SYMBOL_GPL(intel_th_free);
658 * intel_th_trace_enable() - enable tracing for an output device
659 * @thdev: output device that requests tracing be enabled
661 int intel_th_trace_enable(struct intel_th_device *thdev)
663 struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
664 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
666 if (WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH))
669 if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
672 hubdrv->enable(hub, &thdev->output);
676 EXPORT_SYMBOL_GPL(intel_th_trace_enable);
679 * intel_th_trace_disable() - disable tracing for an output device
680 * @thdev: output device that requests tracing be disabled
682 int intel_th_trace_disable(struct intel_th_device *thdev)
684 struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
685 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
687 WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH);
688 if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
691 hubdrv->disable(hub, &thdev->output);
695 EXPORT_SYMBOL_GPL(intel_th_trace_disable);
697 int intel_th_set_output(struct intel_th_device *thdev,
700 struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
701 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
703 if (!hubdrv->set_output)
706 return hubdrv->set_output(hub, master);
708 EXPORT_SYMBOL_GPL(intel_th_set_output);
710 static int __init intel_th_init(void)
712 intel_th_debug_init();
714 return bus_register(&intel_th_bus);
716 subsys_initcall(intel_th_init);
718 static void __exit intel_th_exit(void)
720 intel_th_debug_done();
722 bus_unregister(&intel_th_bus);
724 module_exit(intel_th_exit);
726 MODULE_LICENSE("GPL v2");
727 MODULE_DESCRIPTION("Intel(R) Trace Hub controller driver");
728 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");