core.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  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 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 stm_source_write(struct stm_source_data *data, unsigned int chan,
  894. const char *buf, size_t count)
  895. {
  896. struct stm_source_device *src = data->src;
  897. struct stm_device *stm;
  898. int idx;
  899. if (!src->output.nr_chans)
  900. return -ENODEV;
  901. if (chan >= src->output.nr_chans)
  902. return -EINVAL;
  903. idx = srcu_read_lock(&stm_source_srcu);
  904. stm = srcu_dereference(src->link, &stm_source_srcu);
  905. if (stm)
  906. count = stm_write(stm->data, src->output.master,
  907. src->output.channel + chan,
  908. buf, count);
  909. else
  910. count = -ENODEV;
  911. srcu_read_unlock(&stm_source_srcu, idx);
  912. return count;
  913. }
  914. EXPORT_SYMBOL_GPL(stm_source_write);
  915. static int __init stm_core_init(void)
  916. {
  917. int err;
  918. err = class_register(&stm_class);
  919. if (err)
  920. return err;
  921. err = class_register(&stm_source_class);
  922. if (err)
  923. goto err_stm;
  924. err = stp_configfs_init();
  925. if (err)
  926. goto err_src;
  927. init_srcu_struct(&stm_source_srcu);
  928. stm_core_up++;
  929. return 0;
  930. err_src:
  931. class_unregister(&stm_source_class);
  932. err_stm:
  933. class_unregister(&stm_class);
  934. return err;
  935. }
  936. module_init(stm_core_init);
  937. static void __exit stm_core_exit(void)
  938. {
  939. cleanup_srcu_struct(&stm_source_srcu);
  940. class_unregister(&stm_source_class);
  941. class_unregister(&stm_class);
  942. stp_configfs_exit();
  943. }
  944. module_exit(stm_core_exit);
  945. MODULE_LICENSE("GPL v2");
  946. MODULE_DESCRIPTION("System Trace Module device class");
  947. MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");