core.c 27 KB

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