core.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intel(R) Trace Hub driver core
  4. *
  5. * Copyright (C) 2014-2015 Intel Corporation.
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/types.h>
  9. #include <linux/module.h>
  10. #include <linux/device.h>
  11. #include <linux/sysfs.h>
  12. #include <linux/kdev_t.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/idr.h>
  15. #include <linux/pci.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/dma-mapping.h>
  18. #include "intel_th.h"
  19. #include "debug.h"
  20. static bool host_mode __read_mostly;
  21. module_param(host_mode, bool, 0444);
  22. static DEFINE_IDA(intel_th_ida);
  23. static int intel_th_match(struct device *dev, const struct device_driver *driver)
  24. {
  25. const struct intel_th_driver *thdrv = to_intel_th_driver(driver);
  26. struct intel_th_device *thdev = to_intel_th_device(dev);
  27. if (thdev->type == INTEL_TH_SWITCH &&
  28. (!thdrv->enable || !thdrv->disable))
  29. return 0;
  30. return !strcmp(thdev->name, driver->name);
  31. }
  32. static int intel_th_child_remove(struct device *dev, void *data)
  33. {
  34. device_release_driver(dev);
  35. return 0;
  36. }
  37. static int intel_th_probe(struct device *dev)
  38. {
  39. struct intel_th_driver *thdrv = to_intel_th_driver(dev->driver);
  40. struct intel_th_device *thdev = to_intel_th_device(dev);
  41. struct intel_th_driver *hubdrv;
  42. struct intel_th_device *hub = NULL;
  43. int ret;
  44. if (thdev->type == INTEL_TH_SWITCH)
  45. hub = thdev;
  46. else if (dev->parent)
  47. hub = to_intel_th_device(dev->parent);
  48. if (!hub || !hub->dev.driver)
  49. return -EPROBE_DEFER;
  50. hubdrv = to_intel_th_driver(hub->dev.driver);
  51. pm_runtime_set_active(dev);
  52. pm_runtime_no_callbacks(dev);
  53. pm_runtime_enable(dev);
  54. ret = thdrv->probe(to_intel_th_device(dev));
  55. if (ret)
  56. goto out_pm;
  57. if (thdrv->attr_group) {
  58. ret = sysfs_create_group(&thdev->dev.kobj, thdrv->attr_group);
  59. if (ret)
  60. goto out;
  61. }
  62. if (thdev->type == INTEL_TH_OUTPUT &&
  63. !intel_th_output_assigned(thdev))
  64. /* does not talk to hardware */
  65. ret = hubdrv->assign(hub, thdev);
  66. out:
  67. if (ret)
  68. thdrv->remove(thdev);
  69. out_pm:
  70. if (ret)
  71. pm_runtime_disable(dev);
  72. return ret;
  73. }
  74. static void intel_th_device_remove(struct intel_th_device *thdev);
  75. static void intel_th_remove(struct device *dev)
  76. {
  77. struct intel_th_driver *thdrv = to_intel_th_driver(dev->driver);
  78. struct intel_th_device *thdev = to_intel_th_device(dev);
  79. struct intel_th_device *hub = to_intel_th_hub(thdev);
  80. if (thdev->type == INTEL_TH_SWITCH) {
  81. struct intel_th *th = to_intel_th(hub);
  82. int i, lowest;
  83. /*
  84. * disconnect outputs
  85. *
  86. * intel_th_child_remove returns 0 unconditionally, so there is
  87. * no need to check the return value of device_for_each_child.
  88. */
  89. device_for_each_child(dev, thdev, intel_th_child_remove);
  90. /*
  91. * Remove outputs, that is, hub's children: they are created
  92. * at hub's probe time by having the hub call
  93. * intel_th_output_enable() for each of them.
  94. */
  95. for (i = 0, lowest = -1; i < th->num_thdevs; i++) {
  96. /*
  97. * Move the non-output devices from higher up the
  98. * th->thdev[] array to lower positions to maintain
  99. * a contiguous array.
  100. */
  101. if (th->thdev[i]->type != INTEL_TH_OUTPUT) {
  102. if (lowest >= 0) {
  103. th->thdev[lowest] = th->thdev[i];
  104. th->thdev[i] = NULL;
  105. ++lowest;
  106. }
  107. continue;
  108. }
  109. if (lowest == -1)
  110. lowest = i;
  111. intel_th_device_remove(th->thdev[i]);
  112. th->thdev[i] = NULL;
  113. }
  114. if (lowest >= 0)
  115. th->num_thdevs = lowest;
  116. }
  117. if (thdrv->attr_group)
  118. sysfs_remove_group(&thdev->dev.kobj, thdrv->attr_group);
  119. pm_runtime_get_sync(dev);
  120. thdrv->remove(thdev);
  121. if (intel_th_output_assigned(thdev)) {
  122. struct intel_th_driver *hubdrv =
  123. to_intel_th_driver(dev->parent->driver);
  124. if (hub->dev.driver)
  125. /* does not talk to hardware */
  126. hubdrv->unassign(hub, thdev);
  127. }
  128. pm_runtime_disable(dev);
  129. pm_runtime_set_active(dev);
  130. pm_runtime_enable(dev);
  131. }
  132. static struct bus_type intel_th_bus = {
  133. .name = "intel_th",
  134. .match = intel_th_match,
  135. .probe = intel_th_probe,
  136. .remove = intel_th_remove,
  137. };
  138. static void intel_th_device_free(struct intel_th_device *thdev);
  139. static void intel_th_device_release(struct device *dev)
  140. {
  141. intel_th_device_free(to_intel_th_device(dev));
  142. }
  143. static const struct device_type intel_th_source_device_type = {
  144. .name = "intel_th_source_device",
  145. .release = intel_th_device_release,
  146. };
  147. static char *intel_th_output_devnode(const struct device *dev, umode_t *mode,
  148. kuid_t *uid, kgid_t *gid)
  149. {
  150. const struct intel_th_device *thdev = to_intel_th_device(dev);
  151. const struct intel_th *th = to_intel_th(thdev);
  152. char *node;
  153. if (thdev->id >= 0)
  154. node = kasprintf(GFP_KERNEL, "intel_th%d/%s%d", th->id,
  155. thdev->name, thdev->id);
  156. else
  157. node = kasprintf(GFP_KERNEL, "intel_th%d/%s", th->id,
  158. thdev->name);
  159. return node;
  160. }
  161. static ssize_t port_show(struct device *dev, struct device_attribute *attr,
  162. char *buf)
  163. {
  164. struct intel_th_device *thdev = to_intel_th_device(dev);
  165. if (thdev->output.port >= 0)
  166. return scnprintf(buf, PAGE_SIZE, "%u\n", thdev->output.port);
  167. return scnprintf(buf, PAGE_SIZE, "unassigned\n");
  168. }
  169. static DEVICE_ATTR_RO(port);
  170. static void intel_th_trace_prepare(struct intel_th_device *thdev)
  171. {
  172. struct intel_th_device *hub = to_intel_th_hub(thdev);
  173. struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
  174. if (hub->type != INTEL_TH_SWITCH)
  175. return;
  176. if (thdev->type != INTEL_TH_OUTPUT)
  177. return;
  178. pm_runtime_get_sync(&thdev->dev);
  179. hubdrv->prepare(hub, &thdev->output);
  180. pm_runtime_put(&thdev->dev);
  181. }
  182. static int intel_th_output_activate(struct intel_th_device *thdev)
  183. {
  184. struct intel_th_driver *thdrv =
  185. to_intel_th_driver_or_null(thdev->dev.driver);
  186. struct intel_th *th = to_intel_th(thdev);
  187. int ret = 0;
  188. if (!thdrv)
  189. return -ENODEV;
  190. if (!try_module_get(thdrv->driver.owner))
  191. return -ENODEV;
  192. pm_runtime_get_sync(&thdev->dev);
  193. if (th->activate)
  194. ret = th->activate(th);
  195. if (ret)
  196. goto fail_put;
  197. intel_th_trace_prepare(thdev);
  198. if (thdrv->activate)
  199. ret = thdrv->activate(thdev);
  200. else
  201. intel_th_trace_enable(thdev);
  202. if (ret)
  203. goto fail_deactivate;
  204. return 0;
  205. fail_deactivate:
  206. if (th->deactivate)
  207. th->deactivate(th);
  208. fail_put:
  209. pm_runtime_put(&thdev->dev);
  210. module_put(thdrv->driver.owner);
  211. return ret;
  212. }
  213. static void intel_th_output_deactivate(struct intel_th_device *thdev)
  214. {
  215. struct intel_th_driver *thdrv =
  216. to_intel_th_driver_or_null(thdev->dev.driver);
  217. struct intel_th *th = to_intel_th(thdev);
  218. if (!thdrv)
  219. return;
  220. if (thdrv->deactivate)
  221. thdrv->deactivate(thdev);
  222. else
  223. intel_th_trace_disable(thdev);
  224. if (th->deactivate)
  225. th->deactivate(th);
  226. pm_runtime_put(&thdev->dev);
  227. module_put(thdrv->driver.owner);
  228. }
  229. static ssize_t active_show(struct device *dev, struct device_attribute *attr,
  230. char *buf)
  231. {
  232. struct intel_th_device *thdev = to_intel_th_device(dev);
  233. return scnprintf(buf, PAGE_SIZE, "%d\n", thdev->output.active);
  234. }
  235. static ssize_t active_store(struct device *dev, struct device_attribute *attr,
  236. const char *buf, size_t size)
  237. {
  238. struct intel_th_device *thdev = to_intel_th_device(dev);
  239. unsigned long val;
  240. int ret;
  241. ret = kstrtoul(buf, 10, &val);
  242. if (ret)
  243. return ret;
  244. if (!!val != thdev->output.active) {
  245. if (val)
  246. ret = intel_th_output_activate(thdev);
  247. else
  248. intel_th_output_deactivate(thdev);
  249. }
  250. return ret ? ret : size;
  251. }
  252. static DEVICE_ATTR_RW(active);
  253. static struct attribute *intel_th_output_attrs[] = {
  254. &dev_attr_port.attr,
  255. &dev_attr_active.attr,
  256. NULL,
  257. };
  258. ATTRIBUTE_GROUPS(intel_th_output);
  259. static const struct device_type intel_th_output_device_type = {
  260. .name = "intel_th_output_device",
  261. .groups = intel_th_output_groups,
  262. .release = intel_th_device_release,
  263. .devnode = intel_th_output_devnode,
  264. };
  265. static const struct device_type intel_th_switch_device_type = {
  266. .name = "intel_th_switch_device",
  267. .release = intel_th_device_release,
  268. };
  269. static const struct device_type *intel_th_device_type[] = {
  270. [INTEL_TH_SOURCE] = &intel_th_source_device_type,
  271. [INTEL_TH_OUTPUT] = &intel_th_output_device_type,
  272. [INTEL_TH_SWITCH] = &intel_th_switch_device_type,
  273. };
  274. int intel_th_driver_register(struct intel_th_driver *thdrv)
  275. {
  276. if (!thdrv->probe || !thdrv->remove)
  277. return -EINVAL;
  278. thdrv->driver.bus = &intel_th_bus;
  279. return driver_register(&thdrv->driver);
  280. }
  281. EXPORT_SYMBOL_GPL(intel_th_driver_register);
  282. void intel_th_driver_unregister(struct intel_th_driver *thdrv)
  283. {
  284. driver_unregister(&thdrv->driver);
  285. }
  286. EXPORT_SYMBOL_GPL(intel_th_driver_unregister);
  287. static struct intel_th_device *
  288. intel_th_device_alloc(struct intel_th *th, unsigned int type, const char *name,
  289. int id)
  290. {
  291. struct device *parent;
  292. struct intel_th_device *thdev;
  293. if (type == INTEL_TH_OUTPUT)
  294. parent = &th->hub->dev;
  295. else
  296. parent = th->dev;
  297. thdev = kzalloc(sizeof(*thdev) + strlen(name) + 1, GFP_KERNEL);
  298. if (!thdev)
  299. return NULL;
  300. thdev->id = id;
  301. thdev->type = type;
  302. strcpy(thdev->name, name);
  303. device_initialize(&thdev->dev);
  304. thdev->dev.bus = &intel_th_bus;
  305. thdev->dev.type = intel_th_device_type[type];
  306. thdev->dev.parent = parent;
  307. thdev->dev.dma_mask = parent->dma_mask;
  308. thdev->dev.dma_parms = parent->dma_parms;
  309. dma_set_coherent_mask(&thdev->dev, parent->coherent_dma_mask);
  310. if (id >= 0)
  311. dev_set_name(&thdev->dev, "%d-%s%d", th->id, name, id);
  312. else
  313. dev_set_name(&thdev->dev, "%d-%s", th->id, name);
  314. return thdev;
  315. }
  316. static int intel_th_device_add_resources(struct intel_th_device *thdev,
  317. struct resource *res, int nres)
  318. {
  319. struct resource *r;
  320. r = kmemdup(res, sizeof(*res) * nres, GFP_KERNEL);
  321. if (!r)
  322. return -ENOMEM;
  323. thdev->resource = r;
  324. thdev->num_resources = nres;
  325. return 0;
  326. }
  327. static void intel_th_device_remove(struct intel_th_device *thdev)
  328. {
  329. device_del(&thdev->dev);
  330. put_device(&thdev->dev);
  331. }
  332. static void intel_th_device_free(struct intel_th_device *thdev)
  333. {
  334. kfree(thdev->resource);
  335. kfree(thdev);
  336. }
  337. /*
  338. * Intel(R) Trace Hub subdevices
  339. */
  340. static const struct intel_th_subdevice {
  341. const char *name;
  342. struct resource res[3];
  343. unsigned nres;
  344. unsigned type;
  345. unsigned otype;
  346. bool mknode;
  347. unsigned scrpd;
  348. int id;
  349. } intel_th_subdevices[] = {
  350. {
  351. .nres = 1,
  352. .res = {
  353. {
  354. /* Handle TSCU and CTS from GTH driver */
  355. .start = REG_GTH_OFFSET,
  356. .end = REG_CTS_OFFSET + REG_CTS_LENGTH - 1,
  357. .flags = IORESOURCE_MEM,
  358. },
  359. },
  360. .name = "gth",
  361. .type = INTEL_TH_SWITCH,
  362. .id = -1,
  363. },
  364. {
  365. .nres = 2,
  366. .res = {
  367. {
  368. .start = REG_MSU_OFFSET,
  369. .end = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
  370. .flags = IORESOURCE_MEM,
  371. },
  372. {
  373. .start = BUF_MSU_OFFSET,
  374. .end = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
  375. .flags = IORESOURCE_MEM,
  376. },
  377. },
  378. .name = "msc",
  379. .id = 0,
  380. .type = INTEL_TH_OUTPUT,
  381. .mknode = true,
  382. .otype = GTH_MSU,
  383. .scrpd = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC0_IS_ENABLED,
  384. },
  385. {
  386. .nres = 2,
  387. .res = {
  388. {
  389. .start = REG_MSU_OFFSET,
  390. .end = REG_MSU_OFFSET + REG_MSU_LENGTH - 1,
  391. .flags = IORESOURCE_MEM,
  392. },
  393. {
  394. .start = BUF_MSU_OFFSET,
  395. .end = BUF_MSU_OFFSET + BUF_MSU_LENGTH - 1,
  396. .flags = IORESOURCE_MEM,
  397. },
  398. },
  399. .name = "msc",
  400. .id = 1,
  401. .type = INTEL_TH_OUTPUT,
  402. .mknode = true,
  403. .otype = GTH_MSU,
  404. .scrpd = SCRPD_MEM_IS_PRIM_DEST | SCRPD_MSC1_IS_ENABLED,
  405. },
  406. {
  407. .nres = 2,
  408. .res = {
  409. {
  410. .start = REG_STH_OFFSET,
  411. .end = REG_STH_OFFSET + REG_STH_LENGTH - 1,
  412. .flags = IORESOURCE_MEM,
  413. },
  414. {
  415. .start = TH_MMIO_SW,
  416. .end = 0,
  417. .flags = IORESOURCE_MEM,
  418. },
  419. },
  420. .id = -1,
  421. .name = "sth",
  422. .type = INTEL_TH_SOURCE,
  423. },
  424. {
  425. .nres = 2,
  426. .res = {
  427. {
  428. .start = REG_STH_OFFSET,
  429. .end = REG_STH_OFFSET + REG_STH_LENGTH - 1,
  430. .flags = IORESOURCE_MEM,
  431. },
  432. {
  433. .start = TH_MMIO_RTIT,
  434. .end = 0,
  435. .flags = IORESOURCE_MEM,
  436. },
  437. },
  438. .id = -1,
  439. .name = "rtit",
  440. .type = INTEL_TH_SOURCE,
  441. },
  442. {
  443. .nres = 1,
  444. .res = {
  445. {
  446. .start = REG_PTI_OFFSET,
  447. .end = REG_PTI_OFFSET + REG_PTI_LENGTH - 1,
  448. .flags = IORESOURCE_MEM,
  449. },
  450. },
  451. .id = -1,
  452. .name = "pti",
  453. .type = INTEL_TH_OUTPUT,
  454. .otype = GTH_PTI,
  455. .scrpd = SCRPD_PTI_IS_PRIM_DEST,
  456. },
  457. {
  458. .nres = 1,
  459. .res = {
  460. {
  461. .start = REG_PTI_OFFSET,
  462. .end = REG_PTI_OFFSET + REG_PTI_LENGTH - 1,
  463. .flags = IORESOURCE_MEM,
  464. },
  465. },
  466. .id = -1,
  467. .name = "lpp",
  468. .type = INTEL_TH_OUTPUT,
  469. .otype = GTH_LPP,
  470. .scrpd = SCRPD_PTI_IS_PRIM_DEST,
  471. },
  472. {
  473. .nres = 1,
  474. .res = {
  475. {
  476. .start = REG_DCIH_OFFSET,
  477. .end = REG_DCIH_OFFSET + REG_DCIH_LENGTH - 1,
  478. .flags = IORESOURCE_MEM,
  479. },
  480. },
  481. .id = -1,
  482. .name = "dcih",
  483. .type = INTEL_TH_OUTPUT,
  484. },
  485. };
  486. #ifdef CONFIG_MODULES
  487. static void __intel_th_request_hub_module(struct work_struct *work)
  488. {
  489. struct intel_th *th = container_of(work, struct intel_th,
  490. request_module_work);
  491. request_module("intel_th_%s", th->hub->name);
  492. }
  493. static int intel_th_request_hub_module(struct intel_th *th)
  494. {
  495. INIT_WORK(&th->request_module_work, __intel_th_request_hub_module);
  496. schedule_work(&th->request_module_work);
  497. return 0;
  498. }
  499. static void intel_th_request_hub_module_flush(struct intel_th *th)
  500. {
  501. flush_work(&th->request_module_work);
  502. }
  503. #else
  504. static inline int intel_th_request_hub_module(struct intel_th *th)
  505. {
  506. return -EINVAL;
  507. }
  508. static inline void intel_th_request_hub_module_flush(struct intel_th *th)
  509. {
  510. }
  511. #endif /* CONFIG_MODULES */
  512. static struct intel_th_device *
  513. intel_th_subdevice_alloc(struct intel_th *th,
  514. const struct intel_th_subdevice *subdev)
  515. {
  516. struct intel_th_device *thdev;
  517. struct resource res[3];
  518. unsigned int req = 0;
  519. int r, err;
  520. thdev = intel_th_device_alloc(th, subdev->type, subdev->name,
  521. subdev->id);
  522. if (!thdev)
  523. return ERR_PTR(-ENOMEM);
  524. thdev->drvdata = th->drvdata;
  525. memcpy(res, subdev->res,
  526. sizeof(struct resource) * subdev->nres);
  527. for (r = 0; r < subdev->nres; r++) {
  528. struct resource *devres = th->resource;
  529. int bar = TH_MMIO_CONFIG;
  530. /*
  531. * Take .end == 0 to mean 'take the whole bar',
  532. * .start then tells us which bar it is. Default to
  533. * TH_MMIO_CONFIG.
  534. */
  535. if (!res[r].end && res[r].flags == IORESOURCE_MEM) {
  536. bar = res[r].start;
  537. err = -ENODEV;
  538. if (bar >= th->num_resources)
  539. goto fail_put_device;
  540. res[r].start = 0;
  541. res[r].end = resource_size(&devres[bar]) - 1;
  542. }
  543. if (res[r].flags & IORESOURCE_MEM) {
  544. res[r].start += devres[bar].start;
  545. res[r].end += devres[bar].start;
  546. dev_dbg(th->dev, "%s:%d @ %pR\n",
  547. subdev->name, r, &res[r]);
  548. } else if (res[r].flags & IORESOURCE_IRQ) {
  549. /*
  550. * Only pass on the IRQ if we have useful interrupts:
  551. * the ones that can be configured via MINTCTL.
  552. */
  553. if (INTEL_TH_CAP(th, has_mintctl) && th->irq != -1)
  554. res[r].start = th->irq;
  555. }
  556. }
  557. err = intel_th_device_add_resources(thdev, res, subdev->nres);
  558. if (err)
  559. goto fail_put_device;
  560. if (subdev->type == INTEL_TH_OUTPUT) {
  561. if (subdev->mknode)
  562. thdev->dev.devt = MKDEV(th->major, th->num_thdevs);
  563. thdev->output.type = subdev->otype;
  564. thdev->output.port = -1;
  565. thdev->output.scratchpad = subdev->scrpd;
  566. } else if (subdev->type == INTEL_TH_SWITCH) {
  567. thdev->host_mode =
  568. INTEL_TH_CAP(th, host_mode_only) ? true : host_mode;
  569. th->hub = thdev;
  570. }
  571. err = device_add(&thdev->dev);
  572. if (err)
  573. goto fail_free_res;
  574. /* need switch driver to be loaded to enumerate the rest */
  575. if (subdev->type == INTEL_TH_SWITCH && !req) {
  576. err = intel_th_request_hub_module(th);
  577. if (!err)
  578. req++;
  579. }
  580. return thdev;
  581. fail_free_res:
  582. kfree(thdev->resource);
  583. fail_put_device:
  584. put_device(&thdev->dev);
  585. return ERR_PTR(err);
  586. }
  587. /**
  588. * intel_th_output_enable() - find and enable a device for a given output type
  589. * @th: Intel TH instance
  590. * @otype: output type
  591. *
  592. * Go through the unallocated output devices, find the first one whos type
  593. * matches @otype and instantiate it. These devices are removed when the hub
  594. * device is removed, see intel_th_remove().
  595. */
  596. int intel_th_output_enable(struct intel_th *th, unsigned int otype)
  597. {
  598. struct intel_th_device *thdev;
  599. int src = 0, dst = 0;
  600. for (src = 0, dst = 0; dst <= th->num_thdevs; src++, dst++) {
  601. for (; src < ARRAY_SIZE(intel_th_subdevices); src++) {
  602. if (intel_th_subdevices[src].type != INTEL_TH_OUTPUT)
  603. continue;
  604. if (intel_th_subdevices[src].otype != otype)
  605. continue;
  606. break;
  607. }
  608. /* no unallocated matching subdevices */
  609. if (src == ARRAY_SIZE(intel_th_subdevices))
  610. return -ENODEV;
  611. for (; dst < th->num_thdevs; dst++) {
  612. if (th->thdev[dst]->type != INTEL_TH_OUTPUT)
  613. continue;
  614. if (th->thdev[dst]->output.type != otype)
  615. continue;
  616. break;
  617. }
  618. /*
  619. * intel_th_subdevices[src] matches our requirements and is
  620. * not matched in th::thdev[]
  621. */
  622. if (dst == th->num_thdevs)
  623. goto found;
  624. }
  625. return -ENODEV;
  626. found:
  627. thdev = intel_th_subdevice_alloc(th, &intel_th_subdevices[src]);
  628. if (IS_ERR(thdev))
  629. return PTR_ERR(thdev);
  630. th->thdev[th->num_thdevs++] = thdev;
  631. return 0;
  632. }
  633. EXPORT_SYMBOL_GPL(intel_th_output_enable);
  634. static int intel_th_populate(struct intel_th *th)
  635. {
  636. int src;
  637. /* create devices for each intel_th_subdevice */
  638. for (src = 0; src < ARRAY_SIZE(intel_th_subdevices); src++) {
  639. const struct intel_th_subdevice *subdev =
  640. &intel_th_subdevices[src];
  641. struct intel_th_device *thdev;
  642. /* only allow SOURCE and SWITCH devices in host mode */
  643. if ((INTEL_TH_CAP(th, host_mode_only) || host_mode) &&
  644. subdev->type == INTEL_TH_OUTPUT)
  645. continue;
  646. /*
  647. * don't enable port OUTPUTs in this path; SWITCH enables them
  648. * via intel_th_output_enable()
  649. */
  650. if (subdev->type == INTEL_TH_OUTPUT &&
  651. subdev->otype != GTH_NONE)
  652. continue;
  653. thdev = intel_th_subdevice_alloc(th, subdev);
  654. /* note: caller should free subdevices from th::thdev[] */
  655. if (IS_ERR(thdev)) {
  656. /* ENODEV for individual subdevices is allowed */
  657. if (PTR_ERR(thdev) == -ENODEV)
  658. continue;
  659. return PTR_ERR(thdev);
  660. }
  661. th->thdev[th->num_thdevs++] = thdev;
  662. }
  663. return 0;
  664. }
  665. static int intel_th_output_open(struct inode *inode, struct file *file)
  666. {
  667. const struct file_operations *fops;
  668. struct intel_th_driver *thdrv;
  669. struct device *dev;
  670. int err;
  671. dev = bus_find_device_by_devt(&intel_th_bus, inode->i_rdev);
  672. if (!dev || !dev->driver)
  673. return -ENODEV;
  674. thdrv = to_intel_th_driver(dev->driver);
  675. fops = fops_get(thdrv->fops);
  676. if (!fops)
  677. return -ENODEV;
  678. replace_fops(file, fops);
  679. file->private_data = to_intel_th_device(dev);
  680. if (file->f_op->open) {
  681. err = file->f_op->open(inode, file);
  682. return err;
  683. }
  684. return 0;
  685. }
  686. static const struct file_operations intel_th_output_fops = {
  687. .open = intel_th_output_open,
  688. .llseek = noop_llseek,
  689. };
  690. static irqreturn_t intel_th_irq(int irq, void *data)
  691. {
  692. struct intel_th *th = data;
  693. irqreturn_t ret = IRQ_NONE;
  694. struct intel_th_driver *d;
  695. int i;
  696. for (i = 0; i < th->num_thdevs; i++) {
  697. if (th->thdev[i]->type != INTEL_TH_OUTPUT)
  698. continue;
  699. d = to_intel_th_driver(th->thdev[i]->dev.driver);
  700. if (d && d->irq)
  701. ret |= d->irq(th->thdev[i]);
  702. }
  703. return ret;
  704. }
  705. /**
  706. * intel_th_alloc() - allocate a new Intel TH device and its subdevices
  707. * @dev: parent device
  708. * @devres: resources indexed by th_mmio_idx
  709. * @irq: irq number
  710. */
  711. struct intel_th *
  712. intel_th_alloc(struct device *dev, const struct intel_th_drvdata *drvdata,
  713. struct resource *devres, unsigned int ndevres)
  714. {
  715. int err, r, nr_mmios = 0;
  716. struct intel_th *th;
  717. th = kzalloc(sizeof(*th), GFP_KERNEL);
  718. if (!th)
  719. return ERR_PTR(-ENOMEM);
  720. th->id = ida_alloc(&intel_th_ida, GFP_KERNEL);
  721. if (th->id < 0) {
  722. err = th->id;
  723. goto err_alloc;
  724. }
  725. th->major = __register_chrdev(0, 0, TH_POSSIBLE_OUTPUTS,
  726. "intel_th/output", &intel_th_output_fops);
  727. if (th->major < 0) {
  728. err = th->major;
  729. goto err_ida;
  730. }
  731. th->irq = -1;
  732. th->dev = dev;
  733. th->drvdata = drvdata;
  734. for (r = 0; r < ndevres; r++)
  735. switch (devres[r].flags & IORESOURCE_TYPE_BITS) {
  736. case IORESOURCE_MEM:
  737. th->resource[nr_mmios++] = devres[r];
  738. break;
  739. case IORESOURCE_IRQ:
  740. err = devm_request_irq(dev, devres[r].start,
  741. intel_th_irq, IRQF_SHARED,
  742. dev_name(dev), th);
  743. if (err)
  744. goto err_chrdev;
  745. if (th->irq == -1)
  746. th->irq = devres[r].start;
  747. th->num_irqs++;
  748. break;
  749. default:
  750. dev_warn(dev, "Unknown resource type %lx\n",
  751. devres[r].flags);
  752. break;
  753. }
  754. th->num_resources = nr_mmios;
  755. dev_set_drvdata(dev, th);
  756. pm_runtime_no_callbacks(dev);
  757. pm_runtime_put(dev);
  758. pm_runtime_allow(dev);
  759. err = intel_th_populate(th);
  760. if (err) {
  761. /* free the subdevices and undo everything */
  762. intel_th_free(th);
  763. return ERR_PTR(err);
  764. }
  765. return th;
  766. err_chrdev:
  767. __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
  768. "intel_th/output");
  769. err_ida:
  770. ida_free(&intel_th_ida, th->id);
  771. err_alloc:
  772. kfree(th);
  773. return ERR_PTR(err);
  774. }
  775. EXPORT_SYMBOL_GPL(intel_th_alloc);
  776. void intel_th_free(struct intel_th *th)
  777. {
  778. int i;
  779. intel_th_request_hub_module_flush(th);
  780. intel_th_device_remove(th->hub);
  781. for (i = 0; i < th->num_thdevs; i++) {
  782. if (th->thdev[i] != th->hub)
  783. intel_th_device_remove(th->thdev[i]);
  784. th->thdev[i] = NULL;
  785. }
  786. th->num_thdevs = 0;
  787. for (i = 0; i < th->num_irqs; i++)
  788. devm_free_irq(th->dev, th->irq + i, th);
  789. pm_runtime_get_sync(th->dev);
  790. pm_runtime_forbid(th->dev);
  791. __unregister_chrdev(th->major, 0, TH_POSSIBLE_OUTPUTS,
  792. "intel_th/output");
  793. ida_free(&intel_th_ida, th->id);
  794. kfree(th);
  795. }
  796. EXPORT_SYMBOL_GPL(intel_th_free);
  797. /**
  798. * intel_th_trace_enable() - enable tracing for an output device
  799. * @thdev: output device that requests tracing be enabled
  800. */
  801. int intel_th_trace_enable(struct intel_th_device *thdev)
  802. {
  803. struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
  804. struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
  805. if (WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH))
  806. return -EINVAL;
  807. if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
  808. return -EINVAL;
  809. pm_runtime_get_sync(&thdev->dev);
  810. hubdrv->enable(hub, &thdev->output);
  811. return 0;
  812. }
  813. EXPORT_SYMBOL_GPL(intel_th_trace_enable);
  814. /**
  815. * intel_th_trace_switch() - execute a switch sequence
  816. * @thdev: output device that requests tracing switch
  817. */
  818. int intel_th_trace_switch(struct intel_th_device *thdev)
  819. {
  820. struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
  821. struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
  822. if (WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH))
  823. return -EINVAL;
  824. if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
  825. return -EINVAL;
  826. hubdrv->trig_switch(hub, &thdev->output);
  827. return 0;
  828. }
  829. EXPORT_SYMBOL_GPL(intel_th_trace_switch);
  830. /**
  831. * intel_th_trace_disable() - disable tracing for an output device
  832. * @thdev: output device that requests tracing be disabled
  833. */
  834. int intel_th_trace_disable(struct intel_th_device *thdev)
  835. {
  836. struct intel_th_device *hub = to_intel_th_device(thdev->dev.parent);
  837. struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
  838. WARN_ON_ONCE(hub->type != INTEL_TH_SWITCH);
  839. if (WARN_ON_ONCE(thdev->type != INTEL_TH_OUTPUT))
  840. return -EINVAL;
  841. hubdrv->disable(hub, &thdev->output);
  842. pm_runtime_put(&thdev->dev);
  843. return 0;
  844. }
  845. EXPORT_SYMBOL_GPL(intel_th_trace_disable);
  846. int intel_th_set_output(struct intel_th_device *thdev,
  847. unsigned int master)
  848. {
  849. struct intel_th_device *hub = to_intel_th_hub(thdev);
  850. struct intel_th_driver *hubdrv = to_intel_th_driver(hub->dev.driver);
  851. int ret;
  852. /* In host mode, this is up to the external debugger, do nothing. */
  853. if (hub->host_mode)
  854. return 0;
  855. /*
  856. * hub is instantiated together with the source device that
  857. * calls here, so guaranteed to be present.
  858. */
  859. hubdrv = to_intel_th_driver(hub->dev.driver);
  860. if (!hubdrv || !try_module_get(hubdrv->driver.owner))
  861. return -EINVAL;
  862. if (!hubdrv->set_output) {
  863. ret = -ENOTSUPP;
  864. goto out;
  865. }
  866. ret = hubdrv->set_output(hub, master);
  867. out:
  868. module_put(hubdrv->driver.owner);
  869. return ret;
  870. }
  871. EXPORT_SYMBOL_GPL(intel_th_set_output);
  872. static int __init intel_th_init(void)
  873. {
  874. intel_th_debug_init();
  875. return bus_register(&intel_th_bus);
  876. }
  877. subsys_initcall(intel_th_init);
  878. static void __exit intel_th_exit(void)
  879. {
  880. intel_th_debug_done();
  881. bus_unregister(&intel_th_bus);
  882. }
  883. module_exit(intel_th_exit);
  884. MODULE_LICENSE("GPL v2");
  885. MODULE_DESCRIPTION("Intel(R) Trace Hub controller driver");
  886. MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");