core.c 25 KB

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