core.c 25 KB

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