core.c 18 KB

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