gth.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intel(R) Trace Hub Global Trace Hub
  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/io.h>
  12. #include <linux/mm.h>
  13. #include <linux/slab.h>
  14. #include <linux/bitmap.h>
  15. #include <linux/pm_runtime.h>
  16. #include "intel_th.h"
  17. #include "gth.h"
  18. struct gth_device;
  19. /**
  20. * struct gth_output - GTH view on an output port
  21. * @gth: backlink to the GTH device
  22. * @output: link to output device's output descriptor
  23. * @index: output port number
  24. * @port_type: one of GTH_* port type values
  25. * @master: bitmap of masters configured for this output
  26. */
  27. struct gth_output {
  28. struct gth_device *gth;
  29. struct intel_th_output *output;
  30. unsigned int index;
  31. unsigned int port_type;
  32. DECLARE_BITMAP(master, TH_CONFIGURABLE_MASTERS + 1);
  33. };
  34. /**
  35. * struct gth_device - GTH device
  36. * @dev: driver core's device
  37. * @base: register window base address
  38. * @output_group: attributes describing output ports
  39. * @master_group: attributes describing master assignments
  40. * @output: output ports
  41. * @master: master/output port assignments
  42. * @gth_lock: serializes accesses to GTH bits
  43. */
  44. struct gth_device {
  45. struct device *dev;
  46. void __iomem *base;
  47. struct attribute_group output_group;
  48. struct attribute_group master_group;
  49. struct gth_output output[TH_POSSIBLE_OUTPUTS];
  50. signed char master[TH_CONFIGURABLE_MASTERS + 1];
  51. spinlock_t gth_lock;
  52. };
  53. static void gth_output_set(struct gth_device *gth, int port,
  54. unsigned int config)
  55. {
  56. unsigned long reg = port & 4 ? REG_GTH_GTHOPT1 : REG_GTH_GTHOPT0;
  57. u32 val;
  58. int shift = (port & 3) * 8;
  59. val = ioread32(gth->base + reg);
  60. val &= ~(0xff << shift);
  61. val |= config << shift;
  62. iowrite32(val, gth->base + reg);
  63. }
  64. static unsigned int gth_output_get(struct gth_device *gth, int port)
  65. {
  66. unsigned long reg = port & 4 ? REG_GTH_GTHOPT1 : REG_GTH_GTHOPT0;
  67. u32 val;
  68. int shift = (port & 3) * 8;
  69. val = ioread32(gth->base + reg);
  70. val &= 0xff << shift;
  71. val >>= shift;
  72. return val;
  73. }
  74. static void gth_smcfreq_set(struct gth_device *gth, int port,
  75. unsigned int freq)
  76. {
  77. unsigned long reg = REG_GTH_SMCR0 + ((port / 2) * 4);
  78. int shift = (port & 1) * 16;
  79. u32 val;
  80. val = ioread32(gth->base + reg);
  81. val &= ~(0xffff << shift);
  82. val |= freq << shift;
  83. iowrite32(val, gth->base + reg);
  84. }
  85. static unsigned int gth_smcfreq_get(struct gth_device *gth, int port)
  86. {
  87. unsigned long reg = REG_GTH_SMCR0 + ((port / 2) * 4);
  88. int shift = (port & 1) * 16;
  89. u32 val;
  90. val = ioread32(gth->base + reg);
  91. val &= 0xffff << shift;
  92. val >>= shift;
  93. return val;
  94. }
  95. /*
  96. * "masters" attribute group
  97. */
  98. struct master_attribute {
  99. struct device_attribute attr;
  100. struct gth_device *gth;
  101. unsigned int master;
  102. };
  103. static void
  104. gth_master_set(struct gth_device *gth, unsigned int master, int port)
  105. {
  106. unsigned int reg = REG_GTH_SWDEST0 + ((master >> 1) & ~3u);
  107. unsigned int shift = (master & 0x7) * 4;
  108. u32 val;
  109. if (master >= 256) {
  110. reg = REG_GTH_GSWTDEST;
  111. shift = 0;
  112. }
  113. val = ioread32(gth->base + reg);
  114. val &= ~(0xf << shift);
  115. if (port >= 0)
  116. val |= (0x8 | port) << shift;
  117. iowrite32(val, gth->base + reg);
  118. }
  119. static ssize_t master_attr_show(struct device *dev,
  120. struct device_attribute *attr,
  121. char *buf)
  122. {
  123. struct master_attribute *ma =
  124. container_of(attr, struct master_attribute, attr);
  125. struct gth_device *gth = ma->gth;
  126. size_t count;
  127. int port;
  128. spin_lock(&gth->gth_lock);
  129. port = gth->master[ma->master];
  130. spin_unlock(&gth->gth_lock);
  131. if (port >= 0)
  132. count = sysfs_emit(buf, "%x\n", port);
  133. else
  134. count = sysfs_emit(buf, "disabled\n");
  135. return count;
  136. }
  137. static ssize_t master_attr_store(struct device *dev,
  138. struct device_attribute *attr,
  139. const char *buf, size_t count)
  140. {
  141. struct master_attribute *ma =
  142. container_of(attr, struct master_attribute, attr);
  143. struct gth_device *gth = ma->gth;
  144. int old_port, port;
  145. if (kstrtoint(buf, 10, &port) < 0)
  146. return -EINVAL;
  147. if (port >= TH_POSSIBLE_OUTPUTS || port < -1)
  148. return -EINVAL;
  149. spin_lock(&gth->gth_lock);
  150. /* disconnect from the previous output port, if any */
  151. old_port = gth->master[ma->master];
  152. if (old_port >= 0) {
  153. gth->master[ma->master] = -1;
  154. clear_bit(ma->master, gth->output[old_port].master);
  155. /*
  156. * if the port is active, program this setting,
  157. * implies that runtime PM is on
  158. */
  159. if (gth->output[old_port].output->active)
  160. gth_master_set(gth, ma->master, -1);
  161. }
  162. /* connect to the new output port, if any */
  163. if (port >= 0) {
  164. /* check if there's a driver for this port */
  165. if (!gth->output[port].output) {
  166. count = -ENODEV;
  167. goto unlock;
  168. }
  169. set_bit(ma->master, gth->output[port].master);
  170. /* if the port is active, program this setting, see above */
  171. if (gth->output[port].output->active)
  172. gth_master_set(gth, ma->master, port);
  173. }
  174. gth->master[ma->master] = port;
  175. unlock:
  176. spin_unlock(&gth->gth_lock);
  177. return count;
  178. }
  179. struct output_attribute {
  180. struct device_attribute attr;
  181. struct gth_device *gth;
  182. unsigned int port;
  183. unsigned int parm;
  184. };
  185. #define OUTPUT_PARM(_name, _mask, _r, _w, _what) \
  186. [TH_OUTPUT_PARM(_name)] = { .name = __stringify(_name), \
  187. .get = gth_ ## _what ## _get, \
  188. .set = gth_ ## _what ## _set, \
  189. .mask = (_mask), \
  190. .readable = (_r), \
  191. .writable = (_w) }
  192. static const struct output_parm {
  193. const char *name;
  194. unsigned int (*get)(struct gth_device *gth, int port);
  195. void (*set)(struct gth_device *gth, int port,
  196. unsigned int val);
  197. unsigned int mask;
  198. unsigned int readable : 1,
  199. writable : 1;
  200. } output_parms[] = {
  201. OUTPUT_PARM(port, 0x7, 1, 0, output),
  202. OUTPUT_PARM(null, BIT(3), 1, 1, output),
  203. OUTPUT_PARM(drop, BIT(4), 1, 1, output),
  204. OUTPUT_PARM(reset, BIT(5), 1, 0, output),
  205. OUTPUT_PARM(flush, BIT(7), 0, 1, output),
  206. OUTPUT_PARM(smcfreq, 0xffff, 1, 1, smcfreq),
  207. };
  208. static void
  209. gth_output_parm_set(struct gth_device *gth, int port, unsigned int parm,
  210. unsigned int val)
  211. {
  212. unsigned int config = output_parms[parm].get(gth, port);
  213. unsigned int mask = output_parms[parm].mask;
  214. unsigned int shift = __ffs(mask);
  215. config &= ~mask;
  216. config |= (val << shift) & mask;
  217. output_parms[parm].set(gth, port, config);
  218. }
  219. static unsigned int
  220. gth_output_parm_get(struct gth_device *gth, int port, unsigned int parm)
  221. {
  222. unsigned int config = output_parms[parm].get(gth, port);
  223. unsigned int mask = output_parms[parm].mask;
  224. unsigned int shift = __ffs(mask);
  225. config &= mask;
  226. config >>= shift;
  227. return config;
  228. }
  229. /*
  230. * Reset outputs and sources
  231. */
  232. static int intel_th_gth_reset(struct gth_device *gth)
  233. {
  234. u32 reg;
  235. int port, i;
  236. reg = ioread32(gth->base + REG_GTH_SCRPD0);
  237. if (reg & SCRPD_DEBUGGER_IN_USE)
  238. return -EBUSY;
  239. /* Always save/restore STH and TU registers in S0ix entry/exit */
  240. reg |= SCRPD_STH_IS_ENABLED | SCRPD_TRIGGER_IS_ENABLED;
  241. iowrite32(reg, gth->base + REG_GTH_SCRPD0);
  242. /* output ports */
  243. for (port = 0; port < 8; port++) {
  244. if (gth_output_parm_get(gth, port, TH_OUTPUT_PARM(port)) ==
  245. GTH_NONE)
  246. continue;
  247. gth_output_set(gth, port, 0);
  248. gth_smcfreq_set(gth, port, 16);
  249. }
  250. /* disable overrides */
  251. iowrite32(0, gth->base + REG_GTH_DESTOVR);
  252. /* masters swdest_0~31 and gswdest */
  253. for (i = 0; i < 33; i++)
  254. iowrite32(0, gth->base + REG_GTH_SWDEST0 + i * 4);
  255. /* sources */
  256. iowrite32(0, gth->base + REG_GTH_SCR);
  257. iowrite32(0xfc, gth->base + REG_GTH_SCR2);
  258. /* setup CTS for single trigger */
  259. iowrite32(CTS_EVENT_ENABLE_IF_ANYTHING, gth->base + REG_CTS_C0S0_EN);
  260. iowrite32(CTS_ACTION_CONTROL_SET_STATE(CTS_STATE_IDLE) |
  261. CTS_ACTION_CONTROL_TRIGGER, gth->base + REG_CTS_C0S0_ACT);
  262. return 0;
  263. }
  264. /*
  265. * "outputs" attribute group
  266. */
  267. static ssize_t output_attr_show(struct device *dev,
  268. struct device_attribute *attr,
  269. char *buf)
  270. {
  271. struct output_attribute *oa =
  272. container_of(attr, struct output_attribute, attr);
  273. struct gth_device *gth = oa->gth;
  274. size_t count;
  275. pm_runtime_get_sync(dev);
  276. spin_lock(&gth->gth_lock);
  277. count = sysfs_emit(buf, "%x\n",
  278. gth_output_parm_get(gth, oa->port, oa->parm));
  279. spin_unlock(&gth->gth_lock);
  280. pm_runtime_put(dev);
  281. return count;
  282. }
  283. static ssize_t output_attr_store(struct device *dev,
  284. struct device_attribute *attr,
  285. const char *buf, size_t count)
  286. {
  287. struct output_attribute *oa =
  288. container_of(attr, struct output_attribute, attr);
  289. struct gth_device *gth = oa->gth;
  290. unsigned int config;
  291. if (kstrtouint(buf, 16, &config) < 0)
  292. return -EINVAL;
  293. pm_runtime_get_sync(dev);
  294. spin_lock(&gth->gth_lock);
  295. gth_output_parm_set(gth, oa->port, oa->parm, config);
  296. spin_unlock(&gth->gth_lock);
  297. pm_runtime_put(dev);
  298. return count;
  299. }
  300. static int intel_th_master_attributes(struct gth_device *gth)
  301. {
  302. struct master_attribute *master_attrs;
  303. struct attribute **attrs;
  304. int i, nattrs = TH_CONFIGURABLE_MASTERS + 2;
  305. attrs = devm_kcalloc(gth->dev, nattrs, sizeof(void *), GFP_KERNEL);
  306. if (!attrs)
  307. return -ENOMEM;
  308. master_attrs = devm_kcalloc(gth->dev, nattrs,
  309. sizeof(struct master_attribute),
  310. GFP_KERNEL);
  311. if (!master_attrs)
  312. return -ENOMEM;
  313. for (i = 0; i < TH_CONFIGURABLE_MASTERS + 1; i++) {
  314. char *name;
  315. name = devm_kasprintf(gth->dev, GFP_KERNEL, "%d%s", i,
  316. i == TH_CONFIGURABLE_MASTERS ? "+" : "");
  317. if (!name)
  318. return -ENOMEM;
  319. master_attrs[i].attr.attr.name = name;
  320. master_attrs[i].attr.attr.mode = S_IRUGO | S_IWUSR;
  321. master_attrs[i].attr.show = master_attr_show;
  322. master_attrs[i].attr.store = master_attr_store;
  323. sysfs_attr_init(&master_attrs[i].attr.attr);
  324. attrs[i] = &master_attrs[i].attr.attr;
  325. master_attrs[i].gth = gth;
  326. master_attrs[i].master = i;
  327. }
  328. gth->master_group.name = "masters";
  329. gth->master_group.attrs = attrs;
  330. return sysfs_create_group(&gth->dev->kobj, &gth->master_group);
  331. }
  332. static int intel_th_output_attributes(struct gth_device *gth)
  333. {
  334. struct output_attribute *out_attrs;
  335. struct attribute **attrs;
  336. int i, j, nouts = TH_POSSIBLE_OUTPUTS;
  337. int nparms = ARRAY_SIZE(output_parms);
  338. int nattrs = nouts * nparms + 1;
  339. attrs = devm_kcalloc(gth->dev, nattrs, sizeof(void *), GFP_KERNEL);
  340. if (!attrs)
  341. return -ENOMEM;
  342. out_attrs = devm_kcalloc(gth->dev, nattrs,
  343. sizeof(struct output_attribute),
  344. GFP_KERNEL);
  345. if (!out_attrs)
  346. return -ENOMEM;
  347. for (i = 0; i < nouts; i++) {
  348. for (j = 0; j < nparms; j++) {
  349. unsigned int idx = i * nparms + j;
  350. char *name;
  351. name = devm_kasprintf(gth->dev, GFP_KERNEL, "%d_%s", i,
  352. output_parms[j].name);
  353. if (!name)
  354. return -ENOMEM;
  355. out_attrs[idx].attr.attr.name = name;
  356. if (output_parms[j].readable) {
  357. out_attrs[idx].attr.attr.mode |= S_IRUGO;
  358. out_attrs[idx].attr.show = output_attr_show;
  359. }
  360. if (output_parms[j].writable) {
  361. out_attrs[idx].attr.attr.mode |= S_IWUSR;
  362. out_attrs[idx].attr.store = output_attr_store;
  363. }
  364. sysfs_attr_init(&out_attrs[idx].attr.attr);
  365. attrs[idx] = &out_attrs[idx].attr.attr;
  366. out_attrs[idx].gth = gth;
  367. out_attrs[idx].port = i;
  368. out_attrs[idx].parm = j;
  369. }
  370. }
  371. gth->output_group.name = "outputs";
  372. gth->output_group.attrs = attrs;
  373. return sysfs_create_group(&gth->dev->kobj, &gth->output_group);
  374. }
  375. /**
  376. * intel_th_gth_stop() - stop tracing to an output device
  377. * @gth: GTH device
  378. * @output: output device's descriptor
  379. * @capture_done: set when no more traces will be captured
  380. *
  381. * This will stop tracing using force storeEn off signal and wait for the
  382. * pipelines to be empty for the corresponding output port.
  383. */
  384. static void intel_th_gth_stop(struct gth_device *gth,
  385. struct intel_th_output *output,
  386. bool capture_done)
  387. {
  388. struct intel_th_device *outdev =
  389. container_of(output, struct intel_th_device, output);
  390. struct intel_th_driver *outdrv =
  391. to_intel_th_driver(outdev->dev.driver);
  392. unsigned long count;
  393. u32 reg;
  394. u32 scr2 = 0xfc | (capture_done ? 1 : 0);
  395. iowrite32(0, gth->base + REG_GTH_SCR);
  396. iowrite32(scr2, gth->base + REG_GTH_SCR2);
  397. /* wait on pipeline empty for the given port */
  398. for (reg = 0, count = GTH_PLE_WAITLOOP_DEPTH;
  399. count && !(reg & BIT(output->port)); count--) {
  400. reg = ioread32(gth->base + REG_GTH_STAT);
  401. cpu_relax();
  402. }
  403. if (!count)
  404. dev_dbg(gth->dev, "timeout waiting for GTH[%d] PLE\n",
  405. output->port);
  406. /* wait on output piepline empty */
  407. if (outdrv->wait_empty)
  408. outdrv->wait_empty(outdev);
  409. /* clear force capture done for next captures */
  410. iowrite32(0xfc, gth->base + REG_GTH_SCR2);
  411. }
  412. /**
  413. * intel_th_gth_start() - start tracing to an output device
  414. * @gth: GTH device
  415. * @output: output device's descriptor
  416. *
  417. * This will start tracing using force storeEn signal.
  418. */
  419. static void intel_th_gth_start(struct gth_device *gth,
  420. struct intel_th_output *output)
  421. {
  422. u32 scr = 0xfc0000;
  423. if (output->multiblock)
  424. scr |= 0xff;
  425. iowrite32(scr, gth->base + REG_GTH_SCR);
  426. iowrite32(0, gth->base + REG_GTH_SCR2);
  427. }
  428. /**
  429. * intel_th_gth_disable() - disable tracing to an output device
  430. * @thdev: GTH device
  431. * @output: output device's descriptor
  432. *
  433. * This will deconfigure all masters set to output to this device,
  434. * disable tracing using force storeEn off signal and wait for the
  435. * "pipeline empty" bit for corresponding output port.
  436. */
  437. static void intel_th_gth_disable(struct intel_th_device *thdev,
  438. struct intel_th_output *output)
  439. {
  440. struct gth_device *gth = dev_get_drvdata(&thdev->dev);
  441. int master;
  442. u32 reg;
  443. spin_lock(&gth->gth_lock);
  444. output->active = false;
  445. for_each_set_bit(master, gth->output[output->port].master,
  446. TH_CONFIGURABLE_MASTERS + 1) {
  447. gth_master_set(gth, master, -1);
  448. }
  449. spin_unlock(&gth->gth_lock);
  450. intel_th_gth_stop(gth, output, true);
  451. reg = ioread32(gth->base + REG_GTH_SCRPD0);
  452. reg &= ~output->scratchpad;
  453. iowrite32(reg, gth->base + REG_GTH_SCRPD0);
  454. }
  455. static void gth_tscu_resync(struct gth_device *gth)
  456. {
  457. u32 reg;
  458. reg = ioread32(gth->base + REG_TSCU_TSUCTRL);
  459. reg &= ~TSUCTRL_CTCRESYNC;
  460. iowrite32(reg, gth->base + REG_TSCU_TSUCTRL);
  461. }
  462. static void intel_th_gth_prepare(struct intel_th_device *thdev,
  463. struct intel_th_output *output)
  464. {
  465. struct gth_device *gth = dev_get_drvdata(&thdev->dev);
  466. int count;
  467. /*
  468. * Wait until the output port is in reset before we start
  469. * programming it.
  470. */
  471. for (count = GTH_PLE_WAITLOOP_DEPTH;
  472. count && !(gth_output_get(gth, output->port) & BIT(5)); count--)
  473. cpu_relax();
  474. }
  475. /**
  476. * intel_th_gth_enable() - enable tracing to an output device
  477. * @thdev: GTH device
  478. * @output: output device's descriptor
  479. *
  480. * This will configure all masters set to output to this device and
  481. * enable tracing using force storeEn signal.
  482. */
  483. static void intel_th_gth_enable(struct intel_th_device *thdev,
  484. struct intel_th_output *output)
  485. {
  486. struct gth_device *gth = dev_get_drvdata(&thdev->dev);
  487. struct intel_th *th = to_intel_th(thdev);
  488. int master;
  489. u32 scrpd;
  490. spin_lock(&gth->gth_lock);
  491. for_each_set_bit(master, gth->output[output->port].master,
  492. TH_CONFIGURABLE_MASTERS + 1) {
  493. gth_master_set(gth, master, output->port);
  494. }
  495. output->active = true;
  496. spin_unlock(&gth->gth_lock);
  497. if (INTEL_TH_CAP(th, tscu_enable))
  498. gth_tscu_resync(gth);
  499. scrpd = ioread32(gth->base + REG_GTH_SCRPD0);
  500. scrpd |= output->scratchpad;
  501. iowrite32(scrpd, gth->base + REG_GTH_SCRPD0);
  502. intel_th_gth_start(gth, output);
  503. }
  504. /**
  505. * intel_th_gth_switch() - execute a switch sequence
  506. * @thdev: GTH device
  507. * @output: output device's descriptor
  508. *
  509. * This will execute a switch sequence that will trigger a switch window
  510. * when tracing to MSC in multi-block mode.
  511. */
  512. static void intel_th_gth_switch(struct intel_th_device *thdev,
  513. struct intel_th_output *output)
  514. {
  515. struct gth_device *gth = dev_get_drvdata(&thdev->dev);
  516. unsigned long count;
  517. u32 reg;
  518. /* trigger */
  519. iowrite32(0, gth->base + REG_CTS_CTL);
  520. iowrite32(CTS_CTL_SEQUENCER_ENABLE, gth->base + REG_CTS_CTL);
  521. /* wait on trigger status */
  522. for (reg = 0, count = CTS_TRIG_WAITLOOP_DEPTH;
  523. count && !(reg & BIT(4)); count--) {
  524. reg = ioread32(gth->base + REG_CTS_STAT);
  525. cpu_relax();
  526. }
  527. if (!count)
  528. dev_dbg(&thdev->dev, "timeout waiting for CTS Trigger\n");
  529. /* De-assert the trigger */
  530. iowrite32(0, gth->base + REG_CTS_CTL);
  531. intel_th_gth_stop(gth, output, false);
  532. intel_th_gth_start(gth, output);
  533. }
  534. /**
  535. * intel_th_gth_assign() - assign output device to a GTH output port
  536. * @thdev: GTH device
  537. * @othdev: output device
  538. *
  539. * This will match a given output device parameters against present
  540. * output ports on the GTH and fill out relevant bits in output device's
  541. * descriptor.
  542. *
  543. * Return: 0 on success, -errno on error.
  544. */
  545. static int intel_th_gth_assign(struct intel_th_device *thdev,
  546. struct intel_th_device *othdev)
  547. {
  548. struct gth_device *gth = dev_get_drvdata(&thdev->dev);
  549. int i, id;
  550. if (thdev->host_mode)
  551. return -EBUSY;
  552. if (othdev->type != INTEL_TH_OUTPUT)
  553. return -EINVAL;
  554. for (i = 0, id = 0; i < TH_POSSIBLE_OUTPUTS; i++) {
  555. if (gth->output[i].port_type != othdev->output.type)
  556. continue;
  557. if (othdev->id == -1 || othdev->id == id)
  558. goto found;
  559. id++;
  560. }
  561. return -ENOENT;
  562. found:
  563. spin_lock(&gth->gth_lock);
  564. othdev->output.port = i;
  565. othdev->output.active = false;
  566. gth->output[i].output = &othdev->output;
  567. spin_unlock(&gth->gth_lock);
  568. return 0;
  569. }
  570. /**
  571. * intel_th_gth_unassign() - deassociate an output device from its output port
  572. * @thdev: GTH device
  573. * @othdev: output device
  574. */
  575. static void intel_th_gth_unassign(struct intel_th_device *thdev,
  576. struct intel_th_device *othdev)
  577. {
  578. struct gth_device *gth = dev_get_drvdata(&thdev->dev);
  579. int port = othdev->output.port;
  580. int master;
  581. if (thdev->host_mode)
  582. return;
  583. spin_lock(&gth->gth_lock);
  584. othdev->output.port = -1;
  585. othdev->output.active = false;
  586. gth->output[port].output = NULL;
  587. for (master = 0; master < TH_CONFIGURABLE_MASTERS + 1; master++)
  588. if (gth->master[master] == port)
  589. gth->master[master] = -1;
  590. spin_unlock(&gth->gth_lock);
  591. }
  592. static int
  593. intel_th_gth_set_output(struct intel_th_device *thdev, unsigned int master)
  594. {
  595. struct gth_device *gth = dev_get_drvdata(&thdev->dev);
  596. int port = 0; /* FIXME: make default output configurable */
  597. /*
  598. * everything above TH_CONFIGURABLE_MASTERS is controlled by the
  599. * same register
  600. */
  601. if (master > TH_CONFIGURABLE_MASTERS)
  602. master = TH_CONFIGURABLE_MASTERS;
  603. spin_lock(&gth->gth_lock);
  604. if (gth->master[master] == -1) {
  605. set_bit(master, gth->output[port].master);
  606. gth->master[master] = port;
  607. }
  608. spin_unlock(&gth->gth_lock);
  609. return 0;
  610. }
  611. static int intel_th_gth_probe(struct intel_th_device *thdev)
  612. {
  613. struct device *dev = &thdev->dev;
  614. struct intel_th *th = dev_get_drvdata(dev->parent);
  615. struct gth_device *gth;
  616. struct resource *res;
  617. void __iomem *base;
  618. int i, ret;
  619. res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
  620. if (!res)
  621. return -ENODEV;
  622. base = devm_ioremap(dev, res->start, resource_size(res));
  623. if (!base)
  624. return -ENOMEM;
  625. gth = devm_kzalloc(dev, sizeof(*gth), GFP_KERNEL);
  626. if (!gth)
  627. return -ENOMEM;
  628. gth->dev = dev;
  629. gth->base = base;
  630. spin_lock_init(&gth->gth_lock);
  631. dev_set_drvdata(dev, gth);
  632. /*
  633. * Host mode can be signalled via SW means or via SCRPD_DEBUGGER_IN_USE
  634. * bit. Either way, don't reset HW in this case, and don't export any
  635. * capture configuration attributes. Also, refuse to assign output
  636. * drivers to ports, see intel_th_gth_assign().
  637. */
  638. if (thdev->host_mode)
  639. return 0;
  640. ret = intel_th_gth_reset(gth);
  641. if (ret) {
  642. if (ret != -EBUSY)
  643. return ret;
  644. thdev->host_mode = true;
  645. return 0;
  646. }
  647. for (i = 0; i < TH_CONFIGURABLE_MASTERS + 1; i++)
  648. gth->master[i] = -1;
  649. for (i = 0; i < TH_POSSIBLE_OUTPUTS; i++) {
  650. gth->output[i].gth = gth;
  651. gth->output[i].index = i;
  652. gth->output[i].port_type =
  653. gth_output_parm_get(gth, i, TH_OUTPUT_PARM(port));
  654. if (gth->output[i].port_type == GTH_NONE)
  655. continue;
  656. ret = intel_th_output_enable(th, gth->output[i].port_type);
  657. /* -ENODEV is ok, we just won't have that device enumerated */
  658. if (ret && ret != -ENODEV)
  659. return ret;
  660. }
  661. if (intel_th_output_attributes(gth) ||
  662. intel_th_master_attributes(gth)) {
  663. pr_warn("Can't initialize sysfs attributes\n");
  664. if (gth->output_group.attrs)
  665. sysfs_remove_group(&gth->dev->kobj, &gth->output_group);
  666. return -ENOMEM;
  667. }
  668. return 0;
  669. }
  670. static void intel_th_gth_remove(struct intel_th_device *thdev)
  671. {
  672. struct gth_device *gth = dev_get_drvdata(&thdev->dev);
  673. sysfs_remove_group(&gth->dev->kobj, &gth->output_group);
  674. sysfs_remove_group(&gth->dev->kobj, &gth->master_group);
  675. }
  676. static struct intel_th_driver intel_th_gth_driver = {
  677. .probe = intel_th_gth_probe,
  678. .remove = intel_th_gth_remove,
  679. .assign = intel_th_gth_assign,
  680. .unassign = intel_th_gth_unassign,
  681. .set_output = intel_th_gth_set_output,
  682. .prepare = intel_th_gth_prepare,
  683. .enable = intel_th_gth_enable,
  684. .trig_switch = intel_th_gth_switch,
  685. .disable = intel_th_gth_disable,
  686. .driver = {
  687. .name = "gth",
  688. .owner = THIS_MODULE,
  689. },
  690. };
  691. module_driver(intel_th_gth_driver,
  692. intel_th_driver_register,
  693. intel_th_driver_unregister);
  694. MODULE_ALIAS("intel_th_switch");
  695. MODULE_LICENSE("GPL v2");
  696. MODULE_DESCRIPTION("Intel(R) Trace Hub Global Trace Hub driver");
  697. MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");