tegra30-devfreq.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * A devfreq driver for NVIDIA Tegra SoCs
  4. *
  5. * Copyright (c) 2014 NVIDIA CORPORATION. All rights reserved.
  6. * Copyright (C) 2014 Google, Inc
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/cpufreq.h>
  10. #include <linux/devfreq.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/irq.h>
  14. #include <linux/module.h>
  15. #include <linux/of_device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_opp.h>
  18. #include <linux/reset.h>
  19. #include <linux/workqueue.h>
  20. #include <soc/tegra/fuse.h>
  21. #include "governor.h"
  22. #define ACTMON_GLB_STATUS 0x0
  23. #define ACTMON_GLB_PERIOD_CTRL 0x4
  24. #define ACTMON_DEV_CTRL 0x0
  25. #define ACTMON_DEV_CTRL_K_VAL_SHIFT 10
  26. #define ACTMON_DEV_CTRL_ENB_PERIODIC BIT(18)
  27. #define ACTMON_DEV_CTRL_AVG_BELOW_WMARK_EN BIT(20)
  28. #define ACTMON_DEV_CTRL_AVG_ABOVE_WMARK_EN BIT(21)
  29. #define ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_NUM_SHIFT 23
  30. #define ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_NUM_SHIFT 26
  31. #define ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN BIT(29)
  32. #define ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN BIT(30)
  33. #define ACTMON_DEV_CTRL_ENB BIT(31)
  34. #define ACTMON_DEV_CTRL_STOP 0x00000000
  35. #define ACTMON_DEV_UPPER_WMARK 0x4
  36. #define ACTMON_DEV_LOWER_WMARK 0x8
  37. #define ACTMON_DEV_INIT_AVG 0xc
  38. #define ACTMON_DEV_AVG_UPPER_WMARK 0x10
  39. #define ACTMON_DEV_AVG_LOWER_WMARK 0x14
  40. #define ACTMON_DEV_COUNT_WEIGHT 0x18
  41. #define ACTMON_DEV_AVG_COUNT 0x20
  42. #define ACTMON_DEV_INTR_STATUS 0x24
  43. #define ACTMON_INTR_STATUS_CLEAR 0xffffffff
  44. #define ACTMON_DEV_INTR_CONSECUTIVE_UPPER BIT(31)
  45. #define ACTMON_DEV_INTR_CONSECUTIVE_LOWER BIT(30)
  46. #define ACTMON_ABOVE_WMARK_WINDOW 1
  47. #define ACTMON_BELOW_WMARK_WINDOW 3
  48. #define ACTMON_BOOST_FREQ_STEP 16000
  49. /*
  50. * ACTMON_AVERAGE_WINDOW_LOG2: default value for @DEV_CTRL_K_VAL, which
  51. * translates to 2 ^ (K_VAL + 1). ex: 2 ^ (6 + 1) = 128
  52. */
  53. #define ACTMON_AVERAGE_WINDOW_LOG2 6
  54. #define ACTMON_SAMPLING_PERIOD 12 /* ms */
  55. #define ACTMON_DEFAULT_AVG_BAND 6 /* 1/10 of % */
  56. #define KHZ 1000
  57. #define KHZ_MAX (ULONG_MAX / KHZ)
  58. /* Assume that the bus is saturated if the utilization is 25% */
  59. #define BUS_SATURATION_RATIO 25
  60. /**
  61. * struct tegra_devfreq_device_config - configuration specific to an ACTMON
  62. * device
  63. *
  64. * Coefficients and thresholds are percentages unless otherwise noted
  65. */
  66. struct tegra_devfreq_device_config {
  67. u32 offset;
  68. u32 irq_mask;
  69. /* Factors applied to boost_freq every consecutive watermark breach */
  70. unsigned int boost_up_coeff;
  71. unsigned int boost_down_coeff;
  72. /* Define the watermark bounds when applied to the current avg */
  73. unsigned int boost_up_threshold;
  74. unsigned int boost_down_threshold;
  75. /*
  76. * Threshold of activity (cycles translated to kHz) below which the
  77. * CPU frequency isn't to be taken into account. This is to avoid
  78. * increasing the EMC frequency when the CPU is very busy but not
  79. * accessing the bus often.
  80. */
  81. u32 avg_dependency_threshold;
  82. };
  83. enum tegra_actmon_device {
  84. MCALL = 0,
  85. MCCPU,
  86. };
  87. static const struct tegra_devfreq_device_config tegra124_device_configs[] = {
  88. {
  89. /* MCALL: All memory accesses (including from the CPUs) */
  90. .offset = 0x1c0,
  91. .irq_mask = 1 << 26,
  92. .boost_up_coeff = 200,
  93. .boost_down_coeff = 50,
  94. .boost_up_threshold = 60,
  95. .boost_down_threshold = 40,
  96. },
  97. {
  98. /* MCCPU: memory accesses from the CPUs */
  99. .offset = 0x200,
  100. .irq_mask = 1 << 25,
  101. .boost_up_coeff = 800,
  102. .boost_down_coeff = 40,
  103. .boost_up_threshold = 27,
  104. .boost_down_threshold = 10,
  105. .avg_dependency_threshold = 16000, /* 16MHz in kHz units */
  106. },
  107. };
  108. static const struct tegra_devfreq_device_config tegra30_device_configs[] = {
  109. {
  110. /* MCALL: All memory accesses (including from the CPUs) */
  111. .offset = 0x1c0,
  112. .irq_mask = 1 << 26,
  113. .boost_up_coeff = 200,
  114. .boost_down_coeff = 50,
  115. .boost_up_threshold = 20,
  116. .boost_down_threshold = 10,
  117. },
  118. {
  119. /* MCCPU: memory accesses from the CPUs */
  120. .offset = 0x200,
  121. .irq_mask = 1 << 25,
  122. .boost_up_coeff = 800,
  123. .boost_down_coeff = 40,
  124. .boost_up_threshold = 27,
  125. .boost_down_threshold = 10,
  126. .avg_dependency_threshold = 16000, /* 16MHz in kHz units */
  127. },
  128. };
  129. /**
  130. * struct tegra_devfreq_device - state specific to an ACTMON device
  131. *
  132. * Frequencies are in kHz.
  133. */
  134. struct tegra_devfreq_device {
  135. const struct tegra_devfreq_device_config *config;
  136. void __iomem *regs;
  137. /* Average event count sampled in the last interrupt */
  138. u32 avg_count;
  139. /*
  140. * Extra frequency to increase the target by due to consecutive
  141. * watermark breaches.
  142. */
  143. unsigned long boost_freq;
  144. /* Optimal frequency calculated from the stats for this device */
  145. unsigned long target_freq;
  146. };
  147. struct tegra_devfreq_soc_data {
  148. const struct tegra_devfreq_device_config *configs;
  149. /* Weight value for count measurements */
  150. unsigned int count_weight;
  151. };
  152. struct tegra_devfreq {
  153. struct devfreq *devfreq;
  154. struct reset_control *reset;
  155. struct clk *clock;
  156. void __iomem *regs;
  157. struct clk *emc_clock;
  158. unsigned long max_freq;
  159. unsigned long cur_freq;
  160. struct notifier_block clk_rate_change_nb;
  161. struct delayed_work cpufreq_update_work;
  162. struct notifier_block cpu_rate_change_nb;
  163. struct tegra_devfreq_device devices[2];
  164. unsigned int irq;
  165. bool started;
  166. const struct tegra_devfreq_soc_data *soc;
  167. };
  168. struct tegra_actmon_emc_ratio {
  169. unsigned long cpu_freq;
  170. unsigned long emc_freq;
  171. };
  172. static const struct tegra_actmon_emc_ratio actmon_emc_ratios[] = {
  173. { 1400000, KHZ_MAX },
  174. { 1200000, 750000 },
  175. { 1100000, 600000 },
  176. { 1000000, 500000 },
  177. { 800000, 375000 },
  178. { 500000, 200000 },
  179. { 250000, 100000 },
  180. };
  181. static u32 actmon_readl(struct tegra_devfreq *tegra, u32 offset)
  182. {
  183. return readl_relaxed(tegra->regs + offset);
  184. }
  185. static void actmon_writel(struct tegra_devfreq *tegra, u32 val, u32 offset)
  186. {
  187. writel_relaxed(val, tegra->regs + offset);
  188. }
  189. static u32 device_readl(struct tegra_devfreq_device *dev, u32 offset)
  190. {
  191. return readl_relaxed(dev->regs + offset);
  192. }
  193. static void device_writel(struct tegra_devfreq_device *dev, u32 val,
  194. u32 offset)
  195. {
  196. writel_relaxed(val, dev->regs + offset);
  197. }
  198. static unsigned long do_percent(unsigned long long val, unsigned int pct)
  199. {
  200. val = val * pct;
  201. do_div(val, 100);
  202. /*
  203. * High freq + high boosting percent + large polling interval are
  204. * resulting in integer overflow when watermarks are calculated.
  205. */
  206. return min_t(u64, val, U32_MAX);
  207. }
  208. static void tegra_devfreq_update_avg_wmark(struct tegra_devfreq *tegra,
  209. struct tegra_devfreq_device *dev)
  210. {
  211. u32 avg_band_freq = tegra->max_freq * ACTMON_DEFAULT_AVG_BAND / KHZ;
  212. u32 band = avg_band_freq * tegra->devfreq->profile->polling_ms;
  213. u32 avg;
  214. avg = min(dev->avg_count, U32_MAX - band);
  215. device_writel(dev, avg + band, ACTMON_DEV_AVG_UPPER_WMARK);
  216. avg = max(dev->avg_count, band);
  217. device_writel(dev, avg - band, ACTMON_DEV_AVG_LOWER_WMARK);
  218. }
  219. static void tegra_devfreq_update_wmark(struct tegra_devfreq *tegra,
  220. struct tegra_devfreq_device *dev)
  221. {
  222. u32 val = tegra->cur_freq * tegra->devfreq->profile->polling_ms;
  223. device_writel(dev, do_percent(val, dev->config->boost_up_threshold),
  224. ACTMON_DEV_UPPER_WMARK);
  225. device_writel(dev, do_percent(val, dev->config->boost_down_threshold),
  226. ACTMON_DEV_LOWER_WMARK);
  227. }
  228. static void actmon_isr_device(struct tegra_devfreq *tegra,
  229. struct tegra_devfreq_device *dev)
  230. {
  231. u32 intr_status, dev_ctrl;
  232. dev->avg_count = device_readl(dev, ACTMON_DEV_AVG_COUNT);
  233. tegra_devfreq_update_avg_wmark(tegra, dev);
  234. intr_status = device_readl(dev, ACTMON_DEV_INTR_STATUS);
  235. dev_ctrl = device_readl(dev, ACTMON_DEV_CTRL);
  236. if (intr_status & ACTMON_DEV_INTR_CONSECUTIVE_UPPER) {
  237. /*
  238. * new_boost = min(old_boost * up_coef + step, max_freq)
  239. */
  240. dev->boost_freq = do_percent(dev->boost_freq,
  241. dev->config->boost_up_coeff);
  242. dev->boost_freq += ACTMON_BOOST_FREQ_STEP;
  243. dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
  244. if (dev->boost_freq >= tegra->max_freq) {
  245. dev_ctrl &= ~ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
  246. dev->boost_freq = tegra->max_freq;
  247. }
  248. } else if (intr_status & ACTMON_DEV_INTR_CONSECUTIVE_LOWER) {
  249. /*
  250. * new_boost = old_boost * down_coef
  251. * or 0 if (old_boost * down_coef < step / 2)
  252. */
  253. dev->boost_freq = do_percent(dev->boost_freq,
  254. dev->config->boost_down_coeff);
  255. dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
  256. if (dev->boost_freq < (ACTMON_BOOST_FREQ_STEP >> 1)) {
  257. dev_ctrl &= ~ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
  258. dev->boost_freq = 0;
  259. }
  260. }
  261. device_writel(dev, dev_ctrl, ACTMON_DEV_CTRL);
  262. device_writel(dev, ACTMON_INTR_STATUS_CLEAR, ACTMON_DEV_INTR_STATUS);
  263. }
  264. static unsigned long actmon_cpu_to_emc_rate(struct tegra_devfreq *tegra,
  265. unsigned long cpu_freq)
  266. {
  267. unsigned int i;
  268. const struct tegra_actmon_emc_ratio *ratio = actmon_emc_ratios;
  269. for (i = 0; i < ARRAY_SIZE(actmon_emc_ratios); i++, ratio++) {
  270. if (cpu_freq >= ratio->cpu_freq) {
  271. if (ratio->emc_freq >= tegra->max_freq)
  272. return tegra->max_freq;
  273. else
  274. return ratio->emc_freq;
  275. }
  276. }
  277. return 0;
  278. }
  279. static unsigned long actmon_device_target_freq(struct tegra_devfreq *tegra,
  280. struct tegra_devfreq_device *dev)
  281. {
  282. unsigned int avg_sustain_coef;
  283. unsigned long target_freq;
  284. target_freq = dev->avg_count / tegra->devfreq->profile->polling_ms;
  285. avg_sustain_coef = 100 * 100 / dev->config->boost_up_threshold;
  286. target_freq = do_percent(target_freq, avg_sustain_coef);
  287. return target_freq;
  288. }
  289. static void actmon_update_target(struct tegra_devfreq *tegra,
  290. struct tegra_devfreq_device *dev)
  291. {
  292. unsigned long cpu_freq = 0;
  293. unsigned long static_cpu_emc_freq = 0;
  294. dev->target_freq = actmon_device_target_freq(tegra, dev);
  295. if (dev->config->avg_dependency_threshold &&
  296. dev->config->avg_dependency_threshold <= dev->target_freq) {
  297. cpu_freq = cpufreq_quick_get(0);
  298. static_cpu_emc_freq = actmon_cpu_to_emc_rate(tegra, cpu_freq);
  299. dev->target_freq += dev->boost_freq;
  300. dev->target_freq = max(dev->target_freq, static_cpu_emc_freq);
  301. } else {
  302. dev->target_freq += dev->boost_freq;
  303. }
  304. }
  305. static irqreturn_t actmon_thread_isr(int irq, void *data)
  306. {
  307. struct tegra_devfreq *tegra = data;
  308. bool handled = false;
  309. unsigned int i;
  310. u32 val;
  311. mutex_lock(&tegra->devfreq->lock);
  312. val = actmon_readl(tegra, ACTMON_GLB_STATUS);
  313. for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
  314. if (val & tegra->devices[i].config->irq_mask) {
  315. actmon_isr_device(tegra, tegra->devices + i);
  316. handled = true;
  317. }
  318. }
  319. if (handled)
  320. update_devfreq(tegra->devfreq);
  321. mutex_unlock(&tegra->devfreq->lock);
  322. return handled ? IRQ_HANDLED : IRQ_NONE;
  323. }
  324. static int tegra_actmon_clk_notify_cb(struct notifier_block *nb,
  325. unsigned long action, void *ptr)
  326. {
  327. struct clk_notifier_data *data = ptr;
  328. struct tegra_devfreq *tegra;
  329. struct tegra_devfreq_device *dev;
  330. unsigned int i;
  331. if (action != POST_RATE_CHANGE)
  332. return NOTIFY_OK;
  333. tegra = container_of(nb, struct tegra_devfreq, clk_rate_change_nb);
  334. tegra->cur_freq = data->new_rate / KHZ;
  335. for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
  336. dev = &tegra->devices[i];
  337. tegra_devfreq_update_wmark(tegra, dev);
  338. }
  339. return NOTIFY_OK;
  340. }
  341. static void tegra_actmon_delayed_update(struct work_struct *work)
  342. {
  343. struct tegra_devfreq *tegra = container_of(work, struct tegra_devfreq,
  344. cpufreq_update_work.work);
  345. mutex_lock(&tegra->devfreq->lock);
  346. update_devfreq(tegra->devfreq);
  347. mutex_unlock(&tegra->devfreq->lock);
  348. }
  349. static unsigned long
  350. tegra_actmon_cpufreq_contribution(struct tegra_devfreq *tegra,
  351. unsigned int cpu_freq)
  352. {
  353. struct tegra_devfreq_device *actmon_dev = &tegra->devices[MCCPU];
  354. unsigned long static_cpu_emc_freq, dev_freq;
  355. dev_freq = actmon_device_target_freq(tegra, actmon_dev);
  356. /* check whether CPU's freq is taken into account at all */
  357. if (dev_freq < actmon_dev->config->avg_dependency_threshold)
  358. return 0;
  359. static_cpu_emc_freq = actmon_cpu_to_emc_rate(tegra, cpu_freq);
  360. if (dev_freq + actmon_dev->boost_freq >= static_cpu_emc_freq)
  361. return 0;
  362. return static_cpu_emc_freq;
  363. }
  364. static int tegra_actmon_cpu_notify_cb(struct notifier_block *nb,
  365. unsigned long action, void *ptr)
  366. {
  367. struct cpufreq_freqs *freqs = ptr;
  368. struct tegra_devfreq *tegra;
  369. unsigned long old, new, delay;
  370. if (action != CPUFREQ_POSTCHANGE)
  371. return NOTIFY_OK;
  372. tegra = container_of(nb, struct tegra_devfreq, cpu_rate_change_nb);
  373. /*
  374. * Quickly check whether CPU frequency should be taken into account
  375. * at all, without blocking CPUFreq's core.
  376. */
  377. if (mutex_trylock(&tegra->devfreq->lock)) {
  378. old = tegra_actmon_cpufreq_contribution(tegra, freqs->old);
  379. new = tegra_actmon_cpufreq_contribution(tegra, freqs->new);
  380. mutex_unlock(&tegra->devfreq->lock);
  381. /*
  382. * If CPU's frequency shouldn't be taken into account at
  383. * the moment, then there is no need to update the devfreq's
  384. * state because ISR will re-check CPU's frequency on the
  385. * next interrupt.
  386. */
  387. if (old == new)
  388. return NOTIFY_OK;
  389. }
  390. /*
  391. * CPUFreq driver should support CPUFREQ_ASYNC_NOTIFICATION in order
  392. * to allow asynchronous notifications. This means we can't block
  393. * here for too long, otherwise CPUFreq's core will complain with a
  394. * warning splat.
  395. */
  396. delay = msecs_to_jiffies(ACTMON_SAMPLING_PERIOD);
  397. schedule_delayed_work(&tegra->cpufreq_update_work, delay);
  398. return NOTIFY_OK;
  399. }
  400. static void tegra_actmon_configure_device(struct tegra_devfreq *tegra,
  401. struct tegra_devfreq_device *dev)
  402. {
  403. u32 val = 0;
  404. /* reset boosting on governor's restart */
  405. dev->boost_freq = 0;
  406. dev->target_freq = tegra->cur_freq;
  407. dev->avg_count = tegra->cur_freq * tegra->devfreq->profile->polling_ms;
  408. device_writel(dev, dev->avg_count, ACTMON_DEV_INIT_AVG);
  409. tegra_devfreq_update_avg_wmark(tegra, dev);
  410. tegra_devfreq_update_wmark(tegra, dev);
  411. device_writel(dev, tegra->soc->count_weight, ACTMON_DEV_COUNT_WEIGHT);
  412. device_writel(dev, ACTMON_INTR_STATUS_CLEAR, ACTMON_DEV_INTR_STATUS);
  413. val |= ACTMON_DEV_CTRL_ENB_PERIODIC;
  414. val |= (ACTMON_AVERAGE_WINDOW_LOG2 - 1)
  415. << ACTMON_DEV_CTRL_K_VAL_SHIFT;
  416. val |= (ACTMON_BELOW_WMARK_WINDOW - 1)
  417. << ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_NUM_SHIFT;
  418. val |= (ACTMON_ABOVE_WMARK_WINDOW - 1)
  419. << ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_NUM_SHIFT;
  420. val |= ACTMON_DEV_CTRL_AVG_ABOVE_WMARK_EN;
  421. val |= ACTMON_DEV_CTRL_AVG_BELOW_WMARK_EN;
  422. val |= ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
  423. val |= ACTMON_DEV_CTRL_ENB;
  424. device_writel(dev, val, ACTMON_DEV_CTRL);
  425. }
  426. static void tegra_actmon_stop_devices(struct tegra_devfreq *tegra)
  427. {
  428. struct tegra_devfreq_device *dev = tegra->devices;
  429. unsigned int i;
  430. for (i = 0; i < ARRAY_SIZE(tegra->devices); i++, dev++) {
  431. device_writel(dev, ACTMON_DEV_CTRL_STOP, ACTMON_DEV_CTRL);
  432. device_writel(dev, ACTMON_INTR_STATUS_CLEAR,
  433. ACTMON_DEV_INTR_STATUS);
  434. }
  435. }
  436. static int tegra_actmon_resume(struct tegra_devfreq *tegra)
  437. {
  438. unsigned int i;
  439. int err;
  440. if (!tegra->devfreq->profile->polling_ms || !tegra->started)
  441. return 0;
  442. actmon_writel(tegra, tegra->devfreq->profile->polling_ms - 1,
  443. ACTMON_GLB_PERIOD_CTRL);
  444. /*
  445. * CLK notifications are needed in order to reconfigure the upper
  446. * consecutive watermark in accordance to the actual clock rate
  447. * to avoid unnecessary upper interrupts.
  448. */
  449. err = clk_notifier_register(tegra->emc_clock,
  450. &tegra->clk_rate_change_nb);
  451. if (err) {
  452. dev_err(tegra->devfreq->dev.parent,
  453. "Failed to register rate change notifier\n");
  454. return err;
  455. }
  456. tegra->cur_freq = clk_get_rate(tegra->emc_clock) / KHZ;
  457. for (i = 0; i < ARRAY_SIZE(tegra->devices); i++)
  458. tegra_actmon_configure_device(tegra, &tegra->devices[i]);
  459. /*
  460. * We are estimating CPU's memory bandwidth requirement based on
  461. * amount of memory accesses and system's load, judging by CPU's
  462. * frequency. We also don't want to receive events about CPU's
  463. * frequency transaction when governor is stopped, hence notifier
  464. * is registered dynamically.
  465. */
  466. err = cpufreq_register_notifier(&tegra->cpu_rate_change_nb,
  467. CPUFREQ_TRANSITION_NOTIFIER);
  468. if (err) {
  469. dev_err(tegra->devfreq->dev.parent,
  470. "Failed to register rate change notifier: %d\n", err);
  471. goto err_stop;
  472. }
  473. enable_irq(tegra->irq);
  474. return 0;
  475. err_stop:
  476. tegra_actmon_stop_devices(tegra);
  477. clk_notifier_unregister(tegra->emc_clock, &tegra->clk_rate_change_nb);
  478. return err;
  479. }
  480. static int tegra_actmon_start(struct tegra_devfreq *tegra)
  481. {
  482. int ret = 0;
  483. if (!tegra->started) {
  484. tegra->started = true;
  485. ret = tegra_actmon_resume(tegra);
  486. if (ret)
  487. tegra->started = false;
  488. }
  489. return ret;
  490. }
  491. static void tegra_actmon_pause(struct tegra_devfreq *tegra)
  492. {
  493. if (!tegra->devfreq->profile->polling_ms || !tegra->started)
  494. return;
  495. disable_irq(tegra->irq);
  496. cpufreq_unregister_notifier(&tegra->cpu_rate_change_nb,
  497. CPUFREQ_TRANSITION_NOTIFIER);
  498. cancel_delayed_work_sync(&tegra->cpufreq_update_work);
  499. tegra_actmon_stop_devices(tegra);
  500. clk_notifier_unregister(tegra->emc_clock, &tegra->clk_rate_change_nb);
  501. }
  502. static void tegra_actmon_stop(struct tegra_devfreq *tegra)
  503. {
  504. tegra_actmon_pause(tegra);
  505. tegra->started = false;
  506. }
  507. static int tegra_devfreq_target(struct device *dev, unsigned long *freq,
  508. u32 flags)
  509. {
  510. struct dev_pm_opp *opp;
  511. int ret;
  512. opp = devfreq_recommended_opp(dev, freq, flags);
  513. if (IS_ERR(opp)) {
  514. dev_err(dev, "Failed to find opp for %lu Hz\n", *freq);
  515. return PTR_ERR(opp);
  516. }
  517. ret = dev_pm_opp_set_opp(dev, opp);
  518. dev_pm_opp_put(opp);
  519. return ret;
  520. }
  521. static int tegra_devfreq_get_dev_status(struct device *dev,
  522. struct devfreq_dev_status *stat)
  523. {
  524. struct tegra_devfreq *tegra = dev_get_drvdata(dev);
  525. struct tegra_devfreq_device *actmon_dev;
  526. unsigned long cur_freq;
  527. cur_freq = READ_ONCE(tegra->cur_freq);
  528. /* To be used by the tegra governor */
  529. stat->private_data = tegra;
  530. /* The below are to be used by the other governors */
  531. stat->current_frequency = cur_freq * KHZ;
  532. actmon_dev = &tegra->devices[MCALL];
  533. /* Number of cycles spent on memory access */
  534. stat->busy_time = device_readl(actmon_dev, ACTMON_DEV_AVG_COUNT);
  535. /* The bus can be considered to be saturated way before 100% */
  536. stat->busy_time *= 100 / BUS_SATURATION_RATIO;
  537. /* Number of cycles in a sampling period */
  538. stat->total_time = tegra->devfreq->profile->polling_ms * cur_freq;
  539. stat->busy_time = min(stat->busy_time, stat->total_time);
  540. return 0;
  541. }
  542. static struct devfreq_dev_profile tegra_devfreq_profile = {
  543. .polling_ms = ACTMON_SAMPLING_PERIOD,
  544. .target = tegra_devfreq_target,
  545. .get_dev_status = tegra_devfreq_get_dev_status,
  546. .is_cooling_device = true,
  547. };
  548. static int tegra_governor_get_target(struct devfreq *devfreq,
  549. unsigned long *freq)
  550. {
  551. struct devfreq_dev_status *stat;
  552. struct tegra_devfreq *tegra;
  553. struct tegra_devfreq_device *dev;
  554. unsigned long target_freq = 0;
  555. unsigned int i;
  556. int err;
  557. err = devfreq_update_stats(devfreq);
  558. if (err)
  559. return err;
  560. stat = &devfreq->last_status;
  561. tegra = stat->private_data;
  562. for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
  563. dev = &tegra->devices[i];
  564. actmon_update_target(tegra, dev);
  565. target_freq = max(target_freq, dev->target_freq);
  566. }
  567. /*
  568. * tegra-devfreq driver operates with KHz units, while OPP table
  569. * entries use Hz units. Hence we need to convert the units for the
  570. * devfreq core.
  571. */
  572. *freq = target_freq * KHZ;
  573. return 0;
  574. }
  575. static int tegra_governor_event_handler(struct devfreq *devfreq,
  576. unsigned int event, void *data)
  577. {
  578. struct tegra_devfreq *tegra = dev_get_drvdata(devfreq->dev.parent);
  579. unsigned int *new_delay = data;
  580. int ret = 0;
  581. /*
  582. * Couple devfreq-device with the governor early because it is
  583. * needed at the moment of governor's start (used by ISR).
  584. */
  585. tegra->devfreq = devfreq;
  586. switch (event) {
  587. case DEVFREQ_GOV_START:
  588. devfreq_monitor_start(devfreq);
  589. ret = tegra_actmon_start(tegra);
  590. break;
  591. case DEVFREQ_GOV_STOP:
  592. tegra_actmon_stop(tegra);
  593. devfreq_monitor_stop(devfreq);
  594. break;
  595. case DEVFREQ_GOV_UPDATE_INTERVAL:
  596. /*
  597. * ACTMON hardware supports up to 256 milliseconds for the
  598. * sampling period.
  599. */
  600. if (*new_delay > 256) {
  601. ret = -EINVAL;
  602. break;
  603. }
  604. tegra_actmon_pause(tegra);
  605. devfreq_update_interval(devfreq, new_delay);
  606. ret = tegra_actmon_resume(tegra);
  607. break;
  608. case DEVFREQ_GOV_SUSPEND:
  609. tegra_actmon_stop(tegra);
  610. devfreq_monitor_suspend(devfreq);
  611. break;
  612. case DEVFREQ_GOV_RESUME:
  613. devfreq_monitor_resume(devfreq);
  614. ret = tegra_actmon_start(tegra);
  615. break;
  616. }
  617. return ret;
  618. }
  619. static struct devfreq_governor tegra_devfreq_governor = {
  620. .name = "tegra_actmon",
  621. .attrs = DEVFREQ_GOV_ATTR_POLLING_INTERVAL,
  622. .flags = DEVFREQ_GOV_FLAG_IMMUTABLE
  623. | DEVFREQ_GOV_FLAG_IRQ_DRIVEN,
  624. .get_target_freq = tegra_governor_get_target,
  625. .event_handler = tegra_governor_event_handler,
  626. };
  627. static void devm_tegra_devfreq_deinit_hw(void *data)
  628. {
  629. struct tegra_devfreq *tegra = data;
  630. reset_control_reset(tegra->reset);
  631. clk_disable_unprepare(tegra->clock);
  632. }
  633. static int devm_tegra_devfreq_init_hw(struct device *dev,
  634. struct tegra_devfreq *tegra)
  635. {
  636. int err;
  637. err = clk_prepare_enable(tegra->clock);
  638. if (err) {
  639. dev_err(dev, "Failed to prepare and enable ACTMON clock\n");
  640. return err;
  641. }
  642. err = devm_add_action_or_reset(dev, devm_tegra_devfreq_deinit_hw,
  643. tegra);
  644. if (err)
  645. return err;
  646. err = reset_control_reset(tegra->reset);
  647. if (err) {
  648. dev_err(dev, "Failed to reset hardware: %d\n", err);
  649. return err;
  650. }
  651. return err;
  652. }
  653. static int tegra_devfreq_config_clks_nop(struct device *dev,
  654. struct opp_table *opp_table,
  655. struct dev_pm_opp *opp, void *data,
  656. bool scaling_down)
  657. {
  658. /* We want to skip clk configuration via dev_pm_opp_set_opp() */
  659. return 0;
  660. }
  661. static int tegra_devfreq_probe(struct platform_device *pdev)
  662. {
  663. u32 hw_version = BIT(tegra_sku_info.soc_speedo_id);
  664. struct tegra_devfreq_device *dev;
  665. struct tegra_devfreq *tegra;
  666. struct devfreq *devfreq;
  667. unsigned int i;
  668. long rate;
  669. int err;
  670. const char *clk_names[] = { "actmon", NULL };
  671. struct dev_pm_opp_config config = {
  672. .supported_hw = &hw_version,
  673. .supported_hw_count = 1,
  674. .clk_names = clk_names,
  675. .config_clks = tegra_devfreq_config_clks_nop,
  676. };
  677. tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
  678. if (!tegra)
  679. return -ENOMEM;
  680. tegra->soc = of_device_get_match_data(&pdev->dev);
  681. tegra->regs = devm_platform_ioremap_resource(pdev, 0);
  682. if (IS_ERR(tegra->regs))
  683. return PTR_ERR(tegra->regs);
  684. tegra->reset = devm_reset_control_get(&pdev->dev, "actmon");
  685. if (IS_ERR(tegra->reset)) {
  686. dev_err(&pdev->dev, "Failed to get reset\n");
  687. return PTR_ERR(tegra->reset);
  688. }
  689. tegra->clock = devm_clk_get(&pdev->dev, "actmon");
  690. if (IS_ERR(tegra->clock)) {
  691. dev_err(&pdev->dev, "Failed to get actmon clock\n");
  692. return PTR_ERR(tegra->clock);
  693. }
  694. tegra->emc_clock = devm_clk_get(&pdev->dev, "emc");
  695. if (IS_ERR(tegra->emc_clock))
  696. return dev_err_probe(&pdev->dev, PTR_ERR(tegra->emc_clock),
  697. "Failed to get emc clock\n");
  698. err = platform_get_irq(pdev, 0);
  699. if (err < 0)
  700. return err;
  701. tegra->irq = err;
  702. irq_set_status_flags(tegra->irq, IRQ_NOAUTOEN);
  703. err = devm_request_threaded_irq(&pdev->dev, tegra->irq, NULL,
  704. actmon_thread_isr, IRQF_ONESHOT,
  705. "tegra-devfreq", tegra);
  706. if (err) {
  707. dev_err(&pdev->dev, "Interrupt request failed: %d\n", err);
  708. return err;
  709. }
  710. err = devm_pm_opp_set_config(&pdev->dev, &config);
  711. if (err) {
  712. dev_err(&pdev->dev, "Failed to set OPP config: %d\n", err);
  713. return err;
  714. }
  715. err = devm_pm_opp_of_add_table_indexed(&pdev->dev, 0);
  716. if (err) {
  717. dev_err(&pdev->dev, "Failed to add OPP table: %d\n", err);
  718. return err;
  719. }
  720. err = devm_tegra_devfreq_init_hw(&pdev->dev, tegra);
  721. if (err)
  722. return err;
  723. rate = clk_round_rate(tegra->emc_clock, ULONG_MAX);
  724. if (rate <= 0) {
  725. dev_err(&pdev->dev, "Failed to round clock rate: %ld\n", rate);
  726. return rate ?: -EINVAL;
  727. }
  728. tegra->max_freq = rate / KHZ;
  729. for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
  730. dev = tegra->devices + i;
  731. dev->config = tegra->soc->configs + i;
  732. dev->regs = tegra->regs + dev->config->offset;
  733. }
  734. platform_set_drvdata(pdev, tegra);
  735. tegra->clk_rate_change_nb.notifier_call = tegra_actmon_clk_notify_cb;
  736. tegra->cpu_rate_change_nb.notifier_call = tegra_actmon_cpu_notify_cb;
  737. INIT_DELAYED_WORK(&tegra->cpufreq_update_work,
  738. tegra_actmon_delayed_update);
  739. err = devm_devfreq_add_governor(&pdev->dev, &tegra_devfreq_governor);
  740. if (err) {
  741. dev_err(&pdev->dev, "Failed to add governor: %d\n", err);
  742. return err;
  743. }
  744. tegra_devfreq_profile.initial_freq = clk_get_rate(tegra->emc_clock);
  745. devfreq = devm_devfreq_add_device(&pdev->dev, &tegra_devfreq_profile,
  746. "tegra_actmon", NULL);
  747. if (IS_ERR(devfreq)) {
  748. dev_err(&pdev->dev, "Failed to add device: %pe\n", devfreq);
  749. return PTR_ERR(devfreq);
  750. }
  751. return 0;
  752. }
  753. static const struct tegra_devfreq_soc_data tegra124_soc = {
  754. .configs = tegra124_device_configs,
  755. /*
  756. * Activity counter is incremented every 256 memory transactions,
  757. * and each transaction takes 4 EMC clocks.
  758. */
  759. .count_weight = 4 * 256,
  760. };
  761. static const struct tegra_devfreq_soc_data tegra30_soc = {
  762. .configs = tegra30_device_configs,
  763. .count_weight = 2 * 256,
  764. };
  765. static const struct of_device_id tegra_devfreq_of_match[] = {
  766. { .compatible = "nvidia,tegra30-actmon", .data = &tegra30_soc, },
  767. { .compatible = "nvidia,tegra124-actmon", .data = &tegra124_soc, },
  768. { },
  769. };
  770. MODULE_DEVICE_TABLE(of, tegra_devfreq_of_match);
  771. static struct platform_driver tegra_devfreq_driver = {
  772. .probe = tegra_devfreq_probe,
  773. .driver = {
  774. .name = "tegra-devfreq",
  775. .of_match_table = tegra_devfreq_of_match,
  776. },
  777. };
  778. module_platform_driver(tegra_devfreq_driver);
  779. MODULE_LICENSE("GPL v2");
  780. MODULE_DESCRIPTION("Tegra devfreq driver");
  781. MODULE_AUTHOR("Tomeu Vizoso <[email protected]>");