intel_th: Set root device's drvdata early
[deliverable/linux.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/dma-mapping.h>
27
28 #include "intel_th.h"
29 #include "debug.h"
30
31 static DEFINE_IDA(intel_th_ida);
32
33 static int intel_th_match(struct device *dev, struct device_driver *driver)
34 {
35 struct intel_th_driver *thdrv = to_intel_th_driver(driver);
36 struct intel_th_device *thdev = to_intel_th_device(dev);
37
38 if (thdev->type == INTEL_TH_SWITCH &&
39 (!thdrv->enable || !thdrv->disable))
40 return 0;
41
42 return !strcmp(thdev->name, driver->name);
43 }
44
45 static int intel_th_child_remove(struct device *dev, void *data)
46 {
47 device_release_driver(dev);
48
49 return 0;
50 }
51
52 static int intel_th_probe(struct device *dev)
53 {
54 struct intel_th_driver *thdrv = to_intel_th_driver(dev->driver);
55 struct intel_th_device *thdev = to_intel_th_device(dev);
56 struct intel_th_driver *hubdrv;
57 struct intel_th_device *hub = NULL;
58 int ret;
59
60 if (thdev->type == INTEL_TH_SWITCH)
61 hub = thdev;
62 else if (dev->parent)
63 hub = to_intel_th_device(dev->parent);
64
65 if (!hub || !hub->dev.driver)
66 return -EPROBE_DEFER;
67
68 hubdrv = to_intel_th_driver(hub->dev.driver);
69
70 ret = thdrv->probe(to_intel_th_device(dev));
71 if (ret)
72 return ret;
73
74 if (thdev->type == INTEL_TH_OUTPUT &&
75 !intel_th_output_assigned(thdev))
76 ret = hubdrv->assign(hub, thdev);
77
78 return ret;
79 }
80
81 static int intel_th_remove(struct device *dev)
82 {
83 struct intel_th_driver *thdrv = to_intel_th_driver(dev->driver);
84 struct intel_th_device *thdev = to_intel_th_device(dev);
85 struct intel_th_device *hub = to_intel_th_device(dev->parent);
86 int err;
87
88 if (thdev->type == INTEL_TH_SWITCH) {
89 err = device_for_each_child(dev, thdev, intel_th_child_remove);
90 if (err)
91 return err;
92 }
93
94 thdrv->remove(thdev);
95
96 if (intel_th_output_assigned(thdev)) {
97 struct intel_th_driver *hubdrv =
98 to_intel_th_driver(dev->parent->driver);
99
100 if (hub->dev.driver)
101 hubdrv->unassign(hub, thdev);
102 }
103
104 return 0;
105 }
106
107 static struct bus_type intel_th_bus = {
108 .name = "intel_th",
109 .dev_attrs = NULL,
110 .match = intel_th_match,
111 .probe = intel_th_probe,
112 .remove = intel_th_remove,
113 };
114
115 static void intel_th_device_free(struct intel_th_device *thdev);
116
117 static void intel_th_device_release(struct device *dev)
118 {
119 intel_th_device_free(to_intel_th_device(dev));
120 }
121
122 static struct device_type intel_th_source_device_type = {
123 .name = "intel_th_source_device",
124 .release = intel_th_device_release,
125 };
126
127 static char *intel_th_output_devnode(struct device *dev, umode_t *mode,
128 kuid_t *uid, kgid_t *gid)
129 {
130 struct intel_th_device *thdev = to_intel_th_device(dev);
131 char *node;
132
133 if (thdev->id >= 0)
134 node = kasprintf(GFP_KERNEL, "intel_th%d/%s%d", 0, thdev->name,
135 thdev->id);
136 else
137 node = kasprintf(GFP_KERNEL, "intel_th%d/%s", 0, thdev->name);
138
139 return node;
140 }
141
142 static ssize_t port_show(struct device *dev, struct device_attribute *attr,
143 char *buf)
144 {
145 struct intel_th_device *thdev = to_intel_th_device(dev);
146
147 if (thdev->output.port >= 0)
148 return scnprintf(buf, PAGE_SIZE, "%u\n", thdev->output.port);
149
150 return scnprintf(buf, PAGE_SIZE, "unassigned\n");
151 }
152
153 static DEVICE_ATTR_RO(port);
154
155 static int intel_th_output_activate(struct intel_th_device *thdev)
156 {
157 struct intel_th_driver *thdrv = to_intel_th_driver(thdev->dev.driver);
158
159 if (thdrv->activate)
160 return thdrv->activate(thdev);
161
162 intel_th_trace_enable(thdev);
163
164 return 0;
165 }
166
167 static void intel_th_output_deactivate(struct intel_th_device *thdev)
168 {
169 struct intel_th_driver *thdrv = to_intel_th_driver(thdev->dev.driver);
170
171 if (thdrv->deactivate)
172 thdrv->deactivate(thdev);
173 else
174 intel_th_trace_disable(thdev);
175 }
176
177 static ssize_t active_show(struct device *dev, struct device_attribute *attr,
178 char *buf)
179 {
180 struct intel_th_device *thdev = to_intel_th_device(dev);
181
182 return scnprintf(buf, PAGE_SIZE, "%d\n", thdev->output.active);
183 }
184
185 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
186 const char *buf, size_t size)
187 {
188 struct intel_th_device *thdev = to_intel_th_device(dev);
189 unsigned long val;
190 int ret;
191
192 ret = kstrtoul(buf, 10, &val);
193 if (ret)
194 return ret;
195
196 if (!!val != thdev->output.active) {
197 if (val)
198 ret = intel_th_output_activate(thdev);
199 else
200 intel_th_output_deactivate(thdev);
201 }
202
203 return ret ? ret : size;
204 }
205
206 static DEVICE_ATTR_RW(active);
207
208 static struct attribute *intel_th_output_attrs[] = {
209 &dev_attr_port.attr,
210 &dev_attr_active.attr,
211 NULL,
212 };
213
214 ATTRIBUTE_GROUPS(intel_th_output);
215
216 static struct device_type intel_th_output_device_type = {
217 .name = "intel_th_output_device",
218 .groups = intel_th_output_groups,
219 .release = intel_th_device_release,
220 .devnode = intel_th_output_devnode,
221 };
222
223 static struct device_type intel_th_switch_device_type = {
224 .name = "intel_th_switch_device",
225 .release = intel_th_device_release,
226 };
227
228 static struct device_type *intel_th_device_type[] = {
229 [INTEL_TH_SOURCE] = &intel_th_source_device_type,
230 [INTEL_TH_OUTPUT] = &intel_th_output_device_type,
231 [INTEL_TH_SWITCH] = &intel_th_switch_device_type,
232 };
233
234 int intel_th_driver_register(struct intel_th_driver *thdrv)
235 {
236 if (!thdrv->probe || !thdrv->remove)
237 return -EINVAL;
238
239 thdrv->driver.bus = &intel_th_bus;
240
241 return driver_register(&thdrv->driver);
242 }
243 EXPORT_SYMBOL_GPL(intel_th_driver_register);
244
245 void intel_th_driver_unregister(struct intel_th_driver *thdrv)
246 {
247 driver_unregister(&thdrv->driver);
248 }
249 EXPORT_SYMBOL_GPL(intel_th_driver_unregister);
250
251 static struct intel_th_device *
252 intel_th_device_alloc(struct intel_th *th, unsigned int type, const char *name,
253 int id)
254 {
255 struct device *parent;
256 struct intel_th_device *thdev;
257
258 if (type == INTEL_TH_SWITCH)
259 parent = th->dev;
260 else
261 parent = &th->hub->dev;
262
263 thdev = kzalloc(sizeof(*thdev) + strlen(name) + 1, GFP_KERNEL);
264 if (!thdev)
265 return NULL;
266
267 thdev->id = id;
268 thdev->type = type;
269
270 strcpy(thdev->name, name);
271 device_initialize(&thdev->dev);
272 thdev->dev.bus = &intel_th_bus;
273 thdev->dev.type = intel_th_device_type[type];
274 thdev->dev.parent = parent;
275 thdev->dev.dma_mask = parent->dma_mask;
276 thdev->dev.dma_parms = parent->dma_parms;
277 dma_set_coherent_mask(&thdev->dev, parent->coherent_dma_mask);
278 if (id >= 0)
279 dev_set_name(&thdev->dev, "%d-%s%d", th->id, name, id);
280 else
281 dev_set_name(&thdev->dev, "%d-%s", th->id, name);
282
283 return thdev;
284 }
285
286 static int intel_th_device_add_resources(struct intel_th_device *thdev,
287 struct resource *res, int nres)
288 {
289 struct resource *r;
290
291 r = kmemdup(res, sizeof(*res) * nres, GFP_KERNEL);
292 if (!r)
293 return -ENOMEM;
294
295 thdev->resource = r;
296 thdev->num_resources = nres;
297
298 return 0;
299 }
300
301 static void intel_th_device_remove(struct intel_th_device *thdev)
302 {
303 device_del(&thdev->dev);
304 put_device(&thdev->dev);
305 }
306
307 static void intel_th_device_free(struct intel_th_device *thdev)
308 {
309 kfree(thdev->resource);
310 kfree(thdev);
311 }
312
313 /*
314 * Intel(R) Trace Hub subdevices
315 */
316 static struct intel_th_subdevice {
317 const char *name;
318 struct resource res[3];
319 unsigned nres;
320 unsigned type;
321 unsigned otype;
322 unsigned scrpd;
323 int id;
324 } intel_th_subdevices[TH_SUBDEVICE_MAX] = {
325 {
326 .nres = 1,
327 .res = {
328 {
329 .start = REG_GTH_OFFSET,
330 .end = REG_GTH_OFFSET + REG_GTH_LENGTH - 1,
331 .flags = IORESOURCE_MEM,
332 },
333 },
334 .name = "gth",
335 .type = INTEL_TH_SWITCH,
336 .id = -1,
337 },
338 {
339 .nres = 2,
340 .res = {
341 {
342 .start = REG_MSU_OFFSET,
343 .end = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
344 .flags = IORESOURCE_MEM,
345 },
346 {
347 .start = BUF_MSU_OFFSET,
348 .end = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
349 .flags = IORESOURCE_MEM,
350 },
351 },
352 .name = "msc",
353 .id = 0,
354 .type = INTEL_TH_OUTPUT,
355 .otype = GTH_MSU,
356 .scrpd = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC0_IS_ENABLED,
357 },
358 {
359 .nres = 2,
360 .res = {
361 {
362 .start = REG_MSU_OFFSET,
363 .end = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
364 .flags = IORESOURCE_MEM,
365 },
366 {
367 .start = BUF_MSU_OFFSET,
368 .end = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
369 .flags = IORESOURCE_MEM,
370 },
371 },
372 .name = "msc",
373 .id = 1,
374 .type = INTEL_TH_OUTPUT,
375 .otype = GTH_MSU,
376 .scrpd = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC1_IS_ENABLED,
377 },
378 {
379 .nres = 2,
380 .res = {
381 {
382 .start = REG_STH_OFFSET,
383 .end = REG_STH_OFFSET + REG_STH_LENGTH - 1,
384 .flags = IORESOURCE_MEM,
385 },
386 {
387 .start = TH_MMIO_SW,
388 .end = 0,
389 .flags = IORESOURCE_MEM,
390 },
391 },
392 .id = -1,
393 .name = "sth",
394 .type = INTEL_TH_SOURCE,
395 },
396 {
397 .nres = 1,
398 .res = {
399 {
400 .start = REG_PTI_OFFSET,
401 .end = REG_PTI_OFFSET + REG_PTI_LENGTH - 1,
402 .flags = IORESOURCE_MEM,
403 },
404 },
405 .id = -1,
406 .name = "pti",
407 .type = INTEL_TH_OUTPUT,
408 .otype = GTH_PTI,
409 .scrpd = SCRPD_PTI_IS_PRIM_DEST,
410 },
411 {
412 .nres = 1,
413 .res = {
414 {
415 .start = REG_DCIH_OFFSET,
416 .end = REG_DCIH_OFFSET + REG_DCIH_LENGTH - 1,
417 .flags = IORESOURCE_MEM,
418 },
419 },
420 .id = -1,
421 .name = "dcih",
422 .type = INTEL_TH_OUTPUT,
423 },
424 };
425
426 static int intel_th_populate(struct intel_th *th, struct resource *devres,
427 unsigned int ndevres, int irq)
428 {
429 struct resource res[3];
430 unsigned int req = 0;
431 int i, err;
432
433 /* create devices for each intel_th_subdevice */
434 for (i = 0; i < ARRAY_SIZE(intel_th_subdevices); i++) {
435 struct intel_th_subdevice *subdev = &intel_th_subdevices[i];
436 struct intel_th_device *thdev;
437 int r;
438
439 thdev = intel_th_device_alloc(th, subdev->type, subdev->name,
440 subdev->id);
441 if (!thdev) {
442 err = -ENOMEM;
443 goto kill_subdevs;
444 }
445
446 memcpy(res, subdev->res,
447 sizeof(struct resource) * subdev->nres);
448
449 for (r = 0; r < subdev->nres; r++) {
450 int bar = TH_MMIO_CONFIG;
451
452 /*
453 * Take .end == 0 to mean 'take the whole bar',
454 * .start then tells us which bar it is. Default to
455 * TH_MMIO_CONFIG.
456 */
457 if (!res[r].end && res[r].flags == IORESOURCE_MEM) {
458 bar = res[r].start;
459 res[r].start = 0;
460 res[r].end = resource_size(&devres[bar]) - 1;
461 }
462
463 if (res[r].flags & IORESOURCE_MEM) {
464 res[r].start += devres[bar].start;
465 res[r].end += devres[bar].start;
466
467 dev_dbg(th->dev, "%s:%d @ %pR\n",
468 subdev->name, r, &res[r]);
469 } else if (res[r].flags & IORESOURCE_IRQ) {
470 res[r].start = irq;
471 }
472 }
473
474 err = intel_th_device_add_resources(thdev, res, subdev->nres);
475 if (err) {
476 put_device(&thdev->dev);
477 goto kill_subdevs;
478 }
479
480 if (subdev->type == INTEL_TH_OUTPUT) {
481 thdev->dev.devt = MKDEV(th->major, i);
482 thdev->output.type = subdev->otype;
483 thdev->output.port = -1;
484 thdev->output.scratchpad = subdev->scrpd;
485 }
486
487 err = device_add(&thdev->dev);
488 if (err) {
489 put_device(&thdev->dev);
490 goto kill_subdevs;
491 }
492
493 /* need switch driver to be loaded to enumerate the rest */
494 if (subdev->type == INTEL_TH_SWITCH && !req) {
495 th->hub = thdev;
496 err = request_module("intel_th_%s", subdev->name);
497 if (!err)
498 req++;
499 }
500
501 th->thdev[i] = thdev;
502 }
503
504 return 0;
505
506 kill_subdevs:
507 for (i-- ; i >= 0; i--)
508 intel_th_device_remove(th->thdev[i]);
509
510 return err;
511 }
512
513 static int match_devt(struct device *dev, void *data)
514 {
515 dev_t devt = (dev_t)(unsigned long)data;
516
517 return dev->devt == devt;
518 }
519
520 static int intel_th_output_open(struct inode *inode, struct file *file)
521 {
522 const struct file_operations *fops;
523 struct intel_th_driver *thdrv;
524 struct device *dev;
525 int err;
526
527 dev = bus_find_device(&intel_th_bus, NULL,
528 (void *)(unsigned long)inode->i_rdev,
529 match_devt);
530 if (!dev || !dev->driver)
531 return -ENODEV;
532
533 thdrv = to_intel_th_driver(dev->driver);
534 fops = fops_get(thdrv->fops);
535 if (!fops)
536 return -ENODEV;
537
538 replace_fops(file, fops);
539
540 file->private_data = to_intel_th_device(dev);
541
542 if (file->f_op->open) {
543 err = file->f_op->open(inode, file);
544 return err;
545 }
546
547 return 0;
548 }
549
550 static const struct file_operations intel_th_output_fops = {
551 .open = intel_th_output_open,
552 .llseek = noop_llseek,
553 };
554
555 /**
556 * intel_th_alloc() - allocate a new Intel TH device and its subdevices
557 * @dev: parent device
558 * @devres: parent's resources
559 * @ndevres: number of resources
560 * @irq: irq number
561 */
562 struct intel_th *
563 intel_th_alloc(struct device *dev, struct resource *devres,
564 unsigned int ndevres, int irq)
565 {
566 struct intel_th *th;
567 int err;
568
569 th = kzalloc(sizeof(*th), GFP_KERNEL);
570 if (!th)
571 return ERR_PTR(-ENOMEM);
572
573 th->id = ida_simple_get(&intel_th_ida, 0, 0, GFP_KERNEL);
574 if (th->id < 0) {
575 err = th->id;
576 goto err_alloc;
577 }
578
579 th->major = __register_chrdev(0, 0, TH_POSSIBLE_OUTPUTS,
580 "intel_th/output", &intel_th_output_fops);
581 if (th->major < 0) {
582 err = th->major;
583 goto err_ida;
584 }
585 th->dev = dev;
586
587 dev_set_drvdata(dev, th);
588
589 err = intel_th_populate(th, devres, ndevres, irq);
590 if (err)
591 goto err_chrdev;
592
593 return th;
594
595 err_chrdev:
596 __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
597 "intel_th/output");
598
599 err_ida:
600 ida_simple_remove(&intel_th_ida, th->id);
601
602 err_alloc:
603 kfree(th);
604
605 return ERR_PTR(err);
606 }
607 EXPORT_SYMBOL_GPL(intel_th_alloc);
608
609 void intel_th_free(struct intel_th *th)
610 {
611 int i;
612
613 for (i = 0; i < TH_SUBDEVICE_MAX; i++)
614 if (th->thdev[i] != th->hub)
615 intel_th_device_remove(th->thdev[i]);
616
617 intel_th_device_remove(th->hub);
618
619 __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
620 "intel_th/output");
621
622 ida_simple_remove(&intel_th_ida, th->id);
623
624 kfree(th);
625 }
626 EXPORT_SYMBOL_GPL(intel_th_free);
627
628 /**
629 * intel_th_trace_enable() - enable tracing for an output device
630 * @thdev: output device that requests tracing be enabled
631 */
632 int intel_th_trace_enable(struct intel_th_device *thdev)
633 {
634 struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
635 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
636
637 if (WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH))
638 return -EINVAL;
639
640 if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
641 return -EINVAL;
642
643 hubdrv->enable(hub, &thdev->output);
644
645 return 0;
646 }
647 EXPORT_SYMBOL_GPL(intel_th_trace_enable);
648
649 /**
650 * intel_th_trace_disable() - disable tracing for an output device
651 * @thdev: output device that requests tracing be disabled
652 */
653 int intel_th_trace_disable(struct intel_th_device *thdev)
654 {
655 struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
656 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
657
658 WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH);
659 if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
660 return -EINVAL;
661
662 hubdrv->disable(hub, &thdev->output);
663
664 return 0;
665 }
666 EXPORT_SYMBOL_GPL(intel_th_trace_disable);
667
668 int intel_th_set_output(struct intel_th_device *thdev,
669 unsigned int master)
670 {
671 struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
672 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
673
674 if (!hubdrv->set_output)
675 return -ENOTSUPP;
676
677 return hubdrv->set_output(hub, master);
678 }
679 EXPORT_SYMBOL_GPL(intel_th_set_output);
680
681 static int __init intel_th_init(void)
682 {
683 intel_th_debug_init();
684
685 return bus_register(&intel_th_bus);
686 }
687 subsys_initcall(intel_th_init);
688
689 static void __exit intel_th_exit(void)
690 {
691 intel_th_debug_done();
692
693 bus_unregister(&intel_th_bus);
694 }
695 module_exit(intel_th_exit);
696
697 MODULE_LICENSE("GPL v2");
698 MODULE_DESCRIPTION("Intel(R) Trace Hub controller driver");
699 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");
This page took 0.062737 seconds and 5 git commands to generate.