core.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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. mutex_init(&stm->link_mutex);
  508. spin_lock_init(&stm->link_lock);
  509. INIT_LIST_HEAD(&stm->link_list);
  510. spin_lock_init(&stm->mc_lock);
  511. mutex_init(&stm->policy_mutex);
  512. stm->sw_nmasters = nmasters;
  513. stm->owner = owner;
  514. stm->data = stm_data;
  515. stm_data->stm = stm;
  516. return 0;
  517. err_device:
  518. put_device(&stm->dev);
  519. err_free:
  520. kfree(stm);
  521. return err;
  522. }
  523. EXPORT_SYMBOL_GPL(stm_register_device);
  524. static void __stm_source_link_drop(struct stm_source_device *src,
  525. struct stm_device *stm);
  526. void stm_unregister_device(struct stm_data *stm_data)
  527. {
  528. struct stm_device *stm = stm_data->stm;
  529. struct stm_source_device *src, *iter;
  530. int i;
  531. mutex_lock(&stm->link_mutex);
  532. list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) {
  533. __stm_source_link_drop(src, stm);
  534. }
  535. mutex_unlock(&stm->link_mutex);
  536. synchronize_srcu(&stm_source_srcu);
  537. unregister_chrdev(stm->major, stm_data->name);
  538. mutex_lock(&stm->policy_mutex);
  539. if (stm->policy)
  540. stp_policy_unbind(stm->policy);
  541. mutex_unlock(&stm->policy_mutex);
  542. for (i = 0; i < stm->sw_nmasters; i++)
  543. stp_master_free(stm, i);
  544. device_unregister(&stm->dev);
  545. stm_data->stm = NULL;
  546. }
  547. EXPORT_SYMBOL_GPL(stm_unregister_device);
  548. /*
  549. * stm::link_list access serialization uses a spinlock and a mutex; holding
  550. * either of them guarantees that the list is stable; modification requires
  551. * holding both of them.
  552. *
  553. * Lock ordering is as follows:
  554. * stm::link_mutex
  555. * stm::link_lock
  556. * src::link_lock
  557. */
  558. /**
  559. * stm_source_link_add() - connect an stm_source device to an stm device
  560. * @src: stm_source device
  561. * @stm: stm device
  562. *
  563. * This function establishes a link from stm_source to an stm device so that
  564. * the former can send out trace data to the latter.
  565. *
  566. * Return: 0 on success, -errno otherwise.
  567. */
  568. static int stm_source_link_add(struct stm_source_device *src,
  569. struct stm_device *stm)
  570. {
  571. char *id;
  572. int err;
  573. mutex_lock(&stm->link_mutex);
  574. spin_lock(&stm->link_lock);
  575. spin_lock(&src->link_lock);
  576. /* src->link is dereferenced under stm_source_srcu but not the list */
  577. rcu_assign_pointer(src->link, stm);
  578. list_add_tail(&src->link_entry, &stm->link_list);
  579. spin_unlock(&src->link_lock);
  580. spin_unlock(&stm->link_lock);
  581. mutex_unlock(&stm->link_mutex);
  582. id = kstrdup(src->data->name, GFP_KERNEL);
  583. if (id) {
  584. src->policy_node =
  585. stp_policy_node_lookup(stm, id);
  586. kfree(id);
  587. }
  588. err = stm_output_assign(stm, src->data->nr_chans,
  589. src->policy_node, &src->output);
  590. if (src->policy_node)
  591. stp_policy_node_put(src->policy_node);
  592. if (err)
  593. goto fail_detach;
  594. /* this is to notify the STM device that a new link has been made */
  595. if (stm->data->link)
  596. err = stm->data->link(stm->data, src->output.master,
  597. src->output.channel);
  598. if (err)
  599. goto fail_free_output;
  600. /* this is to let the source carry out all necessary preparations */
  601. if (src->data->link)
  602. src->data->link(src->data);
  603. return 0;
  604. fail_free_output:
  605. stm_output_free(stm, &src->output);
  606. stm_put_device(stm);
  607. fail_detach:
  608. mutex_lock(&stm->link_mutex);
  609. spin_lock(&stm->link_lock);
  610. spin_lock(&src->link_lock);
  611. rcu_assign_pointer(src->link, NULL);
  612. list_del_init(&src->link_entry);
  613. spin_unlock(&src->link_lock);
  614. spin_unlock(&stm->link_lock);
  615. mutex_unlock(&stm->link_mutex);
  616. return err;
  617. }
  618. /**
  619. * __stm_source_link_drop() - detach stm_source from an stm device
  620. * @src: stm_source device
  621. * @stm: stm device
  622. *
  623. * If @stm is @src::link, disconnect them from one another and put the
  624. * reference on the @stm device.
  625. *
  626. * Caller must hold stm::link_mutex.
  627. */
  628. static void __stm_source_link_drop(struct stm_source_device *src,
  629. struct stm_device *stm)
  630. {
  631. struct stm_device *link;
  632. lockdep_assert_held(&stm->link_mutex);
  633. if (src->data->unlink)
  634. src->data->unlink(src->data);
  635. /* for stm::link_list modification, we hold both mutex and spinlock */
  636. spin_lock(&stm->link_lock);
  637. spin_lock(&src->link_lock);
  638. link = srcu_dereference_check(src->link, &stm_source_srcu, 1);
  639. if (WARN_ON_ONCE(link != stm)) {
  640. spin_unlock(&src->link_lock);
  641. return;
  642. }
  643. stm_output_free(link, &src->output);
  644. list_del_init(&src->link_entry);
  645. /* matches stm_find_device() from stm_source_link_store() */
  646. stm_put_device(link);
  647. rcu_assign_pointer(src->link, NULL);
  648. spin_unlock(&src->link_lock);
  649. spin_unlock(&stm->link_lock);
  650. }
  651. /**
  652. * stm_source_link_drop() - detach stm_source from its stm device
  653. * @src: stm_source device
  654. *
  655. * Unlinking means disconnecting from source's STM device; after this
  656. * writes will be unsuccessful until it is linked to a new STM device.
  657. *
  658. * This will happen on "stm_source_link" sysfs attribute write to undo
  659. * the existing link (if any), or on linked STM device's de-registration.
  660. */
  661. static void stm_source_link_drop(struct stm_source_device *src)
  662. {
  663. struct stm_device *stm;
  664. int idx;
  665. idx = srcu_read_lock(&stm_source_srcu);
  666. stm = srcu_dereference(src->link, &stm_source_srcu);
  667. if (stm) {
  668. mutex_lock(&stm->link_mutex);
  669. __stm_source_link_drop(src, stm);
  670. mutex_unlock(&stm->link_mutex);
  671. }
  672. srcu_read_unlock(&stm_source_srcu, idx);
  673. }
  674. static ssize_t stm_source_link_show(struct device *dev,
  675. struct device_attribute *attr,
  676. char *buf)
  677. {
  678. struct stm_source_device *src = to_stm_source_device(dev);
  679. struct stm_device *stm;
  680. int idx, ret;
  681. idx = srcu_read_lock(&stm_source_srcu);
  682. stm = srcu_dereference(src->link, &stm_source_srcu);
  683. ret = sprintf(buf, "%s\n",
  684. stm ? dev_name(&stm->dev) : "<none>");
  685. srcu_read_unlock(&stm_source_srcu, idx);
  686. return ret;
  687. }
  688. static ssize_t stm_source_link_store(struct device *dev,
  689. struct device_attribute *attr,
  690. const char *buf, size_t count)
  691. {
  692. struct stm_source_device *src = to_stm_source_device(dev);
  693. struct stm_device *link;
  694. int err;
  695. stm_source_link_drop(src);
  696. link = stm_find_device(buf);
  697. if (!link)
  698. return -EINVAL;
  699. err = stm_source_link_add(src, link);
  700. if (err)
  701. stm_put_device(link);
  702. return err ? : count;
  703. }
  704. static DEVICE_ATTR_RW(stm_source_link);
  705. static struct attribute *stm_source_attrs[] = {
  706. &dev_attr_stm_source_link.attr,
  707. NULL,
  708. };
  709. ATTRIBUTE_GROUPS(stm_source);
  710. static struct class stm_source_class = {
  711. .name = "stm_source",
  712. .dev_groups = stm_source_groups,
  713. };
  714. static void stm_source_device_release(struct device *dev)
  715. {
  716. struct stm_source_device *src = to_stm_source_device(dev);
  717. kfree(src);
  718. }
  719. /**
  720. * stm_source_register_device() - register an stm_source device
  721. * @parent: parent device
  722. * @data: device description structure
  723. *
  724. * This will create a device of stm_source class that can write
  725. * data to an stm device once linked.
  726. *
  727. * Return: 0 on success, -errno otherwise.
  728. */
  729. int stm_source_register_device(struct device *parent,
  730. struct stm_source_data *data)
  731. {
  732. struct stm_source_device *src;
  733. int err;
  734. if (!stm_core_up)
  735. return -EPROBE_DEFER;
  736. src = kzalloc(sizeof(*src), GFP_KERNEL);
  737. if (!src)
  738. return -ENOMEM;
  739. device_initialize(&src->dev);
  740. src->dev.class = &stm_source_class;
  741. src->dev.parent = parent;
  742. src->dev.release = stm_source_device_release;
  743. err = kobject_set_name(&src->dev.kobj, "%s", data->name);
  744. if (err)
  745. goto err;
  746. err = device_add(&src->dev);
  747. if (err)
  748. goto err;
  749. spin_lock_init(&src->link_lock);
  750. INIT_LIST_HEAD(&src->link_entry);
  751. src->data = data;
  752. data->src = src;
  753. return 0;
  754. err:
  755. put_device(&src->dev);
  756. kfree(src);
  757. return err;
  758. }
  759. EXPORT_SYMBOL_GPL(stm_source_register_device);
  760. /**
  761. * stm_source_unregister_device() - unregister an stm_source device
  762. * @data: device description that was used to register the device
  763. *
  764. * This will remove a previously created stm_source device from the system.
  765. */
  766. void stm_source_unregister_device(struct stm_source_data *data)
  767. {
  768. struct stm_source_device *src = data->src;
  769. stm_source_link_drop(src);
  770. device_destroy(&stm_source_class, src->dev.devt);
  771. }
  772. EXPORT_SYMBOL_GPL(stm_source_unregister_device);
  773. int stm_source_write(struct stm_source_data *data, unsigned int chan,
  774. const char *buf, size_t count)
  775. {
  776. struct stm_source_device *src = data->src;
  777. struct stm_device *stm;
  778. int idx;
  779. if (!src->output.nr_chans)
  780. return -ENODEV;
  781. if (chan >= src->output.nr_chans)
  782. return -EINVAL;
  783. idx = srcu_read_lock(&stm_source_srcu);
  784. stm = srcu_dereference(src->link, &stm_source_srcu);
  785. if (stm)
  786. stm_write(stm->data, src->output.master,
  787. src->output.channel + chan,
  788. buf, count);
  789. else
  790. count = -ENODEV;
  791. srcu_read_unlock(&stm_source_srcu, idx);
  792. return count;
  793. }
  794. EXPORT_SYMBOL_GPL(stm_source_write);
  795. static int __init stm_core_init(void)
  796. {
  797. int err;
  798. err = class_register(&stm_class);
  799. if (err)
  800. return err;
  801. err = class_register(&stm_source_class);
  802. if (err)
  803. goto err_stm;
  804. err = stp_configfs_init();
  805. if (err)
  806. goto err_src;
  807. init_srcu_struct(&stm_source_srcu);
  808. stm_core_up++;
  809. return 0;
  810. err_src:
  811. class_unregister(&stm_source_class);
  812. err_stm:
  813. class_unregister(&stm_class);
  814. return err;
  815. }
  816. module_init(stm_core_init);
  817. static void __exit stm_core_exit(void)
  818. {
  819. cleanup_srcu_struct(&stm_source_srcu);
  820. class_unregister(&stm_source_class);
  821. class_unregister(&stm_class);
  822. stp_configfs_exit();
  823. }
  824. module_exit(stm_core_exit);
  825. MODULE_LICENSE("GPL v2");
  826. MODULE_DESCRIPTION("System Trace Module device class");
  827. MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");