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