tegra30-devfreq.c 25 KB

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