core.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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_DCIH_OFFSET,
  420. .end = REG_DCIH_OFFSET + REG_DCIH_LENGTH - 1,
  421. .flags = IORESOURCE_MEM,
  422. },
  423. },
  424. .id = -1,
  425. .name = "dcih",
  426. .type = INTEL_TH_OUTPUT,
  427. },
  428. };
  429. #ifdef CONFIG_MODULES
  430. static void __intel_th_request_hub_module(struct work_struct *work)
  431. {
  432. struct intel_th *th = container_of(work, struct intel_th,
  433. request_module_work);
  434. request_module("intel_th_%s", th->hub->name);
  435. }
  436. static int intel_th_request_hub_module(struct intel_th *th)
  437. {
  438. INIT_WORK(&th->request_module_work, __intel_th_request_hub_module);
  439. schedule_work(&th->request_module_work);
  440. return 0;
  441. }
  442. static void intel_th_request_hub_module_flush(struct intel_th *th)
  443. {
  444. flush_work(&th->request_module_work);
  445. }
  446. #else
  447. static inline int intel_th_request_hub_module(struct intel_th *th)
  448. {
  449. return -EINVAL;
  450. }
  451. static inline void intel_th_request_hub_module_flush(struct intel_th *th)
  452. {
  453. }
  454. #endif /* CONFIG_MODULES */
  455. static struct intel_th_device *
  456. intel_th_subdevice_alloc(struct intel_th *th,
  457. const struct intel_th_subdevice *subdev)
  458. {
  459. struct intel_th_device *thdev;
  460. struct resource res[3];
  461. unsigned int req = 0;
  462. int r, err;
  463. thdev = intel_th_device_alloc(th, subdev->type, subdev->name,
  464. subdev->id);
  465. if (!thdev)
  466. return ERR_PTR(-ENOMEM);
  467. memcpy(res, subdev->res,
  468. sizeof(struct resource) * subdev->nres);
  469. for (r = 0; r < subdev->nres; r++) {
  470. struct resource *devres = th->resource;
  471. int bar = TH_MMIO_CONFIG;
  472. /*
  473. * Take .end == 0 to mean 'take the whole bar',
  474. * .start then tells us which bar it is. Default to
  475. * TH_MMIO_CONFIG.
  476. */
  477. if (!res[r].end && res[r].flags == IORESOURCE_MEM) {
  478. bar = res[r].start;
  479. res[r].start = 0;
  480. res[r].end = resource_size(&devres[bar]) - 1;
  481. }
  482. if (res[r].flags & IORESOURCE_MEM) {
  483. res[r].start += devres[bar].start;
  484. res[r].end += devres[bar].start;
  485. dev_dbg(th->dev, "%s:%d @ %pR\n",
  486. subdev->name, r, &res[r]);
  487. } else if (res[r].flags & IORESOURCE_IRQ) {
  488. res[r].start = th->irq;
  489. }
  490. }
  491. err = intel_th_device_add_resources(thdev, res, subdev->nres);
  492. if (err) {
  493. put_device(&thdev->dev);
  494. goto fail_put_device;
  495. }
  496. if (subdev->type == INTEL_TH_OUTPUT) {
  497. thdev->dev.devt = MKDEV(th->major, th->num_thdevs);
  498. thdev->output.type = subdev->otype;
  499. thdev->output.port = -1;
  500. thdev->output.scratchpad = subdev->scrpd;
  501. } else if (subdev->type == INTEL_TH_SWITCH) {
  502. thdev->host_mode = host_mode;
  503. th->hub = thdev;
  504. }
  505. err = device_add(&thdev->dev);
  506. if (err) {
  507. put_device(&thdev->dev);
  508. goto fail_free_res;
  509. }
  510. /* need switch driver to be loaded to enumerate the rest */
  511. if (subdev->type == INTEL_TH_SWITCH && !req) {
  512. err = intel_th_request_hub_module(th);
  513. if (!err)
  514. req++;
  515. }
  516. return thdev;
  517. fail_free_res:
  518. kfree(thdev->resource);
  519. fail_put_device:
  520. put_device(&thdev->dev);
  521. return ERR_PTR(err);
  522. }
  523. /**
  524. * intel_th_output_enable() - find and enable a device for a given output type
  525. * @th: Intel TH instance
  526. * @otype: output type
  527. *
  528. * Go through the unallocated output devices, find the first one whos type
  529. * matches @otype and instantiate it. These devices are removed when the hub
  530. * device is removed, see intel_th_remove().
  531. */
  532. int intel_th_output_enable(struct intel_th *th, unsigned int otype)
  533. {
  534. struct intel_th_device *thdev;
  535. int src = 0, dst = 0;
  536. for (src = 0, dst = 0; dst <= th->num_thdevs; src++, dst++) {
  537. for (; src < ARRAY_SIZE(intel_th_subdevices); src++) {
  538. if (intel_th_subdevices[src].type != INTEL_TH_OUTPUT)
  539. continue;
  540. if (intel_th_subdevices[src].otype != otype)
  541. continue;
  542. break;
  543. }
  544. /* no unallocated matching subdevices */
  545. if (src == ARRAY_SIZE(intel_th_subdevices))
  546. return -ENODEV;
  547. for (; dst < th->num_thdevs; dst++) {
  548. if (th->thdev[dst]->type != INTEL_TH_OUTPUT)
  549. continue;
  550. if (th->thdev[dst]->output.type != otype)
  551. continue;
  552. break;
  553. }
  554. /*
  555. * intel_th_subdevices[src] matches our requirements and is
  556. * not matched in th::thdev[]
  557. */
  558. if (dst == th->num_thdevs)
  559. goto found;
  560. }
  561. return -ENODEV;
  562. found:
  563. thdev = intel_th_subdevice_alloc(th, &intel_th_subdevices[src]);
  564. if (IS_ERR(thdev))
  565. return PTR_ERR(thdev);
  566. th->thdev[th->num_thdevs++] = thdev;
  567. return 0;
  568. }
  569. EXPORT_SYMBOL_GPL(intel_th_output_enable);
  570. static int intel_th_populate(struct intel_th *th)
  571. {
  572. int src;
  573. /* create devices for each intel_th_subdevice */
  574. for (src = 0; src < ARRAY_SIZE(intel_th_subdevices); src++) {
  575. const struct intel_th_subdevice *subdev =
  576. &intel_th_subdevices[src];
  577. struct intel_th_device *thdev;
  578. /* only allow SOURCE and SWITCH devices in host mode */
  579. if (host_mode && subdev->type == INTEL_TH_OUTPUT)
  580. continue;
  581. /*
  582. * don't enable port OUTPUTs in this path; SWITCH enables them
  583. * via intel_th_output_enable()
  584. */
  585. if (subdev->type == INTEL_TH_OUTPUT &&
  586. subdev->otype != GTH_NONE)
  587. continue;
  588. thdev = intel_th_subdevice_alloc(th, subdev);
  589. /* note: caller should free subdevices from th::thdev[] */
  590. if (IS_ERR(thdev))
  591. return PTR_ERR(thdev);
  592. th->thdev[th->num_thdevs++] = thdev;
  593. }
  594. return 0;
  595. }
  596. static int match_devt(struct device *dev, void *data)
  597. {
  598. dev_t devt = (dev_t)(unsigned long)data;
  599. return dev->devt == devt;
  600. }
  601. static int intel_th_output_open(struct inode *inode, struct file *file)
  602. {
  603. const struct file_operations *fops;
  604. struct intel_th_driver *thdrv;
  605. struct device *dev;
  606. int err;
  607. dev = bus_find_device(&intel_th_bus, NULL,
  608. (void *)(unsigned long)inode->i_rdev,
  609. match_devt);
  610. if (!dev || !dev->driver)
  611. return -ENODEV;
  612. thdrv = to_intel_th_driver(dev->driver);
  613. fops = fops_get(thdrv->fops);
  614. if (!fops)
  615. return -ENODEV;
  616. replace_fops(file, fops);
  617. file->private_data = to_intel_th_device(dev);
  618. if (file->f_op->open) {
  619. err = file->f_op->open(inode, file);
  620. return err;
  621. }
  622. return 0;
  623. }
  624. static const struct file_operations intel_th_output_fops = {
  625. .open = intel_th_output_open,
  626. .llseek = noop_llseek,
  627. };
  628. /**
  629. * intel_th_alloc() - allocate a new Intel TH device and its subdevices
  630. * @dev: parent device
  631. * @devres: parent's resources
  632. * @ndevres: number of resources
  633. * @irq: irq number
  634. */
  635. struct intel_th *
  636. intel_th_alloc(struct device *dev, struct resource *devres,
  637. unsigned int ndevres, int irq)
  638. {
  639. struct intel_th *th;
  640. int err;
  641. th = kzalloc(sizeof(*th), GFP_KERNEL);
  642. if (!th)
  643. return ERR_PTR(-ENOMEM);
  644. th->id = ida_simple_get(&intel_th_ida, 0, 0, GFP_KERNEL);
  645. if (th->id < 0) {
  646. err = th->id;
  647. goto err_alloc;
  648. }
  649. th->major = __register_chrdev(0, 0, TH_POSSIBLE_OUTPUTS,
  650. "intel_th/output", &intel_th_output_fops);
  651. if (th->major < 0) {
  652. err = th->major;
  653. goto err_ida;
  654. }
  655. th->dev = dev;
  656. th->resource = devres;
  657. th->num_resources = ndevres;
  658. th->irq = irq;
  659. dev_set_drvdata(dev, th);
  660. pm_runtime_no_callbacks(dev);
  661. pm_runtime_put(dev);
  662. pm_runtime_allow(dev);
  663. err = intel_th_populate(th);
  664. if (err) {
  665. /* free the subdevices and undo everything */
  666. intel_th_free(th);
  667. return ERR_PTR(err);
  668. }
  669. return th;
  670. err_ida:
  671. ida_simple_remove(&intel_th_ida, th->id);
  672. err_alloc:
  673. kfree(th);
  674. return ERR_PTR(err);
  675. }
  676. EXPORT_SYMBOL_GPL(intel_th_alloc);
  677. void intel_th_free(struct intel_th *th)
  678. {
  679. int i;
  680. intel_th_request_hub_module_flush(th);
  681. intel_th_device_remove(th->hub);
  682. for (i = 0; i < th->num_thdevs; i++) {
  683. if (th->thdev[i] != th->hub)
  684. intel_th_device_remove(th->thdev[i]);
  685. th->thdev[i] = NULL;
  686. }
  687. th->num_thdevs = 0;
  688. pm_runtime_get_sync(th->dev);
  689. pm_runtime_forbid(th->dev);
  690. __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
  691. "intel_th/output");
  692. ida_simple_remove(&intel_th_ida, th->id);
  693. kfree(th);
  694. }
  695. EXPORT_SYMBOL_GPL(intel_th_free);
  696. /**
  697. * intel_th_trace_enable() - enable tracing for an output device
  698. * @thdev: output device that requests tracing be enabled
  699. */
  700. int intel_th_trace_enable(struct intel_th_device *thdev)
  701. {
  702. struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
  703. struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
  704. if (WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH))
  705. return -EINVAL;
  706. if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
  707. return -EINVAL;
  708. pm_runtime_get_sync(&thdev->dev);
  709. hubdrv->enable(hub, &thdev->output);
  710. return 0;
  711. }
  712. EXPORT_SYMBOL_GPL(intel_th_trace_enable);
  713. /**
  714. * intel_th_trace_disable() - disable tracing for an output device
  715. * @thdev: output device that requests tracing be disabled
  716. */
  717. int intel_th_trace_disable(struct intel_th_device *thdev)
  718. {
  719. struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
  720. struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
  721. WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH);
  722. if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
  723. return -EINVAL;
  724. hubdrv->disable(hub, &thdev->output);
  725. pm_runtime_put(&thdev->dev);
  726. return 0;
  727. }
  728. EXPORT_SYMBOL_GPL(intel_th_trace_disable);
  729. int intel_th_set_output(struct intel_th_device *thdev,
  730. unsigned int master)
  731. {
  732. struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
  733. struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
  734. if (!hubdrv->set_output)
  735. return -ENOTSUPP;
  736. return hubdrv->set_output(hub, master);
  737. }
  738. EXPORT_SYMBOL_GPL(intel_th_set_output);
  739. static int __init intel_th_init(void)
  740. {
  741. intel_th_debug_init();
  742. return bus_register(&intel_th_bus);
  743. }
  744. subsys_initcall(intel_th_init);
  745. static void __exit intel_th_exit(void)
  746. {
  747. intel_th_debug_done();
  748. bus_unregister(&intel_th_bus);
  749. }
  750. module_exit(intel_th_exit);
  751. MODULE_LICENSE("GPL v2");
  752. MODULE_DESCRIPTION("Intel(R) Trace Hub controller driver");
  753. MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");