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