core.c 24 KB

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