core.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * System Trace Module (STM) infrastructure
  4. * Copyright (c) 2014, Intel Corporation.
  5. *
  6. * STM class implements generic infrastructure for System Trace Module devices
  7. * as defined in MIPI STPv2 specification.
  8. */
  9. #include <linux/pm_runtime.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/device.h>
  14. #include <linux/compat.h>
  15. #include <linux/kdev_t.h>
  16. #include <linux/srcu.h>
  17. #include <linux/slab.h>
  18. #include <linux/stm.h>
  19. #include <linux/fs.h>
  20. #include <linux/mm.h>
  21. #include <linux/vmalloc.h>
  22. #include "stm.h"
  23. #include <uapi/linux/stm.h>
  24. static unsigned int stm_core_up;
  25. /*
  26. * The SRCU here makes sure that STM device doesn't disappear from under a
  27. * stm_source_write() caller, which may want to have as little overhead as
  28. * possible.
  29. */
  30. static struct srcu_struct stm_source_srcu;
  31. static ssize_t masters_show(struct device *dev,
  32. struct device_attribute *attr,
  33. char *buf)
  34. {
  35. struct stm_device *stm = to_stm_device(dev);
  36. int ret;
  37. ret = sprintf(buf, "%u %u\n", stm->data->sw_start, stm->data->sw_end);
  38. return ret;
  39. }
  40. static DEVICE_ATTR_RO(masters);
  41. static ssize_t channels_show(struct device *dev,
  42. struct device_attribute *attr,
  43. char *buf)
  44. {
  45. struct stm_device *stm = to_stm_device(dev);
  46. int ret;
  47. ret = sprintf(buf, "%u\n", stm->data->sw_nchannels);
  48. return ret;
  49. }
  50. static DEVICE_ATTR_RO(channels);
  51. static ssize_t hw_override_show(struct device *dev,
  52. struct device_attribute *attr,
  53. char *buf)
  54. {
  55. struct stm_device *stm = to_stm_device(dev);
  56. int ret;
  57. ret = sprintf(buf, "%u\n", stm->data->hw_override);
  58. return ret;
  59. }
  60. static DEVICE_ATTR_RO(hw_override);
  61. static struct attribute *stm_attrs[] = {
  62. &dev_attr_masters.attr,
  63. &dev_attr_channels.attr,
  64. &dev_attr_hw_override.attr,
  65. NULL,
  66. };
  67. ATTRIBUTE_GROUPS(stm);
  68. static struct class stm_class = {
  69. .name = "stm",
  70. .dev_groups = stm_groups,
  71. };
  72. /**
  73. * stm_find_device() - find stm device by name
  74. * @buf: character buffer containing the name
  75. *
  76. * This is called when either policy gets assigned to an stm device or an
  77. * stm_source device gets linked to an stm device.
  78. *
  79. * This grabs device's reference (get_device()) and module reference, both
  80. * of which the calling path needs to make sure to drop with stm_put_device().
  81. *
  82. * Return: stm device pointer or null if lookup failed.
  83. */
  84. struct stm_device *stm_find_device(const char *buf)
  85. {
  86. struct stm_device *stm;
  87. struct device *dev;
  88. if (!stm_core_up)
  89. return NULL;
  90. dev = class_find_device_by_name(&stm_class, buf);
  91. if (!dev)
  92. return NULL;
  93. stm = to_stm_device(dev);
  94. if (!try_module_get(stm->owner)) {
  95. /* matches class_find_device() above */
  96. put_device(dev);
  97. return NULL;
  98. }
  99. return stm;
  100. }
  101. /**
  102. * stm_put_device() - drop references on the stm device
  103. * @stm: stm device, previously acquired by stm_find_device()
  104. *
  105. * This drops the module reference and device reference taken by
  106. * stm_find_device() or stm_char_open().
  107. */
  108. void stm_put_device(struct stm_device *stm)
  109. {
  110. module_put(stm->owner);
  111. put_device(&stm->dev);
  112. }
  113. /*
  114. * Internally we only care about software-writable masters here, that is the
  115. * ones in the range [stm_data->sw_start..stm_data..sw_end], however we need
  116. * original master numbers to be visible externally, since they are the ones
  117. * that will appear in the STP stream. Thus, the internal bookkeeping uses
  118. * $master - stm_data->sw_start to reference master descriptors and such.
  119. */
  120. #define __stm_master(_s, _m) \
  121. ((_s)->masters[(_m) - (_s)->data->sw_start])
  122. static inline struct stp_master *
  123. stm_master(struct stm_device *stm, unsigned int idx)
  124. {
  125. if (idx < stm->data->sw_start || idx > stm->data->sw_end)
  126. return NULL;
  127. return __stm_master(stm, idx);
  128. }
  129. static int stp_master_alloc(struct stm_device *stm, unsigned int idx)
  130. {
  131. struct stp_master *master;
  132. master = kzalloc_flex(*master, chan_map,
  133. BITS_TO_LONGS(stm->data->sw_nchannels),
  134. GFP_ATOMIC);
  135. if (!master)
  136. return -ENOMEM;
  137. master->nr_free = stm->data->sw_nchannels;
  138. __stm_master(stm, idx) = master;
  139. return 0;
  140. }
  141. static void stp_master_free(struct stm_device *stm, unsigned int idx)
  142. {
  143. struct stp_master *master = stm_master(stm, idx);
  144. if (!master)
  145. return;
  146. __stm_master(stm, idx) = NULL;
  147. kfree(master);
  148. }
  149. static void stm_output_claim(struct stm_device *stm, struct stm_output *output)
  150. {
  151. struct stp_master *master = stm_master(stm, output->master);
  152. lockdep_assert_held(&stm->mc_lock);
  153. lockdep_assert_held(&output->lock);
  154. if (WARN_ON_ONCE(master->nr_free < output->nr_chans))
  155. return;
  156. bitmap_allocate_region(&master->chan_map[0], output->channel,
  157. ilog2(output->nr_chans));
  158. master->nr_free -= output->nr_chans;
  159. }
  160. static void
  161. stm_output_disclaim(struct stm_device *stm, struct stm_output *output)
  162. {
  163. struct stp_master *master = stm_master(stm, output->master);
  164. lockdep_assert_held(&stm->mc_lock);
  165. lockdep_assert_held(&output->lock);
  166. bitmap_release_region(&master->chan_map[0], output->channel,
  167. ilog2(output->nr_chans));
  168. master->nr_free += output->nr_chans;
  169. output->nr_chans = 0;
  170. }
  171. /*
  172. * This is like bitmap_find_free_region(), except it can ignore @start bits
  173. * at the beginning.
  174. */
  175. static int find_free_channels(unsigned long *bitmap, unsigned int start,
  176. unsigned int end, unsigned int width)
  177. {
  178. unsigned int pos;
  179. int i;
  180. for (pos = start; pos < end + 1; pos = ALIGN(pos, width)) {
  181. pos = find_next_zero_bit(bitmap, end + 1, pos);
  182. if (pos + width > end + 1)
  183. break;
  184. if (pos & (width - 1))
  185. continue;
  186. for (i = 1; i < width && !test_bit(pos + i, bitmap); i++)
  187. ;
  188. if (i == width)
  189. return pos;
  190. /* step over [pos..pos+i) to continue search */
  191. pos += i;
  192. }
  193. return -1;
  194. }
  195. static int
  196. stm_find_master_chan(struct stm_device *stm, unsigned int width,
  197. unsigned int *mstart, unsigned int mend,
  198. unsigned int *cstart, unsigned int cend)
  199. {
  200. struct stp_master *master;
  201. unsigned int midx;
  202. int pos, err;
  203. for (midx = *mstart; midx <= mend; midx++) {
  204. if (!stm_master(stm, midx)) {
  205. err = stp_master_alloc(stm, midx);
  206. if (err)
  207. return err;
  208. }
  209. master = stm_master(stm, midx);
  210. if (!master->nr_free)
  211. continue;
  212. pos = find_free_channels(master->chan_map, *cstart, cend,
  213. width);
  214. if (pos < 0)
  215. continue;
  216. *mstart = midx;
  217. *cstart = pos;
  218. return 0;
  219. }
  220. return -ENOSPC;
  221. }
  222. static int stm_output_assign(struct stm_device *stm, unsigned int width,
  223. struct stp_policy_node *policy_node,
  224. struct stm_output *output)
  225. {
  226. unsigned int midx, cidx, mend, cend;
  227. int ret = -EINVAL;
  228. if (width > stm->data->sw_nchannels)
  229. return -EINVAL;
  230. /* We no longer accept policy_node==NULL here */
  231. if (WARN_ON_ONCE(!policy_node))
  232. return -EINVAL;
  233. /*
  234. * Also, the caller holds reference to policy_node, so it won't
  235. * disappear on us.
  236. */
  237. stp_policy_node_get_ranges(policy_node, &midx, &mend, &cidx, &cend);
  238. spin_lock(&stm->mc_lock);
  239. spin_lock(&output->lock);
  240. /* output is already assigned -- shouldn't happen */
  241. if (WARN_ON_ONCE(output->nr_chans))
  242. goto unlock;
  243. ret = stm_find_master_chan(stm, width, &midx, mend, &cidx, cend);
  244. if (ret < 0)
  245. goto unlock;
  246. output->master = midx;
  247. output->channel = cidx;
  248. output->nr_chans = width;
  249. if (stm->pdrv->output_open) {
  250. void *priv = stp_policy_node_priv(policy_node);
  251. if (WARN_ON_ONCE(!priv))
  252. goto unlock;
  253. /* configfs subsys mutex is held by the caller */
  254. ret = stm->pdrv->output_open(priv, output);
  255. if (ret)
  256. goto unlock;
  257. }
  258. stm_output_claim(stm, output);
  259. dev_dbg(&stm->dev, "assigned %u:%u (+%u)\n", midx, cidx, width);
  260. ret = 0;
  261. unlock:
  262. if (ret)
  263. output->nr_chans = 0;
  264. spin_unlock(&output->lock);
  265. spin_unlock(&stm->mc_lock);
  266. return ret;
  267. }
  268. static void stm_output_free(struct stm_device *stm, struct stm_output *output)
  269. {
  270. spin_lock(&stm->mc_lock);
  271. spin_lock(&output->lock);
  272. if (output->nr_chans)
  273. stm_output_disclaim(stm, output);
  274. if (stm->pdrv && stm->pdrv->output_close)
  275. stm->pdrv->output_close(output);
  276. spin_unlock(&output->lock);
  277. spin_unlock(&stm->mc_lock);
  278. }
  279. static void stm_output_init(struct stm_output *output)
  280. {
  281. spin_lock_init(&output->lock);
  282. }
  283. static int major_match(struct device *dev, const void *data)
  284. {
  285. unsigned int major = *(unsigned int *)data;
  286. return MAJOR(dev->devt) == major;
  287. }
  288. /*
  289. * Framing protocol management
  290. * Modules can implement STM protocol drivers and (un-)register them
  291. * with the STM class framework.
  292. */
  293. static struct list_head stm_pdrv_head;
  294. static struct mutex stm_pdrv_mutex;
  295. struct stm_pdrv_entry {
  296. struct list_head entry;
  297. const struct stm_protocol_driver *pdrv;
  298. const struct config_item_type *node_type;
  299. };
  300. static const struct stm_pdrv_entry *
  301. __stm_lookup_protocol(const char *name)
  302. {
  303. struct stm_pdrv_entry *pe;
  304. /*
  305. * If no name is given (NULL or ""), fall back to "p_basic".
  306. */
  307. if (!name || !*name)
  308. name = "p_basic";
  309. list_for_each_entry(pe, &stm_pdrv_head, entry) {
  310. if (!strcmp(name, pe->pdrv->name))
  311. return pe;
  312. }
  313. return NULL;
  314. }
  315. int stm_register_protocol(const struct stm_protocol_driver *pdrv)
  316. {
  317. struct stm_pdrv_entry *pe = NULL;
  318. int ret = -ENOMEM;
  319. mutex_lock(&stm_pdrv_mutex);
  320. if (__stm_lookup_protocol(pdrv->name)) {
  321. ret = -EEXIST;
  322. goto unlock;
  323. }
  324. pe = kzalloc_obj(*pe);
  325. if (!pe)
  326. goto unlock;
  327. if (pdrv->policy_attr) {
  328. pe->node_type = get_policy_node_type(pdrv->policy_attr);
  329. if (!pe->node_type)
  330. goto unlock;
  331. }
  332. list_add_tail(&pe->entry, &stm_pdrv_head);
  333. pe->pdrv = pdrv;
  334. ret = 0;
  335. unlock:
  336. mutex_unlock(&stm_pdrv_mutex);
  337. if (ret)
  338. kfree(pe);
  339. return ret;
  340. }
  341. EXPORT_SYMBOL_GPL(stm_register_protocol);
  342. void stm_unregister_protocol(const struct stm_protocol_driver *pdrv)
  343. {
  344. struct stm_pdrv_entry *pe, *iter;
  345. mutex_lock(&stm_pdrv_mutex);
  346. list_for_each_entry_safe(pe, iter, &stm_pdrv_head, entry) {
  347. if (pe->pdrv == pdrv) {
  348. list_del(&pe->entry);
  349. if (pe->node_type) {
  350. kfree(pe->node_type->ct_attrs);
  351. kfree(pe->node_type);
  352. }
  353. kfree(pe);
  354. break;
  355. }
  356. }
  357. mutex_unlock(&stm_pdrv_mutex);
  358. }
  359. EXPORT_SYMBOL_GPL(stm_unregister_protocol);
  360. static bool stm_get_protocol(const struct stm_protocol_driver *pdrv)
  361. {
  362. return try_module_get(pdrv->owner);
  363. }
  364. void stm_put_protocol(const struct stm_protocol_driver *pdrv)
  365. {
  366. module_put(pdrv->owner);
  367. }
  368. int stm_lookup_protocol(const char *name,
  369. const struct stm_protocol_driver **pdrv,
  370. const struct config_item_type **node_type)
  371. {
  372. const struct stm_pdrv_entry *pe;
  373. mutex_lock(&stm_pdrv_mutex);
  374. pe = __stm_lookup_protocol(name);
  375. if (pe && pe->pdrv && stm_get_protocol(pe->pdrv)) {
  376. *pdrv = pe->pdrv;
  377. *node_type = pe->node_type;
  378. }
  379. mutex_unlock(&stm_pdrv_mutex);
  380. return pe ? 0 : -ENOENT;
  381. }
  382. static int stm_char_open(struct inode *inode, struct file *file)
  383. {
  384. struct stm_file *stmf;
  385. struct device *dev;
  386. unsigned int major = imajor(inode);
  387. int err = -ENOMEM;
  388. dev = class_find_device(&stm_class, NULL, &major, major_match);
  389. if (!dev)
  390. return -ENODEV;
  391. stmf = kzalloc_obj(*stmf);
  392. if (!stmf)
  393. goto err_put_device;
  394. err = -ENODEV;
  395. stm_output_init(&stmf->output);
  396. stmf->stm = to_stm_device(dev);
  397. if (!try_module_get(stmf->stm->owner))
  398. goto err_free;
  399. file->private_data = stmf;
  400. return nonseekable_open(inode, file);
  401. err_free:
  402. kfree(stmf);
  403. err_put_device:
  404. /* matches class_find_device() above */
  405. put_device(dev);
  406. return err;
  407. }
  408. static int stm_char_release(struct inode *inode, struct file *file)
  409. {
  410. struct stm_file *stmf = file->private_data;
  411. struct stm_device *stm = stmf->stm;
  412. if (stm->data->unlink)
  413. stm->data->unlink(stm->data, stmf->output.master,
  414. stmf->output.channel);
  415. stm_output_free(stm, &stmf->output);
  416. /*
  417. * matches the stm_char_open()'s
  418. * class_find_device() + try_module_get()
  419. */
  420. stm_put_device(stm);
  421. kfree(stmf);
  422. return 0;
  423. }
  424. static int
  425. stm_assign_first_policy(struct stm_device *stm, struct stm_output *output,
  426. char **ids, unsigned int width)
  427. {
  428. struct stp_policy_node *pn;
  429. int err, n;
  430. /*
  431. * On success, stp_policy_node_lookup() will return holding the
  432. * configfs subsystem mutex, which is then released in
  433. * stp_policy_node_put(). This allows the pdrv->output_open() in
  434. * stm_output_assign() to serialize against the attribute accessors.
  435. */
  436. for (n = 0, pn = NULL; ids[n] && !pn; n++)
  437. pn = stp_policy_node_lookup(stm, ids[n]);
  438. if (!pn)
  439. return -EINVAL;
  440. err = stm_output_assign(stm, width, pn, output);
  441. stp_policy_node_put(pn);
  442. return err;
  443. }
  444. /**
  445. * stm_data_write() - send the given payload as data packets
  446. * @data: stm driver's data
  447. * @m: STP master
  448. * @c: STP channel
  449. * @ts_first: timestamp the first packet
  450. * @buf: data payload buffer
  451. * @count: data payload size
  452. */
  453. ssize_t notrace stm_data_write(struct stm_data *data, unsigned int m,
  454. unsigned int c, bool ts_first, const void *buf,
  455. size_t count)
  456. {
  457. unsigned int flags = ts_first ? STP_PACKET_TIMESTAMPED : 0;
  458. ssize_t sz;
  459. size_t pos;
  460. for (pos = 0, sz = 0; pos < count; pos += sz) {
  461. sz = min_t(unsigned int, count - pos, 8);
  462. sz = data->packet(data, m, c, STP_PACKET_DATA, flags, sz,
  463. &((u8 *)buf)[pos]);
  464. if (sz <= 0)
  465. break;
  466. if (ts_first) {
  467. flags = 0;
  468. ts_first = false;
  469. }
  470. }
  471. return sz < 0 ? sz : pos;
  472. }
  473. EXPORT_SYMBOL_GPL(stm_data_write);
  474. static ssize_t notrace
  475. stm_write(struct stm_device *stm, struct stm_output *output,
  476. unsigned int chan, const char *buf, size_t count, struct stm_source_data *source)
  477. {
  478. int err;
  479. /* stm->pdrv is serialized against policy_mutex */
  480. if (!stm->pdrv)
  481. return -ENODEV;
  482. err = stm->pdrv->write(stm->data, output, chan, buf, count, source);
  483. if (err < 0)
  484. return err;
  485. return err;
  486. }
  487. static ssize_t stm_char_write(struct file *file, const char __user *buf,
  488. size_t count, loff_t *ppos)
  489. {
  490. struct stm_file *stmf = file->private_data;
  491. struct stm_device *stm = stmf->stm;
  492. char *kbuf;
  493. int err;
  494. if (count + 1 > PAGE_SIZE)
  495. count = PAGE_SIZE - 1;
  496. /*
  497. * If no m/c have been assigned to this writer up to this
  498. * point, try to use the task name and "default" policy entries.
  499. */
  500. if (!stmf->output.nr_chans) {
  501. char comm[sizeof(current->comm)];
  502. char *ids[] = { comm, "default", NULL };
  503. get_task_comm(comm, current);
  504. err = stm_assign_first_policy(stmf->stm, &stmf->output, ids, 1);
  505. /*
  506. * EBUSY means that somebody else just assigned this
  507. * output, which is just fine for write()
  508. */
  509. if (err)
  510. return err;
  511. }
  512. kbuf = kmalloc(count + 1, GFP_KERNEL);
  513. if (!kbuf)
  514. return -ENOMEM;
  515. err = copy_from_user(kbuf, buf, count);
  516. if (err) {
  517. kfree(kbuf);
  518. return -EFAULT;
  519. }
  520. pm_runtime_get_sync(&stm->dev);
  521. count = stm_write(stm, &stmf->output, 0, kbuf, count, NULL);
  522. pm_runtime_mark_last_busy(&stm->dev);
  523. pm_runtime_put_autosuspend(&stm->dev);
  524. kfree(kbuf);
  525. return count;
  526. }
  527. static void stm_mmap_open(struct vm_area_struct *vma)
  528. {
  529. struct stm_file *stmf = vma->vm_file->private_data;
  530. struct stm_device *stm = stmf->stm;
  531. pm_runtime_get(&stm->dev);
  532. }
  533. static void stm_mmap_close(struct vm_area_struct *vma)
  534. {
  535. struct stm_file *stmf = vma->vm_file->private_data;
  536. struct stm_device *stm = stmf->stm;
  537. pm_runtime_mark_last_busy(&stm->dev);
  538. pm_runtime_put_autosuspend(&stm->dev);
  539. }
  540. static const struct vm_operations_struct stm_mmap_vmops = {
  541. .open = stm_mmap_open,
  542. .close = stm_mmap_close,
  543. };
  544. static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)
  545. {
  546. struct stm_file *stmf = file->private_data;
  547. struct stm_device *stm = stmf->stm;
  548. unsigned long size, phys;
  549. if (!stm->data->mmio_addr)
  550. return -EOPNOTSUPP;
  551. if (vma->vm_pgoff)
  552. return -EINVAL;
  553. size = vma->vm_end - vma->vm_start;
  554. if (stmf->output.nr_chans * stm->data->sw_mmiosz != size)
  555. return -EINVAL;
  556. phys = stm->data->mmio_addr(stm->data, stmf->output.master,
  557. stmf->output.channel,
  558. stmf->output.nr_chans);
  559. if (!phys)
  560. return -EINVAL;
  561. pm_runtime_get_sync(&stm->dev);
  562. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  563. vm_flags_set(vma, VM_IO | VM_DONTEXPAND | VM_DONTDUMP);
  564. vma->vm_ops = &stm_mmap_vmops;
  565. vm_iomap_memory(vma, phys, size);
  566. return 0;
  567. }
  568. static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg)
  569. {
  570. struct stm_device *stm = stmf->stm;
  571. struct stp_policy_id *id;
  572. char *ids[] = { NULL, NULL };
  573. int ret = -EINVAL, wlimit = 1;
  574. u32 size;
  575. if (stmf->output.nr_chans)
  576. return -EBUSY;
  577. if (copy_from_user(&size, arg, sizeof(size)))
  578. return -EFAULT;
  579. if (size < sizeof(*id) || size >= PATH_MAX + sizeof(*id))
  580. return -EINVAL;
  581. /*
  582. * size + 1 to make sure the .id string at the bottom is terminated,
  583. * which is also why memdup_user() is not useful here
  584. */
  585. id = kzalloc(size + 1, GFP_KERNEL);
  586. if (!id)
  587. return -ENOMEM;
  588. if (copy_from_user(id, arg, size)) {
  589. ret = -EFAULT;
  590. goto err_free;
  591. }
  592. if (id->__reserved_0 || id->__reserved_1)
  593. goto err_free;
  594. if (stm->data->sw_mmiosz)
  595. wlimit = PAGE_SIZE / stm->data->sw_mmiosz;
  596. if (id->width < 1 || id->width > wlimit)
  597. goto err_free;
  598. ids[0] = id->id;
  599. ret = stm_assign_first_policy(stmf->stm, &stmf->output, ids,
  600. id->width);
  601. if (ret)
  602. goto err_free;
  603. if (stm->data->link)
  604. ret = stm->data->link(stm->data, stmf->output.master,
  605. stmf->output.channel);
  606. if (ret)
  607. stm_output_free(stmf->stm, &stmf->output);
  608. err_free:
  609. kfree(id);
  610. return ret;
  611. }
  612. static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg)
  613. {
  614. struct stp_policy_id id = {
  615. .size = sizeof(id),
  616. .master = stmf->output.master,
  617. .channel = stmf->output.channel,
  618. .width = stmf->output.nr_chans,
  619. .__reserved_0 = 0,
  620. .__reserved_1 = 0,
  621. };
  622. return copy_to_user(arg, &id, id.size) ? -EFAULT : 0;
  623. }
  624. static long
  625. stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  626. {
  627. struct stm_file *stmf = file->private_data;
  628. struct stm_data *stm_data = stmf->stm->data;
  629. int err = -ENOTTY;
  630. u64 options;
  631. switch (cmd) {
  632. case STP_POLICY_ID_SET:
  633. err = stm_char_policy_set_ioctl(stmf, (void __user *)arg);
  634. if (err)
  635. return err;
  636. return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
  637. case STP_POLICY_ID_GET:
  638. return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
  639. case STP_SET_OPTIONS:
  640. if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64)))
  641. return -EFAULT;
  642. if (stm_data->set_options)
  643. err = stm_data->set_options(stm_data,
  644. stmf->output.master,
  645. stmf->output.channel,
  646. stmf->output.nr_chans,
  647. options);
  648. break;
  649. default:
  650. break;
  651. }
  652. return err;
  653. }
  654. static const struct file_operations stm_fops = {
  655. .open = stm_char_open,
  656. .release = stm_char_release,
  657. .write = stm_char_write,
  658. .mmap = stm_char_mmap,
  659. .unlocked_ioctl = stm_char_ioctl,
  660. .compat_ioctl = compat_ptr_ioctl,
  661. };
  662. static void stm_device_release(struct device *dev)
  663. {
  664. struct stm_device *stm = to_stm_device(dev);
  665. vfree(stm);
  666. }
  667. int stm_register_device(struct device *parent, struct stm_data *stm_data,
  668. struct module *owner)
  669. {
  670. struct stm_device *stm;
  671. unsigned int nmasters;
  672. int err = -ENOMEM;
  673. if (!stm_core_up)
  674. return -EPROBE_DEFER;
  675. if (!stm_data->packet || !stm_data->sw_nchannels)
  676. return -EINVAL;
  677. nmasters = stm_data->sw_end - stm_data->sw_start + 1;
  678. stm = vzalloc(sizeof(*stm) + nmasters * sizeof(void *));
  679. if (!stm)
  680. return -ENOMEM;
  681. stm->major = register_chrdev(0, stm_data->name, &stm_fops);
  682. if (stm->major < 0) {
  683. err = stm->major;
  684. vfree(stm);
  685. return err;
  686. }
  687. device_initialize(&stm->dev);
  688. stm->dev.devt = MKDEV(stm->major, 0);
  689. stm->dev.class = &stm_class;
  690. stm->dev.parent = parent;
  691. stm->dev.release = stm_device_release;
  692. mutex_init(&stm->link_mutex);
  693. spin_lock_init(&stm->link_lock);
  694. INIT_LIST_HEAD(&stm->link_list);
  695. /* initialize the object before it is accessible via sysfs */
  696. spin_lock_init(&stm->mc_lock);
  697. mutex_init(&stm->policy_mutex);
  698. stm->sw_nmasters = nmasters;
  699. stm->owner = owner;
  700. stm->data = stm_data;
  701. stm_data->stm = stm;
  702. err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name);
  703. if (err)
  704. goto err_device;
  705. err = device_add(&stm->dev);
  706. if (err)
  707. goto err_device;
  708. /*
  709. * Use delayed autosuspend to avoid bouncing back and forth
  710. * on recurring character device writes, with the initial
  711. * delay time of 2 seconds.
  712. */
  713. pm_runtime_no_callbacks(&stm->dev);
  714. pm_runtime_use_autosuspend(&stm->dev);
  715. pm_runtime_set_autosuspend_delay(&stm->dev, 2000);
  716. pm_runtime_set_suspended(&stm->dev);
  717. pm_runtime_enable(&stm->dev);
  718. return 0;
  719. err_device:
  720. unregister_chrdev(stm->major, stm_data->name);
  721. /* calls stm_device_release() */
  722. put_device(&stm->dev);
  723. return err;
  724. }
  725. EXPORT_SYMBOL_GPL(stm_register_device);
  726. static int __stm_source_link_drop(struct stm_source_device *src,
  727. struct stm_device *stm);
  728. void stm_unregister_device(struct stm_data *stm_data)
  729. {
  730. struct stm_device *stm = stm_data->stm;
  731. struct stm_source_device *src, *iter;
  732. int i, ret;
  733. pm_runtime_dont_use_autosuspend(&stm->dev);
  734. pm_runtime_disable(&stm->dev);
  735. mutex_lock(&stm->link_mutex);
  736. list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) {
  737. ret = __stm_source_link_drop(src, stm);
  738. /*
  739. * src <-> stm link must not change under the same
  740. * stm::link_mutex, so complain loudly if it has;
  741. * also in this situation ret!=0 means this src is
  742. * not connected to this stm and it should be otherwise
  743. * safe to proceed with the tear-down of stm.
  744. */
  745. WARN_ON_ONCE(ret);
  746. }
  747. mutex_unlock(&stm->link_mutex);
  748. synchronize_srcu(&stm_source_srcu);
  749. unregister_chrdev(stm->major, stm_data->name);
  750. mutex_lock(&stm->policy_mutex);
  751. if (stm->policy)
  752. stp_policy_unbind(stm->policy);
  753. mutex_unlock(&stm->policy_mutex);
  754. for (i = stm->data->sw_start; i <= stm->data->sw_end; i++)
  755. stp_master_free(stm, i);
  756. device_unregister(&stm->dev);
  757. stm_data->stm = NULL;
  758. }
  759. EXPORT_SYMBOL_GPL(stm_unregister_device);
  760. /*
  761. * stm::link_list access serialization uses a spinlock and a mutex; holding
  762. * either of them guarantees that the list is stable; modification requires
  763. * holding both of them.
  764. *
  765. * Lock ordering is as follows:
  766. * stm::link_mutex
  767. * stm::link_lock
  768. * src::link_lock
  769. */
  770. /**
  771. * stm_source_link_add() - connect an stm_source device to an stm device
  772. * @src: stm_source device
  773. * @stm: stm device
  774. *
  775. * This function establishes a link from stm_source to an stm device so that
  776. * the former can send out trace data to the latter.
  777. *
  778. * Return: 0 on success, -errno otherwise.
  779. */
  780. static int stm_source_link_add(struct stm_source_device *src,
  781. struct stm_device *stm)
  782. {
  783. char *ids[] = { NULL, "default", NULL };
  784. int err = -ENOMEM;
  785. mutex_lock(&stm->link_mutex);
  786. spin_lock(&stm->link_lock);
  787. spin_lock(&src->link_lock);
  788. /* src->link is dereferenced under stm_source_srcu but not the list */
  789. rcu_assign_pointer(src->link, stm);
  790. list_add_tail(&src->link_entry, &stm->link_list);
  791. spin_unlock(&src->link_lock);
  792. spin_unlock(&stm->link_lock);
  793. mutex_unlock(&stm->link_mutex);
  794. ids[0] = kstrdup(src->data->name, GFP_KERNEL);
  795. if (!ids[0])
  796. goto fail_detach;
  797. err = stm_assign_first_policy(stm, &src->output, ids,
  798. src->data->nr_chans);
  799. kfree(ids[0]);
  800. if (err)
  801. goto fail_detach;
  802. /* this is to notify the STM device that a new link has been made */
  803. if (stm->data->link)
  804. err = stm->data->link(stm->data, src->output.master,
  805. src->output.channel);
  806. if (err)
  807. goto fail_free_output;
  808. /* this is to let the source carry out all necessary preparations */
  809. if (src->data->link)
  810. src->data->link(src->data);
  811. return 0;
  812. fail_free_output:
  813. stm_output_free(stm, &src->output);
  814. fail_detach:
  815. mutex_lock(&stm->link_mutex);
  816. spin_lock(&stm->link_lock);
  817. spin_lock(&src->link_lock);
  818. rcu_assign_pointer(src->link, NULL);
  819. list_del_init(&src->link_entry);
  820. spin_unlock(&src->link_lock);
  821. spin_unlock(&stm->link_lock);
  822. mutex_unlock(&stm->link_mutex);
  823. return err;
  824. }
  825. /**
  826. * __stm_source_link_drop() - detach stm_source from an stm device
  827. * @src: stm_source device
  828. * @stm: stm device
  829. *
  830. * If @stm is @src::link, disconnect them from one another and put the
  831. * reference on the @stm device.
  832. *
  833. * Caller must hold stm::link_mutex.
  834. */
  835. static int __stm_source_link_drop(struct stm_source_device *src,
  836. struct stm_device *stm)
  837. {
  838. struct stm_device *link;
  839. int ret = 0;
  840. lockdep_assert_held(&stm->link_mutex);
  841. /* for stm::link_list modification, we hold both mutex and spinlock */
  842. spin_lock(&stm->link_lock);
  843. spin_lock(&src->link_lock);
  844. link = srcu_dereference_check(src->link, &stm_source_srcu, 1);
  845. /*
  846. * The linked device may have changed since we last looked, because
  847. * we weren't holding the src::link_lock back then; if this is the
  848. * case, tell the caller to retry.
  849. */
  850. if (link != stm) {
  851. ret = -EAGAIN;
  852. goto unlock;
  853. }
  854. stm_output_free(link, &src->output);
  855. list_del_init(&src->link_entry);
  856. pm_runtime_mark_last_busy(&link->dev);
  857. pm_runtime_put_autosuspend(&link->dev);
  858. /* matches stm_find_device() from stm_source_link_store() */
  859. stm_put_device(link);
  860. rcu_assign_pointer(src->link, NULL);
  861. unlock:
  862. spin_unlock(&src->link_lock);
  863. spin_unlock(&stm->link_lock);
  864. /*
  865. * Call the unlink callbacks for both source and stm, when we know
  866. * that we have actually performed the unlinking.
  867. */
  868. if (!ret) {
  869. if (src->data->unlink)
  870. src->data->unlink(src->data);
  871. if (stm->data->unlink)
  872. stm->data->unlink(stm->data, src->output.master,
  873. src->output.channel);
  874. }
  875. return ret;
  876. }
  877. /**
  878. * stm_source_link_drop() - detach stm_source from its stm device
  879. * @src: stm_source device
  880. *
  881. * Unlinking means disconnecting from source's STM device; after this
  882. * writes will be unsuccessful until it is linked to a new STM device.
  883. *
  884. * This will happen on "stm_source_link" sysfs attribute write to undo
  885. * the existing link (if any), or on linked STM device's de-registration.
  886. */
  887. static void stm_source_link_drop(struct stm_source_device *src)
  888. {
  889. struct stm_device *stm;
  890. int idx, ret;
  891. retry:
  892. idx = srcu_read_lock(&stm_source_srcu);
  893. /*
  894. * The stm device will be valid for the duration of this
  895. * read section, but the link may change before we grab
  896. * the src::link_lock in __stm_source_link_drop().
  897. */
  898. stm = srcu_dereference(src->link, &stm_source_srcu);
  899. ret = 0;
  900. if (stm) {
  901. mutex_lock(&stm->link_mutex);
  902. ret = __stm_source_link_drop(src, stm);
  903. mutex_unlock(&stm->link_mutex);
  904. }
  905. srcu_read_unlock(&stm_source_srcu, idx);
  906. /* if it did change, retry */
  907. if (ret == -EAGAIN)
  908. goto retry;
  909. }
  910. static ssize_t stm_source_link_show(struct device *dev,
  911. struct device_attribute *attr,
  912. char *buf)
  913. {
  914. struct stm_source_device *src = to_stm_source_device(dev);
  915. struct stm_device *stm;
  916. int idx, ret;
  917. idx = srcu_read_lock(&stm_source_srcu);
  918. stm = srcu_dereference(src->link, &stm_source_srcu);
  919. ret = sprintf(buf, "%s\n",
  920. stm ? dev_name(&stm->dev) : "<none>");
  921. srcu_read_unlock(&stm_source_srcu, idx);
  922. return ret;
  923. }
  924. static ssize_t stm_source_link_store(struct device *dev,
  925. struct device_attribute *attr,
  926. const char *buf, size_t count)
  927. {
  928. struct stm_source_device *src = to_stm_source_device(dev);
  929. struct stm_device *link;
  930. int err;
  931. stm_source_link_drop(src);
  932. link = stm_find_device(buf);
  933. if (!link)
  934. return -EINVAL;
  935. pm_runtime_get(&link->dev);
  936. err = stm_source_link_add(src, link);
  937. if (err) {
  938. pm_runtime_put_autosuspend(&link->dev);
  939. /* matches the stm_find_device() above */
  940. stm_put_device(link);
  941. }
  942. return err ? : count;
  943. }
  944. static DEVICE_ATTR_RW(stm_source_link);
  945. static struct attribute *stm_source_attrs[] = {
  946. &dev_attr_stm_source_link.attr,
  947. NULL,
  948. };
  949. ATTRIBUTE_GROUPS(stm_source);
  950. static struct class stm_source_class = {
  951. .name = "stm_source",
  952. .dev_groups = stm_source_groups,
  953. };
  954. static void stm_source_device_release(struct device *dev)
  955. {
  956. struct stm_source_device *src = to_stm_source_device(dev);
  957. kfree(src);
  958. }
  959. /**
  960. * stm_source_register_device() - register an stm_source device
  961. * @parent: parent device
  962. * @data: device description structure
  963. *
  964. * This will create a device of stm_source class that can write
  965. * data to an stm device once linked.
  966. *
  967. * Return: 0 on success, -errno otherwise.
  968. */
  969. int stm_source_register_device(struct device *parent,
  970. struct stm_source_data *data)
  971. {
  972. struct stm_source_device *src;
  973. int err;
  974. if (!stm_core_up)
  975. return -EPROBE_DEFER;
  976. src = kzalloc_obj(*src);
  977. if (!src)
  978. return -ENOMEM;
  979. device_initialize(&src->dev);
  980. src->dev.class = &stm_source_class;
  981. src->dev.parent = parent;
  982. src->dev.release = stm_source_device_release;
  983. err = kobject_set_name(&src->dev.kobj, "%s", data->name);
  984. if (err)
  985. goto err;
  986. pm_runtime_no_callbacks(&src->dev);
  987. pm_runtime_forbid(&src->dev);
  988. err = device_add(&src->dev);
  989. if (err)
  990. goto err;
  991. stm_output_init(&src->output);
  992. spin_lock_init(&src->link_lock);
  993. INIT_LIST_HEAD(&src->link_entry);
  994. src->data = data;
  995. data->src = src;
  996. return 0;
  997. err:
  998. put_device(&src->dev);
  999. return err;
  1000. }
  1001. EXPORT_SYMBOL_GPL(stm_source_register_device);
  1002. /**
  1003. * stm_source_unregister_device() - unregister an stm_source device
  1004. * @data: device description that was used to register the device
  1005. *
  1006. * This will remove a previously created stm_source device from the system.
  1007. */
  1008. void stm_source_unregister_device(struct stm_source_data *data)
  1009. {
  1010. struct stm_source_device *src = data->src;
  1011. stm_source_link_drop(src);
  1012. device_unregister(&src->dev);
  1013. }
  1014. EXPORT_SYMBOL_GPL(stm_source_unregister_device);
  1015. int notrace stm_source_write(struct stm_source_data *data,
  1016. unsigned int chan,
  1017. const char *buf, size_t count)
  1018. {
  1019. struct stm_source_device *src = data->src;
  1020. struct stm_device *stm;
  1021. int idx;
  1022. if (!src->output.nr_chans)
  1023. return -ENODEV;
  1024. if (chan >= src->output.nr_chans)
  1025. return -EINVAL;
  1026. idx = srcu_read_lock(&stm_source_srcu);
  1027. stm = srcu_dereference(src->link, &stm_source_srcu);
  1028. if (stm)
  1029. count = stm_write(stm, &src->output, chan, buf, count, data);
  1030. else
  1031. count = -ENODEV;
  1032. srcu_read_unlock(&stm_source_srcu, idx);
  1033. return count;
  1034. }
  1035. EXPORT_SYMBOL_GPL(stm_source_write);
  1036. static int __init stm_core_init(void)
  1037. {
  1038. int err;
  1039. err = class_register(&stm_class);
  1040. if (err)
  1041. return err;
  1042. err = class_register(&stm_source_class);
  1043. if (err)
  1044. goto err_stm;
  1045. err = stp_configfs_init();
  1046. if (err)
  1047. goto err_src;
  1048. init_srcu_struct(&stm_source_srcu);
  1049. INIT_LIST_HEAD(&stm_pdrv_head);
  1050. mutex_init(&stm_pdrv_mutex);
  1051. /*
  1052. * So as to not confuse existing users with a requirement
  1053. * to load yet another module, do it here.
  1054. */
  1055. if (IS_ENABLED(CONFIG_STM_PROTO_BASIC))
  1056. (void)request_module_nowait("stm_p_basic");
  1057. stm_core_up++;
  1058. return 0;
  1059. err_src:
  1060. class_unregister(&stm_source_class);
  1061. err_stm:
  1062. class_unregister(&stm_class);
  1063. return err;
  1064. }
  1065. module_init(stm_core_init);
  1066. static void __exit stm_core_exit(void)
  1067. {
  1068. cleanup_srcu_struct(&stm_source_srcu);
  1069. class_unregister(&stm_source_class);
  1070. class_unregister(&stm_class);
  1071. stp_configfs_exit();
  1072. }
  1073. module_exit(stm_core_exit);
  1074. MODULE_LICENSE("GPL v2");
  1075. MODULE_DESCRIPTION("System Trace Module device class");
  1076. MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");