core.c 27 KB

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