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 int intel_th_output_activate(struct intel_th_device *thdev)
219 {
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;
224 
225 	if (!thdrv)
226 		return -ENODEV;
227 
228 	if (!try_module_get(thdrv->driver.owner))
229 		return -ENODEV;
230 
231 	pm_runtime_get_sync(&thdev->dev);
232 
233 	if (th->activate)
234 		ret = th->activate(th);
235 	if (ret)
236 		goto fail_put;
237 
238 	if (thdrv->activate)
239 		ret = thdrv->activate(thdev);
240 	else
241 		intel_th_trace_enable(thdev);
242 
243 	if (ret)
244 		goto fail_deactivate;
245 
246 	return 0;
247 
248 fail_deactivate:
249 	if (th->deactivate)
250 		th->deactivate(th);
251 
252 fail_put:
253 	pm_runtime_put(&thdev->dev);
254 	module_put(thdrv->driver.owner);
255 
256 	return ret;
257 }
258 
259 static void intel_th_output_deactivate(struct intel_th_device *thdev)
260 {
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);
264 
265 	if (!thdrv)
266 		return;
267 
268 	if (thdrv->deactivate)
269 		thdrv->deactivate(thdev);
270 	else
271 		intel_th_trace_disable(thdev);
272 
273 	if (th->deactivate)
274 		th->deactivate(th);
275 
276 	pm_runtime_put(&thdev->dev);
277 	module_put(thdrv->driver.owner);
278 }
279 
280 static ssize_t active_show(struct device *dev, struct device_attribute *attr,
281 			   char *buf)
282 {
283 	struct intel_th_device *thdev = to_intel_th_device(dev);
284 
285 	return scnprintf(buf, PAGE_SIZE, "%d\n", thdev->output.active);
286 }
287 
288 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
289 			    const char *buf, size_t size)
290 {
291 	struct intel_th_device *thdev = to_intel_th_device(dev);
292 	unsigned long val;
293 	int ret;
294 
295 	ret = kstrtoul(buf, 10, &val);
296 	if (ret)
297 		return ret;
298 
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 	}
305 
306 	return ret ? ret : size;
307 }
308 
309 static DEVICE_ATTR_RW(active);
310 
311 static struct attribute *intel_th_output_attrs[] = {
312 	&dev_attr_port.attr,
313 	&dev_attr_active.attr,
314 	NULL,
315 };
316 
317 ATTRIBUTE_GROUPS(intel_th_output);
318 
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 };
325 
326 static struct device_type intel_th_switch_device_type = {
327 	.name		= "intel_th_switch_device",
328 	.release	= intel_th_device_release,
329 };
330 
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 };
336 
337 int intel_th_driver_register(struct intel_th_driver *thdrv)
338 {
339 	if (!thdrv->probe || !thdrv->remove)
340 		return -EINVAL;
341 
342 	thdrv->driver.bus = &intel_th_bus;
343 
344 	return driver_register(&thdrv->driver);
345 }
346 EXPORT_SYMBOL_GPL(intel_th_driver_register);
347 
348 void intel_th_driver_unregister(struct intel_th_driver *thdrv)
349 {
350 	driver_unregister(&thdrv->driver);
351 }
352 EXPORT_SYMBOL_GPL(intel_th_driver_unregister);
353 
354 static struct intel_th_device *
355 intel_th_device_alloc(struct intel_th *th, unsigned int type, const char *name,
356 		      int id)
357 {
358 	struct device *parent;
359 	struct intel_th_device *thdev;
360 
361 	if (type == INTEL_TH_OUTPUT)
362 		parent = &th->hub->dev;
363 	else
364 		parent = th->dev;
365 
366 	thdev = kzalloc(sizeof(*thdev) + strlen(name) + 1, GFP_KERNEL);
367 	if (!thdev)
368 		return NULL;
369 
370 	thdev->id = id;
371 	thdev->type = type;
372 
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);
385 
386 	return thdev;
387 }
388 
389 static int intel_th_device_add_resources(struct intel_th_device *thdev,
390 					 struct resource *res, int nres)
391 {
392 	struct resource *r;
393 
394 	r = kmemdup(res, sizeof(*res) * nres, GFP_KERNEL);
395 	if (!r)
396 		return -ENOMEM;
397 
398 	thdev->resource = r;
399 	thdev->num_resources = nres;
400 
401 	return 0;
402 }
403 
404 static void intel_th_device_remove(struct intel_th_device *thdev)
405 {
406 	device_del(&thdev->dev);
407 	put_device(&thdev->dev);
408 }
409 
410 static void intel_th_device_free(struct intel_th_device *thdev)
411 {
412 	kfree(thdev->resource);
413 	kfree(thdev);
414 }
415 
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 	bool			mknode;
426 	unsigned		scrpd;
427 	int			id;
428 } intel_th_subdevices[] = {
429 	{
430 		.nres	= 1,
431 		.res	= {
432 			{
433 				/* Handle TSCU from GTH driver */
434 				.start	= REG_GTH_OFFSET,
435 				.end	= REG_TSCU_OFFSET + REG_TSCU_LENGTH - 1,
436 				.flags	= IORESOURCE_MEM,
437 			},
438 		},
439 		.name	= "gth",
440 		.type	= INTEL_TH_SWITCH,
441 		.id	= -1,
442 	},
443 	{
444 		.nres	= 2,
445 		.res	= {
446 			{
447 				.start	= REG_MSU_OFFSET,
448 				.end	= REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
449 				.flags	= IORESOURCE_MEM,
450 			},
451 			{
452 				.start	= BUF_MSU_OFFSET,
453 				.end	= BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
454 				.flags	= IORESOURCE_MEM,
455 			},
456 		},
457 		.name	= "msc",
458 		.id	= 0,
459 		.type	= INTEL_TH_OUTPUT,
460 		.mknode	= true,
461 		.otype	= GTH_MSU,
462 		.scrpd	= SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC0_IS_ENABLED,
463 	},
464 	{
465 		.nres	= 2,
466 		.res	= {
467 			{
468 				.start	= REG_MSU_OFFSET,
469 				.end	= REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
470 				.flags	= IORESOURCE_MEM,
471 			},
472 			{
473 				.start	= BUF_MSU_OFFSET,
474 				.end	= BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
475 				.flags	= IORESOURCE_MEM,
476 			},
477 		},
478 		.name	= "msc",
479 		.id	= 1,
480 		.type	= INTEL_TH_OUTPUT,
481 		.mknode	= true,
482 		.otype	= GTH_MSU,
483 		.scrpd	= SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC1_IS_ENABLED,
484 	},
485 	{
486 		.nres	= 2,
487 		.res	= {
488 			{
489 				.start	= REG_STH_OFFSET,
490 				.end	= REG_STH_OFFSET + REG_STH_LENGTH - 1,
491 				.flags	= IORESOURCE_MEM,
492 			},
493 			{
494 				.start	= 1, /* use resource[1] */
495 				.end	= 0,
496 				.flags	= IORESOURCE_MEM,
497 			},
498 		},
499 		.id	= -1,
500 		.name	= "sth",
501 		.type	= INTEL_TH_SOURCE,
502 	},
503 	{
504 		.nres	= 1,
505 		.res	= {
506 			{
507 				.start	= REG_PTI_OFFSET,
508 				.end	= REG_PTI_OFFSET + REG_PTI_LENGTH - 1,
509 				.flags	= IORESOURCE_MEM,
510 			},
511 		},
512 		.id	= -1,
513 		.name	= "pti",
514 		.type	= INTEL_TH_OUTPUT,
515 		.otype	= GTH_PTI,
516 		.scrpd	= SCRPD_PTI_IS_PRIM_DEST,
517 	},
518 	{
519 		.nres	= 1,
520 		.res	= {
521 			{
522 				.start	= REG_PTI_OFFSET,
523 				.end	= REG_PTI_OFFSET + REG_PTI_LENGTH - 1,
524 				.flags	= IORESOURCE_MEM,
525 			},
526 		},
527 		.id	= -1,
528 		.name	= "lpp",
529 		.type	= INTEL_TH_OUTPUT,
530 		.otype	= GTH_LPP,
531 		.scrpd	= SCRPD_PTI_IS_PRIM_DEST,
532 	},
533 	{
534 		.nres	= 1,
535 		.res	= {
536 			{
537 				.start	= REG_DCIH_OFFSET,
538 				.end	= REG_DCIH_OFFSET + REG_DCIH_LENGTH - 1,
539 				.flags	= IORESOURCE_MEM,
540 			},
541 		},
542 		.id	= -1,
543 		.name	= "dcih",
544 		.type	= INTEL_TH_OUTPUT,
545 	},
546 };
547 
548 #ifdef CONFIG_MODULES
549 static void __intel_th_request_hub_module(struct work_struct *work)
550 {
551 	struct intel_th *th = container_of(work, struct intel_th,
552 					   request_module_work);
553 
554 	request_module("intel_th_%s", th->hub->name);
555 }
556 
557 static int intel_th_request_hub_module(struct intel_th *th)
558 {
559 	INIT_WORK(&th->request_module_work, __intel_th_request_hub_module);
560 	schedule_work(&th->request_module_work);
561 
562 	return 0;
563 }
564 
565 static void intel_th_request_hub_module_flush(struct intel_th *th)
566 {
567 	flush_work(&th->request_module_work);
568 }
569 #else
570 static inline int intel_th_request_hub_module(struct intel_th *th)
571 {
572 	return -EINVAL;
573 }
574 
575 static inline void intel_th_request_hub_module_flush(struct intel_th *th)
576 {
577 }
578 #endif /* CONFIG_MODULES */
579 
580 static struct intel_th_device *
581 intel_th_subdevice_alloc(struct intel_th *th,
582 			 const struct intel_th_subdevice *subdev)
583 {
584 	struct intel_th_device *thdev;
585 	struct resource res[3];
586 	unsigned int req = 0;
587 	bool is64bit = false;
588 	int r, err;
589 
590 	thdev = intel_th_device_alloc(th, subdev->type, subdev->name,
591 				      subdev->id);
592 	if (!thdev)
593 		return ERR_PTR(-ENOMEM);
594 
595 	thdev->drvdata = th->drvdata;
596 
597 	for (r = 0; r < th->num_resources; r++)
598 		if (th->resource[r].flags & IORESOURCE_MEM_64) {
599 			is64bit = true;
600 			break;
601 		}
602 
603 	memcpy(res, subdev->res,
604 	       sizeof(struct resource) * subdev->nres);
605 
606 	for (r = 0; r < subdev->nres; r++) {
607 		struct resource *devres = th->resource;
608 		int bar = 0; /* cut subdevices' MMIO from resource[0] */
609 
610 		/*
611 		 * Take .end == 0 to mean 'take the whole bar',
612 		 * .start then tells us which bar it is. Default to
613 		 * TH_MMIO_CONFIG.
614 		 */
615 		if (!res[r].end && res[r].flags == IORESOURCE_MEM) {
616 			bar = res[r].start;
617 			if (is64bit)
618 				bar *= 2;
619 			res[r].start = 0;
620 			res[r].end = resource_size(&devres[bar]) - 1;
621 		}
622 
623 		if (res[r].flags & IORESOURCE_MEM) {
624 			res[r].start	+= devres[bar].start;
625 			res[r].end	+= devres[bar].start;
626 
627 			dev_dbg(th->dev, "%s:%d @ %pR\n",
628 				subdev->name, r, &res[r]);
629 		} else if (res[r].flags & IORESOURCE_IRQ) {
630 			res[r].start	= th->irq;
631 		}
632 	}
633 
634 	err = intel_th_device_add_resources(thdev, res, subdev->nres);
635 	if (err) {
636 		put_device(&thdev->dev);
637 		goto fail_put_device;
638 	}
639 
640 	if (subdev->type == INTEL_TH_OUTPUT) {
641 		if (subdev->mknode)
642 			thdev->dev.devt = MKDEV(th->major, th->num_thdevs);
643 		thdev->output.type = subdev->otype;
644 		thdev->output.port = -1;
645 		thdev->output.scratchpad = subdev->scrpd;
646 	} else if (subdev->type == INTEL_TH_SWITCH) {
647 		thdev->host_mode =
648 			INTEL_TH_CAP(th, host_mode_only) ? true : host_mode;
649 		th->hub = thdev;
650 	}
651 
652 	err = device_add(&thdev->dev);
653 	if (err) {
654 		put_device(&thdev->dev);
655 		goto fail_free_res;
656 	}
657 
658 	/* need switch driver to be loaded to enumerate the rest */
659 	if (subdev->type == INTEL_TH_SWITCH && !req) {
660 		err = intel_th_request_hub_module(th);
661 		if (!err)
662 			req++;
663 	}
664 
665 	return thdev;
666 
667 fail_free_res:
668 	kfree(thdev->resource);
669 
670 fail_put_device:
671 	put_device(&thdev->dev);
672 
673 	return ERR_PTR(err);
674 }
675 
676 /**
677  * intel_th_output_enable() - find and enable a device for a given output type
678  * @th:		Intel TH instance
679  * @otype:	output type
680  *
681  * Go through the unallocated output devices, find the first one whos type
682  * matches @otype and instantiate it. These devices are removed when the hub
683  * device is removed, see intel_th_remove().
684  */
685 int intel_th_output_enable(struct intel_th *th, unsigned int otype)
686 {
687 	struct intel_th_device *thdev;
688 	int src = 0, dst = 0;
689 
690 	for (src = 0, dst = 0; dst <= th->num_thdevs; src++, dst++) {
691 		for (; src < ARRAY_SIZE(intel_th_subdevices); src++) {
692 			if (intel_th_subdevices[src].type != INTEL_TH_OUTPUT)
693 				continue;
694 
695 			if (intel_th_subdevices[src].otype != otype)
696 				continue;
697 
698 			break;
699 		}
700 
701 		/* no unallocated matching subdevices */
702 		if (src == ARRAY_SIZE(intel_th_subdevices))
703 			return -ENODEV;
704 
705 		for (; dst < th->num_thdevs; dst++) {
706 			if (th->thdev[dst]->type != INTEL_TH_OUTPUT)
707 				continue;
708 
709 			if (th->thdev[dst]->output.type != otype)
710 				continue;
711 
712 			break;
713 		}
714 
715 		/*
716 		 * intel_th_subdevices[src] matches our requirements and is
717 		 * not matched in th::thdev[]
718 		 */
719 		if (dst == th->num_thdevs)
720 			goto found;
721 	}
722 
723 	return -ENODEV;
724 
725 found:
726 	thdev = intel_th_subdevice_alloc(th, &intel_th_subdevices[src]);
727 	if (IS_ERR(thdev))
728 		return PTR_ERR(thdev);
729 
730 	th->thdev[th->num_thdevs++] = thdev;
731 
732 	return 0;
733 }
734 EXPORT_SYMBOL_GPL(intel_th_output_enable);
735 
736 static int intel_th_populate(struct intel_th *th)
737 {
738 	int src;
739 
740 	/* create devices for each intel_th_subdevice */
741 	for (src = 0; src < ARRAY_SIZE(intel_th_subdevices); src++) {
742 		const struct intel_th_subdevice *subdev =
743 			&intel_th_subdevices[src];
744 		struct intel_th_device *thdev;
745 
746 		/* only allow SOURCE and SWITCH devices in host mode */
747 		if ((INTEL_TH_CAP(th, host_mode_only) || host_mode) &&
748 		    subdev->type == INTEL_TH_OUTPUT)
749 			continue;
750 
751 		/*
752 		 * don't enable port OUTPUTs in this path; SWITCH enables them
753 		 * via intel_th_output_enable()
754 		 */
755 		if (subdev->type == INTEL_TH_OUTPUT &&
756 		    subdev->otype != GTH_NONE)
757 			continue;
758 
759 		thdev = intel_th_subdevice_alloc(th, subdev);
760 		/* note: caller should free subdevices from th::thdev[] */
761 		if (IS_ERR(thdev))
762 			return PTR_ERR(thdev);
763 
764 		th->thdev[th->num_thdevs++] = thdev;
765 	}
766 
767 	return 0;
768 }
769 
770 static int match_devt(struct device *dev, void *data)
771 {
772 	dev_t devt = (dev_t)(unsigned long)data;
773 
774 	return dev->devt == devt;
775 }
776 
777 static int intel_th_output_open(struct inode *inode, struct file *file)
778 {
779 	const struct file_operations *fops;
780 	struct intel_th_driver *thdrv;
781 	struct device *dev;
782 	int err;
783 
784 	dev = bus_find_device(&intel_th_bus, NULL,
785 			      (void *)(unsigned long)inode->i_rdev,
786 			      match_devt);
787 	if (!dev || !dev->driver)
788 		return -ENODEV;
789 
790 	thdrv = to_intel_th_driver(dev->driver);
791 	fops = fops_get(thdrv->fops);
792 	if (!fops)
793 		return -ENODEV;
794 
795 	replace_fops(file, fops);
796 
797 	file->private_data = to_intel_th_device(dev);
798 
799 	if (file->f_op->open) {
800 		err = file->f_op->open(inode, file);
801 		return err;
802 	}
803 
804 	return 0;
805 }
806 
807 static const struct file_operations intel_th_output_fops = {
808 	.open	= intel_th_output_open,
809 	.llseek	= noop_llseek,
810 };
811 
812 /**
813  * intel_th_alloc() - allocate a new Intel TH device and its subdevices
814  * @dev:	parent device
815  * @devres:	parent's resources
816  * @ndevres:	number of resources
817  * @irq:	irq number
818  */
819 struct intel_th *
820 intel_th_alloc(struct device *dev, struct intel_th_drvdata *drvdata,
821 	       struct resource *devres, unsigned int ndevres, int irq)
822 {
823 	struct intel_th *th;
824 	int err, r;
825 
826 	if (irq == -1)
827 		for (r = 0; r < ndevres; r++)
828 			if (devres[r].flags & IORESOURCE_IRQ) {
829 				irq = devres[r].start;
830 				break;
831 			}
832 
833 	th = kzalloc(sizeof(*th), GFP_KERNEL);
834 	if (!th)
835 		return ERR_PTR(-ENOMEM);
836 
837 	th->id = ida_simple_get(&intel_th_ida, 0, 0, GFP_KERNEL);
838 	if (th->id < 0) {
839 		err = th->id;
840 		goto err_alloc;
841 	}
842 
843 	th->major = __register_chrdev(0, 0, TH_POSSIBLE_OUTPUTS,
844 				      "intel_th/output", &intel_th_output_fops);
845 	if (th->major < 0) {
846 		err = th->major;
847 		goto err_ida;
848 	}
849 	th->dev = dev;
850 	th->drvdata = drvdata;
851 
852 	th->resource = devres;
853 	th->num_resources = ndevres;
854 	th->irq = irq;
855 
856 	dev_set_drvdata(dev, th);
857 
858 	pm_runtime_no_callbacks(dev);
859 	pm_runtime_put(dev);
860 	pm_runtime_allow(dev);
861 
862 	err = intel_th_populate(th);
863 	if (err) {
864 		/* free the subdevices and undo everything */
865 		intel_th_free(th);
866 		return ERR_PTR(err);
867 	}
868 
869 	return th;
870 
871 err_ida:
872 	ida_simple_remove(&intel_th_ida, th->id);
873 
874 err_alloc:
875 	kfree(th);
876 
877 	return ERR_PTR(err);
878 }
879 EXPORT_SYMBOL_GPL(intel_th_alloc);
880 
881 void intel_th_free(struct intel_th *th)
882 {
883 	int i;
884 
885 	intel_th_request_hub_module_flush(th);
886 
887 	intel_th_device_remove(th->hub);
888 	for (i = 0; i < th->num_thdevs; i++) {
889 		if (th->thdev[i] != th->hub)
890 			intel_th_device_remove(th->thdev[i]);
891 		th->thdev[i] = NULL;
892 	}
893 
894 	th->num_thdevs = 0;
895 
896 	pm_runtime_get_sync(th->dev);
897 	pm_runtime_forbid(th->dev);
898 
899 	__unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
900 			    "intel_th/output");
901 
902 	ida_simple_remove(&intel_th_ida, th->id);
903 
904 	kfree(th);
905 }
906 EXPORT_SYMBOL_GPL(intel_th_free);
907 
908 /**
909  * intel_th_trace_enable() - enable tracing for an output device
910  * @thdev:	output device that requests tracing be enabled
911  */
912 int intel_th_trace_enable(struct intel_th_device *thdev)
913 {
914 	struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
915 	struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
916 
917 	if (WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH))
918 		return -EINVAL;
919 
920 	if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
921 		return -EINVAL;
922 
923 	pm_runtime_get_sync(&thdev->dev);
924 	hubdrv->enable(hub, &thdev->output);
925 
926 	return 0;
927 }
928 EXPORT_SYMBOL_GPL(intel_th_trace_enable);
929 
930 /**
931  * intel_th_trace_disable() - disable tracing for an output device
932  * @thdev:	output device that requests tracing be disabled
933  */
934 int intel_th_trace_disable(struct intel_th_device *thdev)
935 {
936 	struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
937 	struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
938 
939 	WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH);
940 	if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
941 		return -EINVAL;
942 
943 	hubdrv->disable(hub, &thdev->output);
944 	pm_runtime_put(&thdev->dev);
945 
946 	return 0;
947 }
948 EXPORT_SYMBOL_GPL(intel_th_trace_disable);
949 
950 int intel_th_set_output(struct intel_th_device *thdev,
951 			unsigned int master)
952 {
953 	struct intel_th_device *hub = to_intel_th_hub(thdev);
954 	struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
955 
956 	/* In host mode, this is up to the external debugger, do nothing. */
957 	if (hub->host_mode)
958 		return 0;
959 
960 	if (!hubdrv->set_output)
961 		return -ENOTSUPP;
962 
963 	return hubdrv->set_output(hub, master);
964 }
965 EXPORT_SYMBOL_GPL(intel_th_set_output);
966 
967 static int __init intel_th_init(void)
968 {
969 	intel_th_debug_init();
970 
971 	return bus_register(&intel_th_bus);
972 }
973 subsys_initcall(intel_th_init);
974 
975 static void __exit intel_th_exit(void)
976 {
977 	intel_th_debug_done();
978 
979 	bus_unregister(&intel_th_bus);
980 }
981 module_exit(intel_th_exit);
982 
983 MODULE_LICENSE("GPL v2");
984 MODULE_DESCRIPTION("Intel(R) Trace Hub controller driver");
985 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");
986