core.c 21 KB

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