GNU Linux-libre 4.19.245-gnu1
[releases.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  */
7
8 #define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
9
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>
20
21 #include "intel_th.h"
22 #include "debug.h"
23
24 static bool host_mode __read_mostly;
25 module_param(host_mode, bool, 0444);
26
27 static DEFINE_IDA(intel_th_ida);
28
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);
33
34         if (thdev->type == INTEL_TH_SWITCH &&
35             (!thdrv->enable || !thdrv->disable))
36                 return 0;
37
38         return !strcmp(thdev->name, driver->name);
39 }
40
41 static int intel_th_child_remove(struct device *dev, void *data)
42 {
43         device_release_driver(dev);
44
45         return 0;
46 }
47
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;
55
56         if (thdev->type == INTEL_TH_SWITCH)
57                 hub = thdev;
58         else if (dev->parent)
59                 hub = to_intel_th_device(dev->parent);
60
61         if (!hub || !hub->dev.driver)
62                 return -EPROBE_DEFER;
63
64         hubdrv = to_intel_th_driver(hub->dev.driver);
65
66         pm_runtime_set_active(dev);
67         pm_runtime_no_callbacks(dev);
68         pm_runtime_enable(dev);
69
70         ret = thdrv->probe(to_intel_th_device(dev));
71         if (ret)
72                 goto out_pm;
73
74         if (thdrv->attr_group) {
75                 ret = sysfs_create_group(&thdev->dev.kobj, thdrv->attr_group);
76                 if (ret)
77                         goto out;
78         }
79
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);
84
85 out:
86         if (ret)
87                 thdrv->remove(thdev);
88
89 out_pm:
90         if (ret)
91                 pm_runtime_disable(dev);
92
93         return ret;
94 }
95
96 static void intel_th_device_remove(struct intel_th_device *thdev);
97
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;
104
105         if (thdev->type == INTEL_TH_SWITCH) {
106                 struct intel_th *th = to_intel_th(hub);
107                 int i, lowest;
108
109                 /* disconnect outputs */
110                 err = device_for_each_child(dev, thdev, intel_th_child_remove);
111                 if (err)
112                         return err;
113
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                                 }
131
132                                 continue;
133                         }
134
135                         if (lowest == -1)
136                                 lowest = i;
137
138                         intel_th_device_remove(th->thdev[i]);
139                         th->thdev[i] = NULL;
140                 }
141
142                 if (lowest >= 0)
143                         th->num_thdevs = lowest;
144         }
145
146         if (thdrv->attr_group)
147                 sysfs_remove_group(&thdev->dev.kobj, thdrv->attr_group);
148
149         pm_runtime_get_sync(dev);
150
151         thdrv->remove(thdev);
152
153         if (intel_th_output_assigned(thdev)) {
154                 struct intel_th_driver *hubdrv =
155                         to_intel_th_driver(dev->parent->driver);
156
157                 if (hub->dev.driver)
158                         /* does not talk to hardware */
159                         hubdrv->unassign(hub, thdev);
160         }
161
162         pm_runtime_disable(dev);
163         pm_runtime_set_active(dev);
164         pm_runtime_enable(dev);
165
166         return 0;
167 }
168
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 };
175
176 static void intel_th_device_free(struct intel_th_device *thdev);
177
178 static void intel_th_device_release(struct device *dev)
179 {
180         intel_th_device_free(to_intel_th_device(dev));
181 }
182
183 static struct device_type intel_th_source_device_type = {
184         .name           = "intel_th_source_device",
185         .release        = intel_th_device_release,
186 };
187
188 static char *intel_th_output_devnode(struct device *dev, umode_t *mode,
189                                      kuid_t *uid, kgid_t *gid)
190 {
191         struct intel_th_device *thdev = to_intel_th_device(dev);
192         struct intel_th *th = to_intel_th(thdev);
193         char *node;
194
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);
201
202         return node;
203 }
204
205 static ssize_t port_show(struct device *dev, struct device_attribute *attr,
206                          char *buf)
207 {
208         struct intel_th_device *thdev = to_intel_th_device(dev);
209
210         if (thdev->output.port >= 0)
211                 return scnprintf(buf, PAGE_SIZE, "%u\n", thdev->output.port);
212
213         return scnprintf(buf, PAGE_SIZE, "unassigned\n");
214 }
215
216 static DEVICE_ATTR_RO(port);
217
218 static void intel_th_trace_prepare(struct intel_th_device *thdev)
219 {
220         struct intel_th_device *hub = to_intel_th_hub(thdev);
221         struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
222
223         if (hub->type != INTEL_TH_SWITCH)
224                 return;
225
226         if (thdev->type != INTEL_TH_OUTPUT)
227                 return;
228
229         pm_runtime_get_sync(&thdev->dev);
230         hubdrv->prepare(hub, &thdev->output);
231         pm_runtime_put(&thdev->dev);
232 }
233
234 static int intel_th_output_activate(struct intel_th_device *thdev)
235 {
236         struct intel_th_driver *thdrv =
237                 to_intel_th_driver_or_null(thdev->dev.driver);
238         struct intel_th *th = to_intel_th(thdev);
239         int ret = 0;
240
241         if (!thdrv)
242                 return -ENODEV;
243
244         if (!try_module_get(thdrv->driver.owner))
245                 return -ENODEV;
246
247         pm_runtime_get_sync(&thdev->dev);
248
249         if (th->activate)
250                 ret = th->activate(th);
251         if (ret)
252                 goto fail_put;
253
254         intel_th_trace_prepare(thdev);
255         if (thdrv->activate)
256                 ret = thdrv->activate(thdev);
257         else
258                 intel_th_trace_enable(thdev);
259
260         if (ret)
261                 goto fail_deactivate;
262
263         return 0;
264
265 fail_deactivate:
266         if (th->deactivate)
267                 th->deactivate(th);
268
269 fail_put:
270         pm_runtime_put(&thdev->dev);
271         module_put(thdrv->driver.owner);
272
273         return ret;
274 }
275
276 static void intel_th_output_deactivate(struct intel_th_device *thdev)
277 {
278         struct intel_th_driver *thdrv =
279                 to_intel_th_driver_or_null(thdev->dev.driver);
280         struct intel_th *th = to_intel_th(thdev);
281
282         if (!thdrv)
283                 return;
284
285         if (thdrv->deactivate)
286                 thdrv->deactivate(thdev);
287         else
288                 intel_th_trace_disable(thdev);
289
290         if (th->deactivate)
291                 th->deactivate(th);
292
293         pm_runtime_put(&thdev->dev);
294         module_put(thdrv->driver.owner);
295 }
296
297 static ssize_t active_show(struct device *dev, struct device_attribute *attr,
298                            char *buf)
299 {
300         struct intel_th_device *thdev = to_intel_th_device(dev);
301
302         return scnprintf(buf, PAGE_SIZE, "%d\n", thdev->output.active);
303 }
304
305 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
306                             const char *buf, size_t size)
307 {
308         struct intel_th_device *thdev = to_intel_th_device(dev);
309         unsigned long val;
310         int ret;
311
312         ret = kstrtoul(buf, 10, &val);
313         if (ret)
314                 return ret;
315
316         if (!!val != thdev->output.active) {
317                 if (val)
318                         ret = intel_th_output_activate(thdev);
319                 else
320                         intel_th_output_deactivate(thdev);
321         }
322
323         return ret ? ret : size;
324 }
325
326 static DEVICE_ATTR_RW(active);
327
328 static struct attribute *intel_th_output_attrs[] = {
329         &dev_attr_port.attr,
330         &dev_attr_active.attr,
331         NULL,
332 };
333
334 ATTRIBUTE_GROUPS(intel_th_output);
335
336 static struct device_type intel_th_output_device_type = {
337         .name           = "intel_th_output_device",
338         .groups         = intel_th_output_groups,
339         .release        = intel_th_device_release,
340         .devnode        = intel_th_output_devnode,
341 };
342
343 static struct device_type intel_th_switch_device_type = {
344         .name           = "intel_th_switch_device",
345         .release        = intel_th_device_release,
346 };
347
348 static struct device_type *intel_th_device_type[] = {
349         [INTEL_TH_SOURCE]       = &intel_th_source_device_type,
350         [INTEL_TH_OUTPUT]       = &intel_th_output_device_type,
351         [INTEL_TH_SWITCH]       = &intel_th_switch_device_type,
352 };
353
354 int intel_th_driver_register(struct intel_th_driver *thdrv)
355 {
356         if (!thdrv->probe || !thdrv->remove)
357                 return -EINVAL;
358
359         thdrv->driver.bus = &intel_th_bus;
360
361         return driver_register(&thdrv->driver);
362 }
363 EXPORT_SYMBOL_GPL(intel_th_driver_register);
364
365 void intel_th_driver_unregister(struct intel_th_driver *thdrv)
366 {
367         driver_unregister(&thdrv->driver);
368 }
369 EXPORT_SYMBOL_GPL(intel_th_driver_unregister);
370
371 static struct intel_th_device *
372 intel_th_device_alloc(struct intel_th *th, unsigned int type, const char *name,
373                       int id)
374 {
375         struct device *parent;
376         struct intel_th_device *thdev;
377
378         if (type == INTEL_TH_OUTPUT)
379                 parent = &th->hub->dev;
380         else
381                 parent = th->dev;
382
383         thdev = kzalloc(sizeof(*thdev) + strlen(name) + 1, GFP_KERNEL);
384         if (!thdev)
385                 return NULL;
386
387         thdev->id = id;
388         thdev->type = type;
389
390         strcpy(thdev->name, name);
391         device_initialize(&thdev->dev);
392         thdev->dev.bus = &intel_th_bus;
393         thdev->dev.type = intel_th_device_type[type];
394         thdev->dev.parent = parent;
395         thdev->dev.dma_mask = parent->dma_mask;
396         thdev->dev.dma_parms = parent->dma_parms;
397         dma_set_coherent_mask(&thdev->dev, parent->coherent_dma_mask);
398         if (id >= 0)
399                 dev_set_name(&thdev->dev, "%d-%s%d", th->id, name, id);
400         else
401                 dev_set_name(&thdev->dev, "%d-%s", th->id, name);
402
403         return thdev;
404 }
405
406 static int intel_th_device_add_resources(struct intel_th_device *thdev,
407                                          struct resource *res, int nres)
408 {
409         struct resource *r;
410
411         r = kmemdup(res, sizeof(*res) * nres, GFP_KERNEL);
412         if (!r)
413                 return -ENOMEM;
414
415         thdev->resource = r;
416         thdev->num_resources = nres;
417
418         return 0;
419 }
420
421 static void intel_th_device_remove(struct intel_th_device *thdev)
422 {
423         device_del(&thdev->dev);
424         put_device(&thdev->dev);
425 }
426
427 static void intel_th_device_free(struct intel_th_device *thdev)
428 {
429         kfree(thdev->resource);
430         kfree(thdev);
431 }
432
433 /*
434  * Intel(R) Trace Hub subdevices
435  */
436 static const struct intel_th_subdevice {
437         const char              *name;
438         struct resource         res[3];
439         unsigned                nres;
440         unsigned                type;
441         unsigned                otype;
442         unsigned                scrpd;
443         int                     id;
444 } intel_th_subdevices[] = {
445         {
446                 .nres   = 1,
447                 .res    = {
448                         {
449                                 /* Handle TSCU from GTH driver */
450                                 .start  = REG_GTH_OFFSET,
451                                 .end    = REG_TSCU_OFFSET + REG_TSCU_LENGTH - 1,
452                                 .flags  = IORESOURCE_MEM,
453                         },
454                 },
455                 .name   = "gth",
456                 .type   = INTEL_TH_SWITCH,
457                 .id     = -1,
458         },
459         {
460                 .nres   = 2,
461                 .res    = {
462                         {
463                                 .start  = REG_MSU_OFFSET,
464                                 .end    = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
465                                 .flags  = IORESOURCE_MEM,
466                         },
467                         {
468                                 .start  = BUF_MSU_OFFSET,
469                                 .end    = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
470                                 .flags  = IORESOURCE_MEM,
471                         },
472                 },
473                 .name   = "msc",
474                 .id     = 0,
475                 .type   = INTEL_TH_OUTPUT,
476                 .otype  = GTH_MSU,
477                 .scrpd  = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC0_IS_ENABLED,
478         },
479         {
480                 .nres   = 2,
481                 .res    = {
482                         {
483                                 .start  = REG_MSU_OFFSET,
484                                 .end    = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
485                                 .flags  = IORESOURCE_MEM,
486                         },
487                         {
488                                 .start  = BUF_MSU_OFFSET,
489                                 .end    = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
490                                 .flags  = IORESOURCE_MEM,
491                         },
492                 },
493                 .name   = "msc",
494                 .id     = 1,
495                 .type   = INTEL_TH_OUTPUT,
496                 .otype  = GTH_MSU,
497                 .scrpd  = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC1_IS_ENABLED,
498         },
499         {
500                 .nres   = 2,
501                 .res    = {
502                         {
503                                 .start  = REG_STH_OFFSET,
504                                 .end    = REG_STH_OFFSET + REG_STH_LENGTH - 1,
505                                 .flags  = IORESOURCE_MEM,
506                         },
507                         {
508                                 .start  = 1, /* use resource[1] */
509                                 .end    = 0,
510                                 .flags  = IORESOURCE_MEM,
511                         },
512                 },
513                 .id     = -1,
514                 .name   = "sth",
515                 .type   = INTEL_TH_SOURCE,
516         },
517         {
518                 .nres   = 1,
519                 .res    = {
520                         {
521                                 .start  = REG_PTI_OFFSET,
522                                 .end    = REG_PTI_OFFSET + REG_PTI_LENGTH - 1,
523                                 .flags  = IORESOURCE_MEM,
524                         },
525                 },
526                 .id     = -1,
527                 .name   = "pti",
528                 .type   = INTEL_TH_OUTPUT,
529                 .otype  = GTH_PTI,
530                 .scrpd  = SCRPD_PTI_IS_PRIM_DEST,
531         },
532         {
533                 .nres   = 1,
534                 .res    = {
535                         {
536                                 .start  = REG_PTI_OFFSET,
537                                 .end    = REG_PTI_OFFSET + REG_PTI_LENGTH - 1,
538                                 .flags  = IORESOURCE_MEM,
539                         },
540                 },
541                 .id     = -1,
542                 .name   = "lpp",
543                 .type   = INTEL_TH_OUTPUT,
544                 .otype  = GTH_LPP,
545                 .scrpd  = SCRPD_PTI_IS_PRIM_DEST,
546         },
547         {
548                 .nres   = 1,
549                 .res    = {
550                         {
551                                 .start  = REG_DCIH_OFFSET,
552                                 .end    = REG_DCIH_OFFSET + REG_DCIH_LENGTH - 1,
553                                 .flags  = IORESOURCE_MEM,
554                         },
555                 },
556                 .id     = -1,
557                 .name   = "dcih",
558                 .type   = INTEL_TH_OUTPUT,
559         },
560 };
561
562 #ifdef CONFIG_MODULES
563 static void __intel_th_request_hub_module(struct work_struct *work)
564 {
565         struct intel_th *th = container_of(work, struct intel_th,
566                                            request_module_work);
567
568         request_module("intel_th_%s", th->hub->name);
569 }
570
571 static int intel_th_request_hub_module(struct intel_th *th)
572 {
573         INIT_WORK(&th->request_module_work, __intel_th_request_hub_module);
574         schedule_work(&th->request_module_work);
575
576         return 0;
577 }
578
579 static void intel_th_request_hub_module_flush(struct intel_th *th)
580 {
581         flush_work(&th->request_module_work);
582 }
583 #else
584 static inline int intel_th_request_hub_module(struct intel_th *th)
585 {
586         return -EINVAL;
587 }
588
589 static inline void intel_th_request_hub_module_flush(struct intel_th *th)
590 {
591 }
592 #endif /* CONFIG_MODULES */
593
594 static struct intel_th_device *
595 intel_th_subdevice_alloc(struct intel_th *th,
596                          const struct intel_th_subdevice *subdev)
597 {
598         struct intel_th_device *thdev;
599         struct resource res[3];
600         unsigned int req = 0;
601         bool is64bit = false;
602         int r, err;
603
604         thdev = intel_th_device_alloc(th, subdev->type, subdev->name,
605                                       subdev->id);
606         if (!thdev)
607                 return ERR_PTR(-ENOMEM);
608
609         thdev->drvdata = th->drvdata;
610
611         for (r = 0; r < th->num_resources; r++)
612                 if (th->resource[r].flags & IORESOURCE_MEM_64) {
613                         is64bit = true;
614                         break;
615                 }
616
617         memcpy(res, subdev->res,
618                sizeof(struct resource) * subdev->nres);
619
620         for (r = 0; r < subdev->nres; r++) {
621                 struct resource *devres = th->resource;
622                 int bar = 0; /* cut subdevices' MMIO from resource[0] */
623
624                 /*
625                  * Take .end == 0 to mean 'take the whole bar',
626                  * .start then tells us which bar it is. Default to
627                  * TH_MMIO_CONFIG.
628                  */
629                 if (!res[r].end && res[r].flags == IORESOURCE_MEM) {
630                         bar = res[r].start;
631                         if (is64bit)
632                                 bar *= 2;
633                         res[r].start = 0;
634                         res[r].end = resource_size(&devres[bar]) - 1;
635                 }
636
637                 if (res[r].flags & IORESOURCE_MEM) {
638                         res[r].start    += devres[bar].start;
639                         res[r].end      += devres[bar].start;
640
641                         dev_dbg(th->dev, "%s:%d @ %pR\n",
642                                 subdev->name, r, &res[r]);
643                 } else if (res[r].flags & IORESOURCE_IRQ) {
644                         res[r].start    = th->irq;
645                 }
646         }
647
648         err = intel_th_device_add_resources(thdev, res, subdev->nres);
649         if (err)
650                 goto fail_put_device;
651
652         if (subdev->type == INTEL_TH_OUTPUT) {
653                 thdev->dev.devt = MKDEV(th->major, th->num_thdevs);
654                 thdev->output.type = subdev->otype;
655                 thdev->output.port = -1;
656                 thdev->output.scratchpad = subdev->scrpd;
657         } else if (subdev->type == INTEL_TH_SWITCH) {
658                 thdev->host_mode =
659                         INTEL_TH_CAP(th, host_mode_only) ? true : host_mode;
660                 th->hub = thdev;
661         }
662
663         err = device_add(&thdev->dev);
664         if (err)
665                 goto fail_free_res;
666
667         /* need switch driver to be loaded to enumerate the rest */
668         if (subdev->type == INTEL_TH_SWITCH && !req) {
669                 err = intel_th_request_hub_module(th);
670                 if (!err)
671                         req++;
672         }
673
674         return thdev;
675
676 fail_free_res:
677         kfree(thdev->resource);
678
679 fail_put_device:
680         put_device(&thdev->dev);
681
682         return ERR_PTR(err);
683 }
684
685 /**
686  * intel_th_output_enable() - find and enable a device for a given output type
687  * @th:         Intel TH instance
688  * @otype:      output type
689  *
690  * Go through the unallocated output devices, find the first one whos type
691  * matches @otype and instantiate it. These devices are removed when the hub
692  * device is removed, see intel_th_remove().
693  */
694 int intel_th_output_enable(struct intel_th *th, unsigned int otype)
695 {
696         struct intel_th_device *thdev;
697         int src = 0, dst = 0;
698
699         for (src = 0, dst = 0; dst <= th->num_thdevs; src++, dst++) {
700                 for (; src < ARRAY_SIZE(intel_th_subdevices); src++) {
701                         if (intel_th_subdevices[src].type != INTEL_TH_OUTPUT)
702                                 continue;
703
704                         if (intel_th_subdevices[src].otype != otype)
705                                 continue;
706
707                         break;
708                 }
709
710                 /* no unallocated matching subdevices */
711                 if (src == ARRAY_SIZE(intel_th_subdevices))
712                         return -ENODEV;
713
714                 for (; dst < th->num_thdevs; dst++) {
715                         if (th->thdev[dst]->type != INTEL_TH_OUTPUT)
716                                 continue;
717
718                         if (th->thdev[dst]->output.type != otype)
719                                 continue;
720
721                         break;
722                 }
723
724                 /*
725                  * intel_th_subdevices[src] matches our requirements and is
726                  * not matched in th::thdev[]
727                  */
728                 if (dst == th->num_thdevs)
729                         goto found;
730         }
731
732         return -ENODEV;
733
734 found:
735         thdev = intel_th_subdevice_alloc(th, &intel_th_subdevices[src]);
736         if (IS_ERR(thdev))
737                 return PTR_ERR(thdev);
738
739         th->thdev[th->num_thdevs++] = thdev;
740
741         return 0;
742 }
743 EXPORT_SYMBOL_GPL(intel_th_output_enable);
744
745 static int intel_th_populate(struct intel_th *th)
746 {
747         int src;
748
749         /* create devices for each intel_th_subdevice */
750         for (src = 0; src < ARRAY_SIZE(intel_th_subdevices); src++) {
751                 const struct intel_th_subdevice *subdev =
752                         &intel_th_subdevices[src];
753                 struct intel_th_device *thdev;
754
755                 /* only allow SOURCE and SWITCH devices in host mode */
756                 if ((INTEL_TH_CAP(th, host_mode_only) || host_mode) &&
757                     subdev->type == INTEL_TH_OUTPUT)
758                         continue;
759
760                 /*
761                  * don't enable port OUTPUTs in this path; SWITCH enables them
762                  * via intel_th_output_enable()
763                  */
764                 if (subdev->type == INTEL_TH_OUTPUT &&
765                     subdev->otype != GTH_NONE)
766                         continue;
767
768                 thdev = intel_th_subdevice_alloc(th, subdev);
769                 /* note: caller should free subdevices from th::thdev[] */
770                 if (IS_ERR(thdev))
771                         return PTR_ERR(thdev);
772
773                 th->thdev[th->num_thdevs++] = thdev;
774         }
775
776         return 0;
777 }
778
779 static int match_devt(struct device *dev, void *data)
780 {
781         dev_t devt = (dev_t)(unsigned long)data;
782
783         return dev->devt == devt;
784 }
785
786 static int intel_th_output_open(struct inode *inode, struct file *file)
787 {
788         const struct file_operations *fops;
789         struct intel_th_driver *thdrv;
790         struct device *dev;
791         int err;
792
793         dev = bus_find_device(&intel_th_bus, NULL,
794                               (void *)(unsigned long)inode->i_rdev,
795                               match_devt);
796         if (!dev || !dev->driver)
797                 return -ENODEV;
798
799         thdrv = to_intel_th_driver(dev->driver);
800         fops = fops_get(thdrv->fops);
801         if (!fops)
802                 return -ENODEV;
803
804         replace_fops(file, fops);
805
806         file->private_data = to_intel_th_device(dev);
807
808         if (file->f_op->open) {
809                 err = file->f_op->open(inode, file);
810                 return err;
811         }
812
813         return 0;
814 }
815
816 static const struct file_operations intel_th_output_fops = {
817         .open   = intel_th_output_open,
818         .llseek = noop_llseek,
819 };
820
821 /**
822  * intel_th_alloc() - allocate a new Intel TH device and its subdevices
823  * @dev:        parent device
824  * @devres:     parent's resources
825  * @ndevres:    number of resources
826  * @irq:        irq number
827  */
828 struct intel_th *
829 intel_th_alloc(struct device *dev, struct intel_th_drvdata *drvdata,
830                struct resource *devres, unsigned int ndevres, int irq)
831 {
832         struct intel_th *th;
833         int err, r;
834
835         if (irq == -1)
836                 for (r = 0; r < ndevres; r++)
837                         if (devres[r].flags & IORESOURCE_IRQ) {
838                                 irq = devres[r].start;
839                                 break;
840                         }
841
842         th = kzalloc(sizeof(*th), GFP_KERNEL);
843         if (!th)
844                 return ERR_PTR(-ENOMEM);
845
846         th->id = ida_simple_get(&intel_th_ida, 0, 0, GFP_KERNEL);
847         if (th->id < 0) {
848                 err = th->id;
849                 goto err_alloc;
850         }
851
852         th->major = __register_chrdev(0, 0, TH_POSSIBLE_OUTPUTS,
853                                       "intel_th/output", &intel_th_output_fops);
854         if (th->major < 0) {
855                 err = th->major;
856                 goto err_ida;
857         }
858         th->dev = dev;
859         th->drvdata = drvdata;
860
861         th->resource = devres;
862         th->num_resources = ndevres;
863         th->irq = irq;
864
865         dev_set_drvdata(dev, th);
866
867         pm_runtime_no_callbacks(dev);
868         pm_runtime_put(dev);
869         pm_runtime_allow(dev);
870
871         err = intel_th_populate(th);
872         if (err) {
873                 /* free the subdevices and undo everything */
874                 intel_th_free(th);
875                 return ERR_PTR(err);
876         }
877
878         return th;
879
880 err_ida:
881         ida_simple_remove(&intel_th_ida, th->id);
882
883 err_alloc:
884         kfree(th);
885
886         return ERR_PTR(err);
887 }
888 EXPORT_SYMBOL_GPL(intel_th_alloc);
889
890 void intel_th_free(struct intel_th *th)
891 {
892         int i;
893
894         intel_th_request_hub_module_flush(th);
895
896         intel_th_device_remove(th->hub);
897         for (i = 0; i < th->num_thdevs; i++) {
898                 if (th->thdev[i] != th->hub)
899                         intel_th_device_remove(th->thdev[i]);
900                 th->thdev[i] = NULL;
901         }
902
903         th->num_thdevs = 0;
904
905         pm_runtime_get_sync(th->dev);
906         pm_runtime_forbid(th->dev);
907
908         __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
909                             "intel_th/output");
910
911         ida_simple_remove(&intel_th_ida, th->id);
912
913         kfree(th);
914 }
915 EXPORT_SYMBOL_GPL(intel_th_free);
916
917 /**
918  * intel_th_trace_enable() - enable tracing for an output device
919  * @thdev:      output device that requests tracing be enabled
920  */
921 int intel_th_trace_enable(struct intel_th_device *thdev)
922 {
923         struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
924         struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
925
926         if (WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH))
927                 return -EINVAL;
928
929         if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
930                 return -EINVAL;
931
932         pm_runtime_get_sync(&thdev->dev);
933         hubdrv->enable(hub, &thdev->output);
934
935         return 0;
936 }
937 EXPORT_SYMBOL_GPL(intel_th_trace_enable);
938
939 /**
940  * intel_th_trace_disable() - disable tracing for an output device
941  * @thdev:      output device that requests tracing be disabled
942  */
943 int intel_th_trace_disable(struct intel_th_device *thdev)
944 {
945         struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
946         struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
947
948         WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH);
949         if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
950                 return -EINVAL;
951
952         hubdrv->disable(hub, &thdev->output);
953         pm_runtime_put(&thdev->dev);
954
955         return 0;
956 }
957 EXPORT_SYMBOL_GPL(intel_th_trace_disable);
958
959 int intel_th_set_output(struct intel_th_device *thdev,
960                         unsigned int master)
961 {
962         struct intel_th_device *hub = to_intel_th_hub(thdev);
963         struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
964         int ret;
965
966         /* In host mode, this is up to the external debugger, do nothing. */
967         if (hub->host_mode)
968                 return 0;
969
970         /*
971          * hub is instantiated together with the source device that
972          * calls here, so guaranteed to be present.
973          */
974         hubdrv = to_intel_th_driver(hub->dev.driver);
975         if (!hubdrv || !try_module_get(hubdrv->driver.owner))
976                 return -EINVAL;
977
978         if (!hubdrv->set_output) {
979                 ret = -ENOTSUPP;
980                 goto out;
981         }
982
983         ret = hubdrv->set_output(hub, master);
984
985 out:
986         module_put(hubdrv->driver.owner);
987         return ret;
988 }
989 EXPORT_SYMBOL_GPL(intel_th_set_output);
990
991 static int __init intel_th_init(void)
992 {
993         intel_th_debug_init();
994
995         return bus_register(&intel_th_bus);
996 }
997 subsys_initcall(intel_th_init);
998
999 static void __exit intel_th_exit(void)
1000 {
1001         intel_th_debug_done();
1002
1003         bus_unregister(&intel_th_bus);
1004 }
1005 module_exit(intel_th_exit);
1006
1007 MODULE_LICENSE("GPL v2");
1008 MODULE_DESCRIPTION("Intel(R) Trace Hub controller driver");
1009 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");