core.c 25 KB

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