core.c 21 KB

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