core.c 27 KB

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