core.c 27 KB

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