core.c 27 KB

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