Upgrade to 4.4.50-rt62
[kvmfornfv.git] / kernel / 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         int                     id;
323 } intel_th_subdevices[TH_SUBDEVICE_MAX] = {
324         {
325                 .nres   = 1,
326                 .res    = {
327                         {
328                                 .start  = REG_GTH_OFFSET,
329                                 .end    = REG_GTH_OFFSET + REG_GTH_LENGTH - 1,
330                                 .flags  = IORESOURCE_MEM,
331                         },
332                 },
333                 .name   = "gth",
334                 .type   = INTEL_TH_SWITCH,
335                 .id     = -1,
336         },
337         {
338                 .nres   = 2,
339                 .res    = {
340                         {
341                                 .start  = REG_MSU_OFFSET,
342                                 .end    = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
343                                 .flags  = IORESOURCE_MEM,
344                         },
345                         {
346                                 .start  = BUF_MSU_OFFSET,
347                                 .end    = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
348                                 .flags  = IORESOURCE_MEM,
349                         },
350                 },
351                 .name   = "msc",
352                 .id     = 0,
353                 .type   = INTEL_TH_OUTPUT,
354                 .otype  = GTH_MSU,
355         },
356         {
357                 .nres   = 2,
358                 .res    = {
359                         {
360                                 .start  = REG_MSU_OFFSET,
361                                 .end    = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
362                                 .flags  = IORESOURCE_MEM,
363                         },
364                         {
365                                 .start  = BUF_MSU_OFFSET,
366                                 .end    = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
367                                 .flags  = IORESOURCE_MEM,
368                         },
369                 },
370                 .name   = "msc",
371                 .id     = 1,
372                 .type   = INTEL_TH_OUTPUT,
373                 .otype  = GTH_MSU,
374         },
375         {
376                 .nres   = 2,
377                 .res    = {
378                         {
379                                 .start  = REG_STH_OFFSET,
380                                 .end    = REG_STH_OFFSET + REG_STH_LENGTH - 1,
381                                 .flags  = IORESOURCE_MEM,
382                         },
383                         {
384                                 .start  = TH_MMIO_SW,
385                                 .end    = 0,
386                                 .flags  = IORESOURCE_MEM,
387                         },
388                 },
389                 .id     = -1,
390                 .name   = "sth",
391                 .type   = INTEL_TH_SOURCE,
392         },
393         {
394                 .nres   = 1,
395                 .res    = {
396                         {
397                                 .start  = REG_PTI_OFFSET,
398                                 .end    = REG_PTI_OFFSET + REG_PTI_LENGTH - 1,
399                                 .flags  = IORESOURCE_MEM,
400                         },
401                 },
402                 .id     = -1,
403                 .name   = "pti",
404                 .type   = INTEL_TH_OUTPUT,
405                 .otype  = GTH_PTI,
406         },
407         {
408                 .nres   = 1,
409                 .res    = {
410                         {
411                                 .start  = REG_DCIH_OFFSET,
412                                 .end    = REG_DCIH_OFFSET + REG_DCIH_LENGTH - 1,
413                                 .flags  = IORESOURCE_MEM,
414                         },
415                 },
416                 .id     = -1,
417                 .name   = "dcih",
418                 .type   = INTEL_TH_OUTPUT,
419         },
420 };
421
422 #ifdef CONFIG_MODULES
423 static void __intel_th_request_hub_module(struct work_struct *work)
424 {
425         struct intel_th *th = container_of(work, struct intel_th,
426                                            request_module_work);
427
428         request_module("intel_th_%s", th->hub->name);
429 }
430
431 static int intel_th_request_hub_module(struct intel_th *th)
432 {
433         INIT_WORK(&th->request_module_work, __intel_th_request_hub_module);
434         schedule_work(&th->request_module_work);
435
436         return 0;
437 }
438
439 static void intel_th_request_hub_module_flush(struct intel_th *th)
440 {
441         flush_work(&th->request_module_work);
442 }
443 #else
444 static inline int intel_th_request_hub_module(struct intel_th *th)
445 {
446         return -EINVAL;
447 }
448
449 static inline void intel_th_request_hub_module_flush(struct intel_th *th)
450 {
451 }
452 #endif /* CONFIG_MODULES */
453
454 static int intel_th_populate(struct intel_th *th, struct resource *devres,
455                              unsigned int ndevres, int irq)
456 {
457         struct resource res[3];
458         unsigned int req = 0;
459         int i, err;
460
461         /* create devices for each intel_th_subdevice */
462         for (i = 0; i < ARRAY_SIZE(intel_th_subdevices); i++) {
463                 struct intel_th_subdevice *subdev = &intel_th_subdevices[i];
464                 struct intel_th_device *thdev;
465                 int r;
466
467                 thdev = intel_th_device_alloc(th, subdev->type, subdev->name,
468                                               subdev->id);
469                 if (!thdev) {
470                         err = -ENOMEM;
471                         goto kill_subdevs;
472                 }
473
474                 memcpy(res, subdev->res,
475                        sizeof(struct resource) * subdev->nres);
476
477                 for (r = 0; r < subdev->nres; r++) {
478                         int bar = TH_MMIO_CONFIG;
479
480                         /*
481                          * Take .end == 0 to mean 'take the whole bar',
482                          * .start then tells us which bar it is. Default to
483                          * TH_MMIO_CONFIG.
484                          */
485                         if (!res[r].end && res[r].flags == IORESOURCE_MEM) {
486                                 bar = res[r].start;
487                                 res[r].start = 0;
488                                 res[r].end = resource_size(&devres[bar]) - 1;
489                         }
490
491                         if (res[r].flags & IORESOURCE_MEM) {
492                                 res[r].start    += devres[bar].start;
493                                 res[r].end      += devres[bar].start;
494
495                                 dev_dbg(th->dev, "%s:%d @ %pR\n",
496                                         subdev->name, r, &res[r]);
497                         } else if (res[r].flags & IORESOURCE_IRQ) {
498                                 res[r].start    = irq;
499                         }
500                 }
501
502                 err = intel_th_device_add_resources(thdev, res, subdev->nres);
503                 if (err) {
504                         put_device(&thdev->dev);
505                         goto kill_subdevs;
506                 }
507
508                 if (subdev->type == INTEL_TH_OUTPUT) {
509                         thdev->dev.devt = MKDEV(th->major, i);
510                         thdev->output.type = subdev->otype;
511                         thdev->output.port = -1;
512                 }
513
514                 err = device_add(&thdev->dev);
515                 if (err) {
516                         put_device(&thdev->dev);
517                         goto kill_subdevs;
518                 }
519
520                 /* need switch driver to be loaded to enumerate the rest */
521                 if (subdev->type == INTEL_TH_SWITCH && !req) {
522                         th->hub = thdev;
523                         err = intel_th_request_hub_module(th);
524                         if (!err)
525                                 req++;
526                 }
527
528                 th->thdev[i] = thdev;
529         }
530
531         return 0;
532
533 kill_subdevs:
534         for (i-- ; i >= 0; i--)
535                 intel_th_device_remove(th->thdev[i]);
536
537         return err;
538 }
539
540 static int match_devt(struct device *dev, void *data)
541 {
542         dev_t devt = (dev_t)(unsigned long)data;
543
544         return dev->devt == devt;
545 }
546
547 static int intel_th_output_open(struct inode *inode, struct file *file)
548 {
549         const struct file_operations *fops;
550         struct intel_th_driver *thdrv;
551         struct device *dev;
552         int err;
553
554         dev = bus_find_device(&intel_th_bus, NULL,
555                               (void *)(unsigned long)inode->i_rdev,
556                               match_devt);
557         if (!dev || !dev->driver)
558                 return -ENODEV;
559
560         thdrv = to_intel_th_driver(dev->driver);
561         fops = fops_get(thdrv->fops);
562         if (!fops)
563                 return -ENODEV;
564
565         replace_fops(file, fops);
566
567         file->private_data = to_intel_th_device(dev);
568
569         if (file->f_op->open) {
570                 err = file->f_op->open(inode, file);
571                 return err;
572         }
573
574         return 0;
575 }
576
577 static const struct file_operations intel_th_output_fops = {
578         .open   = intel_th_output_open,
579         .llseek = noop_llseek,
580 };
581
582 /**
583  * intel_th_alloc() - allocate a new Intel TH device and its subdevices
584  * @dev:        parent device
585  * @devres:     parent's resources
586  * @ndevres:    number of resources
587  * @irq:        irq number
588  */
589 struct intel_th *
590 intel_th_alloc(struct device *dev, struct resource *devres,
591                unsigned int ndevres, int irq)
592 {
593         struct intel_th *th;
594         int err;
595
596         th = kzalloc(sizeof(*th), GFP_KERNEL);
597         if (!th)
598                 return ERR_PTR(-ENOMEM);
599
600         th->id = ida_simple_get(&intel_th_ida, 0, 0, GFP_KERNEL);
601         if (th->id < 0) {
602                 err = th->id;
603                 goto err_alloc;
604         }
605
606         th->major = __register_chrdev(0, 0, TH_POSSIBLE_OUTPUTS,
607                                       "intel_th/output", &intel_th_output_fops);
608         if (th->major < 0) {
609                 err = th->major;
610                 goto err_ida;
611         }
612         th->dev = dev;
613
614         err = intel_th_populate(th, devres, ndevres, irq);
615         if (err)
616                 goto err_chrdev;
617
618         return th;
619
620 err_chrdev:
621         __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
622                             "intel_th/output");
623
624 err_ida:
625         ida_simple_remove(&intel_th_ida, th->id);
626
627 err_alloc:
628         kfree(th);
629
630         return ERR_PTR(err);
631 }
632 EXPORT_SYMBOL_GPL(intel_th_alloc);
633
634 void intel_th_free(struct intel_th *th)
635 {
636         int i;
637
638         intel_th_request_hub_module_flush(th);
639         for (i = 0; i < TH_SUBDEVICE_MAX; i++)
640                 if (th->thdev[i] != th->hub)
641                         intel_th_device_remove(th->thdev[i]);
642
643         intel_th_device_remove(th->hub);
644
645         __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
646                             "intel_th/output");
647
648         ida_simple_remove(&intel_th_ida, th->id);
649
650         kfree(th);
651 }
652 EXPORT_SYMBOL_GPL(intel_th_free);
653
654 /**
655  * intel_th_trace_enable() - enable tracing for an output device
656  * @thdev:      output device that requests tracing be enabled
657  */
658 int intel_th_trace_enable(struct intel_th_device *thdev)
659 {
660         struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
661         struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
662
663         if (WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH))
664                 return -EINVAL;
665
666         if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
667                 return -EINVAL;
668
669         hubdrv->enable(hub, &thdev->output);
670
671         return 0;
672 }
673 EXPORT_SYMBOL_GPL(intel_th_trace_enable);
674
675 /**
676  * intel_th_trace_disable() - disable tracing for an output device
677  * @thdev:      output device that requests tracing be disabled
678  */
679 int intel_th_trace_disable(struct intel_th_device *thdev)
680 {
681         struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
682         struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
683
684         WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH);
685         if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
686                 return -EINVAL;
687
688         hubdrv->disable(hub, &thdev->output);
689
690         return 0;
691 }
692 EXPORT_SYMBOL_GPL(intel_th_trace_disable);
693
694 int intel_th_set_output(struct intel_th_device *thdev,
695                         unsigned int master)
696 {
697         struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
698         struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
699
700         if (!hubdrv->set_output)
701                 return -ENOTSUPP;
702
703         return hubdrv->set_output(hub, master);
704 }
705 EXPORT_SYMBOL_GPL(intel_th_set_output);
706
707 static int __init intel_th_init(void)
708 {
709         intel_th_debug_init();
710
711         return bus_register(&intel_th_bus);
712 }
713 subsys_initcall(intel_th_init);
714
715 static void __exit intel_th_exit(void)
716 {
717         intel_th_debug_done();
718
719         bus_unregister(&intel_th_bus);
720 }
721 module_exit(intel_th_exit);
722
723 MODULE_LICENSE("GPL v2");
724 MODULE_DESCRIPTION("Intel(R) Trace Hub controller driver");
725 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");