core.c 21 KB

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