core.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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. put_device(dev);
  96. return NULL;
  97. }
  98. return stm;
  99. }
  100. /**
  101. * stm_put_device() - drop references on the stm device
  102. * @stm: stm device, previously acquired by stm_find_device()
  103. *
  104. * This drops the module reference and device reference taken by
  105. * stm_find_device().
  106. */
  107. void stm_put_device(struct stm_device *stm)
  108. {
  109. module_put(stm->owner);
  110. put_device(&stm->dev);
  111. }
  112. /*
  113. * Internally we only care about software-writable masters here, that is the
  114. * ones in the range [stm_data->sw_start..stm_data..sw_end], however we need
  115. * original master numbers to be visible externally, since they are the ones
  116. * that will appear in the STP stream. Thus, the internal bookkeeping uses
  117. * $master - stm_data->sw_start to reference master descriptors and such.
  118. */
  119. #define __stm_master(_s, _m) \
  120. ((_s)->masters[(_m) - (_s)->data->sw_start])
  121. static inline struct stp_master *
  122. stm_master(struct stm_device *stm, unsigned int idx)
  123. {
  124. if (idx < stm->data->sw_start || idx > stm->data->sw_end)
  125. return NULL;
  126. return __stm_master(stm, idx);
  127. }
  128. static int stp_master_alloc(struct stm_device *stm, unsigned int idx)
  129. {
  130. struct stp_master *master;
  131. size_t size;
  132. size = ALIGN(stm->data->sw_nchannels, 8) / 8;
  133. size += sizeof(struct stp_master);
  134. master = kzalloc(size, GFP_ATOMIC);
  135. if (!master)
  136. return -ENOMEM;
  137. master->nr_free = stm->data->sw_nchannels;
  138. __stm_master(stm, idx) = master;
  139. return 0;
  140. }
  141. static void stp_master_free(struct stm_device *stm, unsigned int idx)
  142. {
  143. struct stp_master *master = stm_master(stm, idx);
  144. if (!master)
  145. return;
  146. __stm_master(stm, idx) = NULL;
  147. kfree(master);
  148. }
  149. static void stm_output_claim(struct stm_device *stm, struct stm_output *output)
  150. {
  151. struct stp_master *master = stm_master(stm, output->master);
  152. if (WARN_ON_ONCE(master->nr_free < output->nr_chans))
  153. return;
  154. bitmap_allocate_region(&master->chan_map[0], output->channel,
  155. ilog2(output->nr_chans));
  156. master->nr_free -= output->nr_chans;
  157. }
  158. static void
  159. stm_output_disclaim(struct stm_device *stm, struct stm_output *output)
  160. {
  161. struct stp_master *master = stm_master(stm, output->master);
  162. bitmap_release_region(&master->chan_map[0], output->channel,
  163. ilog2(output->nr_chans));
  164. output->nr_chans = 0;
  165. master->nr_free += output->nr_chans;
  166. }
  167. /*
  168. * This is like bitmap_find_free_region(), except it can ignore @start bits
  169. * at the beginning.
  170. */
  171. static int find_free_channels(unsigned long *bitmap, unsigned int start,
  172. unsigned int end, unsigned int width)
  173. {
  174. unsigned int pos;
  175. int i;
  176. for (pos = start; pos < end + 1; pos = ALIGN(pos, width)) {
  177. pos = find_next_zero_bit(bitmap, end + 1, pos);
  178. if (pos + width > end + 1)
  179. break;
  180. if (pos & (width - 1))
  181. continue;
  182. for (i = 1; i < width && !test_bit(pos + i, bitmap); i++)
  183. ;
  184. if (i == width)
  185. return pos;
  186. }
  187. return -1;
  188. }
  189. static unsigned int
  190. stm_find_master_chan(struct stm_device *stm, unsigned int width,
  191. unsigned int *mstart, unsigned int mend,
  192. unsigned int *cstart, unsigned int cend)
  193. {
  194. struct stp_master *master;
  195. unsigned int midx;
  196. int pos, err;
  197. for (midx = *mstart; midx <= mend; midx++) {
  198. if (!stm_master(stm, midx)) {
  199. err = stp_master_alloc(stm, midx);
  200. if (err)
  201. return err;
  202. }
  203. master = stm_master(stm, midx);
  204. if (!master->nr_free)
  205. continue;
  206. pos = find_free_channels(master->chan_map, *cstart, cend,
  207. width);
  208. if (pos < 0)
  209. continue;
  210. *mstart = midx;
  211. *cstart = pos;
  212. return 0;
  213. }
  214. return -ENOSPC;
  215. }
  216. static int stm_output_assign(struct stm_device *stm, unsigned int width,
  217. struct stp_policy_node *policy_node,
  218. struct stm_output *output)
  219. {
  220. unsigned int midx, cidx, mend, cend;
  221. int ret = -EINVAL;
  222. if (width > stm->data->sw_nchannels)
  223. return -EINVAL;
  224. if (policy_node) {
  225. stp_policy_node_get_ranges(policy_node,
  226. &midx, &mend, &cidx, &cend);
  227. } else {
  228. midx = stm->data->sw_start;
  229. cidx = 0;
  230. mend = stm->data->sw_end;
  231. cend = stm->data->sw_nchannels - 1;
  232. }
  233. spin_lock(&stm->mc_lock);
  234. /* output is already assigned -- shouldn't happen */
  235. if (WARN_ON_ONCE(output->nr_chans))
  236. goto unlock;
  237. ret = stm_find_master_chan(stm, width, &midx, mend, &cidx, cend);
  238. if (ret)
  239. goto unlock;
  240. output->master = midx;
  241. output->channel = cidx;
  242. output->nr_chans = width;
  243. stm_output_claim(stm, output);
  244. dev_dbg(&stm->dev, "assigned %u:%u (+%u)\n", midx, cidx, width);
  245. ret = 0;
  246. unlock:
  247. spin_unlock(&stm->mc_lock);
  248. return ret;
  249. }
  250. static void stm_output_free(struct stm_device *stm, struct stm_output *output)
  251. {
  252. spin_lock(&stm->mc_lock);
  253. if (output->nr_chans)
  254. stm_output_disclaim(stm, output);
  255. spin_unlock(&stm->mc_lock);
  256. }
  257. static int major_match(struct device *dev, const void *data)
  258. {
  259. unsigned int major = *(unsigned int *)data;
  260. return MAJOR(dev->devt) == major;
  261. }
  262. static int stm_char_open(struct inode *inode, struct file *file)
  263. {
  264. struct stm_file *stmf;
  265. struct device *dev;
  266. unsigned int major = imajor(inode);
  267. int err = -ENODEV;
  268. dev = class_find_device(&stm_class, NULL, &major, major_match);
  269. if (!dev)
  270. return -ENODEV;
  271. stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
  272. if (!stmf)
  273. return -ENOMEM;
  274. stmf->stm = to_stm_device(dev);
  275. if (!try_module_get(stmf->stm->owner))
  276. goto err_free;
  277. file->private_data = stmf;
  278. return nonseekable_open(inode, file);
  279. err_free:
  280. kfree(stmf);
  281. return err;
  282. }
  283. static int stm_char_release(struct inode *inode, struct file *file)
  284. {
  285. struct stm_file *stmf = file->private_data;
  286. stm_output_free(stmf->stm, &stmf->output);
  287. stm_put_device(stmf->stm);
  288. kfree(stmf);
  289. return 0;
  290. }
  291. static int stm_file_assign(struct stm_file *stmf, char *id, unsigned int width)
  292. {
  293. struct stm_device *stm = stmf->stm;
  294. int ret;
  295. stmf->policy_node = stp_policy_node_lookup(stm, id);
  296. ret = stm_output_assign(stm, width, stmf->policy_node, &stmf->output);
  297. if (stmf->policy_node)
  298. stp_policy_node_put(stmf->policy_node);
  299. return ret;
  300. }
  301. static void stm_write(struct stm_data *data, unsigned int master,
  302. unsigned int channel, const char *buf, size_t count)
  303. {
  304. unsigned int flags = STP_PACKET_TIMESTAMPED;
  305. const unsigned char *p = buf, nil = 0;
  306. size_t pos;
  307. ssize_t sz;
  308. for (pos = 0, p = buf; count > pos; pos += sz, p += sz) {
  309. sz = min_t(unsigned int, count - pos, 8);
  310. sz = data->packet(data, master, channel, STP_PACKET_DATA, flags,
  311. sz, p);
  312. flags = 0;
  313. }
  314. data->packet(data, master, channel, STP_PACKET_FLAG, 0, 0, &nil);
  315. }
  316. static ssize_t stm_char_write(struct file *file, const char __user *buf,
  317. size_t count, loff_t *ppos)
  318. {
  319. struct stm_file *stmf = file->private_data;
  320. struct stm_device *stm = stmf->stm;
  321. char *kbuf;
  322. int err;
  323. /*
  324. * if no m/c have been assigned to this writer up to this
  325. * point, use "default" policy entry
  326. */
  327. if (!stmf->output.nr_chans) {
  328. err = stm_file_assign(stmf, "default", 1);
  329. /*
  330. * EBUSY means that somebody else just assigned this
  331. * output, which is just fine for write()
  332. */
  333. if (err && err != -EBUSY)
  334. return err;
  335. }
  336. kbuf = kmalloc(count + 1, GFP_KERNEL);
  337. if (!kbuf)
  338. return -ENOMEM;
  339. err = copy_from_user(kbuf, buf, count);
  340. if (err) {
  341. kfree(kbuf);
  342. return -EFAULT;
  343. }
  344. stm_write(stm->data, stmf->output.master, stmf->output.channel, kbuf,
  345. count);
  346. kfree(kbuf);
  347. return count;
  348. }
  349. static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)
  350. {
  351. struct stm_file *stmf = file->private_data;
  352. struct stm_device *stm = stmf->stm;
  353. unsigned long size, phys;
  354. if (!stm->data->mmio_addr)
  355. return -EOPNOTSUPP;
  356. if (vma->vm_pgoff)
  357. return -EINVAL;
  358. size = vma->vm_end - vma->vm_start;
  359. if (stmf->output.nr_chans * stm->data->sw_mmiosz != size)
  360. return -EINVAL;
  361. phys = stm->data->mmio_addr(stm->data, stmf->output.master,
  362. stmf->output.channel,
  363. stmf->output.nr_chans);
  364. if (!phys)
  365. return -EINVAL;
  366. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  367. vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
  368. vm_iomap_memory(vma, phys, size);
  369. return 0;
  370. }
  371. static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg)
  372. {
  373. struct stm_device *stm = stmf->stm;
  374. struct stp_policy_id *id;
  375. int ret = -EINVAL;
  376. u32 size;
  377. if (stmf->output.nr_chans)
  378. return -EBUSY;
  379. if (copy_from_user(&size, arg, sizeof(size)))
  380. return -EFAULT;
  381. if (size >= PATH_MAX + sizeof(*id))
  382. return -EINVAL;
  383. /*
  384. * size + 1 to make sure the .id string at the bottom is terminated,
  385. * which is also why memdup_user() is not useful here
  386. */
  387. id = kzalloc(size + 1, GFP_KERNEL);
  388. if (!id)
  389. return -ENOMEM;
  390. if (copy_from_user(id, arg, size)) {
  391. ret = -EFAULT;
  392. goto err_free;
  393. }
  394. if (id->__reserved_0 || id->__reserved_1)
  395. goto err_free;
  396. if (id->width < 1 ||
  397. id->width > PAGE_SIZE / stm->data->sw_mmiosz)
  398. goto err_free;
  399. ret = stm_file_assign(stmf, id->id, id->width);
  400. if (ret)
  401. goto err_free;
  402. ret = 0;
  403. if (stm->data->link)
  404. ret = stm->data->link(stm->data, stmf->output.master,
  405. stmf->output.channel);
  406. if (ret) {
  407. stm_output_free(stmf->stm, &stmf->output);
  408. stm_put_device(stmf->stm);
  409. }
  410. err_free:
  411. kfree(id);
  412. return ret;
  413. }
  414. static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg)
  415. {
  416. struct stp_policy_id id = {
  417. .size = sizeof(id),
  418. .master = stmf->output.master,
  419. .channel = stmf->output.channel,
  420. .width = stmf->output.nr_chans,
  421. .__reserved_0 = 0,
  422. .__reserved_1 = 0,
  423. };
  424. return copy_to_user(arg, &id, id.size) ? -EFAULT : 0;
  425. }
  426. static long
  427. stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  428. {
  429. struct stm_file *stmf = file->private_data;
  430. struct stm_data *stm_data = stmf->stm->data;
  431. int err = -ENOTTY;
  432. u64 options;
  433. switch (cmd) {
  434. case STP_POLICY_ID_SET:
  435. err = stm_char_policy_set_ioctl(stmf, (void __user *)arg);
  436. if (err)
  437. return err;
  438. return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
  439. case STP_POLICY_ID_GET:
  440. return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
  441. case STP_SET_OPTIONS:
  442. if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64)))
  443. return -EFAULT;
  444. if (stm_data->set_options)
  445. err = stm_data->set_options(stm_data,
  446. stmf->output.master,
  447. stmf->output.channel,
  448. stmf->output.nr_chans,
  449. options);
  450. break;
  451. default:
  452. break;
  453. }
  454. return err;
  455. }
  456. #ifdef CONFIG_COMPAT
  457. static long
  458. stm_char_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  459. {
  460. return stm_char_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  461. }
  462. #else
  463. #define stm_char_compat_ioctl NULL
  464. #endif
  465. static const struct file_operations stm_fops = {
  466. .open = stm_char_open,
  467. .release = stm_char_release,
  468. .write = stm_char_write,
  469. .mmap = stm_char_mmap,
  470. .unlocked_ioctl = stm_char_ioctl,
  471. .compat_ioctl = stm_char_compat_ioctl,
  472. .llseek = no_llseek,
  473. };
  474. static void stm_device_release(struct device *dev)
  475. {
  476. struct stm_device *stm = to_stm_device(dev);
  477. kfree(stm);
  478. }
  479. int stm_register_device(struct device *parent, struct stm_data *stm_data,
  480. struct module *owner)
  481. {
  482. struct stm_device *stm;
  483. unsigned int nmasters;
  484. int err = -ENOMEM;
  485. if (!stm_core_up)
  486. return -EPROBE_DEFER;
  487. if (!stm_data->packet || !stm_data->sw_nchannels)
  488. return -EINVAL;
  489. nmasters = stm_data->sw_end - stm_data->sw_start;
  490. stm = kzalloc(sizeof(*stm) + nmasters * sizeof(void *), GFP_KERNEL);
  491. if (!stm)
  492. return -ENOMEM;
  493. stm->major = register_chrdev(0, stm_data->name, &stm_fops);
  494. if (stm->major < 0)
  495. goto err_free;
  496. device_initialize(&stm->dev);
  497. stm->dev.devt = MKDEV(stm->major, 0);
  498. stm->dev.class = &stm_class;
  499. stm->dev.parent = parent;
  500. stm->dev.release = stm_device_release;
  501. err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name);
  502. if (err)
  503. goto err_device;
  504. err = device_add(&stm->dev);
  505. if (err)
  506. goto err_device;
  507. spin_lock_init(&stm->link_lock);
  508. INIT_LIST_HEAD(&stm->link_list);
  509. spin_lock_init(&stm->mc_lock);
  510. mutex_init(&stm->policy_mutex);
  511. stm->sw_nmasters = nmasters;
  512. stm->owner = owner;
  513. stm->data = stm_data;
  514. stm_data->stm = stm;
  515. return 0;
  516. err_device:
  517. put_device(&stm->dev);
  518. err_free:
  519. kfree(stm);
  520. return err;
  521. }
  522. EXPORT_SYMBOL_GPL(stm_register_device);
  523. static void __stm_source_link_drop(struct stm_source_device *src,
  524. struct stm_device *stm);
  525. void stm_unregister_device(struct stm_data *stm_data)
  526. {
  527. struct stm_device *stm = stm_data->stm;
  528. struct stm_source_device *src, *iter;
  529. int i;
  530. spin_lock(&stm->link_lock);
  531. list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) {
  532. __stm_source_link_drop(src, stm);
  533. }
  534. spin_unlock(&stm->link_lock);
  535. synchronize_srcu(&stm_source_srcu);
  536. unregister_chrdev(stm->major, stm_data->name);
  537. mutex_lock(&stm->policy_mutex);
  538. if (stm->policy)
  539. stp_policy_unbind(stm->policy);
  540. mutex_unlock(&stm->policy_mutex);
  541. for (i = 0; i < stm->sw_nmasters; i++)
  542. stp_master_free(stm, i);
  543. device_unregister(&stm->dev);
  544. stm_data->stm = NULL;
  545. }
  546. EXPORT_SYMBOL_GPL(stm_unregister_device);
  547. /**
  548. * stm_source_link_add() - connect an stm_source device to an stm device
  549. * @src: stm_source device
  550. * @stm: stm device
  551. *
  552. * This function establishes a link from stm_source to an stm device so that
  553. * the former can send out trace data to the latter.
  554. *
  555. * Return: 0 on success, -errno otherwise.
  556. */
  557. static int stm_source_link_add(struct stm_source_device *src,
  558. struct stm_device *stm)
  559. {
  560. char *id;
  561. int err;
  562. spin_lock(&stm->link_lock);
  563. spin_lock(&src->link_lock);
  564. /* src->link is dereferenced under stm_source_srcu but not the list */
  565. rcu_assign_pointer(src->link, stm);
  566. list_add_tail(&src->link_entry, &stm->link_list);
  567. spin_unlock(&src->link_lock);
  568. spin_unlock(&stm->link_lock);
  569. id = kstrdup(src->data->name, GFP_KERNEL);
  570. if (id) {
  571. src->policy_node =
  572. stp_policy_node_lookup(stm, id);
  573. kfree(id);
  574. }
  575. err = stm_output_assign(stm, src->data->nr_chans,
  576. src->policy_node, &src->output);
  577. if (src->policy_node)
  578. stp_policy_node_put(src->policy_node);
  579. if (err)
  580. goto fail_detach;
  581. /* this is to notify the STM device that a new link has been made */
  582. if (stm->data->link)
  583. err = stm->data->link(stm->data, src->output.master,
  584. src->output.channel);
  585. if (err)
  586. goto fail_free_output;
  587. /* this is to let the source carry out all necessary preparations */
  588. if (src->data->link)
  589. src->data->link(src->data);
  590. return 0;
  591. fail_free_output:
  592. stm_output_free(stm, &src->output);
  593. stm_put_device(stm);
  594. fail_detach:
  595. spin_lock(&stm->link_lock);
  596. spin_lock(&src->link_lock);
  597. rcu_assign_pointer(src->link, NULL);
  598. list_del_init(&src->link_entry);
  599. spin_unlock(&src->link_lock);
  600. spin_unlock(&stm->link_lock);
  601. return err;
  602. }
  603. /**
  604. * __stm_source_link_drop() - detach stm_source from an stm device
  605. * @src: stm_source device
  606. * @stm: stm device
  607. *
  608. * If @stm is @src::link, disconnect them from one another and put the
  609. * reference on the @stm device.
  610. *
  611. * Caller must hold stm::link_lock.
  612. */
  613. static void __stm_source_link_drop(struct stm_source_device *src,
  614. struct stm_device *stm)
  615. {
  616. struct stm_device *link;
  617. spin_lock(&src->link_lock);
  618. link = srcu_dereference_check(src->link, &stm_source_srcu, 1);
  619. if (WARN_ON_ONCE(link != stm)) {
  620. spin_unlock(&src->link_lock);
  621. return;
  622. }
  623. stm_output_free(link, &src->output);
  624. /* caller must hold stm::link_lock */
  625. list_del_init(&src->link_entry);
  626. /* matches stm_find_device() from stm_source_link_store() */
  627. stm_put_device(link);
  628. rcu_assign_pointer(src->link, NULL);
  629. spin_unlock(&src->link_lock);
  630. }
  631. /**
  632. * stm_source_link_drop() - detach stm_source from its stm device
  633. * @src: stm_source device
  634. *
  635. * Unlinking means disconnecting from source's STM device; after this
  636. * writes will be unsuccessful until it is linked to a new STM device.
  637. *
  638. * This will happen on "stm_source_link" sysfs attribute write to undo
  639. * the existing link (if any), or on linked STM device's de-registration.
  640. */
  641. static void stm_source_link_drop(struct stm_source_device *src)
  642. {
  643. struct stm_device *stm;
  644. int idx;
  645. idx = srcu_read_lock(&stm_source_srcu);
  646. stm = srcu_dereference(src->link, &stm_source_srcu);
  647. if (stm) {
  648. if (src->data->unlink)
  649. src->data->unlink(src->data);
  650. spin_lock(&stm->link_lock);
  651. __stm_source_link_drop(src, stm);
  652. spin_unlock(&stm->link_lock);
  653. }
  654. srcu_read_unlock(&stm_source_srcu, idx);
  655. }
  656. static ssize_t stm_source_link_show(struct device *dev,
  657. struct device_attribute *attr,
  658. char *buf)
  659. {
  660. struct stm_source_device *src = to_stm_source_device(dev);
  661. struct stm_device *stm;
  662. int idx, ret;
  663. idx = srcu_read_lock(&stm_source_srcu);
  664. stm = srcu_dereference(src->link, &stm_source_srcu);
  665. ret = sprintf(buf, "%s\n",
  666. stm ? dev_name(&stm->dev) : "<none>");
  667. srcu_read_unlock(&stm_source_srcu, idx);
  668. return ret;
  669. }
  670. static ssize_t stm_source_link_store(struct device *dev,
  671. struct device_attribute *attr,
  672. const char *buf, size_t count)
  673. {
  674. struct stm_source_device *src = to_stm_source_device(dev);
  675. struct stm_device *link;
  676. int err;
  677. stm_source_link_drop(src);
  678. link = stm_find_device(buf);
  679. if (!link)
  680. return -EINVAL;
  681. err = stm_source_link_add(src, link);
  682. if (err)
  683. stm_put_device(link);
  684. return err ? : count;
  685. }
  686. static DEVICE_ATTR_RW(stm_source_link);
  687. static struct attribute *stm_source_attrs[] = {
  688. &dev_attr_stm_source_link.attr,
  689. NULL,
  690. };
  691. ATTRIBUTE_GROUPS(stm_source);
  692. static struct class stm_source_class = {
  693. .name = "stm_source",
  694. .dev_groups = stm_source_groups,
  695. };
  696. static void stm_source_device_release(struct device *dev)
  697. {
  698. struct stm_source_device *src = to_stm_source_device(dev);
  699. kfree(src);
  700. }
  701. /**
  702. * stm_source_register_device() - register an stm_source device
  703. * @parent: parent device
  704. * @data: device description structure
  705. *
  706. * This will create a device of stm_source class that can write
  707. * data to an stm device once linked.
  708. *
  709. * Return: 0 on success, -errno otherwise.
  710. */
  711. int stm_source_register_device(struct device *parent,
  712. struct stm_source_data *data)
  713. {
  714. struct stm_source_device *src;
  715. int err;
  716. if (!stm_core_up)
  717. return -EPROBE_DEFER;
  718. src = kzalloc(sizeof(*src), GFP_KERNEL);
  719. if (!src)
  720. return -ENOMEM;
  721. device_initialize(&src->dev);
  722. src->dev.class = &stm_source_class;
  723. src->dev.parent = parent;
  724. src->dev.release = stm_source_device_release;
  725. err = kobject_set_name(&src->dev.kobj, "%s", data->name);
  726. if (err)
  727. goto err;
  728. err = device_add(&src->dev);
  729. if (err)
  730. goto err;
  731. spin_lock_init(&src->link_lock);
  732. INIT_LIST_HEAD(&src->link_entry);
  733. src->data = data;
  734. data->src = src;
  735. return 0;
  736. err:
  737. put_device(&src->dev);
  738. kfree(src);
  739. return err;
  740. }
  741. EXPORT_SYMBOL_GPL(stm_source_register_device);
  742. /**
  743. * stm_source_unregister_device() - unregister an stm_source device
  744. * @data: device description that was used to register the device
  745. *
  746. * This will remove a previously created stm_source device from the system.
  747. */
  748. void stm_source_unregister_device(struct stm_source_data *data)
  749. {
  750. struct stm_source_device *src = data->src;
  751. stm_source_link_drop(src);
  752. device_destroy(&stm_source_class, src->dev.devt);
  753. }
  754. EXPORT_SYMBOL_GPL(stm_source_unregister_device);
  755. int stm_source_write(struct stm_source_data *data, unsigned int chan,
  756. const char *buf, size_t count)
  757. {
  758. struct stm_source_device *src = data->src;
  759. struct stm_device *stm;
  760. int idx;
  761. if (!src->output.nr_chans)
  762. return -ENODEV;
  763. if (chan >= src->output.nr_chans)
  764. return -EINVAL;
  765. idx = srcu_read_lock(&stm_source_srcu);
  766. stm = srcu_dereference(src->link, &stm_source_srcu);
  767. if (stm)
  768. stm_write(stm->data, src->output.master,
  769. src->output.channel + chan,
  770. buf, count);
  771. else
  772. count = -ENODEV;
  773. srcu_read_unlock(&stm_source_srcu, idx);
  774. return count;
  775. }
  776. EXPORT_SYMBOL_GPL(stm_source_write);
  777. static int __init stm_core_init(void)
  778. {
  779. int err;
  780. err = class_register(&stm_class);
  781. if (err)
  782. return err;
  783. err = class_register(&stm_source_class);
  784. if (err)
  785. goto err_stm;
  786. err = stp_configfs_init();
  787. if (err)
  788. goto err_src;
  789. init_srcu_struct(&stm_source_srcu);
  790. stm_core_up++;
  791. return 0;
  792. err_src:
  793. class_unregister(&stm_source_class);
  794. err_stm:
  795. class_unregister(&stm_class);
  796. return err;
  797. }
  798. module_init(stm_core_init);
  799. static void __exit stm_core_exit(void)
  800. {
  801. cleanup_srcu_struct(&stm_source_srcu);
  802. class_unregister(&stm_source_class);
  803. class_unregister(&stm_class);
  804. stp_configfs_exit();
  805. }
  806. module_exit(stm_core_exit);
  807. MODULE_LICENSE("GPL v2");
  808. MODULE_DESCRIPTION("System Trace Module device class");
  809. MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");