]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - rpmsg/rpmsg.git/blob - drivers/hwtracing/intel_th/core.c
Merge tag 'alloc-args-v4.19-rc8' of https://git.kernel.org/pub/scm/linux/kernel/git...
[rpmsg/rpmsg.git] / drivers / hwtracing / intel_th / core.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Intel(R) Trace Hub driver core
4  *
5  * Copyright (C) 2014-2015 Intel Corporation.
6  */
8 #define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
10 #include <linux/types.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/sysfs.h>
14 #include <linux/kdev_t.h>
15 #include <linux/debugfs.h>
16 #include <linux/idr.h>
17 #include <linux/pci.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/dma-mapping.h>
21 #include "intel_th.h"
22 #include "debug.h"
24 static bool host_mode __read_mostly;
25 module_param(host_mode, bool, 0444);
27 static DEFINE_IDA(intel_th_ida);
29 static int intel_th_match(struct device *dev, struct device_driver *driver)
30 {
31         struct intel_th_driver *thdrv = to_intel_th_driver(driver);
32         struct intel_th_device *thdev = to_intel_th_device(dev);
34         if (thdev->type == INTEL_TH_SWITCH &&
35             (!thdrv->enable || !thdrv->disable))
36                 return 0;
38         return !strcmp(thdev->name, driver->name);
39 }
41 static int intel_th_child_remove(struct device *dev, void *data)
42 {
43         device_release_driver(dev);
45         return 0;
46 }
48 static int intel_th_probe(struct device *dev)
49 {
50         struct intel_th_driver *thdrv = to_intel_th_driver(dev->driver);
51         struct intel_th_device *thdev = to_intel_th_device(dev);
52         struct intel_th_driver *hubdrv;
53         struct intel_th_device *hub = NULL;
54         int ret;
56         if (thdev->type == INTEL_TH_SWITCH)
57                 hub = thdev;
58         else if (dev->parent)
59                 hub = to_intel_th_device(dev->parent);
61         if (!hub || !hub->dev.driver)
62                 return -EPROBE_DEFER;
64         hubdrv = to_intel_th_driver(hub->dev.driver);
66         pm_runtime_set_active(dev);
67         pm_runtime_no_callbacks(dev);
68         pm_runtime_enable(dev);
70         ret = thdrv->probe(to_intel_th_device(dev));
71         if (ret)
72                 goto out_pm;
74         if (thdrv->attr_group) {
75                 ret = sysfs_create_group(&thdev->dev.kobj, thdrv->attr_group);
76                 if (ret)
77                         goto out;
78         }
80         if (thdev->type == INTEL_TH_OUTPUT &&
81             !intel_th_output_assigned(thdev))
82                 /* does not talk to hardware */
83                 ret = hubdrv->assign(hub, thdev);
85 out:
86         if (ret)
87                 thdrv->remove(thdev);
89 out_pm:
90         if (ret)
91                 pm_runtime_disable(dev);
93         return ret;
94 }
96 static void intel_th_device_remove(struct intel_th_device *thdev);
98 static int intel_th_remove(struct device *dev)
99 {
100         struct intel_th_driver *thdrv = to_intel_th_driver(dev->driver);
101         struct intel_th_device *thdev = to_intel_th_device(dev);
102         struct intel_th_device *hub = to_intel_th_hub(thdev);
103         int err;
105         if (thdev->type == INTEL_TH_SWITCH) {
106                 struct intel_th *th = to_intel_th(hub);
107                 int i, lowest;
109                 /* disconnect outputs */
110                 err = device_for_each_child(dev, thdev, intel_th_child_remove);
111                 if (err)
112                         return err;
114                 /*
115                  * Remove outputs, that is, hub's children: they are created
116                  * at hub's probe time by having the hub call
117                  * intel_th_output_enable() for each of them.
118                  */
119                 for (i = 0, lowest = -1; i < th->num_thdevs; i++) {
120                         /*
121                          * Move the non-output devices from higher up the
122                          * th->thdev[] array to lower positions to maintain
123                          * a contiguous array.
124                          */
125                         if (th->thdev[i]->type != INTEL_TH_OUTPUT) {
126                                 if (lowest >= 0) {
127                                         th->thdev[lowest] = th->thdev[i];
128                                         th->thdev[i] = NULL;
129                                         ++lowest;
130                                 }
132                                 continue;
133                         }
135                         if (lowest == -1)
136                                 lowest = i;
138                         intel_th_device_remove(th->thdev[i]);
139                         th->thdev[i] = NULL;
140                 }
142                 if (lowest >= 0)
143                         th->num_thdevs = lowest;
144         }
146         if (thdrv->attr_group)
147                 sysfs_remove_group(&thdev->dev.kobj, thdrv->attr_group);
149         pm_runtime_get_sync(dev);
151         thdrv->remove(thdev);
153         if (intel_th_output_assigned(thdev)) {
154                 struct intel_th_driver *hubdrv =
155                         to_intel_th_driver(dev->parent->driver);
157                 if (hub->dev.driver)
158                         /* does not talk to hardware */
159                         hubdrv->unassign(hub, thdev);
160         }
162         pm_runtime_disable(dev);
163         pm_runtime_set_active(dev);
164         pm_runtime_enable(dev);
166         return 0;
169 static struct bus_type intel_th_bus = {
170         .name           = "intel_th",
171         .match          = intel_th_match,
172         .probe          = intel_th_probe,
173         .remove         = intel_th_remove,
174 };
176 static void intel_th_device_free(struct intel_th_device *thdev);
178 static void intel_th_device_release(struct device *dev)
180         intel_th_device_free(to_intel_th_device(dev));
183 static struct device_type intel_th_source_device_type = {
184         .name           = "intel_th_source_device",
185         .release        = intel_th_device_release,
186 };
188 static char *intel_th_output_devnode(struct device *dev, umode_t *mode,
189                                      kuid_t *uid, kgid_t *gid)
191         struct intel_th_device *thdev = to_intel_th_device(dev);
192         struct intel_th *th = to_intel_th(thdev);
193         char *node;
195         if (thdev->id >= 0)
196                 node = kasprintf(GFP_KERNEL, "intel_th%d/%s%d", th->id,
197                                  thdev->name, thdev->id);
198         else
199                 node = kasprintf(GFP_KERNEL, "intel_th%d/%s", th->id,
200                                  thdev->name);
202         return node;
205 static ssize_t port_show(struct device *dev, struct device_attribute *attr,
206                          char *buf)
208         struct intel_th_device *thdev = to_intel_th_device(dev);
210         if (thdev->output.port >= 0)
211                 return scnprintf(buf, PAGE_SIZE, "%u\n", thdev->output.port);
213         return scnprintf(buf, PAGE_SIZE, "unassigned\n");
216 static DEVICE_ATTR_RO(port);
218 static int intel_th_output_activate(struct intel_th_device *thdev)
220         struct intel_th_driver *thdrv =
221                 to_intel_th_driver_or_null(thdev->dev.driver);
222         struct intel_th *th = to_intel_th(thdev);
223         int ret = 0;
225         if (!thdrv)
226                 return -ENODEV;
228         if (!try_module_get(thdrv->driver.owner))
229                 return -ENODEV;
231         pm_runtime_get_sync(&thdev->dev);
233         if (th->activate)
234                 ret = th->activate(th);
235         if (ret)
236                 goto fail_put;
238         if (thdrv->activate)
239                 ret = thdrv->activate(thdev);
240         else
241                 intel_th_trace_enable(thdev);
243         if (ret)
244                 goto fail_deactivate;
246         return 0;
248 fail_deactivate:
249         if (th->deactivate)
250                 th->deactivate(th);
252 fail_put:
253         pm_runtime_put(&thdev->dev);
254         module_put(thdrv->driver.owner);
256         return ret;
259 static void intel_th_output_deactivate(struct intel_th_device *thdev)
261         struct intel_th_driver *thdrv =
262                 to_intel_th_driver_or_null(thdev->dev.driver);
263         struct intel_th *th = to_intel_th(thdev);
265         if (!thdrv)
266                 return;
268         if (thdrv->deactivate)
269                 thdrv->deactivate(thdev);
270         else
271                 intel_th_trace_disable(thdev);
273         if (th->deactivate)
274                 th->deactivate(th);
276         pm_runtime_put(&thdev->dev);
277         module_put(thdrv->driver.owner);
280 static ssize_t active_show(struct device *dev, struct device_attribute *attr,
281                            char *buf)
283         struct intel_th_device *thdev = to_intel_th_device(dev);
285         return scnprintf(buf, PAGE_SIZE, "%d\n", thdev->output.active);
288 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
289                             const char *buf, size_t size)
291         struct intel_th_device *thdev = to_intel_th_device(dev);
292         unsigned long val;
293         int ret;
295         ret = kstrtoul(buf, 10, &val);
296         if (ret)
297                 return ret;
299         if (!!val != thdev->output.active) {
300                 if (val)
301                         ret = intel_th_output_activate(thdev);
302                 else
303                         intel_th_output_deactivate(thdev);
304         }
306         return ret ? ret : size;
309 static DEVICE_ATTR_RW(active);
311 static struct attribute *intel_th_output_attrs[] = {
312         &dev_attr_port.attr,
313         &dev_attr_active.attr,
314         NULL,
315 };
317 ATTRIBUTE_GROUPS(intel_th_output);
319 static struct device_type intel_th_output_device_type = {
320         .name           = "intel_th_output_device",
321         .groups         = intel_th_output_groups,
322         .release        = intel_th_device_release,
323         .devnode        = intel_th_output_devnode,
324 };
326 static struct device_type intel_th_switch_device_type = {
327         .name           = "intel_th_switch_device",
328         .release        = intel_th_device_release,
329 };
331 static struct device_type *intel_th_device_type[] = {
332         [INTEL_TH_SOURCE]       = &intel_th_source_device_type,
333         [INTEL_TH_OUTPUT]       = &intel_th_output_device_type,
334         [INTEL_TH_SWITCH]       = &intel_th_switch_device_type,
335 };
337 int intel_th_driver_register(struct intel_th_driver *thdrv)
339         if (!thdrv->probe || !thdrv->remove)
340                 return -EINVAL;
342         thdrv->driver.bus = &intel_th_bus;
344         return driver_register(&thdrv->driver);
346 EXPORT_SYMBOL_GPL(intel_th_driver_register);
348 void intel_th_driver_unregister(struct intel_th_driver *thdrv)
350         driver_unregister(&thdrv->driver);
352 EXPORT_SYMBOL_GPL(intel_th_driver_unregister);
354 static struct intel_th_device *
355 intel_th_device_alloc(struct intel_th *th, unsigned int type, const char *name,
356                       int id)
358         struct device *parent;
359         struct intel_th_device *thdev;
361         if (type == INTEL_TH_OUTPUT)
362                 parent = &th->hub->dev;
363         else
364                 parent = th->dev;
366         thdev = kzalloc(sizeof(*thdev) + strlen(name) + 1, GFP_KERNEL);
367         if (!thdev)
368                 return NULL;
370         thdev->id = id;
371         thdev->type = type;
373         strcpy(thdev->name, name);
374         device_initialize(&thdev->dev);
375         thdev->dev.bus = &intel_th_bus;
376         thdev->dev.type = intel_th_device_type[type];
377         thdev->dev.parent = parent;
378         thdev->dev.dma_mask = parent->dma_mask;
379         thdev->dev.dma_parms = parent->dma_parms;
380         dma_set_coherent_mask(&thdev->dev, parent->coherent_dma_mask);
381         if (id >= 0)
382                 dev_set_name(&thdev->dev, "%d-%s%d", th->id, name, id);
383         else
384                 dev_set_name(&thdev->dev, "%d-%s", th->id, name);
386         return thdev;
389 static int intel_th_device_add_resources(struct intel_th_device *thdev,
390                                          struct resource *res, int nres)
392         struct resource *r;
394         r = kmemdup(res, sizeof(*res) * nres, GFP_KERNEL);
395         if (!r)
396                 return -ENOMEM;
398         thdev->resource = r;
399         thdev->num_resources = nres;
401         return 0;
404 static void intel_th_device_remove(struct intel_th_device *thdev)
406         device_del(&thdev->dev);
407         put_device(&thdev->dev);
410 static void intel_th_device_free(struct intel_th_device *thdev)
412         kfree(thdev->resource);
413         kfree(thdev);
416 /*
417  * Intel(R) Trace Hub subdevices
418  */
419 static const struct intel_th_subdevice {
420         const char              *name;
421         struct resource         res[3];
422         unsigned                nres;
423         unsigned                type;
424         unsigned                otype;
425         unsigned                scrpd;
426         int                     id;
427 } intel_th_subdevices[] = {
428         {
429                 .nres   = 1,
430                 .res    = {
431                         {
432                                 /* Handle TSCU from GTH driver */
433                                 .start  = REG_GTH_OFFSET,
434                                 .end    = REG_TSCU_OFFSET + REG_TSCU_LENGTH - 1,
435                                 .flags  = IORESOURCE_MEM,
436                         },
437                 },
438                 .name   = "gth",
439                 .type   = INTEL_TH_SWITCH,
440                 .id     = -1,
441         },
442         {
443                 .nres   = 2,
444                 .res    = {
445                         {
446                                 .start  = REG_MSU_OFFSET,
447                                 .end    = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
448                                 .flags  = IORESOURCE_MEM,
449                         },
450                         {
451                                 .start  = BUF_MSU_OFFSET,
452                                 .end    = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
453                                 .flags  = IORESOURCE_MEM,
454                         },
455                 },
456                 .name   = "msc",
457                 .id     = 0,
458                 .type   = INTEL_TH_OUTPUT,
459                 .otype  = GTH_MSU,
460                 .scrpd  = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC0_IS_ENABLED,
461         },
462         {
463                 .nres   = 2,
464                 .res    = {
465                         {
466                                 .start  = REG_MSU_OFFSET,
467                                 .end    = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
468                                 .flags  = IORESOURCE_MEM,
469                         },
470                         {
471                                 .start  = BUF_MSU_OFFSET,
472                                 .end    = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
473                                 .flags  = IORESOURCE_MEM,
474                         },
475                 },
476                 .name   = "msc",
477                 .id     = 1,
478                 .type   = INTEL_TH_OUTPUT,
479                 .otype  = GTH_MSU,
480                 .scrpd  = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC1_IS_ENABLED,
481         },
482         {
483                 .nres   = 2,
484                 .res    = {
485                         {
486                                 .start  = REG_STH_OFFSET,
487                                 .end    = REG_STH_OFFSET + REG_STH_LENGTH - 1,
488                                 .flags  = IORESOURCE_MEM,
489                         },
490                         {
491                                 .start  = 1, /* use resource[1] */
492                                 .end    = 0,
493                                 .flags  = IORESOURCE_MEM,
494                         },
495                 },
496                 .id     = -1,
497                 .name   = "sth",
498                 .type   = INTEL_TH_SOURCE,
499         },
500         {
501                 .nres   = 1,
502                 .res    = {
503                         {
504                                 .start  = REG_PTI_OFFSET,
505                                 .end    = REG_PTI_OFFSET + REG_PTI_LENGTH - 1,
506                                 .flags  = IORESOURCE_MEM,
507                         },
508                 },
509                 .id     = -1,
510                 .name   = "pti",
511                 .type   = INTEL_TH_OUTPUT,
512                 .otype  = GTH_PTI,
513                 .scrpd  = SCRPD_PTI_IS_PRIM_DEST,
514         },
515         {
516                 .nres   = 1,
517                 .res    = {
518                         {
519                                 .start  = REG_PTI_OFFSET,
520                                 .end    = REG_PTI_OFFSET + REG_PTI_LENGTH - 1,
521                                 .flags  = IORESOURCE_MEM,
522                         },
523                 },
524                 .id     = -1,
525                 .name   = "lpp",
526                 .type   = INTEL_TH_OUTPUT,
527                 .otype  = GTH_LPP,
528                 .scrpd  = SCRPD_PTI_IS_PRIM_DEST,
529         },
530         {
531                 .nres   = 1,
532                 .res    = {
533                         {
534                                 .start  = REG_DCIH_OFFSET,
535                                 .end    = REG_DCIH_OFFSET + REG_DCIH_LENGTH - 1,
536                                 .flags  = IORESOURCE_MEM,
537                         },
538                 },
539                 .id     = -1,
540                 .name   = "dcih",
541                 .type   = INTEL_TH_OUTPUT,
542         },
543 };
545 #ifdef CONFIG_MODULES
546 static void __intel_th_request_hub_module(struct work_struct *work)
548         struct intel_th *th = container_of(work, struct intel_th,
549                                            request_module_work);
551         request_module("intel_th_%s", th->hub->name);
554 static int intel_th_request_hub_module(struct intel_th *th)
556         INIT_WORK(&th->request_module_work, __intel_th_request_hub_module);
557         schedule_work(&th->request_module_work);
559         return 0;
562 static void intel_th_request_hub_module_flush(struct intel_th *th)
564         flush_work(&th->request_module_work);
566 #else
567 static inline int intel_th_request_hub_module(struct intel_th *th)
569         return -EINVAL;
572 static inline void intel_th_request_hub_module_flush(struct intel_th *th)
575 #endif /* CONFIG_MODULES */
577 static struct intel_th_device *
578 intel_th_subdevice_alloc(struct intel_th *th,
579                          const struct intel_th_subdevice *subdev)
581         struct intel_th_device *thdev;
582         struct resource res[3];
583         unsigned int req = 0;
584         bool is64bit = false;
585         int r, err;
587         thdev = intel_th_device_alloc(th, subdev->type, subdev->name,
588                                       subdev->id);
589         if (!thdev)
590                 return ERR_PTR(-ENOMEM);
592         thdev->drvdata = th->drvdata;
594         for (r = 0; r < th->num_resources; r++)
595                 if (th->resource[r].flags & IORESOURCE_MEM_64) {
596                         is64bit = true;
597                         break;
598                 }
600         memcpy(res, subdev->res,
601                sizeof(struct resource) * subdev->nres);
603         for (r = 0; r < subdev->nres; r++) {
604                 struct resource *devres = th->resource;
605                 int bar = 0; /* cut subdevices' MMIO from resource[0] */
607                 /*
608                  * Take .end == 0 to mean 'take the whole bar',
609                  * .start then tells us which bar it is. Default to
610                  * TH_MMIO_CONFIG.
611                  */
612                 if (!res[r].end && res[r].flags == IORESOURCE_MEM) {
613                         bar = res[r].start;
614                         if (is64bit)
615                                 bar *= 2;
616                         res[r].start = 0;
617                         res[r].end = resource_size(&devres[bar]) - 1;
618                 }
620                 if (res[r].flags & IORESOURCE_MEM) {
621                         res[r].start    += devres[bar].start;
622                         res[r].end      += devres[bar].start;
624                         dev_dbg(th->dev, "%s:%d @ %pR\n",
625                                 subdev->name, r, &res[r]);
626                 } else if (res[r].flags & IORESOURCE_IRQ) {
627                         res[r].start    = th->irq;
628                 }
629         }
631         err = intel_th_device_add_resources(thdev, res, subdev->nres);
632         if (err) {
633                 put_device(&thdev->dev);
634                 goto fail_put_device;
635         }
637         if (subdev->type == INTEL_TH_OUTPUT) {
638                 thdev->dev.devt = MKDEV(th->major, th->num_thdevs);
639                 thdev->output.type = subdev->otype;
640                 thdev->output.port = -1;
641                 thdev->output.scratchpad = subdev->scrpd;
642         } else if (subdev->type == INTEL_TH_SWITCH) {
643                 thdev->host_mode =
644                         INTEL_TH_CAP(th, host_mode_only) ? true : host_mode;
645                 th->hub = thdev;
646         }
648         err = device_add(&thdev->dev);
649         if (err) {
650                 put_device(&thdev->dev);
651                 goto fail_free_res;
652         }
654         /* need switch driver to be loaded to enumerate the rest */
655         if (subdev->type == INTEL_TH_SWITCH && !req) {
656                 err = intel_th_request_hub_module(th);
657                 if (!err)
658                         req++;
659         }
661         return thdev;
663 fail_free_res:
664         kfree(thdev->resource);
666 fail_put_device:
667         put_device(&thdev->dev);
669         return ERR_PTR(err);
672 /**
673  * intel_th_output_enable() - find and enable a device for a given output type
674  * @th:         Intel TH instance
675  * @otype:      output type
676  *
677  * Go through the unallocated output devices, find the first one whos type
678  * matches @otype and instantiate it. These devices are removed when the hub
679  * device is removed, see intel_th_remove().
680  */
681 int intel_th_output_enable(struct intel_th *th, unsigned int otype)
683         struct intel_th_device *thdev;
684         int src = 0, dst = 0;
686         for (src = 0, dst = 0; dst <= th->num_thdevs; src++, dst++) {
687                 for (; src < ARRAY_SIZE(intel_th_subdevices); src++) {
688                         if (intel_th_subdevices[src].type != INTEL_TH_OUTPUT)
689                                 continue;
691                         if (intel_th_subdevices[src].otype != otype)
692                                 continue;
694                         break;
695                 }
697                 /* no unallocated matching subdevices */
698                 if (src == ARRAY_SIZE(intel_th_subdevices))
699                         return -ENODEV;
701                 for (; dst < th->num_thdevs; dst++) {
702                         if (th->thdev[dst]->type != INTEL_TH_OUTPUT)
703                                 continue;
705                         if (th->thdev[dst]->output.type != otype)
706                                 continue;
708                         break;
709                 }
711                 /*
712                  * intel_th_subdevices[src] matches our requirements and is
713                  * not matched in th::thdev[]
714                  */
715                 if (dst == th->num_thdevs)
716                         goto found;
717         }
719         return -ENODEV;
721 found:
722         thdev = intel_th_subdevice_alloc(th, &intel_th_subdevices[src]);
723         if (IS_ERR(thdev))
724                 return PTR_ERR(thdev);
726         th->thdev[th->num_thdevs++] = thdev;
728         return 0;
730 EXPORT_SYMBOL_GPL(intel_th_output_enable);
732 static int intel_th_populate(struct intel_th *th)
734         int src;
736         /* create devices for each intel_th_subdevice */
737         for (src = 0; src < ARRAY_SIZE(intel_th_subdevices); src++) {
738                 const struct intel_th_subdevice *subdev =
739                         &intel_th_subdevices[src];
740                 struct intel_th_device *thdev;
742                 /* only allow SOURCE and SWITCH devices in host mode */
743                 if ((INTEL_TH_CAP(th, host_mode_only) || host_mode) &&
744                     subdev->type == INTEL_TH_OUTPUT)
745                         continue;
747                 /*
748                  * don't enable port OUTPUTs in this path; SWITCH enables them
749                  * via intel_th_output_enable()
750                  */
751                 if (subdev->type == INTEL_TH_OUTPUT &&
752                     subdev->otype != GTH_NONE)
753                         continue;
755                 thdev = intel_th_subdevice_alloc(th, subdev);
756                 /* note: caller should free subdevices from th::thdev[] */
757                 if (IS_ERR(thdev))
758                         return PTR_ERR(thdev);
760                 th->thdev[th->num_thdevs++] = thdev;
761         }
763         return 0;
766 static int match_devt(struct device *dev, void *data)
768         dev_t devt = (dev_t)(unsigned long)data;
770         return dev->devt == devt;
773 static int intel_th_output_open(struct inode *inode, struct file *file)
775         const struct file_operations *fops;
776         struct intel_th_driver *thdrv;
777         struct device *dev;
778         int err;
780         dev = bus_find_device(&intel_th_bus, NULL,
781                               (void *)(unsigned long)inode->i_rdev,
782                               match_devt);
783         if (!dev || !dev->driver)
784                 return -ENODEV;
786         thdrv = to_intel_th_driver(dev->driver);
787         fops = fops_get(thdrv->fops);
788         if (!fops)
789                 return -ENODEV;
791         replace_fops(file, fops);
793         file->private_data = to_intel_th_device(dev);
795         if (file->f_op->open) {
796                 err = file->f_op->open(inode, file);
797                 return err;
798         }
800         return 0;
803 static const struct file_operations intel_th_output_fops = {
804         .open   = intel_th_output_open,
805         .llseek = noop_llseek,
806 };
808 /**
809  * intel_th_alloc() - allocate a new Intel TH device and its subdevices
810  * @dev:        parent device
811  * @devres:     parent's resources
812  * @ndevres:    number of resources
813  * @irq:        irq number
814  */
815 struct intel_th *
816 intel_th_alloc(struct device *dev, struct intel_th_drvdata *drvdata,
817                struct resource *devres, unsigned int ndevres, int irq)
819         struct intel_th *th;
820         int err, r;
822         if (irq == -1)
823                 for (r = 0; r < ndevres; r++)
824                         if (devres[r].flags & IORESOURCE_IRQ) {
825                                 irq = devres[r].start;
826                                 break;
827                         }
829         th = kzalloc(sizeof(*th), GFP_KERNEL);
830         if (!th)
831                 return ERR_PTR(-ENOMEM);
833         th->id = ida_simple_get(&intel_th_ida, 0, 0, GFP_KERNEL);
834         if (th->id < 0) {
835                 err = th->id;
836                 goto err_alloc;
837         }
839         th->major = __register_chrdev(0, 0, TH_POSSIBLE_OUTPUTS,
840                                       "intel_th/output", &intel_th_output_fops);
841         if (th->major < 0) {
842                 err = th->major;
843                 goto err_ida;
844         }
845         th->dev = dev;
846         th->drvdata = drvdata;
848         th->resource = devres;
849         th->num_resources = ndevres;
850         th->irq = irq;
852         dev_set_drvdata(dev, th);
854         pm_runtime_no_callbacks(dev);
855         pm_runtime_put(dev);
856         pm_runtime_allow(dev);
858         err = intel_th_populate(th);
859         if (err) {
860                 /* free the subdevices and undo everything */
861                 intel_th_free(th);
862                 return ERR_PTR(err);
863         }
865         return th;
867 err_ida:
868         ida_simple_remove(&intel_th_ida, th->id);
870 err_alloc:
871         kfree(th);
873         return ERR_PTR(err);
875 EXPORT_SYMBOL_GPL(intel_th_alloc);
877 void intel_th_free(struct intel_th *th)
879         int i;
881         intel_th_request_hub_module_flush(th);
883         intel_th_device_remove(th->hub);
884         for (i = 0; i < th->num_thdevs; i++) {
885                 if (th->thdev[i] != th->hub)
886                         intel_th_device_remove(th->thdev[i]);
887                 th->thdev[i] = NULL;
888         }
890         th->num_thdevs = 0;
892         pm_runtime_get_sync(th->dev);
893         pm_runtime_forbid(th->dev);
895         __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
896                             "intel_th/output");
898         ida_simple_remove(&intel_th_ida, th->id);
900         kfree(th);
902 EXPORT_SYMBOL_GPL(intel_th_free);
904 /**
905  * intel_th_trace_enable() - enable tracing for an output device
906  * @thdev:      output device that requests tracing be enabled
907  */
908 int intel_th_trace_enable(struct intel_th_device *thdev)
910         struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
911         struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
913         if (WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH))
914                 return -EINVAL;
916         if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
917                 return -EINVAL;
919         pm_runtime_get_sync(&thdev->dev);
920         hubdrv->enable(hub, &thdev->output);
922         return 0;
924 EXPORT_SYMBOL_GPL(intel_th_trace_enable);
926 /**
927  * intel_th_trace_disable() - disable tracing for an output device
928  * @thdev:      output device that requests tracing be disabled
929  */
930 int intel_th_trace_disable(struct intel_th_device *thdev)
932         struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
933         struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
935         WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH);
936         if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
937                 return -EINVAL;
939         hubdrv->disable(hub, &thdev->output);
940         pm_runtime_put(&thdev->dev);
942         return 0;
944 EXPORT_SYMBOL_GPL(intel_th_trace_disable);
946 int intel_th_set_output(struct intel_th_device *thdev,
947                         unsigned int master)
949         struct intel_th_device *hub = to_intel_th_hub(thdev);
950         struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
952         /* In host mode, this is up to the external debugger, do nothing. */
953         if (hub->host_mode)
954                 return 0;
956         if (!hubdrv->set_output)
957                 return -ENOTSUPP;
959         return hubdrv->set_output(hub, master);
961 EXPORT_SYMBOL_GPL(intel_th_set_output);
963 static int __init intel_th_init(void)
965         intel_th_debug_init();
967         return bus_register(&intel_th_bus);
969 subsys_initcall(intel_th_init);
971 static void __exit intel_th_exit(void)
973         intel_th_debug_done();
975         bus_unregister(&intel_th_bus);
977 module_exit(intel_th_exit);
979 MODULE_LICENSE("GPL v2");
980 MODULE_DESCRIPTION("Intel(R) Trace Hub controller driver");
981 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");