0001
0002
0003
0004
0005
0006
0007
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009
0010 #include <linux/types.h>
0011 #include <linux/module.h>
0012 #include <linux/device.h>
0013 #include <linux/sysfs.h>
0014 #include <linux/kdev_t.h>
0015 #include <linux/debugfs.h>
0016 #include <linux/idr.h>
0017 #include <linux/pci.h>
0018 #include <linux/pm_runtime.h>
0019 #include <linux/dma-mapping.h>
0020
0021 #include "intel_th.h"
0022 #include "debug.h"
0023
0024 static bool host_mode __read_mostly;
0025 module_param(host_mode, bool, 0444);
0026
0027 static DEFINE_IDA(intel_th_ida);
0028
0029 static int intel_th_match(struct device *dev, struct device_driver *driver)
0030 {
0031 struct intel_th_driver *thdrv = to_intel_th_driver(driver);
0032 struct intel_th_device *thdev = to_intel_th_device(dev);
0033
0034 if (thdev->type == INTEL_TH_SWITCH &&
0035 (!thdrv->enable || !thdrv->disable))
0036 return 0;
0037
0038 return !strcmp(thdev->name, driver->name);
0039 }
0040
0041 static int intel_th_child_remove(struct device *dev, void *data)
0042 {
0043 device_release_driver(dev);
0044
0045 return 0;
0046 }
0047
0048 static int intel_th_probe(struct device *dev)
0049 {
0050 struct intel_th_driver *thdrv = to_intel_th_driver(dev->driver);
0051 struct intel_th_device *thdev = to_intel_th_device(dev);
0052 struct intel_th_driver *hubdrv;
0053 struct intel_th_device *hub = NULL;
0054 int ret;
0055
0056 if (thdev->type == INTEL_TH_SWITCH)
0057 hub = thdev;
0058 else if (dev->parent)
0059 hub = to_intel_th_device(dev->parent);
0060
0061 if (!hub || !hub->dev.driver)
0062 return -EPROBE_DEFER;
0063
0064 hubdrv = to_intel_th_driver(hub->dev.driver);
0065
0066 pm_runtime_set_active(dev);
0067 pm_runtime_no_callbacks(dev);
0068 pm_runtime_enable(dev);
0069
0070 ret = thdrv->probe(to_intel_th_device(dev));
0071 if (ret)
0072 goto out_pm;
0073
0074 if (thdrv->attr_group) {
0075 ret = sysfs_create_group(&thdev->dev.kobj, thdrv->attr_group);
0076 if (ret)
0077 goto out;
0078 }
0079
0080 if (thdev->type == INTEL_TH_OUTPUT &&
0081 !intel_th_output_assigned(thdev))
0082
0083 ret = hubdrv->assign(hub, thdev);
0084
0085 out:
0086 if (ret)
0087 thdrv->remove(thdev);
0088
0089 out_pm:
0090 if (ret)
0091 pm_runtime_disable(dev);
0092
0093 return ret;
0094 }
0095
0096 static void intel_th_device_remove(struct intel_th_device *thdev);
0097
0098 static void intel_th_remove(struct device *dev)
0099 {
0100 struct intel_th_driver *thdrv = to_intel_th_driver(dev->driver);
0101 struct intel_th_device *thdev = to_intel_th_device(dev);
0102 struct intel_th_device *hub = to_intel_th_hub(thdev);
0103
0104 if (thdev->type == INTEL_TH_SWITCH) {
0105 struct intel_th *th = to_intel_th(hub);
0106 int i, lowest;
0107
0108
0109
0110
0111
0112
0113
0114 device_for_each_child(dev, thdev, intel_th_child_remove);
0115
0116
0117
0118
0119
0120
0121 for (i = 0, lowest = -1; i < th->num_thdevs; i++) {
0122
0123
0124
0125
0126
0127 if (th->thdev[i]->type != INTEL_TH_OUTPUT) {
0128 if (lowest >= 0) {
0129 th->thdev[lowest] = th->thdev[i];
0130 th->thdev[i] = NULL;
0131 ++lowest;
0132 }
0133
0134 continue;
0135 }
0136
0137 if (lowest == -1)
0138 lowest = i;
0139
0140 intel_th_device_remove(th->thdev[i]);
0141 th->thdev[i] = NULL;
0142 }
0143
0144 if (lowest >= 0)
0145 th->num_thdevs = lowest;
0146 }
0147
0148 if (thdrv->attr_group)
0149 sysfs_remove_group(&thdev->dev.kobj, thdrv->attr_group);
0150
0151 pm_runtime_get_sync(dev);
0152
0153 thdrv->remove(thdev);
0154
0155 if (intel_th_output_assigned(thdev)) {
0156 struct intel_th_driver *hubdrv =
0157 to_intel_th_driver(dev->parent->driver);
0158
0159 if (hub->dev.driver)
0160
0161 hubdrv->unassign(hub, thdev);
0162 }
0163
0164 pm_runtime_disable(dev);
0165 pm_runtime_set_active(dev);
0166 pm_runtime_enable(dev);
0167 }
0168
0169 static struct bus_type intel_th_bus = {
0170 .name = "intel_th",
0171 .match = intel_th_match,
0172 .probe = intel_th_probe,
0173 .remove = intel_th_remove,
0174 };
0175
0176 static void intel_th_device_free(struct intel_th_device *thdev);
0177
0178 static void intel_th_device_release(struct device *dev)
0179 {
0180 intel_th_device_free(to_intel_th_device(dev));
0181 }
0182
0183 static struct device_type intel_th_source_device_type = {
0184 .name = "intel_th_source_device",
0185 .release = intel_th_device_release,
0186 };
0187
0188 static char *intel_th_output_devnode(struct device *dev, umode_t *mode,
0189 kuid_t *uid, kgid_t *gid)
0190 {
0191 struct intel_th_device *thdev = to_intel_th_device(dev);
0192 struct intel_th *th = to_intel_th(thdev);
0193 char *node;
0194
0195 if (thdev->id >= 0)
0196 node = kasprintf(GFP_KERNEL, "intel_th%d/%s%d", th->id,
0197 thdev->name, thdev->id);
0198 else
0199 node = kasprintf(GFP_KERNEL, "intel_th%d/%s", th->id,
0200 thdev->name);
0201
0202 return node;
0203 }
0204
0205 static ssize_t port_show(struct device *dev, struct device_attribute *attr,
0206 char *buf)
0207 {
0208 struct intel_th_device *thdev = to_intel_th_device(dev);
0209
0210 if (thdev->output.port >= 0)
0211 return scnprintf(buf, PAGE_SIZE, "%u\n", thdev->output.port);
0212
0213 return scnprintf(buf, PAGE_SIZE, "unassigned\n");
0214 }
0215
0216 static DEVICE_ATTR_RO(port);
0217
0218 static void intel_th_trace_prepare(struct intel_th_device *thdev)
0219 {
0220 struct intel_th_device *hub = to_intel_th_hub(thdev);
0221 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
0222
0223 if (hub->type != INTEL_TH_SWITCH)
0224 return;
0225
0226 if (thdev->type != INTEL_TH_OUTPUT)
0227 return;
0228
0229 pm_runtime_get_sync(&thdev->dev);
0230 hubdrv->prepare(hub, &thdev->output);
0231 pm_runtime_put(&thdev->dev);
0232 }
0233
0234 static int intel_th_output_activate(struct intel_th_device *thdev)
0235 {
0236 struct intel_th_driver *thdrv =
0237 to_intel_th_driver_or_null(thdev->dev.driver);
0238 struct intel_th *th = to_intel_th(thdev);
0239 int ret = 0;
0240
0241 if (!thdrv)
0242 return -ENODEV;
0243
0244 if (!try_module_get(thdrv->driver.owner))
0245 return -ENODEV;
0246
0247 pm_runtime_get_sync(&thdev->dev);
0248
0249 if (th->activate)
0250 ret = th->activate(th);
0251 if (ret)
0252 goto fail_put;
0253
0254 intel_th_trace_prepare(thdev);
0255 if (thdrv->activate)
0256 ret = thdrv->activate(thdev);
0257 else
0258 intel_th_trace_enable(thdev);
0259
0260 if (ret)
0261 goto fail_deactivate;
0262
0263 return 0;
0264
0265 fail_deactivate:
0266 if (th->deactivate)
0267 th->deactivate(th);
0268
0269 fail_put:
0270 pm_runtime_put(&thdev->dev);
0271 module_put(thdrv->driver.owner);
0272
0273 return ret;
0274 }
0275
0276 static void intel_th_output_deactivate(struct intel_th_device *thdev)
0277 {
0278 struct intel_th_driver *thdrv =
0279 to_intel_th_driver_or_null(thdev->dev.driver);
0280 struct intel_th *th = to_intel_th(thdev);
0281
0282 if (!thdrv)
0283 return;
0284
0285 if (thdrv->deactivate)
0286 thdrv->deactivate(thdev);
0287 else
0288 intel_th_trace_disable(thdev);
0289
0290 if (th->deactivate)
0291 th->deactivate(th);
0292
0293 pm_runtime_put(&thdev->dev);
0294 module_put(thdrv->driver.owner);
0295 }
0296
0297 static ssize_t active_show(struct device *dev, struct device_attribute *attr,
0298 char *buf)
0299 {
0300 struct intel_th_device *thdev = to_intel_th_device(dev);
0301
0302 return scnprintf(buf, PAGE_SIZE, "%d\n", thdev->output.active);
0303 }
0304
0305 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
0306 const char *buf, size_t size)
0307 {
0308 struct intel_th_device *thdev = to_intel_th_device(dev);
0309 unsigned long val;
0310 int ret;
0311
0312 ret = kstrtoul(buf, 10, &val);
0313 if (ret)
0314 return ret;
0315
0316 if (!!val != thdev->output.active) {
0317 if (val)
0318 ret = intel_th_output_activate(thdev);
0319 else
0320 intel_th_output_deactivate(thdev);
0321 }
0322
0323 return ret ? ret : size;
0324 }
0325
0326 static DEVICE_ATTR_RW(active);
0327
0328 static struct attribute *intel_th_output_attrs[] = {
0329 &dev_attr_port.attr,
0330 &dev_attr_active.attr,
0331 NULL,
0332 };
0333
0334 ATTRIBUTE_GROUPS(intel_th_output);
0335
0336 static struct device_type intel_th_output_device_type = {
0337 .name = "intel_th_output_device",
0338 .groups = intel_th_output_groups,
0339 .release = intel_th_device_release,
0340 .devnode = intel_th_output_devnode,
0341 };
0342
0343 static struct device_type intel_th_switch_device_type = {
0344 .name = "intel_th_switch_device",
0345 .release = intel_th_device_release,
0346 };
0347
0348 static struct device_type *intel_th_device_type[] = {
0349 [INTEL_TH_SOURCE] = &intel_th_source_device_type,
0350 [INTEL_TH_OUTPUT] = &intel_th_output_device_type,
0351 [INTEL_TH_SWITCH] = &intel_th_switch_device_type,
0352 };
0353
0354 int intel_th_driver_register(struct intel_th_driver *thdrv)
0355 {
0356 if (!thdrv->probe || !thdrv->remove)
0357 return -EINVAL;
0358
0359 thdrv->driver.bus = &intel_th_bus;
0360
0361 return driver_register(&thdrv->driver);
0362 }
0363 EXPORT_SYMBOL_GPL(intel_th_driver_register);
0364
0365 void intel_th_driver_unregister(struct intel_th_driver *thdrv)
0366 {
0367 driver_unregister(&thdrv->driver);
0368 }
0369 EXPORT_SYMBOL_GPL(intel_th_driver_unregister);
0370
0371 static struct intel_th_device *
0372 intel_th_device_alloc(struct intel_th *th, unsigned int type, const char *name,
0373 int id)
0374 {
0375 struct device *parent;
0376 struct intel_th_device *thdev;
0377
0378 if (type == INTEL_TH_OUTPUT)
0379 parent = &th->hub->dev;
0380 else
0381 parent = th->dev;
0382
0383 thdev = kzalloc(sizeof(*thdev) + strlen(name) + 1, GFP_KERNEL);
0384 if (!thdev)
0385 return NULL;
0386
0387 thdev->id = id;
0388 thdev->type = type;
0389
0390 strcpy(thdev->name, name);
0391 device_initialize(&thdev->dev);
0392 thdev->dev.bus = &intel_th_bus;
0393 thdev->dev.type = intel_th_device_type[type];
0394 thdev->dev.parent = parent;
0395 thdev->dev.dma_mask = parent->dma_mask;
0396 thdev->dev.dma_parms = parent->dma_parms;
0397 dma_set_coherent_mask(&thdev->dev, parent->coherent_dma_mask);
0398 if (id >= 0)
0399 dev_set_name(&thdev->dev, "%d-%s%d", th->id, name, id);
0400 else
0401 dev_set_name(&thdev->dev, "%d-%s", th->id, name);
0402
0403 return thdev;
0404 }
0405
0406 static int intel_th_device_add_resources(struct intel_th_device *thdev,
0407 struct resource *res, int nres)
0408 {
0409 struct resource *r;
0410
0411 r = kmemdup(res, sizeof(*res) * nres, GFP_KERNEL);
0412 if (!r)
0413 return -ENOMEM;
0414
0415 thdev->resource = r;
0416 thdev->num_resources = nres;
0417
0418 return 0;
0419 }
0420
0421 static void intel_th_device_remove(struct intel_th_device *thdev)
0422 {
0423 device_del(&thdev->dev);
0424 put_device(&thdev->dev);
0425 }
0426
0427 static void intel_th_device_free(struct intel_th_device *thdev)
0428 {
0429 kfree(thdev->resource);
0430 kfree(thdev);
0431 }
0432
0433
0434
0435
0436 static const struct intel_th_subdevice {
0437 const char *name;
0438 struct resource res[3];
0439 unsigned nres;
0440 unsigned type;
0441 unsigned otype;
0442 bool mknode;
0443 unsigned scrpd;
0444 int id;
0445 } intel_th_subdevices[] = {
0446 {
0447 .nres = 1,
0448 .res = {
0449 {
0450
0451 .start = REG_GTH_OFFSET,
0452 .end = REG_CTS_OFFSET + REG_CTS_LENGTH - 1,
0453 .flags = IORESOURCE_MEM,
0454 },
0455 },
0456 .name = "gth",
0457 .type = INTEL_TH_SWITCH,
0458 .id = -1,
0459 },
0460 {
0461 .nres = 2,
0462 .res = {
0463 {
0464 .start = REG_MSU_OFFSET,
0465 .end = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
0466 .flags = IORESOURCE_MEM,
0467 },
0468 {
0469 .start = BUF_MSU_OFFSET,
0470 .end = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
0471 .flags = IORESOURCE_MEM,
0472 },
0473 },
0474 .name = "msc",
0475 .id = 0,
0476 .type = INTEL_TH_OUTPUT,
0477 .mknode = true,
0478 .otype = GTH_MSU,
0479 .scrpd = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC0_IS_ENABLED,
0480 },
0481 {
0482 .nres = 2,
0483 .res = {
0484 {
0485 .start = REG_MSU_OFFSET,
0486 .end = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
0487 .flags = IORESOURCE_MEM,
0488 },
0489 {
0490 .start = BUF_MSU_OFFSET,
0491 .end = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
0492 .flags = IORESOURCE_MEM,
0493 },
0494 },
0495 .name = "msc",
0496 .id = 1,
0497 .type = INTEL_TH_OUTPUT,
0498 .mknode = true,
0499 .otype = GTH_MSU,
0500 .scrpd = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC1_IS_ENABLED,
0501 },
0502 {
0503 .nres = 2,
0504 .res = {
0505 {
0506 .start = REG_STH_OFFSET,
0507 .end = REG_STH_OFFSET + REG_STH_LENGTH - 1,
0508 .flags = IORESOURCE_MEM,
0509 },
0510 {
0511 .start = TH_MMIO_SW,
0512 .end = 0,
0513 .flags = IORESOURCE_MEM,
0514 },
0515 },
0516 .id = -1,
0517 .name = "sth",
0518 .type = INTEL_TH_SOURCE,
0519 },
0520 {
0521 .nres = 2,
0522 .res = {
0523 {
0524 .start = REG_STH_OFFSET,
0525 .end = REG_STH_OFFSET + REG_STH_LENGTH - 1,
0526 .flags = IORESOURCE_MEM,
0527 },
0528 {
0529 .start = TH_MMIO_RTIT,
0530 .end = 0,
0531 .flags = IORESOURCE_MEM,
0532 },
0533 },
0534 .id = -1,
0535 .name = "rtit",
0536 .type = INTEL_TH_SOURCE,
0537 },
0538 {
0539 .nres = 1,
0540 .res = {
0541 {
0542 .start = REG_PTI_OFFSET,
0543 .end = REG_PTI_OFFSET + REG_PTI_LENGTH - 1,
0544 .flags = IORESOURCE_MEM,
0545 },
0546 },
0547 .id = -1,
0548 .name = "pti",
0549 .type = INTEL_TH_OUTPUT,
0550 .otype = GTH_PTI,
0551 .scrpd = SCRPD_PTI_IS_PRIM_DEST,
0552 },
0553 {
0554 .nres = 1,
0555 .res = {
0556 {
0557 .start = REG_PTI_OFFSET,
0558 .end = REG_PTI_OFFSET + REG_PTI_LENGTH - 1,
0559 .flags = IORESOURCE_MEM,
0560 },
0561 },
0562 .id = -1,
0563 .name = "lpp",
0564 .type = INTEL_TH_OUTPUT,
0565 .otype = GTH_LPP,
0566 .scrpd = SCRPD_PTI_IS_PRIM_DEST,
0567 },
0568 {
0569 .nres = 1,
0570 .res = {
0571 {
0572 .start = REG_DCIH_OFFSET,
0573 .end = REG_DCIH_OFFSET + REG_DCIH_LENGTH - 1,
0574 .flags = IORESOURCE_MEM,
0575 },
0576 },
0577 .id = -1,
0578 .name = "dcih",
0579 .type = INTEL_TH_OUTPUT,
0580 },
0581 };
0582
0583 #ifdef CONFIG_MODULES
0584 static void __intel_th_request_hub_module(struct work_struct *work)
0585 {
0586 struct intel_th *th = container_of(work, struct intel_th,
0587 request_module_work);
0588
0589 request_module("intel_th_%s", th->hub->name);
0590 }
0591
0592 static int intel_th_request_hub_module(struct intel_th *th)
0593 {
0594 INIT_WORK(&th->request_module_work, __intel_th_request_hub_module);
0595 schedule_work(&th->request_module_work);
0596
0597 return 0;
0598 }
0599
0600 static void intel_th_request_hub_module_flush(struct intel_th *th)
0601 {
0602 flush_work(&th->request_module_work);
0603 }
0604 #else
0605 static inline int intel_th_request_hub_module(struct intel_th *th)
0606 {
0607 return -EINVAL;
0608 }
0609
0610 static inline void intel_th_request_hub_module_flush(struct intel_th *th)
0611 {
0612 }
0613 #endif
0614
0615 static struct intel_th_device *
0616 intel_th_subdevice_alloc(struct intel_th *th,
0617 const struct intel_th_subdevice *subdev)
0618 {
0619 struct intel_th_device *thdev;
0620 struct resource res[3];
0621 unsigned int req = 0;
0622 int r, err;
0623
0624 thdev = intel_th_device_alloc(th, subdev->type, subdev->name,
0625 subdev->id);
0626 if (!thdev)
0627 return ERR_PTR(-ENOMEM);
0628
0629 thdev->drvdata = th->drvdata;
0630
0631 memcpy(res, subdev->res,
0632 sizeof(struct resource) * subdev->nres);
0633
0634 for (r = 0; r < subdev->nres; r++) {
0635 struct resource *devres = th->resource;
0636 int bar = TH_MMIO_CONFIG;
0637
0638
0639
0640
0641
0642
0643 if (!res[r].end && res[r].flags == IORESOURCE_MEM) {
0644 bar = res[r].start;
0645 err = -ENODEV;
0646 if (bar >= th->num_resources)
0647 goto fail_put_device;
0648 res[r].start = 0;
0649 res[r].end = resource_size(&devres[bar]) - 1;
0650 }
0651
0652 if (res[r].flags & IORESOURCE_MEM) {
0653 res[r].start += devres[bar].start;
0654 res[r].end += devres[bar].start;
0655
0656 dev_dbg(th->dev, "%s:%d @ %pR\n",
0657 subdev->name, r, &res[r]);
0658 } else if (res[r].flags & IORESOURCE_IRQ) {
0659
0660
0661
0662
0663 if (INTEL_TH_CAP(th, has_mintctl) && th->irq != -1)
0664 res[r].start = th->irq;
0665 }
0666 }
0667
0668 err = intel_th_device_add_resources(thdev, res, subdev->nres);
0669 if (err)
0670 goto fail_put_device;
0671
0672 if (subdev->type == INTEL_TH_OUTPUT) {
0673 if (subdev->mknode)
0674 thdev->dev.devt = MKDEV(th->major, th->num_thdevs);
0675 thdev->output.type = subdev->otype;
0676 thdev->output.port = -1;
0677 thdev->output.scratchpad = subdev->scrpd;
0678 } else if (subdev->type == INTEL_TH_SWITCH) {
0679 thdev->host_mode =
0680 INTEL_TH_CAP(th, host_mode_only) ? true : host_mode;
0681 th->hub = thdev;
0682 }
0683
0684 err = device_add(&thdev->dev);
0685 if (err)
0686 goto fail_free_res;
0687
0688
0689 if (subdev->type == INTEL_TH_SWITCH && !req) {
0690 err = intel_th_request_hub_module(th);
0691 if (!err)
0692 req++;
0693 }
0694
0695 return thdev;
0696
0697 fail_free_res:
0698 kfree(thdev->resource);
0699
0700 fail_put_device:
0701 put_device(&thdev->dev);
0702
0703 return ERR_PTR(err);
0704 }
0705
0706
0707
0708
0709
0710
0711
0712
0713
0714
0715 int intel_th_output_enable(struct intel_th *th, unsigned int otype)
0716 {
0717 struct intel_th_device *thdev;
0718 int src = 0, dst = 0;
0719
0720 for (src = 0, dst = 0; dst <= th->num_thdevs; src++, dst++) {
0721 for (; src < ARRAY_SIZE(intel_th_subdevices); src++) {
0722 if (intel_th_subdevices[src].type != INTEL_TH_OUTPUT)
0723 continue;
0724
0725 if (intel_th_subdevices[src].otype != otype)
0726 continue;
0727
0728 break;
0729 }
0730
0731
0732 if (src == ARRAY_SIZE(intel_th_subdevices))
0733 return -ENODEV;
0734
0735 for (; dst < th->num_thdevs; dst++) {
0736 if (th->thdev[dst]->type != INTEL_TH_OUTPUT)
0737 continue;
0738
0739 if (th->thdev[dst]->output.type != otype)
0740 continue;
0741
0742 break;
0743 }
0744
0745
0746
0747
0748
0749 if (dst == th->num_thdevs)
0750 goto found;
0751 }
0752
0753 return -ENODEV;
0754
0755 found:
0756 thdev = intel_th_subdevice_alloc(th, &intel_th_subdevices[src]);
0757 if (IS_ERR(thdev))
0758 return PTR_ERR(thdev);
0759
0760 th->thdev[th->num_thdevs++] = thdev;
0761
0762 return 0;
0763 }
0764 EXPORT_SYMBOL_GPL(intel_th_output_enable);
0765
0766 static int intel_th_populate(struct intel_th *th)
0767 {
0768 int src;
0769
0770
0771 for (src = 0; src < ARRAY_SIZE(intel_th_subdevices); src++) {
0772 const struct intel_th_subdevice *subdev =
0773 &intel_th_subdevices[src];
0774 struct intel_th_device *thdev;
0775
0776
0777 if ((INTEL_TH_CAP(th, host_mode_only) || host_mode) &&
0778 subdev->type == INTEL_TH_OUTPUT)
0779 continue;
0780
0781
0782
0783
0784
0785 if (subdev->type == INTEL_TH_OUTPUT &&
0786 subdev->otype != GTH_NONE)
0787 continue;
0788
0789 thdev = intel_th_subdevice_alloc(th, subdev);
0790
0791 if (IS_ERR(thdev)) {
0792
0793 if (PTR_ERR(thdev) == -ENODEV)
0794 continue;
0795
0796 return PTR_ERR(thdev);
0797 }
0798
0799 th->thdev[th->num_thdevs++] = thdev;
0800 }
0801
0802 return 0;
0803 }
0804
0805 static int intel_th_output_open(struct inode *inode, struct file *file)
0806 {
0807 const struct file_operations *fops;
0808 struct intel_th_driver *thdrv;
0809 struct device *dev;
0810 int err;
0811
0812 dev = bus_find_device_by_devt(&intel_th_bus, inode->i_rdev);
0813 if (!dev || !dev->driver)
0814 return -ENODEV;
0815
0816 thdrv = to_intel_th_driver(dev->driver);
0817 fops = fops_get(thdrv->fops);
0818 if (!fops)
0819 return -ENODEV;
0820
0821 replace_fops(file, fops);
0822
0823 file->private_data = to_intel_th_device(dev);
0824
0825 if (file->f_op->open) {
0826 err = file->f_op->open(inode, file);
0827 return err;
0828 }
0829
0830 return 0;
0831 }
0832
0833 static const struct file_operations intel_th_output_fops = {
0834 .open = intel_th_output_open,
0835 .llseek = noop_llseek,
0836 };
0837
0838 static irqreturn_t intel_th_irq(int irq, void *data)
0839 {
0840 struct intel_th *th = data;
0841 irqreturn_t ret = IRQ_NONE;
0842 struct intel_th_driver *d;
0843 int i;
0844
0845 for (i = 0; i < th->num_thdevs; i++) {
0846 if (th->thdev[i]->type != INTEL_TH_OUTPUT)
0847 continue;
0848
0849 d = to_intel_th_driver(th->thdev[i]->dev.driver);
0850 if (d && d->irq)
0851 ret |= d->irq(th->thdev[i]);
0852 }
0853
0854 return ret;
0855 }
0856
0857
0858
0859
0860
0861
0862
0863 struct intel_th *
0864 intel_th_alloc(struct device *dev, const struct intel_th_drvdata *drvdata,
0865 struct resource *devres, unsigned int ndevres)
0866 {
0867 int err, r, nr_mmios = 0;
0868 struct intel_th *th;
0869
0870 th = kzalloc(sizeof(*th), GFP_KERNEL);
0871 if (!th)
0872 return ERR_PTR(-ENOMEM);
0873
0874 th->id = ida_simple_get(&intel_th_ida, 0, 0, GFP_KERNEL);
0875 if (th->id < 0) {
0876 err = th->id;
0877 goto err_alloc;
0878 }
0879
0880 th->major = __register_chrdev(0, 0, TH_POSSIBLE_OUTPUTS,
0881 "intel_th/output", &intel_th_output_fops);
0882 if (th->major < 0) {
0883 err = th->major;
0884 goto err_ida;
0885 }
0886 th->irq = -1;
0887 th->dev = dev;
0888 th->drvdata = drvdata;
0889
0890 for (r = 0; r < ndevres; r++)
0891 switch (devres[r].flags & IORESOURCE_TYPE_BITS) {
0892 case IORESOURCE_MEM:
0893 th->resource[nr_mmios++] = devres[r];
0894 break;
0895 case IORESOURCE_IRQ:
0896 err = devm_request_irq(dev, devres[r].start,
0897 intel_th_irq, IRQF_SHARED,
0898 dev_name(dev), th);
0899 if (err)
0900 goto err_chrdev;
0901
0902 if (th->irq == -1)
0903 th->irq = devres[r].start;
0904 th->num_irqs++;
0905 break;
0906 default:
0907 dev_warn(dev, "Unknown resource type %lx\n",
0908 devres[r].flags);
0909 break;
0910 }
0911
0912 th->num_resources = nr_mmios;
0913
0914 dev_set_drvdata(dev, th);
0915
0916 pm_runtime_no_callbacks(dev);
0917 pm_runtime_put(dev);
0918 pm_runtime_allow(dev);
0919
0920 err = intel_th_populate(th);
0921 if (err) {
0922
0923 intel_th_free(th);
0924 return ERR_PTR(err);
0925 }
0926
0927 return th;
0928
0929 err_chrdev:
0930 __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
0931 "intel_th/output");
0932
0933 err_ida:
0934 ida_simple_remove(&intel_th_ida, th->id);
0935
0936 err_alloc:
0937 kfree(th);
0938
0939 return ERR_PTR(err);
0940 }
0941 EXPORT_SYMBOL_GPL(intel_th_alloc);
0942
0943 void intel_th_free(struct intel_th *th)
0944 {
0945 int i;
0946
0947 intel_th_request_hub_module_flush(th);
0948
0949 intel_th_device_remove(th->hub);
0950 for (i = 0; i < th->num_thdevs; i++) {
0951 if (th->thdev[i] != th->hub)
0952 intel_th_device_remove(th->thdev[i]);
0953 th->thdev[i] = NULL;
0954 }
0955
0956 th->num_thdevs = 0;
0957
0958 for (i = 0; i < th->num_irqs; i++)
0959 devm_free_irq(th->dev, th->irq + i, th);
0960
0961 pm_runtime_get_sync(th->dev);
0962 pm_runtime_forbid(th->dev);
0963
0964 __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
0965 "intel_th/output");
0966
0967 ida_simple_remove(&intel_th_ida, th->id);
0968
0969 kfree(th);
0970 }
0971 EXPORT_SYMBOL_GPL(intel_th_free);
0972
0973
0974
0975
0976
0977 int intel_th_trace_enable(struct intel_th_device *thdev)
0978 {
0979 struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
0980 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
0981
0982 if (WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH))
0983 return -EINVAL;
0984
0985 if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
0986 return -EINVAL;
0987
0988 pm_runtime_get_sync(&thdev->dev);
0989 hubdrv->enable(hub, &thdev->output);
0990
0991 return 0;
0992 }
0993 EXPORT_SYMBOL_GPL(intel_th_trace_enable);
0994
0995
0996
0997
0998
0999 int intel_th_trace_switch(struct intel_th_device *thdev)
1000 {
1001 struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
1002 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
1003
1004 if (WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH))
1005 return -EINVAL;
1006
1007 if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
1008 return -EINVAL;
1009
1010 hubdrv->trig_switch(hub, &thdev->output);
1011
1012 return 0;
1013 }
1014 EXPORT_SYMBOL_GPL(intel_th_trace_switch);
1015
1016
1017
1018
1019
1020 int intel_th_trace_disable(struct intel_th_device *thdev)
1021 {
1022 struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
1023 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
1024
1025 WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH);
1026 if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
1027 return -EINVAL;
1028
1029 hubdrv->disable(hub, &thdev->output);
1030 pm_runtime_put(&thdev->dev);
1031
1032 return 0;
1033 }
1034 EXPORT_SYMBOL_GPL(intel_th_trace_disable);
1035
1036 int intel_th_set_output(struct intel_th_device *thdev,
1037 unsigned int master)
1038 {
1039 struct intel_th_device *hub = to_intel_th_hub(thdev);
1040 struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
1041 int ret;
1042
1043
1044 if (hub->host_mode)
1045 return 0;
1046
1047
1048
1049
1050
1051 hubdrv = to_intel_th_driver(hub->dev.driver);
1052 if (!hubdrv || !try_module_get(hubdrv->driver.owner))
1053 return -EINVAL;
1054
1055 if (!hubdrv->set_output) {
1056 ret = -ENOTSUPP;
1057 goto out;
1058 }
1059
1060 ret = hubdrv->set_output(hub, master);
1061
1062 out:
1063 module_put(hubdrv->driver.owner);
1064 return ret;
1065 }
1066 EXPORT_SYMBOL_GPL(intel_th_set_output);
1067
1068 static int __init intel_th_init(void)
1069 {
1070 intel_th_debug_init();
1071
1072 return bus_register(&intel_th_bus);
1073 }
1074 subsys_initcall(intel_th_init);
1075
1076 static void __exit intel_th_exit(void)
1077 {
1078 intel_th_debug_done();
1079
1080 bus_unregister(&intel_th_bus);
1081 }
1082 module_exit(intel_th_exit);
1083
1084 MODULE_LICENSE("GPL v2");
1085 MODULE_DESCRIPTION("Intel(R) Trace Hub controller driver");
1086 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");