core.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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. master = kzalloc(struct_size(master, chan_map,
  138. BITS_TO_LONGS(stm->data->sw_nchannels)),
  139. GFP_ATOMIC);
  140. if (!master)
  141. return -ENOMEM;
  142. master->nr_free = stm->data->sw_nchannels;
  143. __stm_master(stm, idx) = master;
  144. return 0;
  145. }
  146. static void stp_master_free(struct stm_device *stm, unsigned int idx)
  147. {
  148. struct stp_master *master = stm_master(stm, idx);
  149. if (!master)
  150. return;
  151. __stm_master(stm, idx) = NULL;
  152. kfree(master);
  153. }
  154. static void stm_output_claim(struct stm_device *stm, struct stm_output *output)
  155. {
  156. struct stp_master *master = stm_master(stm, output->master);
  157. lockdep_assert_held(&stm->mc_lock);
  158. lockdep_assert_held(&output->lock);
  159. if (WARN_ON_ONCE(master->nr_free < output->nr_chans))
  160. return;
  161. bitmap_allocate_region(&master->chan_map[0], output->channel,
  162. ilog2(output->nr_chans));
  163. master->nr_free -= output->nr_chans;
  164. }
  165. static void
  166. stm_output_disclaim(struct stm_device *stm, struct stm_output *output)
  167. {
  168. struct stp_master *master = stm_master(stm, output->master);
  169. lockdep_assert_held(&stm->mc_lock);
  170. lockdep_assert_held(&output->lock);
  171. bitmap_release_region(&master->chan_map[0], output->channel,
  172. ilog2(output->nr_chans));
  173. master->nr_free += output->nr_chans;
  174. output->nr_chans = 0;
  175. }
  176. /*
  177. * This is like bitmap_find_free_region(), except it can ignore @start bits
  178. * at the beginning.
  179. */
  180. static int find_free_channels(unsigned long *bitmap, unsigned int start,
  181. unsigned int end, unsigned int width)
  182. {
  183. unsigned int pos;
  184. int i;
  185. for (pos = start; pos < end + 1; pos = ALIGN(pos, width)) {
  186. pos = find_next_zero_bit(bitmap, end + 1, pos);
  187. if (pos + width > end + 1)
  188. break;
  189. if (pos & (width - 1))
  190. continue;
  191. for (i = 1; i < width && !test_bit(pos + i, bitmap); i++)
  192. ;
  193. if (i == width)
  194. return pos;
  195. /* step over [pos..pos+i) to continue search */
  196. pos += i;
  197. }
  198. return -1;
  199. }
  200. static int
  201. stm_find_master_chan(struct stm_device *stm, unsigned int width,
  202. unsigned int *mstart, unsigned int mend,
  203. unsigned int *cstart, unsigned int cend)
  204. {
  205. struct stp_master *master;
  206. unsigned int midx;
  207. int pos, err;
  208. for (midx = *mstart; midx <= mend; midx++) {
  209. if (!stm_master(stm, midx)) {
  210. err = stp_master_alloc(stm, midx);
  211. if (err)
  212. return err;
  213. }
  214. master = stm_master(stm, midx);
  215. if (!master->nr_free)
  216. continue;
  217. pos = find_free_channels(master->chan_map, *cstart, cend,
  218. width);
  219. if (pos < 0)
  220. continue;
  221. *mstart = midx;
  222. *cstart = pos;
  223. return 0;
  224. }
  225. return -ENOSPC;
  226. }
  227. static int stm_output_assign(struct stm_device *stm, unsigned int width,
  228. struct stp_policy_node *policy_node,
  229. struct stm_output *output)
  230. {
  231. unsigned int midx, cidx, mend, cend;
  232. int ret = -EINVAL;
  233. if (width > stm->data->sw_nchannels)
  234. return -EINVAL;
  235. if (policy_node) {
  236. stp_policy_node_get_ranges(policy_node,
  237. &midx, &mend, &cidx, &cend);
  238. } else {
  239. midx = stm->data->sw_start;
  240. cidx = 0;
  241. mend = stm->data->sw_end;
  242. cend = stm->data->sw_nchannels - 1;
  243. }
  244. spin_lock(&stm->mc_lock);
  245. spin_lock(&output->lock);
  246. /* output is already assigned -- shouldn't happen */
  247. if (WARN_ON_ONCE(output->nr_chans))
  248. goto unlock;
  249. ret = stm_find_master_chan(stm, width, &midx, mend, &cidx, cend);
  250. if (ret < 0)
  251. goto unlock;
  252. output->master = midx;
  253. output->channel = cidx;
  254. output->nr_chans = width;
  255. stm_output_claim(stm, output);
  256. dev_dbg(&stm->dev, "assigned %u:%u (+%u)\n", midx, cidx, width);
  257. ret = 0;
  258. unlock:
  259. spin_unlock(&output->lock);
  260. spin_unlock(&stm->mc_lock);
  261. return ret;
  262. }
  263. static void stm_output_free(struct stm_device *stm, struct stm_output *output)
  264. {
  265. spin_lock(&stm->mc_lock);
  266. spin_lock(&output->lock);
  267. if (output->nr_chans)
  268. stm_output_disclaim(stm, output);
  269. spin_unlock(&output->lock);
  270. spin_unlock(&stm->mc_lock);
  271. }
  272. static void stm_output_init(struct stm_output *output)
  273. {
  274. spin_lock_init(&output->lock);
  275. }
  276. static int major_match(struct device *dev, const void *data)
  277. {
  278. unsigned int major = *(unsigned int *)data;
  279. return MAJOR(dev->devt) == major;
  280. }
  281. static int stm_char_open(struct inode *inode, struct file *file)
  282. {
  283. struct stm_file *stmf;
  284. struct device *dev;
  285. unsigned int major = imajor(inode);
  286. int err = -ENOMEM;
  287. dev = class_find_device(&stm_class, NULL, &major, major_match);
  288. if (!dev)
  289. return -ENODEV;
  290. stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
  291. if (!stmf)
  292. goto err_put_device;
  293. err = -ENODEV;
  294. stm_output_init(&stmf->output);
  295. stmf->stm = to_stm_device(dev);
  296. if (!try_module_get(stmf->stm->owner))
  297. goto err_free;
  298. file->private_data = stmf;
  299. return nonseekable_open(inode, file);
  300. err_free:
  301. kfree(stmf);
  302. err_put_device:
  303. /* matches class_find_device() above */
  304. put_device(dev);
  305. return err;
  306. }
  307. static int stm_char_release(struct inode *inode, struct file *file)
  308. {
  309. struct stm_file *stmf = file->private_data;
  310. struct stm_device *stm = stmf->stm;
  311. if (stm->data->unlink)
  312. stm->data->unlink(stm->data, stmf->output.master,
  313. stmf->output.channel);
  314. stm_output_free(stm, &stmf->output);
  315. /*
  316. * matches the stm_char_open()'s
  317. * class_find_device() + try_module_get()
  318. */
  319. stm_put_device(stm);
  320. kfree(stmf);
  321. return 0;
  322. }
  323. static int stm_file_assign(struct stm_file *stmf, char *id, unsigned int width)
  324. {
  325. struct stm_device *stm = stmf->stm;
  326. int ret;
  327. stmf->policy_node = stp_policy_node_lookup(stm, id);
  328. ret = stm_output_assign(stm, width, stmf->policy_node, &stmf->output);
  329. if (stmf->policy_node)
  330. stp_policy_node_put(stmf->policy_node);
  331. return ret;
  332. }
  333. static ssize_t notrace stm_write(struct stm_data *data, unsigned int master,
  334. unsigned int channel, const char *buf, size_t count)
  335. {
  336. unsigned int flags = STP_PACKET_TIMESTAMPED;
  337. const unsigned char *p = buf, nil = 0;
  338. size_t pos;
  339. ssize_t sz;
  340. for (pos = 0, p = buf; count > pos; pos += sz, p += sz) {
  341. sz = min_t(unsigned int, count - pos, 8);
  342. sz = data->packet(data, master, channel, STP_PACKET_DATA, flags,
  343. sz, p);
  344. flags = 0;
  345. if (sz < 0)
  346. break;
  347. }
  348. data->packet(data, master, channel, STP_PACKET_FLAG, 0, 0, &nil);
  349. return pos;
  350. }
  351. static ssize_t stm_char_write(struct file *file, const char __user *buf,
  352. size_t count, loff_t *ppos)
  353. {
  354. struct stm_file *stmf = file->private_data;
  355. struct stm_device *stm = stmf->stm;
  356. char *kbuf;
  357. int err;
  358. if (count + 1 > PAGE_SIZE)
  359. count = PAGE_SIZE - 1;
  360. /*
  361. * if no m/c have been assigned to this writer up to this
  362. * point, use "default" policy entry
  363. */
  364. if (!stmf->output.nr_chans) {
  365. err = stm_file_assign(stmf, "default", 1);
  366. /*
  367. * EBUSY means that somebody else just assigned this
  368. * output, which is just fine for write()
  369. */
  370. if (err && err != -EBUSY)
  371. return err;
  372. }
  373. kbuf = kmalloc(count + 1, GFP_KERNEL);
  374. if (!kbuf)
  375. return -ENOMEM;
  376. err = copy_from_user(kbuf, buf, count);
  377. if (err) {
  378. kfree(kbuf);
  379. return -EFAULT;
  380. }
  381. pm_runtime_get_sync(&stm->dev);
  382. count = stm_write(stm->data, stmf->output.master, stmf->output.channel,
  383. kbuf, count);
  384. pm_runtime_mark_last_busy(&stm->dev);
  385. pm_runtime_put_autosuspend(&stm->dev);
  386. kfree(kbuf);
  387. return count;
  388. }
  389. static void stm_mmap_open(struct vm_area_struct *vma)
  390. {
  391. struct stm_file *stmf = vma->vm_file->private_data;
  392. struct stm_device *stm = stmf->stm;
  393. pm_runtime_get(&stm->dev);
  394. }
  395. static void stm_mmap_close(struct vm_area_struct *vma)
  396. {
  397. struct stm_file *stmf = vma->vm_file->private_data;
  398. struct stm_device *stm = stmf->stm;
  399. pm_runtime_mark_last_busy(&stm->dev);
  400. pm_runtime_put_autosuspend(&stm->dev);
  401. }
  402. static const struct vm_operations_struct stm_mmap_vmops = {
  403. .open = stm_mmap_open,
  404. .close = stm_mmap_close,
  405. };
  406. static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)
  407. {
  408. struct stm_file *stmf = file->private_data;
  409. struct stm_device *stm = stmf->stm;
  410. unsigned long size, phys;
  411. if (!stm->data->mmio_addr)
  412. return -EOPNOTSUPP;
  413. if (vma->vm_pgoff)
  414. return -EINVAL;
  415. size = vma->vm_end - vma->vm_start;
  416. if (stmf->output.nr_chans * stm->data->sw_mmiosz != size)
  417. return -EINVAL;
  418. phys = stm->data->mmio_addr(stm->data, stmf->output.master,
  419. stmf->output.channel,
  420. stmf->output.nr_chans);
  421. if (!phys)
  422. return -EINVAL;
  423. pm_runtime_get_sync(&stm->dev);
  424. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  425. vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
  426. vma->vm_ops = &stm_mmap_vmops;
  427. vm_iomap_memory(vma, phys, size);
  428. return 0;
  429. }
  430. static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg)
  431. {
  432. struct stm_device *stm = stmf->stm;
  433. struct stp_policy_id *id;
  434. int ret = -EINVAL, wlimit = 1;
  435. u32 size;
  436. if (stmf->output.nr_chans)
  437. return -EBUSY;
  438. if (copy_from_user(&size, arg, sizeof(size)))
  439. return -EFAULT;
  440. if (size < sizeof(*id) || size >= PATH_MAX + sizeof(*id))
  441. return -EINVAL;
  442. /*
  443. * size + 1 to make sure the .id string at the bottom is terminated,
  444. * which is also why memdup_user() is not useful here
  445. */
  446. id = kzalloc(size + 1, GFP_KERNEL);
  447. if (!id)
  448. return -ENOMEM;
  449. if (copy_from_user(id, arg, size)) {
  450. ret = -EFAULT;
  451. goto err_free;
  452. }
  453. if (id->__reserved_0 || id->__reserved_1)
  454. goto err_free;
  455. if (stm->data->sw_mmiosz)
  456. wlimit = PAGE_SIZE / stm->data->sw_mmiosz;
  457. if (id->width < 1 || id->width > wlimit)
  458. goto err_free;
  459. ret = stm_file_assign(stmf, id->id, id->width);
  460. if (ret)
  461. goto err_free;
  462. if (stm->data->link)
  463. ret = stm->data->link(stm->data, stmf->output.master,
  464. stmf->output.channel);
  465. if (ret)
  466. stm_output_free(stmf->stm, &stmf->output);
  467. err_free:
  468. kfree(id);
  469. return ret;
  470. }
  471. static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg)
  472. {
  473. struct stp_policy_id id = {
  474. .size = sizeof(id),
  475. .master = stmf->output.master,
  476. .channel = stmf->output.channel,
  477. .width = stmf->output.nr_chans,
  478. .__reserved_0 = 0,
  479. .__reserved_1 = 0,
  480. };
  481. return copy_to_user(arg, &id, id.size) ? -EFAULT : 0;
  482. }
  483. static long
  484. stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  485. {
  486. struct stm_file *stmf = file->private_data;
  487. struct stm_data *stm_data = stmf->stm->data;
  488. int err = -ENOTTY;
  489. u64 options;
  490. switch (cmd) {
  491. case STP_POLICY_ID_SET:
  492. err = stm_char_policy_set_ioctl(stmf, (void __user *)arg);
  493. if (err)
  494. return err;
  495. return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
  496. case STP_POLICY_ID_GET:
  497. return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
  498. case STP_SET_OPTIONS:
  499. if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64)))
  500. return -EFAULT;
  501. if (stm_data->set_options)
  502. err = stm_data->set_options(stm_data,
  503. stmf->output.master,
  504. stmf->output.channel,
  505. stmf->output.nr_chans,
  506. options);
  507. break;
  508. default:
  509. break;
  510. }
  511. return err;
  512. }
  513. #ifdef CONFIG_COMPAT
  514. static long
  515. stm_char_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  516. {
  517. return stm_char_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  518. }
  519. #else
  520. #define stm_char_compat_ioctl NULL
  521. #endif
  522. static const struct file_operations stm_fops = {
  523. .open = stm_char_open,
  524. .release = stm_char_release,
  525. .write = stm_char_write,
  526. .mmap = stm_char_mmap,
  527. .unlocked_ioctl = stm_char_ioctl,
  528. .compat_ioctl = stm_char_compat_ioctl,
  529. .llseek = no_llseek,
  530. };
  531. static void stm_device_release(struct device *dev)
  532. {
  533. struct stm_device *stm = to_stm_device(dev);
  534. vfree(stm);
  535. }
  536. int stm_register_device(struct device *parent, struct stm_data *stm_data,
  537. struct module *owner)
  538. {
  539. struct stm_device *stm;
  540. unsigned int nmasters;
  541. int err = -ENOMEM;
  542. if (!stm_core_up)
  543. return -EPROBE_DEFER;
  544. if (!stm_data->packet || !stm_data->sw_nchannels)
  545. return -EINVAL;
  546. nmasters = stm_data->sw_end - stm_data->sw_start + 1;
  547. stm = vzalloc(sizeof(*stm) + nmasters * sizeof(void *));
  548. if (!stm)
  549. return -ENOMEM;
  550. stm->major = register_chrdev(0, stm_data->name, &stm_fops);
  551. if (stm->major < 0)
  552. goto err_free;
  553. device_initialize(&stm->dev);
  554. stm->dev.devt = MKDEV(stm->major, 0);
  555. stm->dev.class = &stm_class;
  556. stm->dev.parent = parent;
  557. stm->dev.release = stm_device_release;
  558. mutex_init(&stm->link_mutex);
  559. spin_lock_init(&stm->link_lock);
  560. INIT_LIST_HEAD(&stm->link_list);
  561. /* initialize the object before it is accessible via sysfs */
  562. spin_lock_init(&stm->mc_lock);
  563. mutex_init(&stm->policy_mutex);
  564. stm->sw_nmasters = nmasters;
  565. stm->owner = owner;
  566. stm->data = stm_data;
  567. stm_data->stm = stm;
  568. err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name);
  569. if (err)
  570. goto err_device;
  571. err = device_add(&stm->dev);
  572. if (err)
  573. goto err_device;
  574. /*
  575. * Use delayed autosuspend to avoid bouncing back and forth
  576. * on recurring character device writes, with the initial
  577. * delay time of 2 seconds.
  578. */
  579. pm_runtime_no_callbacks(&stm->dev);
  580. pm_runtime_use_autosuspend(&stm->dev);
  581. pm_runtime_set_autosuspend_delay(&stm->dev, 2000);
  582. pm_runtime_set_suspended(&stm->dev);
  583. pm_runtime_enable(&stm->dev);
  584. return 0;
  585. err_device:
  586. unregister_chrdev(stm->major, stm_data->name);
  587. /* matches device_initialize() above */
  588. put_device(&stm->dev);
  589. err_free:
  590. vfree(stm);
  591. return err;
  592. }
  593. EXPORT_SYMBOL_GPL(stm_register_device);
  594. static int __stm_source_link_drop(struct stm_source_device *src,
  595. struct stm_device *stm);
  596. void stm_unregister_device(struct stm_data *stm_data)
  597. {
  598. struct stm_device *stm = stm_data->stm;
  599. struct stm_source_device *src, *iter;
  600. int i, ret;
  601. pm_runtime_dont_use_autosuspend(&stm->dev);
  602. pm_runtime_disable(&stm->dev);
  603. mutex_lock(&stm->link_mutex);
  604. list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) {
  605. ret = __stm_source_link_drop(src, stm);
  606. /*
  607. * src <-> stm link must not change under the same
  608. * stm::link_mutex, so complain loudly if it has;
  609. * also in this situation ret!=0 means this src is
  610. * not connected to this stm and it should be otherwise
  611. * safe to proceed with the tear-down of stm.
  612. */
  613. WARN_ON_ONCE(ret);
  614. }
  615. mutex_unlock(&stm->link_mutex);
  616. synchronize_srcu(&stm_source_srcu);
  617. unregister_chrdev(stm->major, stm_data->name);
  618. mutex_lock(&stm->policy_mutex);
  619. if (stm->policy)
  620. stp_policy_unbind(stm->policy);
  621. mutex_unlock(&stm->policy_mutex);
  622. for (i = stm->data->sw_start; i <= stm->data->sw_end; i++)
  623. stp_master_free(stm, i);
  624. device_unregister(&stm->dev);
  625. stm_data->stm = NULL;
  626. }
  627. EXPORT_SYMBOL_GPL(stm_unregister_device);
  628. /*
  629. * stm::link_list access serialization uses a spinlock and a mutex; holding
  630. * either of them guarantees that the list is stable; modification requires
  631. * holding both of them.
  632. *
  633. * Lock ordering is as follows:
  634. * stm::link_mutex
  635. * stm::link_lock
  636. * src::link_lock
  637. */
  638. /**
  639. * stm_source_link_add() - connect an stm_source device to an stm device
  640. * @src: stm_source device
  641. * @stm: stm device
  642. *
  643. * This function establishes a link from stm_source to an stm device so that
  644. * the former can send out trace data to the latter.
  645. *
  646. * Return: 0 on success, -errno otherwise.
  647. */
  648. static int stm_source_link_add(struct stm_source_device *src,
  649. struct stm_device *stm)
  650. {
  651. char *id;
  652. int err;
  653. mutex_lock(&stm->link_mutex);
  654. spin_lock(&stm->link_lock);
  655. spin_lock(&src->link_lock);
  656. /* src->link is dereferenced under stm_source_srcu but not the list */
  657. rcu_assign_pointer(src->link, stm);
  658. list_add_tail(&src->link_entry, &stm->link_list);
  659. spin_unlock(&src->link_lock);
  660. spin_unlock(&stm->link_lock);
  661. mutex_unlock(&stm->link_mutex);
  662. id = kstrdup(src->data->name, GFP_KERNEL);
  663. if (id) {
  664. src->policy_node =
  665. stp_policy_node_lookup(stm, id);
  666. kfree(id);
  667. }
  668. err = stm_output_assign(stm, src->data->nr_chans,
  669. src->policy_node, &src->output);
  670. if (src->policy_node)
  671. stp_policy_node_put(src->policy_node);
  672. if (err)
  673. goto fail_detach;
  674. /* this is to notify the STM device that a new link has been made */
  675. if (stm->data->link)
  676. err = stm->data->link(stm->data, src->output.master,
  677. src->output.channel);
  678. if (err)
  679. goto fail_free_output;
  680. /* this is to let the source carry out all necessary preparations */
  681. if (src->data->link)
  682. src->data->link(src->data);
  683. return 0;
  684. fail_free_output:
  685. stm_output_free(stm, &src->output);
  686. fail_detach:
  687. mutex_lock(&stm->link_mutex);
  688. spin_lock(&stm->link_lock);
  689. spin_lock(&src->link_lock);
  690. rcu_assign_pointer(src->link, NULL);
  691. list_del_init(&src->link_entry);
  692. spin_unlock(&src->link_lock);
  693. spin_unlock(&stm->link_lock);
  694. mutex_unlock(&stm->link_mutex);
  695. return err;
  696. }
  697. /**
  698. * __stm_source_link_drop() - detach stm_source from an stm device
  699. * @src: stm_source device
  700. * @stm: stm device
  701. *
  702. * If @stm is @src::link, disconnect them from one another and put the
  703. * reference on the @stm device.
  704. *
  705. * Caller must hold stm::link_mutex.
  706. */
  707. static int __stm_source_link_drop(struct stm_source_device *src,
  708. struct stm_device *stm)
  709. {
  710. struct stm_device *link;
  711. int ret = 0;
  712. lockdep_assert_held(&stm->link_mutex);
  713. /* for stm::link_list modification, we hold both mutex and spinlock */
  714. spin_lock(&stm->link_lock);
  715. spin_lock(&src->link_lock);
  716. link = srcu_dereference_check(src->link, &stm_source_srcu, 1);
  717. /*
  718. * The linked device may have changed since we last looked, because
  719. * we weren't holding the src::link_lock back then; if this is the
  720. * case, tell the caller to retry.
  721. */
  722. if (link != stm) {
  723. ret = -EAGAIN;
  724. goto unlock;
  725. }
  726. stm_output_free(link, &src->output);
  727. list_del_init(&src->link_entry);
  728. pm_runtime_mark_last_busy(&link->dev);
  729. pm_runtime_put_autosuspend(&link->dev);
  730. /* matches stm_find_device() from stm_source_link_store() */
  731. stm_put_device(link);
  732. rcu_assign_pointer(src->link, NULL);
  733. unlock:
  734. spin_unlock(&src->link_lock);
  735. spin_unlock(&stm->link_lock);
  736. /*
  737. * Call the unlink callbacks for both source and stm, when we know
  738. * that we have actually performed the unlinking.
  739. */
  740. if (!ret) {
  741. if (src->data->unlink)
  742. src->data->unlink(src->data);
  743. if (stm->data->unlink)
  744. stm->data->unlink(stm->data, src->output.master,
  745. src->output.channel);
  746. }
  747. return ret;
  748. }
  749. /**
  750. * stm_source_link_drop() - detach stm_source from its stm device
  751. * @src: stm_source device
  752. *
  753. * Unlinking means disconnecting from source's STM device; after this
  754. * writes will be unsuccessful until it is linked to a new STM device.
  755. *
  756. * This will happen on "stm_source_link" sysfs attribute write to undo
  757. * the existing link (if any), or on linked STM device's de-registration.
  758. */
  759. static void stm_source_link_drop(struct stm_source_device *src)
  760. {
  761. struct stm_device *stm;
  762. int idx, ret;
  763. retry:
  764. idx = srcu_read_lock(&stm_source_srcu);
  765. /*
  766. * The stm device will be valid for the duration of this
  767. * read section, but the link may change before we grab
  768. * the src::link_lock in __stm_source_link_drop().
  769. */
  770. stm = srcu_dereference(src->link, &stm_source_srcu);
  771. ret = 0;
  772. if (stm) {
  773. mutex_lock(&stm->link_mutex);
  774. ret = __stm_source_link_drop(src, stm);
  775. mutex_unlock(&stm->link_mutex);
  776. }
  777. srcu_read_unlock(&stm_source_srcu, idx);
  778. /* if it did change, retry */
  779. if (ret == -EAGAIN)
  780. goto retry;
  781. }
  782. static ssize_t stm_source_link_show(struct device *dev,
  783. struct device_attribute *attr,
  784. char *buf)
  785. {
  786. struct stm_source_device *src = to_stm_source_device(dev);
  787. struct stm_device *stm;
  788. int idx, ret;
  789. idx = srcu_read_lock(&stm_source_srcu);
  790. stm = srcu_dereference(src->link, &stm_source_srcu);
  791. ret = sprintf(buf, "%s\n",
  792. stm ? dev_name(&stm->dev) : "<none>");
  793. srcu_read_unlock(&stm_source_srcu, idx);
  794. return ret;
  795. }
  796. static ssize_t stm_source_link_store(struct device *dev,
  797. struct device_attribute *attr,
  798. const char *buf, size_t count)
  799. {
  800. struct stm_source_device *src = to_stm_source_device(dev);
  801. struct stm_device *link;
  802. int err;
  803. stm_source_link_drop(src);
  804. link = stm_find_device(buf);
  805. if (!link)
  806. return -EINVAL;
  807. pm_runtime_get(&link->dev);
  808. err = stm_source_link_add(src, link);
  809. if (err) {
  810. pm_runtime_put_autosuspend(&link->dev);
  811. /* matches the stm_find_device() above */
  812. stm_put_device(link);
  813. }
  814. return err ? : count;
  815. }
  816. static DEVICE_ATTR_RW(stm_source_link);
  817. static struct attribute *stm_source_attrs[] = {
  818. &dev_attr_stm_source_link.attr,
  819. NULL,
  820. };
  821. ATTRIBUTE_GROUPS(stm_source);
  822. static struct class stm_source_class = {
  823. .name = "stm_source",
  824. .dev_groups = stm_source_groups,
  825. };
  826. static void stm_source_device_release(struct device *dev)
  827. {
  828. struct stm_source_device *src = to_stm_source_device(dev);
  829. kfree(src);
  830. }
  831. /**
  832. * stm_source_register_device() - register an stm_source device
  833. * @parent: parent device
  834. * @data: device description structure
  835. *
  836. * This will create a device of stm_source class that can write
  837. * data to an stm device once linked.
  838. *
  839. * Return: 0 on success, -errno otherwise.
  840. */
  841. int stm_source_register_device(struct device *parent,
  842. struct stm_source_data *data)
  843. {
  844. struct stm_source_device *src;
  845. int err;
  846. if (!stm_core_up)
  847. return -EPROBE_DEFER;
  848. src = kzalloc(sizeof(*src), GFP_KERNEL);
  849. if (!src)
  850. return -ENOMEM;
  851. device_initialize(&src->dev);
  852. src->dev.class = &stm_source_class;
  853. src->dev.parent = parent;
  854. src->dev.release = stm_source_device_release;
  855. err = kobject_set_name(&src->dev.kobj, "%s", data->name);
  856. if (err)
  857. goto err;
  858. pm_runtime_no_callbacks(&src->dev);
  859. pm_runtime_forbid(&src->dev);
  860. err = device_add(&src->dev);
  861. if (err)
  862. goto err;
  863. stm_output_init(&src->output);
  864. spin_lock_init(&src->link_lock);
  865. INIT_LIST_HEAD(&src->link_entry);
  866. src->data = data;
  867. data->src = src;
  868. return 0;
  869. err:
  870. put_device(&src->dev);
  871. return err;
  872. }
  873. EXPORT_SYMBOL_GPL(stm_source_register_device);
  874. /**
  875. * stm_source_unregister_device() - unregister an stm_source device
  876. * @data: device description that was used to register the device
  877. *
  878. * This will remove a previously created stm_source device from the system.
  879. */
  880. void stm_source_unregister_device(struct stm_source_data *data)
  881. {
  882. struct stm_source_device *src = data->src;
  883. stm_source_link_drop(src);
  884. device_unregister(&src->dev);
  885. }
  886. EXPORT_SYMBOL_GPL(stm_source_unregister_device);
  887. int notrace stm_source_write(struct stm_source_data *data,
  888. unsigned int chan,
  889. const char *buf, size_t count)
  890. {
  891. struct stm_source_device *src = data->src;
  892. struct stm_device *stm;
  893. int idx;
  894. if (!src->output.nr_chans)
  895. return -ENODEV;
  896. if (chan >= src->output.nr_chans)
  897. return -EINVAL;
  898. idx = srcu_read_lock(&stm_source_srcu);
  899. stm = srcu_dereference(src->link, &stm_source_srcu);
  900. if (stm)
  901. count = stm_write(stm->data, src->output.master,
  902. src->output.channel + chan,
  903. buf, count);
  904. else
  905. count = -ENODEV;
  906. srcu_read_unlock(&stm_source_srcu, idx);
  907. return count;
  908. }
  909. EXPORT_SYMBOL_GPL(stm_source_write);
  910. static int __init stm_core_init(void)
  911. {
  912. int err;
  913. err = class_register(&stm_class);
  914. if (err)
  915. return err;
  916. err = class_register(&stm_source_class);
  917. if (err)
  918. goto err_stm;
  919. err = stp_configfs_init();
  920. if (err)
  921. goto err_src;
  922. init_srcu_struct(&stm_source_srcu);
  923. stm_core_up++;
  924. return 0;
  925. err_src:
  926. class_unregister(&stm_source_class);
  927. err_stm:
  928. class_unregister(&stm_class);
  929. return err;
  930. }
  931. module_init(stm_core_init);
  932. static void __exit stm_core_exit(void)
  933. {
  934. cleanup_srcu_struct(&stm_source_srcu);
  935. class_unregister(&stm_source_class);
  936. class_unregister(&stm_class);
  937. stp_configfs_exit();
  938. }
  939. module_exit(stm_core_exit);
  940. MODULE_LICENSE("GPL v2");
  941. MODULE_DESCRIPTION("System Trace Module device class");
  942. MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");