core.c 30 KB

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