exynos-ppmu.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * exynos_ppmu.c - EXYNOS PPMU (Platform Performance Monitoring Unit) support
  3. *
  4. * Copyright (c) 2014 Samsung Electronics Co., Ltd.
  5. * Author : Chanwoo Choi <cw00.choi@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This driver is based on drivers/devfreq/exynos/exynos_ppmu.c
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/io.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/mutex.h>
  18. #include <linux/of_address.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/suspend.h>
  21. #include <linux/devfreq-event.h>
  22. #include "exynos-ppmu.h"
  23. struct exynos_ppmu_data {
  24. void __iomem *base;
  25. struct clk *clk;
  26. };
  27. struct exynos_ppmu {
  28. struct devfreq_event_dev **edev;
  29. struct devfreq_event_desc *desc;
  30. unsigned int num_events;
  31. struct device *dev;
  32. struct mutex lock;
  33. struct exynos_ppmu_data ppmu;
  34. };
  35. #define PPMU_EVENT(name) \
  36. { "ppmu-event0-"#name, PPMU_PMNCNT0 }, \
  37. { "ppmu-event1-"#name, PPMU_PMNCNT1 }, \
  38. { "ppmu-event2-"#name, PPMU_PMNCNT2 }, \
  39. { "ppmu-event3-"#name, PPMU_PMNCNT3 }
  40. struct __exynos_ppmu_events {
  41. char *name;
  42. int id;
  43. } ppmu_events[] = {
  44. /* For Exynos3250, Exynos4 and Exynos5260 */
  45. PPMU_EVENT(g3d),
  46. PPMU_EVENT(fsys),
  47. /* For Exynos4 SoCs and Exynos3250 */
  48. PPMU_EVENT(dmc0),
  49. PPMU_EVENT(dmc1),
  50. PPMU_EVENT(cpu),
  51. PPMU_EVENT(rightbus),
  52. PPMU_EVENT(leftbus),
  53. PPMU_EVENT(lcd0),
  54. PPMU_EVENT(camif),
  55. /* Only for Exynos3250 and Exynos5260 */
  56. PPMU_EVENT(mfc),
  57. /* Only for Exynos4 SoCs */
  58. PPMU_EVENT(mfc-left),
  59. PPMU_EVENT(mfc-right),
  60. /* Only for Exynos5260 SoCs */
  61. PPMU_EVENT(drex0-s0),
  62. PPMU_EVENT(drex0-s1),
  63. PPMU_EVENT(drex1-s0),
  64. PPMU_EVENT(drex1-s1),
  65. PPMU_EVENT(eagle),
  66. PPMU_EVENT(kfc),
  67. PPMU_EVENT(isp),
  68. PPMU_EVENT(fimc),
  69. PPMU_EVENT(gscl),
  70. PPMU_EVENT(mscl),
  71. PPMU_EVENT(fimd0x),
  72. PPMU_EVENT(fimd1x),
  73. { /* sentinel */ },
  74. };
  75. static int exynos_ppmu_find_ppmu_id(struct devfreq_event_dev *edev)
  76. {
  77. int i;
  78. for (i = 0; i < ARRAY_SIZE(ppmu_events); i++)
  79. if (!strcmp(edev->desc->name, ppmu_events[i].name))
  80. return ppmu_events[i].id;
  81. return -EINVAL;
  82. }
  83. static int exynos_ppmu_disable(struct devfreq_event_dev *edev)
  84. {
  85. struct exynos_ppmu *info = devfreq_event_get_drvdata(edev);
  86. u32 pmnc;
  87. /* Disable all counters */
  88. __raw_writel(PPMU_CCNT_MASK |
  89. PPMU_PMCNT0_MASK |
  90. PPMU_PMCNT1_MASK |
  91. PPMU_PMCNT2_MASK |
  92. PPMU_PMCNT3_MASK,
  93. info->ppmu.base + PPMU_CNTENC);
  94. /* Disable PPMU */
  95. pmnc = __raw_readl(info->ppmu.base + PPMU_PMNC);
  96. pmnc &= ~PPMU_PMNC_ENABLE_MASK;
  97. __raw_writel(pmnc, info->ppmu.base + PPMU_PMNC);
  98. return 0;
  99. }
  100. static int exynos_ppmu_set_event(struct devfreq_event_dev *edev)
  101. {
  102. struct exynos_ppmu *info = devfreq_event_get_drvdata(edev);
  103. int id = exynos_ppmu_find_ppmu_id(edev);
  104. u32 pmnc, cntens;
  105. if (id < 0)
  106. return id;
  107. /* Enable specific counter */
  108. cntens = __raw_readl(info->ppmu.base + PPMU_CNTENS);
  109. cntens |= (PPMU_CCNT_MASK | (PPMU_ENABLE << id));
  110. __raw_writel(cntens, info->ppmu.base + PPMU_CNTENS);
  111. /* Set the event of Read/Write data count */
  112. __raw_writel(PPMU_RO_DATA_CNT | PPMU_WO_DATA_CNT,
  113. info->ppmu.base + PPMU_BEVTxSEL(id));
  114. /* Reset cycle counter/performance counter and enable PPMU */
  115. pmnc = __raw_readl(info->ppmu.base + PPMU_PMNC);
  116. pmnc &= ~(PPMU_PMNC_ENABLE_MASK
  117. | PPMU_PMNC_COUNTER_RESET_MASK
  118. | PPMU_PMNC_CC_RESET_MASK);
  119. pmnc |= (PPMU_ENABLE << PPMU_PMNC_ENABLE_SHIFT);
  120. pmnc |= (PPMU_ENABLE << PPMU_PMNC_COUNTER_RESET_SHIFT);
  121. pmnc |= (PPMU_ENABLE << PPMU_PMNC_CC_RESET_SHIFT);
  122. __raw_writel(pmnc, info->ppmu.base + PPMU_PMNC);
  123. return 0;
  124. }
  125. static int exynos_ppmu_get_event(struct devfreq_event_dev *edev,
  126. struct devfreq_event_data *edata)
  127. {
  128. struct exynos_ppmu *info = devfreq_event_get_drvdata(edev);
  129. int id = exynos_ppmu_find_ppmu_id(edev);
  130. u32 pmnc, cntenc;
  131. if (id < 0)
  132. return -EINVAL;
  133. /* Disable PPMU */
  134. pmnc = __raw_readl(info->ppmu.base + PPMU_PMNC);
  135. pmnc &= ~PPMU_PMNC_ENABLE_MASK;
  136. __raw_writel(pmnc, info->ppmu.base + PPMU_PMNC);
  137. /* Read cycle count */
  138. edata->total_count = __raw_readl(info->ppmu.base + PPMU_CCNT);
  139. /* Read performance count */
  140. switch (id) {
  141. case PPMU_PMNCNT0:
  142. case PPMU_PMNCNT1:
  143. case PPMU_PMNCNT2:
  144. edata->load_count
  145. = __raw_readl(info->ppmu.base + PPMU_PMNCT(id));
  146. break;
  147. case PPMU_PMNCNT3:
  148. edata->load_count =
  149. ((__raw_readl(info->ppmu.base + PPMU_PMCNT3_HIGH) << 8)
  150. | __raw_readl(info->ppmu.base + PPMU_PMCNT3_LOW));
  151. break;
  152. default:
  153. return -EINVAL;
  154. }
  155. /* Disable specific counter */
  156. cntenc = __raw_readl(info->ppmu.base + PPMU_CNTENC);
  157. cntenc |= (PPMU_CCNT_MASK | (PPMU_ENABLE << id));
  158. __raw_writel(cntenc, info->ppmu.base + PPMU_CNTENC);
  159. dev_dbg(&edev->dev, "%s (event: %ld/%ld)\n", edev->desc->name,
  160. edata->load_count, edata->total_count);
  161. return 0;
  162. }
  163. static const struct devfreq_event_ops exynos_ppmu_ops = {
  164. .disable = exynos_ppmu_disable,
  165. .set_event = exynos_ppmu_set_event,
  166. .get_event = exynos_ppmu_get_event,
  167. };
  168. static int of_get_devfreq_events(struct device_node *np,
  169. struct exynos_ppmu *info)
  170. {
  171. struct devfreq_event_desc *desc;
  172. struct device *dev = info->dev;
  173. struct device_node *events_np, *node;
  174. int i, j, count;
  175. events_np = of_get_child_by_name(np, "events");
  176. if (!events_np) {
  177. dev_err(dev,
  178. "failed to get child node of devfreq-event devices\n");
  179. return -EINVAL;
  180. }
  181. count = of_get_child_count(events_np);
  182. desc = devm_kzalloc(dev, sizeof(*desc) * count, GFP_KERNEL);
  183. if (!desc)
  184. return -ENOMEM;
  185. info->num_events = count;
  186. j = 0;
  187. for_each_child_of_node(events_np, node) {
  188. for (i = 0; i < ARRAY_SIZE(ppmu_events); i++) {
  189. if (!ppmu_events[i].name)
  190. continue;
  191. if (!of_node_cmp(node->name, ppmu_events[i].name))
  192. break;
  193. }
  194. if (i == ARRAY_SIZE(ppmu_events)) {
  195. dev_warn(dev,
  196. "don't know how to configure events : %s\n",
  197. node->name);
  198. continue;
  199. }
  200. desc[j].ops = &exynos_ppmu_ops;
  201. desc[j].driver_data = info;
  202. of_property_read_string(node, "event-name", &desc[j].name);
  203. j++;
  204. of_node_put(node);
  205. }
  206. info->desc = desc;
  207. of_node_put(events_np);
  208. return 0;
  209. }
  210. static int exynos_ppmu_parse_dt(struct exynos_ppmu *info)
  211. {
  212. struct device *dev = info->dev;
  213. struct device_node *np = dev->of_node;
  214. int ret = 0;
  215. if (!np) {
  216. dev_err(dev, "failed to find devicetree node\n");
  217. return -EINVAL;
  218. }
  219. /* Maps the memory mapped IO to control PPMU register */
  220. info->ppmu.base = of_iomap(np, 0);
  221. if (IS_ERR_OR_NULL(info->ppmu.base)) {
  222. dev_err(dev, "failed to map memory region\n");
  223. return -ENOMEM;
  224. }
  225. info->ppmu.clk = devm_clk_get(dev, "ppmu");
  226. if (IS_ERR(info->ppmu.clk)) {
  227. info->ppmu.clk = NULL;
  228. dev_warn(dev, "cannot get PPMU clock\n");
  229. }
  230. ret = of_get_devfreq_events(np, info);
  231. if (ret < 0) {
  232. dev_err(dev, "failed to parse exynos ppmu dt node\n");
  233. goto err;
  234. }
  235. return 0;
  236. err:
  237. iounmap(info->ppmu.base);
  238. return ret;
  239. }
  240. static int exynos_ppmu_probe(struct platform_device *pdev)
  241. {
  242. struct exynos_ppmu *info;
  243. struct devfreq_event_dev **edev;
  244. struct devfreq_event_desc *desc;
  245. int i, ret = 0, size;
  246. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  247. if (!info)
  248. return -ENOMEM;
  249. mutex_init(&info->lock);
  250. info->dev = &pdev->dev;
  251. /* Parse dt data to get resource */
  252. ret = exynos_ppmu_parse_dt(info);
  253. if (ret < 0) {
  254. dev_err(&pdev->dev,
  255. "failed to parse devicetree for resource\n");
  256. return ret;
  257. }
  258. desc = info->desc;
  259. size = sizeof(struct devfreq_event_dev *) * info->num_events;
  260. info->edev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
  261. if (!info->edev) {
  262. dev_err(&pdev->dev,
  263. "failed to allocate memory devfreq-event devices\n");
  264. return -ENOMEM;
  265. }
  266. edev = info->edev;
  267. platform_set_drvdata(pdev, info);
  268. for (i = 0; i < info->num_events; i++) {
  269. edev[i] = devm_devfreq_event_add_edev(&pdev->dev, &desc[i]);
  270. if (IS_ERR(edev[i])) {
  271. ret = PTR_ERR(edev[i]);
  272. dev_err(&pdev->dev,
  273. "failed to add devfreq-event device\n");
  274. goto err;
  275. }
  276. }
  277. clk_prepare_enable(info->ppmu.clk);
  278. return 0;
  279. err:
  280. iounmap(info->ppmu.base);
  281. return ret;
  282. }
  283. static int exynos_ppmu_remove(struct platform_device *pdev)
  284. {
  285. struct exynos_ppmu *info = platform_get_drvdata(pdev);
  286. clk_disable_unprepare(info->ppmu.clk);
  287. iounmap(info->ppmu.base);
  288. return 0;
  289. }
  290. static struct of_device_id exynos_ppmu_id_match[] = {
  291. { .compatible = "samsung,exynos-ppmu", },
  292. { /* sentinel */ },
  293. };
  294. static struct platform_driver exynos_ppmu_driver = {
  295. .probe = exynos_ppmu_probe,
  296. .remove = exynos_ppmu_remove,
  297. .driver = {
  298. .name = "exynos-ppmu",
  299. .of_match_table = exynos_ppmu_id_match,
  300. },
  301. };
  302. module_platform_driver(exynos_ppmu_driver);
  303. MODULE_DESCRIPTION("Exynos PPMU(Platform Performance Monitoring Unit) driver");
  304. MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
  305. MODULE_LICENSE("GPL");