dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / hwtracing / intel_th / core.c
blobc6ec5c62b7a9e8d13261bfcdcac65487fb59344c
1 /*
2 * Intel(R) Trace Hub driver core
4 * Copyright (C) 2014-2015 Intel Corporation.
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.
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.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
28 #include "intel_th.h"
29 #include "debug.h"
31 static DEFINE_IDA(intel_th_ida);
33 static int intel_th_match(struct device *dev, struct device_driver *driver)
35 struct intel_th_driver *thdrv = to_intel_th_driver(driver);
36 struct intel_th_device *thdev = to_intel_th_device(dev);
38 if (thdev->type == INTEL_TH_SWITCH &&
39 (!thdrv->enable || !thdrv->disable))
40 return 0;
42 return !strcmp(thdev->name, driver->name);
45 static int intel_th_child_remove(struct device *dev, void *data)
47 device_release_driver(dev);
49 return 0;
52 static int intel_th_probe(struct device *dev)
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;
60 if (thdev->type == INTEL_TH_SWITCH)
61 hub = thdev;
62 else if (dev->parent)
63 hub = to_intel_th_device(dev->parent);
65 if (!hub || !hub->dev.driver)
66 return -EPROBE_DEFER;
68 hubdrv = to_intel_th_driver(hub->dev.driver);
70 ret = thdrv->probe(to_intel_th_device(dev));
71 if (ret)
72 return ret;
74 if (thdev->type == INTEL_TH_OUTPUT &&
75 !intel_th_output_assigned(thdev))
76 ret = hubdrv->assign(hub, thdev);
78 return ret;
81 static int intel_th_remove(struct device *dev)
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;
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;
94 thdrv->remove(thdev);
96 if (intel_th_output_assigned(thdev)) {
97 struct intel_th_driver *hubdrv =
98 to_intel_th_driver(dev->parent->driver);
100 if (hub->dev.driver)
101 hubdrv->unassign(hub, thdev);
104 return 0;
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,
115 static void intel_th_device_free(struct intel_th_device *thdev);
117 static void intel_th_device_release(struct device *dev)
119 intel_th_device_free(to_intel_th_device(dev));
122 static struct device_type intel_th_source_device_type = {
123 .name = "intel_th_source_device",
124 .release = intel_th_device_release,
127 static char *intel_th_output_devnode(struct device *dev, umode_t *mode,
128 kuid_t *uid, kgid_t *gid)
130 struct intel_th_device *thdev = to_intel_th_device(dev);
131 char *node;
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);
139 return node;
142 static ssize_t port_show(struct device *dev, struct device_attribute *attr,
143 char *buf)
145 struct intel_th_device *thdev = to_intel_th_device(dev);
147 if (thdev->output.port >= 0)
148 return scnprintf(buf, PAGE_SIZE, "%u\n", thdev->output.port);
150 return scnprintf(buf, PAGE_SIZE, "unassigned\n");
153 static DEVICE_ATTR_RO(port);
155 static int intel_th_output_activate(struct intel_th_device *thdev)
157 struct intel_th_driver *thdrv = to_intel_th_driver(thdev->dev.driver);
159 if (thdrv->activate)
160 return thdrv->activate(thdev);
162 intel_th_trace_enable(thdev);
164 return 0;
167 static void intel_th_output_deactivate(struct intel_th_device *thdev)
169 struct intel_th_driver *thdrv = to_intel_th_driver(thdev->dev.driver);
171 if (thdrv->deactivate)
172 thdrv->deactivate(thdev);
173 else
174 intel_th_trace_disable(thdev);
177 static ssize_t active_show(struct device *dev, struct device_attribute *attr,
178 char *buf)
180 struct intel_th_device *thdev = to_intel_th_device(dev);
182 return scnprintf(buf, PAGE_SIZE, "%d\n", thdev->output.active);
185 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
186 const char *buf, size_t size)
188 struct intel_th_device *thdev = to_intel_th_device(dev);
189 unsigned long val;
190 int ret;
192 ret = kstrtoul(buf, 10, &val);
193 if (ret)
194 return ret;
196 if (!!val != thdev->output.active) {
197 if (val)
198 ret = intel_th_output_activate(thdev);
199 else
200 intel_th_output_deactivate(thdev);
203 return ret ? ret : size;
206 static DEVICE_ATTR_RW(active);
208 static struct attribute *intel_th_output_attrs[] = {
209 &dev_attr_port.attr,
210 &dev_attr_active.attr,
211 NULL,
214 ATTRIBUTE_GROUPS(intel_th_output);
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,
223 static struct device_type intel_th_switch_device_type = {
224 .name = "intel_th_switch_device",
225 .release = intel_th_device_release,
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,
234 int intel_th_driver_register(struct intel_th_driver *thdrv)
236 if (!thdrv->probe || !thdrv->remove)
237 return -EINVAL;
239 thdrv->driver.bus = &intel_th_bus;
241 return driver_register(&thdrv->driver);
243 EXPORT_SYMBOL_GPL(intel_th_driver_register);
245 void intel_th_driver_unregister(struct intel_th_driver *thdrv)
247 driver_unregister(&thdrv->driver);
249 EXPORT_SYMBOL_GPL(intel_th_driver_unregister);
251 static struct intel_th_device *
252 intel_th_device_alloc(struct intel_th *th, unsigned int type, const char *name,
253 int id)
255 struct device *parent;
256 struct intel_th_device *thdev;
258 if (type == INTEL_TH_SWITCH)
259 parent = th->dev;
260 else
261 parent = &th->hub->dev;
263 thdev = kzalloc(sizeof(*thdev) + strlen(name) + 1, GFP_KERNEL);
264 if (!thdev)
265 return NULL;
267 thdev->id = id;
268 thdev->type = type;
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);
283 return thdev;
286 static int intel_th_device_add_resources(struct intel_th_device *thdev,
287 struct resource *res, int nres)
289 struct resource *r;
291 r = kmemdup(res, sizeof(*res) * nres, GFP_KERNEL);
292 if (!r)
293 return -ENOMEM;
295 thdev->resource = r;
296 thdev->num_resources = nres;
298 return 0;
301 static void intel_th_device_remove(struct intel_th_device *thdev)
303 device_del(&thdev->dev);
304 put_device(&thdev->dev);
307 static void intel_th_device_free(struct intel_th_device *thdev)
309 kfree(thdev->resource);
310 kfree(thdev);
314 * Intel(R) Trace Hub subdevices
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] = {
325 .nres = 1,
326 .res = {
328 .start = REG_GTH_OFFSET,
329 .end = REG_GTH_OFFSET + REG_GTH_LENGTH - 1,
330 .flags = IORESOURCE_MEM,
333 .name = "gth",
334 .type = INTEL_TH_SWITCH,
335 .id = -1,
338 .nres = 2,
339 .res = {
341 .start = REG_MSU_OFFSET,
342 .end = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
343 .flags = IORESOURCE_MEM,
346 .start = BUF_MSU_OFFSET,
347 .end = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
348 .flags = IORESOURCE_MEM,
351 .name = "msc",
352 .id = 0,
353 .type = INTEL_TH_OUTPUT,
354 .otype = GTH_MSU,
357 .nres = 2,
358 .res = {
360 .start = REG_MSU_OFFSET,
361 .end = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
362 .flags = IORESOURCE_MEM,
365 .start = BUF_MSU_OFFSET,
366 .end = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
367 .flags = IORESOURCE_MEM,
370 .name = "msc",
371 .id = 1,
372 .type = INTEL_TH_OUTPUT,
373 .otype = GTH_MSU,
376 .nres = 2,
377 .res = {
379 .start = REG_STH_OFFSET,
380 .end = REG_STH_OFFSET + REG_STH_LENGTH - 1,
381 .flags = IORESOURCE_MEM,
384 .start = TH_MMIO_SW,
385 .end = 0,
386 .flags = IORESOURCE_MEM,
389 .id = -1,
390 .name = "sth",
391 .type = INTEL_TH_SOURCE,
394 .nres = 1,
395 .res = {
397 .start = REG_PTI_OFFSET,
398 .end = REG_PTI_OFFSET + REG_PTI_LENGTH - 1,
399 .flags = IORESOURCE_MEM,
402 .id = -1,
403 .name = "pti",
404 .type = INTEL_TH_OUTPUT,
405 .otype = GTH_PTI,
408 .nres = 1,
409 .res = {
411 .start = REG_DCIH_OFFSET,
412 .end = REG_DCIH_OFFSET + REG_DCIH_LENGTH - 1,
413 .flags = IORESOURCE_MEM,
416 .id = -1,
417 .name = "dcih",
418 .type = INTEL_TH_OUTPUT,
422 #ifdef CONFIG_MODULES
423 static void __intel_th_request_hub_module(struct work_struct *work)
425 struct intel_th *th = container_of(work, struct intel_th,
426 request_module_work);
428 request_module("intel_th_%s", th->hub->name);
431 static int intel_th_request_hub_module(struct intel_th *th)
433 INIT_WORK(&th->request_module_work, __intel_th_request_hub_module);
434 schedule_work(&th->request_module_work);
436 return 0;
439 static void intel_th_request_hub_module_flush(struct intel_th *th)
441 flush_work(&th->request_module_work);
443 #else
444 static inline int intel_th_request_hub_module(struct intel_th *th)
446 return -EINVAL;
449 static inline void intel_th_request_hub_module_flush(struct intel_th *th)
452 #endif /* CONFIG_MODULES */
454 static int intel_th_populate(struct intel_th *th, struct resource *devres,
455 unsigned int ndevres, int irq)
457 struct resource res[3];
458 unsigned int req = 0;
459 int i, err;
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;
467 thdev = intel_th_device_alloc(th, subdev->type, subdev->name,
468 subdev->id);
469 if (!thdev) {
470 err = -ENOMEM;
471 goto kill_subdevs;
474 memcpy(res, subdev->res,
475 sizeof(struct resource) * subdev->nres);
477 for (r = 0; r < subdev->nres; r++) {
478 int bar = TH_MMIO_CONFIG;
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.
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;
491 if (res[r].flags & IORESOURCE_MEM) {
492 res[r].start += devres[bar].start;
493 res[r].end += devres[bar].start;
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;
502 err = intel_th_device_add_resources(thdev, res, subdev->nres);
503 if (err) {
504 put_device(&thdev->dev);
505 goto kill_subdevs;
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;
514 err = device_add(&thdev->dev);
515 if (err) {
516 put_device(&thdev->dev);
517 goto kill_subdevs;
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++;
528 th->thdev[i] = thdev;
531 return 0;
533 kill_subdevs:
534 for (i-- ; i >= 0; i--)
535 intel_th_device_remove(th->thdev[i]);
537 return err;
540 static int match_devt(struct device *dev, void *data)
542 dev_t devt = (dev_t)(unsigned long)data;
544 return dev->devt == devt;
547 static int intel_th_output_open(struct inode *inode, struct file *file)
549 const struct file_operations *fops;
550 struct intel_th_driver *thdrv;
551 struct device *dev;
552 int err;
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;
560 thdrv = to_intel_th_driver(dev->driver);
561 fops = fops_get(thdrv->fops);
562 if (!fops)
563 return -ENODEV;
565 replace_fops(file, fops);
567 file->private_data = to_intel_th_device(dev);
569 if (file->f_op->open) {
570 err = file->f_op->open(inode, file);
571 return err;
574 return 0;
577 static const struct file_operations intel_th_output_fops = {
578 .open = intel_th_output_open,
579 .llseek = noop_llseek,
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
589 struct intel_th *
590 intel_th_alloc(struct device *dev, struct resource *devres,
591 unsigned int ndevres, int irq)
593 struct intel_th *th;
594 int err;
596 th = kzalloc(sizeof(*th), GFP_KERNEL);
597 if (!th)
598 return ERR_PTR(-ENOMEM);
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;
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;
612 th->dev = dev;
614 err = intel_th_populate(th, devres, ndevres, irq);
615 if (err)
616 goto err_chrdev;
618 return th;
620 err_chrdev:
621 __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
622 "intel_th/output");
624 err_ida:
625 ida_simple_remove(&intel_th_ida, th->id);
627 err_alloc:
628 kfree(th);
630 return ERR_PTR(err);
632 EXPORT_SYMBOL_GPL(intel_th_alloc);
634 void intel_th_free(struct intel_th *th)
636 int i;
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]);
643 intel_th_device_remove(th->hub);
645 __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
646 "intel_th/output");
648 ida_simple_remove(&intel_th_ida, th->id);
650 kfree(th);
652 EXPORT_SYMBOL_GPL(intel_th_free);
655 * intel_th_trace_enable() - enable tracing for an output device
656 * @thdev: output device that requests tracing be enabled
658 int intel_th_trace_enable(struct intel_th_device *thdev)
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);
663 if (WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH))
664 return -EINVAL;
666 if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
667 return -EINVAL;
669 hubdrv->enable(hub, &thdev->output);
671 return 0;
673 EXPORT_SYMBOL_GPL(intel_th_trace_enable);
676 * intel_th_trace_disable() - disable tracing for an output device
677 * @thdev: output device that requests tracing be disabled
679 int intel_th_trace_disable(struct intel_th_device *thdev)
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);
684 WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH);
685 if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
686 return -EINVAL;
688 hubdrv->disable(hub, &thdev->output);
690 return 0;
692 EXPORT_SYMBOL_GPL(intel_th_trace_disable);
694 int intel_th_set_output(struct intel_th_device *thdev,
695 unsigned int master)
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);
700 if (!hubdrv->set_output)
701 return -ENOTSUPP;
703 return hubdrv->set_output(hub, master);
705 EXPORT_SYMBOL_GPL(intel_th_set_output);
707 static int __init intel_th_init(void)
709 intel_th_debug_init();
711 return bus_register(&intel_th_bus);
713 subsys_initcall(intel_th_init);
715 static void __exit intel_th_exit(void)
717 intel_th_debug_done();
719 bus_unregister(&intel_th_bus);
721 module_exit(intel_th_exit);
723 MODULE_LICENSE("GPL v2");
724 MODULE_DESCRIPTION("Intel(R) Trace Hub controller driver");
725 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");