tegra-devfreq.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /*
  2. * A devfreq driver for NVIDIA Tegra SoCs
  3. *
  4. * Copyright (c) 2014 NVIDIA CORPORATION. All rights reserved.
  5. * Copyright (C) 2014 Google, Inc
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include <linux/clk.h>
  21. #include <linux/cpufreq.h>
  22. #include <linux/devfreq.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/io.h>
  25. #include <linux/module.h>
  26. #include <linux/mod_devicetable.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/pm_opp.h>
  29. #include <linux/reset.h>
  30. #include "governor.h"
  31. #define ACTMON_GLB_STATUS 0x0
  32. #define ACTMON_GLB_PERIOD_CTRL 0x4
  33. #define ACTMON_DEV_CTRL 0x0
  34. #define ACTMON_DEV_CTRL_K_VAL_SHIFT 10
  35. #define ACTMON_DEV_CTRL_ENB_PERIODIC BIT(18)
  36. #define ACTMON_DEV_CTRL_AVG_BELOW_WMARK_EN BIT(20)
  37. #define ACTMON_DEV_CTRL_AVG_ABOVE_WMARK_EN BIT(21)
  38. #define ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_NUM_SHIFT 23
  39. #define ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_NUM_SHIFT 26
  40. #define ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN BIT(29)
  41. #define ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN BIT(30)
  42. #define ACTMON_DEV_CTRL_ENB BIT(31)
  43. #define ACTMON_DEV_UPPER_WMARK 0x4
  44. #define ACTMON_DEV_LOWER_WMARK 0x8
  45. #define ACTMON_DEV_INIT_AVG 0xc
  46. #define ACTMON_DEV_AVG_UPPER_WMARK 0x10
  47. #define ACTMON_DEV_AVG_LOWER_WMARK 0x14
  48. #define ACTMON_DEV_COUNT_WEIGHT 0x18
  49. #define ACTMON_DEV_AVG_COUNT 0x20
  50. #define ACTMON_DEV_INTR_STATUS 0x24
  51. #define ACTMON_INTR_STATUS_CLEAR 0xffffffff
  52. #define ACTMON_DEV_INTR_CONSECUTIVE_UPPER BIT(31)
  53. #define ACTMON_DEV_INTR_CONSECUTIVE_LOWER BIT(30)
  54. #define ACTMON_ABOVE_WMARK_WINDOW 1
  55. #define ACTMON_BELOW_WMARK_WINDOW 3
  56. #define ACTMON_BOOST_FREQ_STEP 16000
  57. /*
  58. * Activity counter is incremented every 256 memory transactions, and each
  59. * transaction takes 4 EMC clocks for Tegra124; So the COUNT_WEIGHT is
  60. * 4 * 256 = 1024.
  61. */
  62. #define ACTMON_COUNT_WEIGHT 0x400
  63. /*
  64. * ACTMON_AVERAGE_WINDOW_LOG2: default value for @DEV_CTRL_K_VAL, which
  65. * translates to 2 ^ (K_VAL + 1). ex: 2 ^ (6 + 1) = 128
  66. */
  67. #define ACTMON_AVERAGE_WINDOW_LOG2 6
  68. #define ACTMON_SAMPLING_PERIOD 12 /* ms */
  69. #define ACTMON_DEFAULT_AVG_BAND 6 /* 1/10 of % */
  70. #define KHZ 1000
  71. #define KHZ_MAX (ULONG_MAX / KHZ)
  72. /* Assume that the bus is saturated if the utilization is 25% */
  73. #define BUS_SATURATION_RATIO 25
  74. /**
  75. * struct tegra_devfreq_device_config - configuration specific to an ACTMON
  76. * device
  77. *
  78. * Coefficients and thresholds are percentages unless otherwise noted
  79. */
  80. struct tegra_devfreq_device_config {
  81. u32 offset;
  82. u32 irq_mask;
  83. /* Factors applied to boost_freq every consecutive watermark breach */
  84. unsigned int boost_up_coeff;
  85. unsigned int boost_down_coeff;
  86. /* Define the watermark bounds when applied to the current avg */
  87. unsigned int boost_up_threshold;
  88. unsigned int boost_down_threshold;
  89. /*
  90. * Threshold of activity (cycles) below which the CPU frequency isn't
  91. * to be taken into account. This is to avoid increasing the EMC
  92. * frequency when the CPU is very busy but not accessing the bus often.
  93. */
  94. u32 avg_dependency_threshold;
  95. };
  96. enum tegra_actmon_device {
  97. MCALL = 0,
  98. MCCPU,
  99. };
  100. static struct tegra_devfreq_device_config actmon_device_configs[] = {
  101. {
  102. /* MCALL: All memory accesses (including from the CPUs) */
  103. .offset = 0x1c0,
  104. .irq_mask = 1 << 26,
  105. .boost_up_coeff = 200,
  106. .boost_down_coeff = 50,
  107. .boost_up_threshold = 60,
  108. .boost_down_threshold = 40,
  109. },
  110. {
  111. /* MCCPU: memory accesses from the CPUs */
  112. .offset = 0x200,
  113. .irq_mask = 1 << 25,
  114. .boost_up_coeff = 800,
  115. .boost_down_coeff = 90,
  116. .boost_up_threshold = 27,
  117. .boost_down_threshold = 10,
  118. .avg_dependency_threshold = 50000,
  119. },
  120. };
  121. /**
  122. * struct tegra_devfreq_device - state specific to an ACTMON device
  123. *
  124. * Frequencies are in kHz.
  125. */
  126. struct tegra_devfreq_device {
  127. const struct tegra_devfreq_device_config *config;
  128. void __iomem *regs;
  129. spinlock_t lock;
  130. /* Average event count sampled in the last interrupt */
  131. u32 avg_count;
  132. /*
  133. * Extra frequency to increase the target by due to consecutive
  134. * watermark breaches.
  135. */
  136. unsigned long boost_freq;
  137. /* Optimal frequency calculated from the stats for this device */
  138. unsigned long target_freq;
  139. };
  140. struct tegra_devfreq {
  141. struct devfreq *devfreq;
  142. struct reset_control *reset;
  143. struct clk *clock;
  144. void __iomem *regs;
  145. struct clk *emc_clock;
  146. unsigned long max_freq;
  147. unsigned long cur_freq;
  148. struct notifier_block rate_change_nb;
  149. struct tegra_devfreq_device devices[ARRAY_SIZE(actmon_device_configs)];
  150. };
  151. struct tegra_actmon_emc_ratio {
  152. unsigned long cpu_freq;
  153. unsigned long emc_freq;
  154. };
  155. static struct tegra_actmon_emc_ratio actmon_emc_ratios[] = {
  156. { 1400000, KHZ_MAX },
  157. { 1200000, 750000 },
  158. { 1100000, 600000 },
  159. { 1000000, 500000 },
  160. { 800000, 375000 },
  161. { 500000, 200000 },
  162. { 250000, 100000 },
  163. };
  164. static u32 actmon_readl(struct tegra_devfreq *tegra, u32 offset)
  165. {
  166. return readl(tegra->regs + offset);
  167. }
  168. static void actmon_writel(struct tegra_devfreq *tegra, u32 val, u32 offset)
  169. {
  170. writel(val, tegra->regs + offset);
  171. }
  172. static u32 device_readl(struct tegra_devfreq_device *dev, u32 offset)
  173. {
  174. return readl(dev->regs + offset);
  175. }
  176. static void device_writel(struct tegra_devfreq_device *dev, u32 val,
  177. u32 offset)
  178. {
  179. writel(val, dev->regs + offset);
  180. }
  181. static unsigned long do_percent(unsigned long val, unsigned int pct)
  182. {
  183. return val * pct / 100;
  184. }
  185. static void tegra_devfreq_update_avg_wmark(struct tegra_devfreq *tegra,
  186. struct tegra_devfreq_device *dev)
  187. {
  188. u32 avg = dev->avg_count;
  189. u32 avg_band_freq = tegra->max_freq * ACTMON_DEFAULT_AVG_BAND / KHZ;
  190. u32 band = avg_band_freq * ACTMON_SAMPLING_PERIOD;
  191. device_writel(dev, avg + band, ACTMON_DEV_AVG_UPPER_WMARK);
  192. avg = max(dev->avg_count, band);
  193. device_writel(dev, avg - band, ACTMON_DEV_AVG_LOWER_WMARK);
  194. }
  195. static void tegra_devfreq_update_wmark(struct tegra_devfreq *tegra,
  196. struct tegra_devfreq_device *dev)
  197. {
  198. u32 val = tegra->cur_freq * ACTMON_SAMPLING_PERIOD;
  199. device_writel(dev, do_percent(val, dev->config->boost_up_threshold),
  200. ACTMON_DEV_UPPER_WMARK);
  201. device_writel(dev, do_percent(val, dev->config->boost_down_threshold),
  202. ACTMON_DEV_LOWER_WMARK);
  203. }
  204. static void actmon_write_barrier(struct tegra_devfreq *tegra)
  205. {
  206. /* ensure the update has reached the ACTMON */
  207. wmb();
  208. actmon_readl(tegra, ACTMON_GLB_STATUS);
  209. }
  210. static void actmon_isr_device(struct tegra_devfreq *tegra,
  211. struct tegra_devfreq_device *dev)
  212. {
  213. unsigned long flags;
  214. u32 intr_status, dev_ctrl;
  215. spin_lock_irqsave(&dev->lock, flags);
  216. dev->avg_count = device_readl(dev, ACTMON_DEV_AVG_COUNT);
  217. tegra_devfreq_update_avg_wmark(tegra, dev);
  218. intr_status = device_readl(dev, ACTMON_DEV_INTR_STATUS);
  219. dev_ctrl = device_readl(dev, ACTMON_DEV_CTRL);
  220. if (intr_status & ACTMON_DEV_INTR_CONSECUTIVE_UPPER) {
  221. /*
  222. * new_boost = min(old_boost * up_coef + step, max_freq)
  223. */
  224. dev->boost_freq = do_percent(dev->boost_freq,
  225. dev->config->boost_up_coeff);
  226. dev->boost_freq += ACTMON_BOOST_FREQ_STEP;
  227. dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
  228. if (dev->boost_freq >= tegra->max_freq)
  229. dev->boost_freq = tegra->max_freq;
  230. else
  231. dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
  232. } else if (intr_status & ACTMON_DEV_INTR_CONSECUTIVE_LOWER) {
  233. /*
  234. * new_boost = old_boost * down_coef
  235. * or 0 if (old_boost * down_coef < step / 2)
  236. */
  237. dev->boost_freq = do_percent(dev->boost_freq,
  238. dev->config->boost_down_coeff);
  239. dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
  240. if (dev->boost_freq < (ACTMON_BOOST_FREQ_STEP >> 1))
  241. dev->boost_freq = 0;
  242. else
  243. dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
  244. }
  245. if (dev->config->avg_dependency_threshold) {
  246. if (dev->avg_count >= dev->config->avg_dependency_threshold)
  247. dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
  248. else if (dev->boost_freq == 0)
  249. dev_ctrl &= ~ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
  250. }
  251. device_writel(dev, dev_ctrl, ACTMON_DEV_CTRL);
  252. device_writel(dev, ACTMON_INTR_STATUS_CLEAR, ACTMON_DEV_INTR_STATUS);
  253. actmon_write_barrier(tegra);
  254. spin_unlock_irqrestore(&dev->lock, flags);
  255. }
  256. static irqreturn_t actmon_isr(int irq, void *data)
  257. {
  258. struct tegra_devfreq *tegra = data;
  259. bool handled = false;
  260. unsigned int i;
  261. u32 val;
  262. val = actmon_readl(tegra, ACTMON_GLB_STATUS);
  263. for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
  264. if (val & tegra->devices[i].config->irq_mask) {
  265. actmon_isr_device(tegra, tegra->devices + i);
  266. handled = true;
  267. }
  268. }
  269. return handled ? IRQ_WAKE_THREAD : IRQ_NONE;
  270. }
  271. static unsigned long actmon_cpu_to_emc_rate(struct tegra_devfreq *tegra,
  272. unsigned long cpu_freq)
  273. {
  274. unsigned int i;
  275. struct tegra_actmon_emc_ratio *ratio = actmon_emc_ratios;
  276. for (i = 0; i < ARRAY_SIZE(actmon_emc_ratios); i++, ratio++) {
  277. if (cpu_freq >= ratio->cpu_freq) {
  278. if (ratio->emc_freq >= tegra->max_freq)
  279. return tegra->max_freq;
  280. else
  281. return ratio->emc_freq;
  282. }
  283. }
  284. return 0;
  285. }
  286. static void actmon_update_target(struct tegra_devfreq *tegra,
  287. struct tegra_devfreq_device *dev)
  288. {
  289. unsigned long cpu_freq = 0;
  290. unsigned long static_cpu_emc_freq = 0;
  291. unsigned int avg_sustain_coef;
  292. unsigned long flags;
  293. if (dev->config->avg_dependency_threshold) {
  294. cpu_freq = cpufreq_get(0);
  295. static_cpu_emc_freq = actmon_cpu_to_emc_rate(tegra, cpu_freq);
  296. }
  297. spin_lock_irqsave(&dev->lock, flags);
  298. dev->target_freq = dev->avg_count / ACTMON_SAMPLING_PERIOD;
  299. avg_sustain_coef = 100 * 100 / dev->config->boost_up_threshold;
  300. dev->target_freq = do_percent(dev->target_freq, avg_sustain_coef);
  301. dev->target_freq += dev->boost_freq;
  302. if (dev->avg_count >= dev->config->avg_dependency_threshold)
  303. dev->target_freq = max(dev->target_freq, static_cpu_emc_freq);
  304. spin_unlock_irqrestore(&dev->lock, flags);
  305. }
  306. static irqreturn_t actmon_thread_isr(int irq, void *data)
  307. {
  308. struct tegra_devfreq *tegra = data;
  309. mutex_lock(&tegra->devfreq->lock);
  310. update_devfreq(tegra->devfreq);
  311. mutex_unlock(&tegra->devfreq->lock);
  312. return IRQ_HANDLED;
  313. }
  314. static int tegra_actmon_rate_notify_cb(struct notifier_block *nb,
  315. unsigned long action, void *ptr)
  316. {
  317. struct clk_notifier_data *data = ptr;
  318. struct tegra_devfreq *tegra;
  319. struct tegra_devfreq_device *dev;
  320. unsigned int i;
  321. unsigned long flags;
  322. if (action != POST_RATE_CHANGE)
  323. return NOTIFY_OK;
  324. tegra = container_of(nb, struct tegra_devfreq, rate_change_nb);
  325. tegra->cur_freq = data->new_rate / KHZ;
  326. for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
  327. dev = &tegra->devices[i];
  328. spin_lock_irqsave(&dev->lock, flags);
  329. tegra_devfreq_update_wmark(tegra, dev);
  330. spin_unlock_irqrestore(&dev->lock, flags);
  331. }
  332. actmon_write_barrier(tegra);
  333. return NOTIFY_OK;
  334. }
  335. static void tegra_actmon_enable_interrupts(struct tegra_devfreq *tegra)
  336. {
  337. struct tegra_devfreq_device *dev;
  338. u32 val;
  339. unsigned int i;
  340. for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
  341. dev = &tegra->devices[i];
  342. val = device_readl(dev, ACTMON_DEV_CTRL);
  343. val |= ACTMON_DEV_CTRL_AVG_ABOVE_WMARK_EN;
  344. val |= ACTMON_DEV_CTRL_AVG_BELOW_WMARK_EN;
  345. val |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
  346. val |= ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
  347. device_writel(dev, val, ACTMON_DEV_CTRL);
  348. }
  349. actmon_write_barrier(tegra);
  350. }
  351. static void tegra_actmon_disable_interrupts(struct tegra_devfreq *tegra)
  352. {
  353. struct tegra_devfreq_device *dev;
  354. u32 val;
  355. unsigned int i;
  356. for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
  357. dev = &tegra->devices[i];
  358. val = device_readl(dev, ACTMON_DEV_CTRL);
  359. val &= ~ACTMON_DEV_CTRL_AVG_ABOVE_WMARK_EN;
  360. val &= ~ACTMON_DEV_CTRL_AVG_BELOW_WMARK_EN;
  361. val &= ~ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
  362. val &= ~ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
  363. device_writel(dev, val, ACTMON_DEV_CTRL);
  364. }
  365. actmon_write_barrier(tegra);
  366. }
  367. static void tegra_actmon_configure_device(struct tegra_devfreq *tegra,
  368. struct tegra_devfreq_device *dev)
  369. {
  370. u32 val = 0;
  371. dev->target_freq = tegra->cur_freq;
  372. dev->avg_count = tegra->cur_freq * ACTMON_SAMPLING_PERIOD;
  373. device_writel(dev, dev->avg_count, ACTMON_DEV_INIT_AVG);
  374. tegra_devfreq_update_avg_wmark(tegra, dev);
  375. tegra_devfreq_update_wmark(tegra, dev);
  376. device_writel(dev, ACTMON_COUNT_WEIGHT, ACTMON_DEV_COUNT_WEIGHT);
  377. device_writel(dev, ACTMON_INTR_STATUS_CLEAR, ACTMON_DEV_INTR_STATUS);
  378. val |= ACTMON_DEV_CTRL_ENB_PERIODIC;
  379. val |= (ACTMON_AVERAGE_WINDOW_LOG2 - 1)
  380. << ACTMON_DEV_CTRL_K_VAL_SHIFT;
  381. val |= (ACTMON_BELOW_WMARK_WINDOW - 1)
  382. << ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_NUM_SHIFT;
  383. val |= (ACTMON_ABOVE_WMARK_WINDOW - 1)
  384. << ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_NUM_SHIFT;
  385. val |= ACTMON_DEV_CTRL_ENB;
  386. device_writel(dev, val, ACTMON_DEV_CTRL);
  387. actmon_write_barrier(tegra);
  388. }
  389. static int tegra_devfreq_target(struct device *dev, unsigned long *freq,
  390. u32 flags)
  391. {
  392. struct tegra_devfreq *tegra = dev_get_drvdata(dev);
  393. struct dev_pm_opp *opp;
  394. unsigned long rate;
  395. opp = devfreq_recommended_opp(dev, freq, flags);
  396. if (IS_ERR(opp)) {
  397. dev_err(dev, "Failed to find opp for %lu Hz\n", *freq);
  398. return PTR_ERR(opp);
  399. }
  400. rate = dev_pm_opp_get_freq(opp);
  401. dev_pm_opp_put(opp);
  402. clk_set_min_rate(tegra->emc_clock, rate);
  403. clk_set_rate(tegra->emc_clock, 0);
  404. return 0;
  405. }
  406. static int tegra_devfreq_get_dev_status(struct device *dev,
  407. struct devfreq_dev_status *stat)
  408. {
  409. struct tegra_devfreq *tegra = dev_get_drvdata(dev);
  410. struct tegra_devfreq_device *actmon_dev;
  411. stat->current_frequency = tegra->cur_freq * KHZ;
  412. /* To be used by the tegra governor */
  413. stat->private_data = tegra;
  414. /* The below are to be used by the other governors */
  415. actmon_dev = &tegra->devices[MCALL];
  416. /* Number of cycles spent on memory access */
  417. stat->busy_time = device_readl(actmon_dev, ACTMON_DEV_AVG_COUNT);
  418. /* The bus can be considered to be saturated way before 100% */
  419. stat->busy_time *= 100 / BUS_SATURATION_RATIO;
  420. /* Number of cycles in a sampling period */
  421. stat->total_time = ACTMON_SAMPLING_PERIOD * tegra->cur_freq;
  422. stat->busy_time = min(stat->busy_time, stat->total_time);
  423. return 0;
  424. }
  425. static struct devfreq_dev_profile tegra_devfreq_profile = {
  426. .polling_ms = 0,
  427. .target = tegra_devfreq_target,
  428. .get_dev_status = tegra_devfreq_get_dev_status,
  429. };
  430. static int tegra_governor_get_target(struct devfreq *devfreq,
  431. unsigned long *freq)
  432. {
  433. struct devfreq_dev_status *stat;
  434. struct tegra_devfreq *tegra;
  435. struct tegra_devfreq_device *dev;
  436. unsigned long target_freq = 0;
  437. unsigned int i;
  438. int err;
  439. err = devfreq_update_stats(devfreq);
  440. if (err)
  441. return err;
  442. stat = &devfreq->last_status;
  443. tegra = stat->private_data;
  444. for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
  445. dev = &tegra->devices[i];
  446. actmon_update_target(tegra, dev);
  447. target_freq = max(target_freq, dev->target_freq);
  448. }
  449. *freq = target_freq * KHZ;
  450. return 0;
  451. }
  452. static int tegra_governor_event_handler(struct devfreq *devfreq,
  453. unsigned int event, void *data)
  454. {
  455. struct tegra_devfreq *tegra;
  456. int ret = 0;
  457. tegra = dev_get_drvdata(devfreq->dev.parent);
  458. switch (event) {
  459. case DEVFREQ_GOV_START:
  460. devfreq_monitor_start(devfreq);
  461. tegra_actmon_enable_interrupts(tegra);
  462. break;
  463. case DEVFREQ_GOV_STOP:
  464. tegra_actmon_disable_interrupts(tegra);
  465. devfreq_monitor_stop(devfreq);
  466. break;
  467. case DEVFREQ_GOV_SUSPEND:
  468. tegra_actmon_disable_interrupts(tegra);
  469. devfreq_monitor_suspend(devfreq);
  470. break;
  471. case DEVFREQ_GOV_RESUME:
  472. devfreq_monitor_resume(devfreq);
  473. tegra_actmon_enable_interrupts(tegra);
  474. break;
  475. }
  476. return ret;
  477. }
  478. static struct devfreq_governor tegra_devfreq_governor = {
  479. .name = "tegra_actmon",
  480. .get_target_freq = tegra_governor_get_target,
  481. .event_handler = tegra_governor_event_handler,
  482. };
  483. static int tegra_devfreq_probe(struct platform_device *pdev)
  484. {
  485. struct tegra_devfreq *tegra;
  486. struct tegra_devfreq_device *dev;
  487. struct resource *res;
  488. unsigned int i;
  489. unsigned long rate;
  490. int irq;
  491. int err;
  492. tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
  493. if (!tegra)
  494. return -ENOMEM;
  495. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  496. tegra->regs = devm_ioremap_resource(&pdev->dev, res);
  497. if (IS_ERR(tegra->regs))
  498. return PTR_ERR(tegra->regs);
  499. tegra->reset = devm_reset_control_get(&pdev->dev, "actmon");
  500. if (IS_ERR(tegra->reset)) {
  501. dev_err(&pdev->dev, "Failed to get reset\n");
  502. return PTR_ERR(tegra->reset);
  503. }
  504. tegra->clock = devm_clk_get(&pdev->dev, "actmon");
  505. if (IS_ERR(tegra->clock)) {
  506. dev_err(&pdev->dev, "Failed to get actmon clock\n");
  507. return PTR_ERR(tegra->clock);
  508. }
  509. tegra->emc_clock = devm_clk_get(&pdev->dev, "emc");
  510. if (IS_ERR(tegra->emc_clock)) {
  511. dev_err(&pdev->dev, "Failed to get emc clock\n");
  512. return PTR_ERR(tegra->emc_clock);
  513. }
  514. clk_set_rate(tegra->emc_clock, ULONG_MAX);
  515. tegra->rate_change_nb.notifier_call = tegra_actmon_rate_notify_cb;
  516. err = clk_notifier_register(tegra->emc_clock, &tegra->rate_change_nb);
  517. if (err) {
  518. dev_err(&pdev->dev,
  519. "Failed to register rate change notifier\n");
  520. return err;
  521. }
  522. reset_control_assert(tegra->reset);
  523. err = clk_prepare_enable(tegra->clock);
  524. if (err) {
  525. dev_err(&pdev->dev,
  526. "Failed to prepare and enable ACTMON clock\n");
  527. return err;
  528. }
  529. reset_control_deassert(tegra->reset);
  530. tegra->max_freq = clk_round_rate(tegra->emc_clock, ULONG_MAX) / KHZ;
  531. tegra->cur_freq = clk_get_rate(tegra->emc_clock) / KHZ;
  532. actmon_writel(tegra, ACTMON_SAMPLING_PERIOD - 1,
  533. ACTMON_GLB_PERIOD_CTRL);
  534. for (i = 0; i < ARRAY_SIZE(actmon_device_configs); i++) {
  535. dev = tegra->devices + i;
  536. dev->config = actmon_device_configs + i;
  537. dev->regs = tegra->regs + dev->config->offset;
  538. spin_lock_init(&dev->lock);
  539. tegra_actmon_configure_device(tegra, dev);
  540. }
  541. for (rate = 0; rate <= tegra->max_freq * KHZ; rate++) {
  542. rate = clk_round_rate(tegra->emc_clock, rate);
  543. dev_pm_opp_add(&pdev->dev, rate, 0);
  544. }
  545. irq = platform_get_irq(pdev, 0);
  546. if (irq < 0) {
  547. dev_err(&pdev->dev, "Failed to get IRQ: %d\n", irq);
  548. return irq;
  549. }
  550. platform_set_drvdata(pdev, tegra);
  551. err = devm_request_threaded_irq(&pdev->dev, irq, actmon_isr,
  552. actmon_thread_isr, IRQF_SHARED,
  553. "tegra-devfreq", tegra);
  554. if (err) {
  555. dev_err(&pdev->dev, "Interrupt request failed\n");
  556. return err;
  557. }
  558. tegra_devfreq_profile.initial_freq = clk_get_rate(tegra->emc_clock);
  559. tegra->devfreq = devm_devfreq_add_device(&pdev->dev,
  560. &tegra_devfreq_profile,
  561. "tegra_actmon",
  562. NULL);
  563. return 0;
  564. }
  565. static int tegra_devfreq_remove(struct platform_device *pdev)
  566. {
  567. struct tegra_devfreq *tegra = platform_get_drvdata(pdev);
  568. int irq = platform_get_irq(pdev, 0);
  569. u32 val;
  570. unsigned int i;
  571. for (i = 0; i < ARRAY_SIZE(actmon_device_configs); i++) {
  572. val = device_readl(&tegra->devices[i], ACTMON_DEV_CTRL);
  573. val &= ~ACTMON_DEV_CTRL_ENB;
  574. device_writel(&tegra->devices[i], val, ACTMON_DEV_CTRL);
  575. }
  576. actmon_write_barrier(tegra);
  577. devm_free_irq(&pdev->dev, irq, tegra);
  578. clk_notifier_unregister(tegra->emc_clock, &tegra->rate_change_nb);
  579. clk_disable_unprepare(tegra->clock);
  580. return 0;
  581. }
  582. static const struct of_device_id tegra_devfreq_of_match[] = {
  583. { .compatible = "nvidia,tegra124-actmon" },
  584. { },
  585. };
  586. MODULE_DEVICE_TABLE(of, tegra_devfreq_of_match);
  587. static struct platform_driver tegra_devfreq_driver = {
  588. .probe = tegra_devfreq_probe,
  589. .remove = tegra_devfreq_remove,
  590. .driver = {
  591. .name = "tegra-devfreq",
  592. .of_match_table = tegra_devfreq_of_match,
  593. },
  594. };
  595. static int __init tegra_devfreq_init(void)
  596. {
  597. int ret = 0;
  598. ret = devfreq_add_governor(&tegra_devfreq_governor);
  599. if (ret) {
  600. pr_err("%s: failed to add governor: %d\n", __func__, ret);
  601. return ret;
  602. }
  603. ret = platform_driver_register(&tegra_devfreq_driver);
  604. if (ret)
  605. devfreq_remove_governor(&tegra_devfreq_governor);
  606. return ret;
  607. }
  608. module_init(tegra_devfreq_init)
  609. static void __exit tegra_devfreq_exit(void)
  610. {
  611. int ret = 0;
  612. platform_driver_unregister(&tegra_devfreq_driver);
  613. ret = devfreq_remove_governor(&tegra_devfreq_governor);
  614. if (ret)
  615. pr_err("%s: failed to remove governor: %d\n", __func__, ret);
  616. }
  617. module_exit(tegra_devfreq_exit)
  618. MODULE_LICENSE("GPL v2");
  619. MODULE_DESCRIPTION("Tegra devfreq driver");
  620. MODULE_AUTHOR("Tomeu Vizoso <tomeu.vizoso@collabora.com>");