core.c 21 KB

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