core.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intel(R) Trace Hub driver core
  4. *
  5. * Copyright (C) 2014-2015 Intel Corporation.
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/types.h>
  9. #include <linux/module.h>
  10. #include <linux/device.h>
  11. #include <linux/sysfs.h>
  12. #include <linux/kdev_t.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/idr.h>
  15. #include <linux/pci.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/dma-mapping.h>
  18. #include "intel_th.h"
  19. #include "debug.h"
  20. static bool host_mode __read_mostly;
  21. module_param(host_mode, bool, 0444);
  22. static DEFINE_IDA(intel_th_ida);
  23. static int intel_th_match(struct device *dev, struct device_driver *driver)
  24. {
  25. struct intel_th_driver *thdrv = to_intel_th_driver(driver);
  26. struct intel_th_device *thdev = to_intel_th_device(dev);
  27. if (thdev->type == INTEL_TH_SWITCH &&
  28. (!thdrv->enable || !thdrv->disable))
  29. return 0;
  30. return !strcmp(thdev->name, driver->name);
  31. }
  32. static int intel_th_child_remove(struct device *dev, void *data)
  33. {
  34. device_release_driver(dev);
  35. return 0;
  36. }
  37. static int intel_th_probe(struct device *dev)
  38. {
  39. struct intel_th_driver *thdrv = to_intel_th_driver(dev->driver);
  40. struct intel_th_device *thdev = to_intel_th_device(dev);
  41. struct intel_th_driver *hubdrv;
  42. struct intel_th_device *hub = NULL;
  43. int ret;
  44. if (thdev->type == INTEL_TH_SWITCH)
  45. hub = thdev;
  46. else if (dev->parent)
  47. hub = to_intel_th_device(dev->parent);
  48. if (!hub || !hub->dev.driver)
  49. return -EPROBE_DEFER;
  50. hubdrv = to_intel_th_driver(hub->dev.driver);
  51. pm_runtime_set_active(dev);
  52. pm_runtime_no_callbacks(dev);
  53. pm_runtime_enable(dev);
  54. ret = thdrv->probe(to_intel_th_device(dev));
  55. if (ret)
  56. goto out_pm;
  57. if (thdrv->attr_group) {
  58. ret = sysfs_create_group(&thdev->dev.kobj, thdrv->attr_group);
  59. if (ret)
  60. goto out;
  61. }
  62. if (thdev->type == INTEL_TH_OUTPUT &&
  63. !intel_th_output_assigned(thdev))
  64. /* does not talk to hardware */
  65. ret = hubdrv->assign(hub, thdev);
  66. out:
  67. if (ret)
  68. thdrv->remove(thdev);
  69. out_pm:
  70. if (ret)
  71. pm_runtime_disable(dev);
  72. return ret;
  73. }
  74. static void intel_th_device_remove(struct intel_th_device *thdev);
  75. static int intel_th_remove(struct device *dev)
  76. {
  77. struct intel_th_driver *thdrv = to_intel_th_driver(dev->driver);
  78. struct intel_th_device *thdev = to_intel_th_device(dev);
  79. struct intel_th_device *hub = to_intel_th_hub(thdev);
  80. int err;
  81. if (thdev->type == INTEL_TH_SWITCH) {
  82. struct intel_th *th = to_intel_th(hub);
  83. int i, lowest;
  84. /* disconnect outputs */
  85. err = device_for_each_child(dev, thdev, intel_th_child_remove);
  86. if (err)
  87. return err;
  88. /*
  89. * Remove outputs, that is, hub's children: they are created
  90. * at hub's probe time by having the hub call
  91. * intel_th_output_enable() for each of them.
  92. */
  93. for (i = 0, lowest = -1; i < th->num_thdevs; i++) {
  94. /*
  95. * Move the non-output devices from higher up the
  96. * th->thdev[] array to lower positions to maintain
  97. * a contiguous array.
  98. */
  99. if (th->thdev[i]->type != INTEL_TH_OUTPUT) {
  100. if (lowest >= 0) {
  101. th->thdev[lowest] = th->thdev[i];
  102. th->thdev[i] = NULL;
  103. ++lowest;
  104. }
  105. continue;
  106. }
  107. if (lowest == -1)
  108. lowest = i;
  109. intel_th_device_remove(th->thdev[i]);
  110. th->thdev[i] = NULL;
  111. }
  112. th->num_thdevs = lowest;
  113. }
  114. if (thdrv->attr_group)
  115. sysfs_remove_group(&thdev->dev.kobj, thdrv->attr_group);
  116. pm_runtime_get_sync(dev);
  117. thdrv->remove(thdev);
  118. if (intel_th_output_assigned(thdev)) {
  119. struct intel_th_driver *hubdrv =
  120. to_intel_th_driver(dev->parent->driver);
  121. if (hub->dev.driver)
  122. /* does not talk to hardware */
  123. hubdrv->unassign(hub, thdev);
  124. }
  125. pm_runtime_disable(dev);
  126. pm_runtime_set_active(dev);
  127. pm_runtime_enable(dev);
  128. return 0;
  129. }
  130. static struct bus_type intel_th_bus = {
  131. .name = "intel_th",
  132. .match = intel_th_match,
  133. .probe = intel_th_probe,
  134. .remove = intel_th_remove,
  135. };
  136. static void intel_th_device_free(struct intel_th_device *thdev);
  137. static void intel_th_device_release(struct device *dev)
  138. {
  139. intel_th_device_free(to_intel_th_device(dev));
  140. }
  141. static struct device_type intel_th_source_device_type = {
  142. .name = "intel_th_source_device",
  143. .release = intel_th_device_release,
  144. };
  145. static char *intel_th_output_devnode(struct device *dev, umode_t *mode,
  146. kuid_t *uid, kgid_t *gid)
  147. {
  148. struct intel_th_device *thdev = to_intel_th_device(dev);
  149. struct intel_th *th = to_intel_th(thdev);
  150. char *node;
  151. if (thdev->id >= 0)
  152. node = kasprintf(GFP_KERNEL, "intel_th%d/%s%d", th->id,
  153. thdev->name, thdev->id);
  154. else
  155. node = kasprintf(GFP_KERNEL, "intel_th%d/%s", th->id,
  156. thdev->name);
  157. return node;
  158. }
  159. static ssize_t port_show(struct device *dev, struct device_attribute *attr,
  160. char *buf)
  161. {
  162. struct intel_th_device *thdev = to_intel_th_device(dev);
  163. if (thdev->output.port >= 0)
  164. return scnprintf(buf, PAGE_SIZE, "%u\n", thdev->output.port);
  165. return scnprintf(buf, PAGE_SIZE, "unassigned\n");
  166. }
  167. static DEVICE_ATTR_RO(port);
  168. static int intel_th_output_activate(struct intel_th_device *thdev)
  169. {
  170. struct intel_th_driver *thdrv =
  171. to_intel_th_driver_or_null(thdev->dev.driver);
  172. struct intel_th *th = to_intel_th(thdev);
  173. int ret = 0;
  174. if (!thdrv)
  175. return -ENODEV;
  176. if (!try_module_get(thdrv->driver.owner))
  177. return -ENODEV;
  178. pm_runtime_get_sync(&thdev->dev);
  179. if (th->activate)
  180. ret = th->activate(th);
  181. if (ret)
  182. goto fail_put;
  183. if (thdrv->activate)
  184. ret = thdrv->activate(thdev);
  185. else
  186. intel_th_trace_enable(thdev);
  187. if (ret)
  188. goto fail_deactivate;
  189. return 0;
  190. fail_deactivate:
  191. if (th->deactivate)
  192. th->deactivate(th);
  193. fail_put:
  194. pm_runtime_put(&thdev->dev);
  195. module_put(thdrv->driver.owner);
  196. return ret;
  197. }
  198. static void intel_th_output_deactivate(struct intel_th_device *thdev)
  199. {
  200. struct intel_th_driver *thdrv =
  201. to_intel_th_driver_or_null(thdev->dev.driver);
  202. struct intel_th *th = to_intel_th(thdev);
  203. if (!thdrv)
  204. return;
  205. if (thdrv->deactivate)
  206. thdrv->deactivate(thdev);
  207. else
  208. intel_th_trace_disable(thdev);
  209. if (th->deactivate)
  210. th->deactivate(th);
  211. pm_runtime_put(&thdev->dev);
  212. module_put(thdrv->driver.owner);
  213. }
  214. static ssize_t active_show(struct device *dev, struct device_attribute *attr,
  215. char *buf)
  216. {
  217. struct intel_th_device *thdev = to_intel_th_device(dev);
  218. return scnprintf(buf, PAGE_SIZE, "%d\n", thdev->output.active);
  219. }
  220. static ssize_t active_store(struct device *dev, struct device_attribute *attr,
  221. const char *buf, size_t size)
  222. {
  223. struct intel_th_device *thdev = to_intel_th_device(dev);
  224. unsigned long val;
  225. int ret;
  226. ret = kstrtoul(buf, 10, &val);
  227. if (ret)
  228. return ret;
  229. if (!!val != thdev->output.active) {
  230. if (val)
  231. ret = intel_th_output_activate(thdev);
  232. else
  233. intel_th_output_deactivate(thdev);
  234. }
  235. return ret ? ret : size;
  236. }
  237. static DEVICE_ATTR_RW(active);
  238. static struct attribute *intel_th_output_attrs[] = {
  239. &dev_attr_port.attr,
  240. &dev_attr_active.attr,
  241. NULL,
  242. };
  243. ATTRIBUTE_GROUPS(intel_th_output);
  244. static struct device_type intel_th_output_device_type = {
  245. .name = "intel_th_output_device",
  246. .groups = intel_th_output_groups,
  247. .release = intel_th_device_release,
  248. .devnode = intel_th_output_devnode,
  249. };
  250. static struct device_type intel_th_switch_device_type = {
  251. .name = "intel_th_switch_device",
  252. .release = intel_th_device_release,
  253. };
  254. static struct device_type *intel_th_device_type[] = {
  255. [INTEL_TH_SOURCE] = &intel_th_source_device_type,
  256. [INTEL_TH_OUTPUT] = &intel_th_output_device_type,
  257. [INTEL_TH_SWITCH] = &intel_th_switch_device_type,
  258. };
  259. int intel_th_driver_register(struct intel_th_driver *thdrv)
  260. {
  261. if (!thdrv->probe || !thdrv->remove)
  262. return -EINVAL;
  263. thdrv->driver.bus = &intel_th_bus;
  264. return driver_register(&thdrv->driver);
  265. }
  266. EXPORT_SYMBOL_GPL(intel_th_driver_register);
  267. void intel_th_driver_unregister(struct intel_th_driver *thdrv)
  268. {
  269. driver_unregister(&thdrv->driver);
  270. }
  271. EXPORT_SYMBOL_GPL(intel_th_driver_unregister);
  272. static struct intel_th_device *
  273. intel_th_device_alloc(struct intel_th *th, unsigned int type, const char *name,
  274. int id)
  275. {
  276. struct device *parent;
  277. struct intel_th_device *thdev;
  278. if (type == INTEL_TH_OUTPUT)
  279. parent = &th->hub->dev;
  280. else
  281. parent = th->dev;
  282. thdev = kzalloc(sizeof(*thdev) + strlen(name) + 1, GFP_KERNEL);
  283. if (!thdev)
  284. return NULL;
  285. thdev->id = id;
  286. thdev->type = type;
  287. strcpy(thdev->name, name);
  288. device_initialize(&thdev->dev);
  289. thdev->dev.bus = &intel_th_bus;
  290. thdev->dev.type = intel_th_device_type[type];
  291. thdev->dev.parent = parent;
  292. thdev->dev.dma_mask = parent->dma_mask;
  293. thdev->dev.dma_parms = parent->dma_parms;
  294. dma_set_coherent_mask(&thdev->dev, parent->coherent_dma_mask);
  295. if (id >= 0)
  296. dev_set_name(&thdev->dev, "%d-%s%d", th->id, name, id);
  297. else
  298. dev_set_name(&thdev->dev, "%d-%s", th->id, name);
  299. return thdev;
  300. }
  301. static int intel_th_device_add_resources(struct intel_th_device *thdev,
  302. struct resource *res, int nres)
  303. {
  304. struct resource *r;
  305. r = kmemdup(res, sizeof(*res) * nres, GFP_KERNEL);
  306. if (!r)
  307. return -ENOMEM;
  308. thdev->resource = r;
  309. thdev->num_resources = nres;
  310. return 0;
  311. }
  312. static void intel_th_device_remove(struct intel_th_device *thdev)
  313. {
  314. device_del(&thdev->dev);
  315. put_device(&thdev->dev);
  316. }
  317. static void intel_th_device_free(struct intel_th_device *thdev)
  318. {
  319. kfree(thdev->resource);
  320. kfree(thdev);
  321. }
  322. /*
  323. * Intel(R) Trace Hub subdevices
  324. */
  325. static const struct intel_th_subdevice {
  326. const char *name;
  327. struct resource res[3];
  328. unsigned nres;
  329. unsigned type;
  330. unsigned otype;
  331. unsigned scrpd;
  332. int id;
  333. } intel_th_subdevices[] = {
  334. {
  335. .nres = 1,
  336. .res = {
  337. {
  338. /* Handle TSCU from GTH driver */
  339. .start = REG_GTH_OFFSET,
  340. .end = REG_TSCU_OFFSET + REG_TSCU_LENGTH - 1,
  341. .flags = IORESOURCE_MEM,
  342. },
  343. },
  344. .name = "gth",
  345. .type = INTEL_TH_SWITCH,
  346. .id = -1,
  347. },
  348. {
  349. .nres = 2,
  350. .res = {
  351. {
  352. .start = REG_MSU_OFFSET,
  353. .end = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
  354. .flags = IORESOURCE_MEM,
  355. },
  356. {
  357. .start = BUF_MSU_OFFSET,
  358. .end = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
  359. .flags = IORESOURCE_MEM,
  360. },
  361. },
  362. .name = "msc",
  363. .id = 0,
  364. .type = INTEL_TH_OUTPUT,
  365. .otype = GTH_MSU,
  366. .scrpd = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC0_IS_ENABLED,
  367. },
  368. {
  369. .nres = 2,
  370. .res = {
  371. {
  372. .start = REG_MSU_OFFSET,
  373. .end = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
  374. .flags = IORESOURCE_MEM,
  375. },
  376. {
  377. .start = BUF_MSU_OFFSET,
  378. .end = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
  379. .flags = IORESOURCE_MEM,
  380. },
  381. },
  382. .name = "msc",
  383. .id = 1,
  384. .type = INTEL_TH_OUTPUT,
  385. .otype = GTH_MSU,
  386. .scrpd = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC1_IS_ENABLED,
  387. },
  388. {
  389. .nres = 2,
  390. .res = {
  391. {
  392. .start = REG_STH_OFFSET,
  393. .end = REG_STH_OFFSET + REG_STH_LENGTH - 1,
  394. .flags = IORESOURCE_MEM,
  395. },
  396. {
  397. .start = TH_MMIO_SW,
  398. .end = 0,
  399. .flags = IORESOURCE_MEM,
  400. },
  401. },
  402. .id = -1,
  403. .name = "sth",
  404. .type = INTEL_TH_SOURCE,
  405. },
  406. {
  407. .nres = 1,
  408. .res = {
  409. {
  410. .start = REG_PTI_OFFSET,
  411. .end = REG_PTI_OFFSET + REG_PTI_LENGTH - 1,
  412. .flags = IORESOURCE_MEM,
  413. },
  414. },
  415. .id = -1,
  416. .name = "pti",
  417. .type = INTEL_TH_OUTPUT,
  418. .otype = GTH_PTI,
  419. .scrpd = SCRPD_PTI_IS_PRIM_DEST,
  420. },
  421. {
  422. .nres = 1,
  423. .res = {
  424. {
  425. .start = REG_PTI_OFFSET,
  426. .end = REG_PTI_OFFSET + REG_PTI_LENGTH - 1,
  427. .flags = IORESOURCE_MEM,
  428. },
  429. },
  430. .id = -1,
  431. .name = "lpp",
  432. .type = INTEL_TH_OUTPUT,
  433. .otype = GTH_LPP,
  434. .scrpd = SCRPD_PTI_IS_PRIM_DEST,
  435. },
  436. {
  437. .nres = 1,
  438. .res = {
  439. {
  440. .start = REG_DCIH_OFFSET,
  441. .end = REG_DCIH_OFFSET + REG_DCIH_LENGTH - 1,
  442. .flags = IORESOURCE_MEM,
  443. },
  444. },
  445. .id = -1,
  446. .name = "dcih",
  447. .type = INTEL_TH_OUTPUT,
  448. },
  449. };
  450. #ifdef CONFIG_MODULES
  451. static void __intel_th_request_hub_module(struct work_struct *work)
  452. {
  453. struct intel_th *th = container_of(work, struct intel_th,
  454. request_module_work);
  455. request_module("intel_th_%s", th->hub->name);
  456. }
  457. static int intel_th_request_hub_module(struct intel_th *th)
  458. {
  459. INIT_WORK(&th->request_module_work, __intel_th_request_hub_module);
  460. schedule_work(&th->request_module_work);
  461. return 0;
  462. }
  463. static void intel_th_request_hub_module_flush(struct intel_th *th)
  464. {
  465. flush_work(&th->request_module_work);
  466. }
  467. #else
  468. static inline int intel_th_request_hub_module(struct intel_th *th)
  469. {
  470. return -EINVAL;
  471. }
  472. static inline void intel_th_request_hub_module_flush(struct intel_th *th)
  473. {
  474. }
  475. #endif /* CONFIG_MODULES */
  476. static struct intel_th_device *
  477. intel_th_subdevice_alloc(struct intel_th *th,
  478. const struct intel_th_subdevice *subdev)
  479. {
  480. struct intel_th_device *thdev;
  481. struct resource res[3];
  482. unsigned int req = 0;
  483. int r, err;
  484. thdev = intel_th_device_alloc(th, subdev->type, subdev->name,
  485. subdev->id);
  486. if (!thdev)
  487. return ERR_PTR(-ENOMEM);
  488. thdev->drvdata = th->drvdata;
  489. memcpy(res, subdev->res,
  490. sizeof(struct resource) * subdev->nres);
  491. for (r = 0; r < subdev->nres; r++) {
  492. struct resource *devres = th->resource;
  493. int bar = TH_MMIO_CONFIG;
  494. /*
  495. * Take .end == 0 to mean 'take the whole bar',
  496. * .start then tells us which bar it is. Default to
  497. * TH_MMIO_CONFIG.
  498. */
  499. if (!res[r].end && res[r].flags == IORESOURCE_MEM) {
  500. bar = res[r].start;
  501. res[r].start = 0;
  502. res[r].end = resource_size(&devres[bar]) - 1;
  503. }
  504. if (res[r].flags & IORESOURCE_MEM) {
  505. res[r].start += devres[bar].start;
  506. res[r].end += devres[bar].start;
  507. dev_dbg(th->dev, "%s:%d @ %pR\n",
  508. subdev->name, r, &res[r]);
  509. } else if (res[r].flags & IORESOURCE_IRQ) {
  510. res[r].start = th->irq;
  511. }
  512. }
  513. err = intel_th_device_add_resources(thdev, res, subdev->nres);
  514. if (err) {
  515. put_device(&thdev->dev);
  516. goto fail_put_device;
  517. }
  518. if (subdev->type == INTEL_TH_OUTPUT) {
  519. thdev->dev.devt = MKDEV(th->major, th->num_thdevs);
  520. thdev->output.type = subdev->otype;
  521. thdev->output.port = -1;
  522. thdev->output.scratchpad = subdev->scrpd;
  523. } else if (subdev->type == INTEL_TH_SWITCH) {
  524. thdev->host_mode =
  525. INTEL_TH_CAP(th, host_mode_only) ? true : host_mode;
  526. th->hub = thdev;
  527. }
  528. err = device_add(&thdev->dev);
  529. if (err) {
  530. put_device(&thdev->dev);
  531. goto fail_free_res;
  532. }
  533. /* need switch driver to be loaded to enumerate the rest */
  534. if (subdev->type == INTEL_TH_SWITCH && !req) {
  535. err = intel_th_request_hub_module(th);
  536. if (!err)
  537. req++;
  538. }
  539. return thdev;
  540. fail_free_res:
  541. kfree(thdev->resource);
  542. fail_put_device:
  543. put_device(&thdev->dev);
  544. return ERR_PTR(err);
  545. }
  546. /**
  547. * intel_th_output_enable() - find and enable a device for a given output type
  548. * @th: Intel TH instance
  549. * @otype: output type
  550. *
  551. * Go through the unallocated output devices, find the first one whos type
  552. * matches @otype and instantiate it. These devices are removed when the hub
  553. * device is removed, see intel_th_remove().
  554. */
  555. int intel_th_output_enable(struct intel_th *th, unsigned int otype)
  556. {
  557. struct intel_th_device *thdev;
  558. int src = 0, dst = 0;
  559. for (src = 0, dst = 0; dst <= th->num_thdevs; src++, dst++) {
  560. for (; src < ARRAY_SIZE(intel_th_subdevices); src++) {
  561. if (intel_th_subdevices[src].type != INTEL_TH_OUTPUT)
  562. continue;
  563. if (intel_th_subdevices[src].otype != otype)
  564. continue;
  565. break;
  566. }
  567. /* no unallocated matching subdevices */
  568. if (src == ARRAY_SIZE(intel_th_subdevices))
  569. return -ENODEV;
  570. for (; dst < th->num_thdevs; dst++) {
  571. if (th->thdev[dst]->type != INTEL_TH_OUTPUT)
  572. continue;
  573. if (th->thdev[dst]->output.type != otype)
  574. continue;
  575. break;
  576. }
  577. /*
  578. * intel_th_subdevices[src] matches our requirements and is
  579. * not matched in th::thdev[]
  580. */
  581. if (dst == th->num_thdevs)
  582. goto found;
  583. }
  584. return -ENODEV;
  585. found:
  586. thdev = intel_th_subdevice_alloc(th, &intel_th_subdevices[src]);
  587. if (IS_ERR(thdev))
  588. return PTR_ERR(thdev);
  589. th->thdev[th->num_thdevs++] = thdev;
  590. return 0;
  591. }
  592. EXPORT_SYMBOL_GPL(intel_th_output_enable);
  593. static int intel_th_populate(struct intel_th *th)
  594. {
  595. int src;
  596. /* create devices for each intel_th_subdevice */
  597. for (src = 0; src < ARRAY_SIZE(intel_th_subdevices); src++) {
  598. const struct intel_th_subdevice *subdev =
  599. &intel_th_subdevices[src];
  600. struct intel_th_device *thdev;
  601. /* only allow SOURCE and SWITCH devices in host mode */
  602. if ((INTEL_TH_CAP(th, host_mode_only) || host_mode) &&
  603. subdev->type == INTEL_TH_OUTPUT)
  604. continue;
  605. /*
  606. * don't enable port OUTPUTs in this path; SWITCH enables them
  607. * via intel_th_output_enable()
  608. */
  609. if (subdev->type == INTEL_TH_OUTPUT &&
  610. subdev->otype != GTH_NONE)
  611. continue;
  612. thdev = intel_th_subdevice_alloc(th, subdev);
  613. /* note: caller should free subdevices from th::thdev[] */
  614. if (IS_ERR(thdev))
  615. return PTR_ERR(thdev);
  616. th->thdev[th->num_thdevs++] = thdev;
  617. }
  618. return 0;
  619. }
  620. static int match_devt(struct device *dev, void *data)
  621. {
  622. dev_t devt = (dev_t)(unsigned long)data;
  623. return dev->devt == devt;
  624. }
  625. static int intel_th_output_open(struct inode *inode, struct file *file)
  626. {
  627. const struct file_operations *fops;
  628. struct intel_th_driver *thdrv;
  629. struct device *dev;
  630. int err;
  631. dev = bus_find_device(&intel_th_bus, NULL,
  632. (void *)(unsigned long)inode->i_rdev,
  633. match_devt);
  634. if (!dev || !dev->driver)
  635. return -ENODEV;
  636. thdrv = to_intel_th_driver(dev->driver);
  637. fops = fops_get(thdrv->fops);
  638. if (!fops)
  639. return -ENODEV;
  640. replace_fops(file, fops);
  641. file->private_data = to_intel_th_device(dev);
  642. if (file->f_op->open) {
  643. err = file->f_op->open(inode, file);
  644. return err;
  645. }
  646. return 0;
  647. }
  648. static const struct file_operations intel_th_output_fops = {
  649. .open = intel_th_output_open,
  650. .llseek = noop_llseek,
  651. };
  652. /**
  653. * intel_th_alloc() - allocate a new Intel TH device and its subdevices
  654. * @dev: parent device
  655. * @devres: parent's resources
  656. * @ndevres: number of resources
  657. * @irq: irq number
  658. */
  659. struct intel_th *
  660. intel_th_alloc(struct device *dev, struct intel_th_drvdata *drvdata,
  661. struct resource *devres, unsigned int ndevres, int irq)
  662. {
  663. struct intel_th *th;
  664. int err, r;
  665. if (irq == -1)
  666. for (r = 0; r < ndevres; r++)
  667. if (devres[r].flags & IORESOURCE_IRQ) {
  668. irq = devres[r].start;
  669. break;
  670. }
  671. th = kzalloc(sizeof(*th), GFP_KERNEL);
  672. if (!th)
  673. return ERR_PTR(-ENOMEM);
  674. th->id = ida_simple_get(&intel_th_ida, 0, 0, GFP_KERNEL);
  675. if (th->id < 0) {
  676. err = th->id;
  677. goto err_alloc;
  678. }
  679. th->major = __register_chrdev(0, 0, TH_POSSIBLE_OUTPUTS,
  680. "intel_th/output", &intel_th_output_fops);
  681. if (th->major < 0) {
  682. err = th->major;
  683. goto err_ida;
  684. }
  685. th->dev = dev;
  686. th->drvdata = drvdata;
  687. th->resource = devres;
  688. th->num_resources = ndevres;
  689. th->irq = irq;
  690. dev_set_drvdata(dev, th);
  691. pm_runtime_no_callbacks(dev);
  692. pm_runtime_put(dev);
  693. pm_runtime_allow(dev);
  694. err = intel_th_populate(th);
  695. if (err) {
  696. /* free the subdevices and undo everything */
  697. intel_th_free(th);
  698. return ERR_PTR(err);
  699. }
  700. return th;
  701. err_ida:
  702. ida_simple_remove(&intel_th_ida, th->id);
  703. err_alloc:
  704. kfree(th);
  705. return ERR_PTR(err);
  706. }
  707. EXPORT_SYMBOL_GPL(intel_th_alloc);
  708. void intel_th_free(struct intel_th *th)
  709. {
  710. int i;
  711. intel_th_request_hub_module_flush(th);
  712. intel_th_device_remove(th->hub);
  713. for (i = 0; i < th->num_thdevs; i++) {
  714. if (th->thdev[i] != th->hub)
  715. intel_th_device_remove(th->thdev[i]);
  716. th->thdev[i] = NULL;
  717. }
  718. th->num_thdevs = 0;
  719. pm_runtime_get_sync(th->dev);
  720. pm_runtime_forbid(th->dev);
  721. __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
  722. "intel_th/output");
  723. ida_simple_remove(&intel_th_ida, th->id);
  724. kfree(th);
  725. }
  726. EXPORT_SYMBOL_GPL(intel_th_free);
  727. /**
  728. * intel_th_trace_enable() - enable tracing for an output device
  729. * @thdev: output device that requests tracing be enabled
  730. */
  731. int intel_th_trace_enable(struct intel_th_device *thdev)
  732. {
  733. struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
  734. struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
  735. if (WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH))
  736. return -EINVAL;
  737. if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
  738. return -EINVAL;
  739. pm_runtime_get_sync(&thdev->dev);
  740. hubdrv->enable(hub, &thdev->output);
  741. return 0;
  742. }
  743. EXPORT_SYMBOL_GPL(intel_th_trace_enable);
  744. /**
  745. * intel_th_trace_disable() - disable tracing for an output device
  746. * @thdev: output device that requests tracing be disabled
  747. */
  748. int intel_th_trace_disable(struct intel_th_device *thdev)
  749. {
  750. struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
  751. struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
  752. WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH);
  753. if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
  754. return -EINVAL;
  755. hubdrv->disable(hub, &thdev->output);
  756. pm_runtime_put(&thdev->dev);
  757. return 0;
  758. }
  759. EXPORT_SYMBOL_GPL(intel_th_trace_disable);
  760. int intel_th_set_output(struct intel_th_device *thdev,
  761. unsigned int master)
  762. {
  763. struct intel_th_device *hub = to_intel_th_hub(thdev);
  764. struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
  765. /* In host mode, this is up to the external debugger, do nothing. */
  766. if (hub->host_mode)
  767. return 0;
  768. if (!hubdrv->set_output)
  769. return -ENOTSUPP;
  770. return hubdrv->set_output(hub, master);
  771. }
  772. EXPORT_SYMBOL_GPL(intel_th_set_output);
  773. static int __init intel_th_init(void)
  774. {
  775. intel_th_debug_init();
  776. return bus_register(&intel_th_bus);
  777. }
  778. subsys_initcall(intel_th_init);
  779. static void __exit intel_th_exit(void)
  780. {
  781. intel_th_debug_done();
  782. bus_unregister(&intel_th_bus);
  783. }
  784. module_exit(intel_th_exit);
  785. MODULE_LICENSE("GPL v2");
  786. MODULE_DESCRIPTION("Intel(R) Trace Hub controller driver");
  787. MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");