lpm-levels.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826
  1. /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
  2. * Copyright (C) 2006-2007 Adam Belay <[email protected]>
  3. * Copyright (C) 2009 Intel Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 and
  7. * only version 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #define pr_fmt(fmt) "%s: " fmt, KBUILD_MODNAME
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/mutex.h>
  22. #include <linux/cpu.h>
  23. #include <linux/of.h>
  24. #include <linux/hrtimer.h>
  25. #include <linux/ktime.h>
  26. #include <linux/tick.h>
  27. #include <linux/suspend.h>
  28. #include <linux/pm_qos.h>
  29. #include <linux/of_platform.h>
  30. #include <linux/smp.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/moduleparam.h>
  33. #include <linux/sched.h>
  34. #include <linux/cpu_pm.h>
  35. #include <linux/cpuhotplug.h>
  36. #include <soc/qcom/pm.h>
  37. #include <soc/qcom/event_timer.h>
  38. #include <soc/qcom/lpm_levels.h>
  39. #include <soc/qcom/lpm-stats.h>
  40. #include <soc/qcom/minidump.h>
  41. #include <asm/arch_timer.h>
  42. #include <asm/suspend.h>
  43. #include <asm/cpuidle.h>
  44. #include "lpm-levels.h"
  45. #include <trace/events/power.h>
  46. #if defined(CONFIG_COMMON_CLK)
  47. #include "../clk/clk.h"
  48. #elif defined(CONFIG_COMMON_CLK_MSM)
  49. #include "../../drivers/clk/msm/clock.h"
  50. #endif /* CONFIG_COMMON_CLK */
  51. #define CREATE_TRACE_POINTS
  52. #include <trace/events/trace_msm_low_power.h>
  53. #define SCLK_HZ (32768)
  54. #define PSCI_POWER_STATE(reset) (reset << 30)
  55. #define PSCI_AFFINITY_LEVEL(lvl) ((lvl & 0x3) << 24)
  56. #define BIAS_HYST (bias_hyst * NSEC_PER_MSEC)
  57. enum {
  58. MSM_LPM_LVL_DBG_SUSPEND_LIMITS = BIT(0),
  59. MSM_LPM_LVL_DBG_IDLE_LIMITS = BIT(1),
  60. };
  61. enum debug_event {
  62. CPU_ENTER,
  63. CPU_EXIT,
  64. CLUSTER_ENTER,
  65. CLUSTER_EXIT,
  66. CPU_HP_STARTING,
  67. CPU_HP_DYING,
  68. };
  69. struct lpm_debug {
  70. cycle_t time;
  71. enum debug_event evt;
  72. int cpu;
  73. uint32_t arg1;
  74. uint32_t arg2;
  75. uint32_t arg3;
  76. uint32_t arg4;
  77. };
  78. static struct system_pm_ops *sys_pm_ops;
  79. static DEFINE_SPINLOCK(bc_timer_lock);
  80. struct lpm_cluster *lpm_root_node;
  81. #define MAXSAMPLES 5
  82. static bool lpm_prediction = true;
  83. module_param_named(lpm_prediction, lpm_prediction, bool, 0664);
  84. static uint32_t bias_hyst;
  85. module_param_named(bias_hyst, bias_hyst, uint, 0664);
  86. struct lpm_history {
  87. uint32_t resi[MAXSAMPLES];
  88. int mode[MAXSAMPLES];
  89. int nsamp;
  90. uint32_t hptr;
  91. uint32_t hinvalid;
  92. uint32_t htmr_wkup;
  93. int64_t stime;
  94. };
  95. static DEFINE_PER_CPU(struct lpm_history, hist);
  96. static DEFINE_PER_CPU(struct lpm_cpu*, cpu_lpm);
  97. static bool suspend_in_progress;
  98. static struct hrtimer lpm_hrtimer;
  99. static DEFINE_PER_CPU(struct hrtimer, histtimer);
  100. static struct lpm_debug *lpm_debug;
  101. static phys_addr_t lpm_debug_phys;
  102. static const int num_dbg_elements = 0x100;
  103. static void cluster_unprepare(struct lpm_cluster *cluster,
  104. const struct cpumask *cpu, int child_idx, bool from_idle,
  105. int64_t time);
  106. static void cluster_prepare(struct lpm_cluster *cluster,
  107. const struct cpumask *cpu, int child_idx, bool from_idle,
  108. int64_t time);
  109. static bool print_parsed_dt;
  110. module_param_named(print_parsed_dt, print_parsed_dt, bool, 0664);
  111. static bool sleep_disabled;
  112. module_param_named(sleep_disabled, sleep_disabled, bool, 0664);
  113. /**
  114. * msm_cpuidle_get_deep_idle_latency - Get deep idle latency value
  115. *
  116. * Returns an s32 latency value
  117. */
  118. s32 msm_cpuidle_get_deep_idle_latency(void)
  119. {
  120. return 10;
  121. }
  122. EXPORT_SYMBOL(msm_cpuidle_get_deep_idle_latency);
  123. uint32_t register_system_pm_ops(struct system_pm_ops *pm_ops)
  124. {
  125. if (sys_pm_ops)
  126. return -EUSERS;
  127. sys_pm_ops = pm_ops;
  128. return 0;
  129. }
  130. static uint32_t least_cluster_latency(struct lpm_cluster *cluster,
  131. struct latency_level *lat_level)
  132. {
  133. struct list_head *list;
  134. struct lpm_cluster_level *level;
  135. struct lpm_cluster *n;
  136. struct power_params *pwr_params;
  137. uint32_t latency = 0;
  138. int i;
  139. if (!cluster->list.next) {
  140. for (i = 0; i < cluster->nlevels; i++) {
  141. level = &cluster->levels[i];
  142. pwr_params = &level->pwr;
  143. if (lat_level->reset_level == level->reset_level) {
  144. if ((latency > pwr_params->latency_us)
  145. || (!latency))
  146. latency = pwr_params->latency_us;
  147. break;
  148. }
  149. }
  150. } else {
  151. list_for_each(list, &cluster->parent->child) {
  152. n = list_entry(list, typeof(*n), list);
  153. if (lat_level->level_name) {
  154. if (strcmp(lat_level->level_name,
  155. n->cluster_name))
  156. continue;
  157. }
  158. for (i = 0; i < n->nlevels; i++) {
  159. level = &n->levels[i];
  160. pwr_params = &level->pwr;
  161. if (lat_level->reset_level ==
  162. level->reset_level) {
  163. if ((latency > pwr_params->latency_us)
  164. || (!latency))
  165. latency =
  166. pwr_params->latency_us;
  167. break;
  168. }
  169. }
  170. }
  171. }
  172. return latency;
  173. }
  174. static uint32_t least_cpu_latency(struct list_head *child,
  175. struct latency_level *lat_level)
  176. {
  177. struct list_head *list;
  178. struct lpm_cpu_level *level;
  179. struct power_params *pwr_params;
  180. struct lpm_cpu *cpu;
  181. struct lpm_cluster *n;
  182. uint32_t lat = 0;
  183. int i;
  184. list_for_each(list, child) {
  185. n = list_entry(list, typeof(*n), list);
  186. if (lat_level->level_name) {
  187. if (strcmp(lat_level->level_name, n->cluster_name))
  188. continue;
  189. }
  190. list_for_each_entry(cpu, &n->cpu, list) {
  191. for (i = 0; i < cpu->nlevels; i++) {
  192. level = &cpu->levels[i];
  193. pwr_params = &level->pwr;
  194. if (lat_level->reset_level
  195. == level->reset_level) {
  196. if ((lat > pwr_params->latency_us)
  197. || (!lat))
  198. lat = pwr_params->latency_us;
  199. break;
  200. }
  201. }
  202. }
  203. }
  204. return lat;
  205. }
  206. static struct lpm_cluster *cluster_aff_match(struct lpm_cluster *cluster,
  207. int affinity_level)
  208. {
  209. struct lpm_cluster *n;
  210. if ((cluster->aff_level == affinity_level)
  211. || ((!list_empty(&cluster->cpu)) && (affinity_level == 0)))
  212. return cluster;
  213. else if (list_empty(&cluster->cpu)) {
  214. n = list_entry(cluster->child.next, typeof(*n), list);
  215. return cluster_aff_match(n, affinity_level);
  216. } else
  217. return NULL;
  218. }
  219. int lpm_get_latency(struct latency_level *level, uint32_t *latency)
  220. {
  221. struct lpm_cluster *cluster;
  222. uint32_t val;
  223. if (!lpm_root_node) {
  224. pr_err("lpm_probe not completed\n");
  225. return -EAGAIN;
  226. }
  227. if ((level->affinity_level < 0)
  228. || (level->affinity_level > lpm_root_node->aff_level)
  229. || (level->reset_level < LPM_RESET_LVL_RET)
  230. || (level->reset_level > LPM_RESET_LVL_PC)
  231. || !latency)
  232. return -EINVAL;
  233. cluster = cluster_aff_match(lpm_root_node, level->affinity_level);
  234. if (!cluster) {
  235. pr_err("No matching cluster found for affinity_level:%d\n",
  236. level->affinity_level);
  237. return -EINVAL;
  238. }
  239. if (level->affinity_level == 0)
  240. val = least_cpu_latency(&cluster->parent->child, level);
  241. else
  242. val = least_cluster_latency(cluster, level);
  243. if (!val) {
  244. pr_err("No mode with affinity_level:%d reset_level:%d\n",
  245. level->affinity_level, level->reset_level);
  246. return -EINVAL;
  247. }
  248. *latency = val;
  249. return 0;
  250. }
  251. EXPORT_SYMBOL(lpm_get_latency);
  252. static void update_debug_pc_event(enum debug_event event, uint32_t arg1,
  253. uint32_t arg2, uint32_t arg3, uint32_t arg4)
  254. {
  255. struct lpm_debug *dbg;
  256. int idx;
  257. static DEFINE_SPINLOCK(debug_lock);
  258. static int pc_event_index;
  259. if (!lpm_debug)
  260. return;
  261. spin_lock(&debug_lock);
  262. idx = pc_event_index++;
  263. dbg = &lpm_debug[idx & (num_dbg_elements - 1)];
  264. dbg->evt = event;
  265. dbg->time = arch_counter_get_cntvct();
  266. dbg->cpu = raw_smp_processor_id();
  267. dbg->arg1 = arg1;
  268. dbg->arg2 = arg2;
  269. dbg->arg3 = arg3;
  270. dbg->arg4 = arg4;
  271. spin_unlock(&debug_lock);
  272. }
  273. static int lpm_dying_cpu(unsigned int cpu)
  274. {
  275. struct lpm_cluster *cluster = per_cpu(cpu_lpm, cpu)->parent;
  276. update_debug_pc_event(CPU_HP_DYING, cpu,
  277. cluster->num_children_in_sync.bits[0],
  278. cluster->child_cpus.bits[0], false);
  279. cluster_prepare(cluster, get_cpu_mask(cpu), NR_LPM_LEVELS, false, 0);
  280. return 0;
  281. }
  282. static int lpm_starting_cpu(unsigned int cpu)
  283. {
  284. struct lpm_cluster *cluster = per_cpu(cpu_lpm, cpu)->parent;
  285. update_debug_pc_event(CPU_HP_STARTING, cpu,
  286. cluster->num_children_in_sync.bits[0],
  287. cluster->child_cpus.bits[0], false);
  288. cluster_unprepare(cluster, get_cpu_mask(cpu), NR_LPM_LEVELS, false, 0);
  289. return 0;
  290. }
  291. static enum hrtimer_restart lpm_hrtimer_cb(struct hrtimer *h)
  292. {
  293. return HRTIMER_NORESTART;
  294. }
  295. static void histtimer_cancel(void)
  296. {
  297. unsigned int cpu = raw_smp_processor_id();
  298. struct hrtimer *cpu_histtimer = &per_cpu(histtimer, cpu);
  299. ktime_t time_rem;
  300. time_rem = hrtimer_get_remaining(cpu_histtimer);
  301. if (ktime_to_us(time_rem) <= 0)
  302. return;
  303. hrtimer_try_to_cancel(cpu_histtimer);
  304. }
  305. static enum hrtimer_restart histtimer_fn(struct hrtimer *h)
  306. {
  307. int cpu = raw_smp_processor_id();
  308. struct lpm_history *history = &per_cpu(hist, cpu);
  309. history->hinvalid = 1;
  310. return HRTIMER_NORESTART;
  311. }
  312. static void histtimer_start(uint32_t time_us)
  313. {
  314. uint64_t time_ns = time_us * NSEC_PER_USEC;
  315. ktime_t hist_ktime = ns_to_ktime(time_ns);
  316. unsigned int cpu = raw_smp_processor_id();
  317. struct hrtimer *cpu_histtimer = &per_cpu(histtimer, cpu);
  318. cpu_histtimer->function = histtimer_fn;
  319. hrtimer_start(cpu_histtimer, hist_ktime, HRTIMER_MODE_REL_PINNED);
  320. }
  321. static void cluster_timer_init(struct lpm_cluster *cluster)
  322. {
  323. struct list_head *list;
  324. if (!cluster)
  325. return;
  326. hrtimer_init(&cluster->histtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  327. list_for_each(list, &cluster->child) {
  328. struct lpm_cluster *n;
  329. n = list_entry(list, typeof(*n), list);
  330. cluster_timer_init(n);
  331. }
  332. }
  333. static void clusttimer_cancel(void)
  334. {
  335. int cpu = raw_smp_processor_id();
  336. struct lpm_cluster *cluster = per_cpu(cpu_lpm, cpu)->parent;
  337. ktime_t time_rem;
  338. time_rem = hrtimer_get_remaining(&cluster->histtimer);
  339. if (ktime_to_us(time_rem) > 0)
  340. hrtimer_try_to_cancel(&cluster->histtimer);
  341. if (cluster->parent) {
  342. time_rem = hrtimer_get_remaining(
  343. &cluster->parent->histtimer);
  344. if (ktime_to_us(time_rem) <= 0)
  345. return;
  346. hrtimer_try_to_cancel(&cluster->parent->histtimer);
  347. }
  348. }
  349. static enum hrtimer_restart clusttimer_fn(struct hrtimer *h)
  350. {
  351. struct lpm_cluster *cluster = container_of(h,
  352. struct lpm_cluster, histtimer);
  353. cluster->history.hinvalid = 1;
  354. return HRTIMER_NORESTART;
  355. }
  356. static void clusttimer_start(struct lpm_cluster *cluster, uint32_t time_us)
  357. {
  358. uint64_t time_ns = time_us * NSEC_PER_USEC;
  359. ktime_t clust_ktime = ns_to_ktime(time_ns);
  360. cluster->histtimer.function = clusttimer_fn;
  361. hrtimer_start(&cluster->histtimer, clust_ktime,
  362. HRTIMER_MODE_REL_PINNED);
  363. }
  364. static void msm_pm_set_timer(uint32_t modified_time_us)
  365. {
  366. u64 modified_time_ns = modified_time_us * NSEC_PER_USEC;
  367. ktime_t modified_ktime = ns_to_ktime(modified_time_ns);
  368. lpm_hrtimer.function = lpm_hrtimer_cb;
  369. hrtimer_start(&lpm_hrtimer, modified_ktime, HRTIMER_MODE_REL_PINNED);
  370. }
  371. static uint64_t lpm_cpuidle_predict(struct cpuidle_device *dev,
  372. struct lpm_cpu *cpu, int *idx_restrict,
  373. uint32_t *idx_restrict_time)
  374. {
  375. int i, j, divisor;
  376. uint64_t max, avg, stddev;
  377. int64_t thresh = LLONG_MAX;
  378. struct lpm_history *history = &per_cpu(hist, dev->cpu);
  379. uint32_t *min_residency = get_per_cpu_min_residency(dev->cpu);
  380. uint32_t *max_residency = get_per_cpu_max_residency(dev->cpu);
  381. if (!lpm_prediction || !cpu->lpm_prediction)
  382. return 0;
  383. /*
  384. * Samples are marked invalid when woken-up due to timer,
  385. * so donot predict.
  386. */
  387. if (history->hinvalid) {
  388. history->hinvalid = 0;
  389. history->htmr_wkup = 1;
  390. history->stime = 0;
  391. return 0;
  392. }
  393. /*
  394. * Predict only when all the samples are collected.
  395. */
  396. if (history->nsamp < MAXSAMPLES) {
  397. history->stime = 0;
  398. return 0;
  399. }
  400. /*
  401. * Check if the samples are not much deviated, if so use the
  402. * average of those as predicted sleep time. Else if any
  403. * specific mode has more premature exits return the index of
  404. * that mode.
  405. */
  406. again:
  407. max = avg = divisor = stddev = 0;
  408. for (i = 0; i < MAXSAMPLES; i++) {
  409. int64_t value = history->resi[i];
  410. if (value <= thresh) {
  411. avg += value;
  412. divisor++;
  413. if (value > max)
  414. max = value;
  415. }
  416. }
  417. do_div(avg, divisor);
  418. for (i = 0; i < MAXSAMPLES; i++) {
  419. int64_t value = history->resi[i];
  420. if (value <= thresh) {
  421. int64_t diff = value - avg;
  422. stddev += diff * diff;
  423. }
  424. }
  425. do_div(stddev, divisor);
  426. stddev = int_sqrt(stddev);
  427. /*
  428. * If the deviation is less, return the average, else
  429. * ignore one maximum sample and retry
  430. */
  431. if (((avg > stddev * 6) && (divisor >= (MAXSAMPLES - 1)))
  432. || stddev <= cpu->ref_stddev) {
  433. history->stime = ktime_to_us(ktime_get()) + avg;
  434. return avg;
  435. } else if (divisor > (MAXSAMPLES - 1)) {
  436. thresh = max - 1;
  437. goto again;
  438. }
  439. /*
  440. * Find the number of premature exits for each of the mode,
  441. * excluding clockgating mode, and they are more than fifty
  442. * percent restrict that and deeper modes.
  443. */
  444. if (history->htmr_wkup != 1) {
  445. for (j = 1; j < cpu->nlevels; j++) {
  446. uint32_t failed = 0;
  447. uint64_t total = 0;
  448. for (i = 0; i < MAXSAMPLES; i++) {
  449. if ((history->mode[i] == j) &&
  450. (history->resi[i] < min_residency[j])) {
  451. failed++;
  452. total += history->resi[i];
  453. }
  454. }
  455. if (failed >= cpu->ref_premature_cnt) {
  456. *idx_restrict = j;
  457. do_div(total, failed);
  458. for (i = 0; i < j; i++) {
  459. if (total < max_residency[i]) {
  460. *idx_restrict = i+1;
  461. total = max_residency[i];
  462. break;
  463. }
  464. }
  465. *idx_restrict_time = total;
  466. history->stime = ktime_to_us(ktime_get())
  467. + *idx_restrict_time;
  468. break;
  469. }
  470. }
  471. }
  472. return 0;
  473. }
  474. static inline void invalidate_predict_history(struct cpuidle_device *dev)
  475. {
  476. struct lpm_history *history = &per_cpu(hist, dev->cpu);
  477. struct lpm_cpu *lpm_cpu = per_cpu(cpu_lpm, dev->cpu);
  478. if (!lpm_prediction || !lpm_cpu->lpm_prediction)
  479. return;
  480. if (history->hinvalid) {
  481. history->hinvalid = 0;
  482. history->htmr_wkup = 1;
  483. history->stime = 0;
  484. }
  485. }
  486. static void clear_predict_history(void)
  487. {
  488. struct lpm_history *history;
  489. int i;
  490. unsigned int cpu;
  491. struct lpm_cpu *lpm_cpu = per_cpu(cpu_lpm, raw_smp_processor_id());
  492. if (!lpm_prediction || !lpm_cpu->lpm_prediction)
  493. return;
  494. for_each_possible_cpu(cpu) {
  495. history = &per_cpu(hist, cpu);
  496. for (i = 0; i < MAXSAMPLES; i++) {
  497. history->resi[i] = 0;
  498. history->mode[i] = -1;
  499. history->hptr = 0;
  500. history->nsamp = 0;
  501. history->stime = 0;
  502. }
  503. }
  504. }
  505. static void update_history(struct cpuidle_device *dev, int idx);
  506. static inline bool is_cpu_biased(int cpu)
  507. {
  508. u64 now = sched_clock();
  509. u64 last = sched_get_cpu_last_busy_time(cpu);
  510. if (!last)
  511. return false;
  512. return (now - last) < BIAS_HYST;
  513. }
  514. static int cpu_power_select(struct cpuidle_device *dev,
  515. struct lpm_cpu *cpu)
  516. {
  517. int best_level = 0;
  518. uint32_t latency_us = pm_qos_request_for_cpu(PM_QOS_CPU_DMA_LATENCY,
  519. dev->cpu);
  520. s64 sleep_us = ktime_to_us(tick_nohz_get_sleep_length());
  521. uint32_t modified_time_us = 0;
  522. uint32_t next_event_us = 0;
  523. int i, idx_restrict;
  524. uint32_t lvl_latency_us = 0;
  525. uint64_t predicted = 0;
  526. uint32_t htime = 0, idx_restrict_time = 0;
  527. uint32_t next_wakeup_us = (uint32_t)sleep_us;
  528. uint32_t *min_residency = get_per_cpu_min_residency(dev->cpu);
  529. uint32_t *max_residency = get_per_cpu_max_residency(dev->cpu);
  530. if ((sleep_disabled && !cpu_isolated(dev->cpu)) || sleep_us < 0)
  531. return best_level;
  532. idx_restrict = cpu->nlevels + 1;
  533. next_event_us = (uint32_t)(ktime_to_us(get_next_event_time(dev->cpu)));
  534. if (is_cpu_biased(dev->cpu) && (!cpu_isolated(dev->cpu)))
  535. goto done_select;
  536. for (i = 0; i < cpu->nlevels; i++) {
  537. struct lpm_cpu_level *level = &cpu->levels[i];
  538. struct power_params *pwr_params = &level->pwr;
  539. bool allow;
  540. allow = i ? lpm_cpu_mode_allow(dev->cpu, i, true) : true;
  541. if (!allow)
  542. continue;
  543. lvl_latency_us = pwr_params->latency_us;
  544. if (latency_us < lvl_latency_us)
  545. break;
  546. if (next_event_us) {
  547. if (next_event_us < lvl_latency_us)
  548. break;
  549. if (((next_event_us - lvl_latency_us) < sleep_us) ||
  550. (next_event_us < sleep_us))
  551. next_wakeup_us = next_event_us - lvl_latency_us;
  552. }
  553. if (!i && !cpu_isolated(dev->cpu)) {
  554. /*
  555. * If the next_wake_us itself is not sufficient for
  556. * deeper low power modes than clock gating do not
  557. * call prediction.
  558. */
  559. if (next_wakeup_us > max_residency[i]) {
  560. predicted = lpm_cpuidle_predict(dev, cpu,
  561. &idx_restrict, &idx_restrict_time);
  562. if (predicted && (predicted < min_residency[i]))
  563. predicted = min_residency[i];
  564. } else
  565. invalidate_predict_history(dev);
  566. }
  567. if (i >= idx_restrict)
  568. break;
  569. best_level = i;
  570. if (next_event_us && next_event_us < sleep_us && !i)
  571. modified_time_us = next_event_us - lvl_latency_us;
  572. else
  573. modified_time_us = 0;
  574. if (predicted ? (predicted <= max_residency[i])
  575. : (next_wakeup_us <= max_residency[i]))
  576. break;
  577. }
  578. if (modified_time_us)
  579. msm_pm_set_timer(modified_time_us);
  580. /*
  581. * Start timer to avoid staying in shallower mode forever
  582. * incase of misprediciton
  583. */
  584. if ((predicted || (idx_restrict != (cpu->nlevels + 1)))
  585. && ((best_level >= 0)
  586. && (best_level < (cpu->nlevels-1)))) {
  587. htime = predicted + cpu->tmr_add;
  588. if (htime == cpu->tmr_add)
  589. htime = idx_restrict_time;
  590. else if (htime > max_residency[best_level])
  591. htime = max_residency[best_level];
  592. if ((next_wakeup_us > htime) &&
  593. ((next_wakeup_us - htime) > max_residency[best_level]))
  594. histtimer_start(htime);
  595. }
  596. done_select:
  597. trace_cpu_power_select(best_level, sleep_us, latency_us, next_event_us);
  598. trace_cpu_pred_select(idx_restrict_time ? 2 : (predicted ? 1 : 0),
  599. predicted, htime);
  600. return best_level;
  601. }
  602. static unsigned int get_next_online_cpu(bool from_idle)
  603. {
  604. unsigned int cpu;
  605. ktime_t next_event;
  606. unsigned int next_cpu = raw_smp_processor_id();
  607. if (!from_idle)
  608. return next_cpu;
  609. next_event.tv64 = KTIME_MAX;
  610. for_each_online_cpu(cpu) {
  611. ktime_t *next_event_c;
  612. next_event_c = get_next_event_cpu(cpu);
  613. if (next_event_c->tv64 < next_event.tv64) {
  614. next_event.tv64 = next_event_c->tv64;
  615. next_cpu = cpu;
  616. }
  617. }
  618. return next_cpu;
  619. }
  620. static uint64_t get_cluster_sleep_time(struct lpm_cluster *cluster,
  621. bool from_idle, uint32_t *pred_time)
  622. {
  623. int cpu;
  624. ktime_t next_event;
  625. struct cpumask online_cpus_in_cluster;
  626. struct lpm_history *history;
  627. int64_t prediction = LONG_MAX;
  628. if (!from_idle)
  629. return ~0ULL;
  630. next_event.tv64 = KTIME_MAX;
  631. cpumask_and(&online_cpus_in_cluster,
  632. &cluster->num_children_in_sync, cpu_online_mask);
  633. for_each_cpu(cpu, &online_cpus_in_cluster) {
  634. ktime_t *next_event_c;
  635. next_event_c = get_next_event_cpu(cpu);
  636. if (next_event_c->tv64 < next_event.tv64) {
  637. next_event.tv64 = next_event_c->tv64;
  638. }
  639. if (from_idle && lpm_prediction && cluster->lpm_prediction) {
  640. history = &per_cpu(hist, cpu);
  641. if (history->stime && (history->stime < prediction))
  642. prediction = history->stime;
  643. }
  644. }
  645. if (from_idle && lpm_prediction && cluster->lpm_prediction) {
  646. if (prediction > ktime_to_us(ktime_get()))
  647. *pred_time = prediction - ktime_to_us(ktime_get());
  648. }
  649. if (ktime_to_us(next_event) > ktime_to_us(ktime_get()))
  650. return ktime_to_us(ktime_sub(next_event, ktime_get()));
  651. else
  652. return 0;
  653. }
  654. static int cluster_predict(struct lpm_cluster *cluster,
  655. uint32_t *pred_us)
  656. {
  657. int i, j;
  658. int ret = 0;
  659. struct cluster_history *history = &cluster->history;
  660. int64_t cur_time = ktime_to_us(ktime_get());
  661. if (!lpm_prediction || !cluster->lpm_prediction)
  662. return 0;
  663. if (history->hinvalid) {
  664. history->hinvalid = 0;
  665. history->htmr_wkup = 1;
  666. history->flag = 0;
  667. return ret;
  668. }
  669. if (history->nsamp == MAXSAMPLES) {
  670. for (i = 0; i < MAXSAMPLES; i++) {
  671. if ((cur_time - history->stime[i])
  672. > CLUST_SMPL_INVLD_TIME)
  673. history->nsamp--;
  674. }
  675. }
  676. if (history->nsamp < MAXSAMPLES) {
  677. history->flag = 0;
  678. return ret;
  679. }
  680. if (history->flag == 2)
  681. history->flag = 0;
  682. if (history->htmr_wkup != 1) {
  683. uint64_t total = 0;
  684. if (history->flag == 1) {
  685. for (i = 0; i < MAXSAMPLES; i++)
  686. total += history->resi[i];
  687. do_div(total, MAXSAMPLES);
  688. *pred_us = total;
  689. return 2;
  690. }
  691. for (j = 1; j < cluster->nlevels; j++) {
  692. uint32_t failed = 0;
  693. total = 0;
  694. for (i = 0; i < MAXSAMPLES; i++) {
  695. if ((history->mode[i] == j) && (history->resi[i]
  696. < cluster->levels[j].pwr.min_residency)) {
  697. failed++;
  698. total += history->resi[i];
  699. }
  700. }
  701. if (failed > (MAXSAMPLES-2)) {
  702. do_div(total, failed);
  703. *pred_us = total;
  704. history->flag = 1;
  705. return 1;
  706. }
  707. }
  708. }
  709. return ret;
  710. }
  711. static void update_cluster_history_time(struct cluster_history *history,
  712. int idx, uint64_t start)
  713. {
  714. history->entry_idx = idx;
  715. history->entry_time = start;
  716. }
  717. static void update_cluster_history(struct cluster_history *history, int idx)
  718. {
  719. uint32_t tmr = 0;
  720. uint32_t residency = 0;
  721. struct lpm_cluster *cluster =
  722. container_of(history, struct lpm_cluster, history);
  723. if (!lpm_prediction || !cluster->lpm_prediction)
  724. return;
  725. if ((history->entry_idx == -1) || (history->entry_idx == idx)) {
  726. residency = ktime_to_us(ktime_get()) - history->entry_time;
  727. history->stime[history->hptr] = history->entry_time;
  728. } else
  729. return;
  730. if (history->htmr_wkup) {
  731. if (!history->hptr)
  732. history->hptr = MAXSAMPLES-1;
  733. else
  734. history->hptr--;
  735. history->resi[history->hptr] += residency;
  736. history->htmr_wkup = 0;
  737. tmr = 1;
  738. } else
  739. history->resi[history->hptr] = residency;
  740. history->mode[history->hptr] = idx;
  741. history->entry_idx = INT_MIN;
  742. history->entry_time = 0;
  743. if (history->nsamp < MAXSAMPLES)
  744. history->nsamp++;
  745. trace_cluster_pred_hist(cluster->cluster_name,
  746. history->mode[history->hptr], history->resi[history->hptr],
  747. history->hptr, tmr);
  748. (history->hptr)++;
  749. if (history->hptr >= MAXSAMPLES)
  750. history->hptr = 0;
  751. }
  752. static void clear_cl_history_each(struct cluster_history *history)
  753. {
  754. int i;
  755. for (i = 0; i < MAXSAMPLES; i++) {
  756. history->resi[i] = 0;
  757. history->mode[i] = -1;
  758. history->stime[i] = 0;
  759. }
  760. history->hptr = 0;
  761. history->nsamp = 0;
  762. history->flag = 0;
  763. history->hinvalid = 0;
  764. history->htmr_wkup = 0;
  765. }
  766. static void clear_cl_predict_history(void)
  767. {
  768. struct lpm_cluster *cluster = lpm_root_node;
  769. struct list_head *list;
  770. if (!lpm_prediction || !cluster->lpm_prediction)
  771. return;
  772. clear_cl_history_each(&cluster->history);
  773. list_for_each(list, &cluster->child) {
  774. struct lpm_cluster *n;
  775. n = list_entry(list, typeof(*n), list);
  776. clear_cl_history_each(&n->history);
  777. }
  778. }
  779. static int cluster_select(struct lpm_cluster *cluster, bool from_idle,
  780. int *ispred)
  781. {
  782. int best_level = -1;
  783. int i;
  784. struct cpumask mask;
  785. uint32_t latency_us = ~0U;
  786. uint32_t sleep_us;
  787. uint32_t cpupred_us = 0, pred_us = 0;
  788. int pred_mode = 0, predicted = 0;
  789. if (!cluster)
  790. return -EINVAL;
  791. sleep_us = (uint32_t)get_cluster_sleep_time(cluster,
  792. from_idle, &cpupred_us);
  793. if (from_idle) {
  794. pred_mode = cluster_predict(cluster, &pred_us);
  795. if (cpupred_us && pred_mode && (cpupred_us < pred_us))
  796. pred_us = cpupred_us;
  797. if (pred_us && pred_mode && (pred_us < sleep_us))
  798. predicted = 1;
  799. if (predicted && (pred_us == cpupred_us))
  800. predicted = 2;
  801. }
  802. if (cpumask_and(&mask, cpu_online_mask, &cluster->child_cpus))
  803. latency_us = pm_qos_request_for_cpumask(PM_QOS_CPU_DMA_LATENCY,
  804. &mask);
  805. for (i = 0; i < cluster->nlevels; i++) {
  806. struct lpm_cluster_level *level = &cluster->levels[i];
  807. struct power_params *pwr_params = &level->pwr;
  808. if (!lpm_cluster_mode_allow(cluster, i, from_idle))
  809. continue;
  810. if (!cpumask_equal(&cluster->num_children_in_sync,
  811. &level->num_cpu_votes))
  812. continue;
  813. if (from_idle && latency_us < pwr_params->latency_us)
  814. break;
  815. if (sleep_us < pwr_params->time_overhead_us)
  816. break;
  817. if (suspend_in_progress && from_idle && level->notify_rpm)
  818. continue;
  819. if (level->notify_rpm) {
  820. if (!(sys_pm_ops && sys_pm_ops->sleep_allowed))
  821. continue;
  822. if (!sys_pm_ops->sleep_allowed())
  823. continue;
  824. }
  825. best_level = i;
  826. if (from_idle &&
  827. (predicted ? (pred_us <= pwr_params->max_residency)
  828. : (sleep_us <= pwr_params->max_residency)))
  829. break;
  830. }
  831. if ((best_level == (cluster->nlevels - 1)) && (pred_mode == 2))
  832. cluster->history.flag = 2;
  833. *ispred = predicted;
  834. trace_cluster_pred_select(cluster->cluster_name, best_level, sleep_us,
  835. latency_us, predicted, pred_us);
  836. return best_level;
  837. }
  838. static void cluster_notify(struct lpm_cluster *cluster,
  839. struct lpm_cluster_level *level, bool enter)
  840. {
  841. if (level->is_reset && enter)
  842. cpu_cluster_pm_enter(cluster->aff_level);
  843. else if (level->is_reset && !enter)
  844. cpu_cluster_pm_exit(cluster->aff_level);
  845. }
  846. static int cluster_configure(struct lpm_cluster *cluster, int idx,
  847. bool from_idle, int predicted)
  848. {
  849. struct lpm_cluster_level *level = &cluster->levels[idx];
  850. struct cpumask online_cpus, cpumask;
  851. unsigned int cpu;
  852. int ret = 0;
  853. cpumask_and(&online_cpus, &cluster->num_children_in_sync,
  854. cpu_online_mask);
  855. if (!cpumask_equal(&cluster->num_children_in_sync, &cluster->child_cpus)
  856. || is_IPI_pending(&online_cpus)) {
  857. return -EPERM;
  858. }
  859. if (idx != cluster->default_level) {
  860. update_debug_pc_event(CLUSTER_ENTER, idx,
  861. cluster->num_children_in_sync.bits[0],
  862. cluster->child_cpus.bits[0], from_idle);
  863. trace_cluster_enter(cluster->cluster_name, idx,
  864. cluster->num_children_in_sync.bits[0],
  865. cluster->child_cpus.bits[0], from_idle);
  866. lpm_stats_cluster_enter(cluster->stats, idx);
  867. if (from_idle && lpm_prediction && cluster->lpm_prediction)
  868. update_cluster_history_time(&cluster->history, idx,
  869. ktime_to_us(ktime_get()));
  870. }
  871. if (level->notify_rpm) {
  872. /*
  873. * Print the clocks which are enabled during system suspend
  874. * This debug information is useful to know which are the
  875. * clocks that are enabled and preventing the system level
  876. * LPMs(XO and Vmin).
  877. */
  878. if (!from_idle)
  879. clock_debug_print_enabled(true);
  880. cpu = get_next_online_cpu(from_idle);
  881. cpumask_copy(&cpumask, cpumask_of(cpu));
  882. clear_predict_history();
  883. clear_cl_predict_history();
  884. if (sys_pm_ops && sys_pm_ops->enter) {
  885. spin_lock(&bc_timer_lock);
  886. ret = sys_pm_ops->enter(&cpumask);
  887. spin_unlock(&bc_timer_lock);
  888. if (ret)
  889. return -EBUSY;
  890. }
  891. }
  892. /* Notify cluster enter event after successfully config completion */
  893. cluster_notify(cluster, level, true);
  894. cluster->last_level = idx;
  895. if (predicted && (idx < (cluster->nlevels - 1))) {
  896. struct power_params *pwr_params = &cluster->levels[idx].pwr;
  897. clusttimer_start(cluster, pwr_params->max_residency +
  898. cluster->tmr_add);
  899. }
  900. return 0;
  901. }
  902. static void cluster_prepare(struct lpm_cluster *cluster,
  903. const struct cpumask *cpu, int child_idx, bool from_idle,
  904. int64_t start_time)
  905. {
  906. int i;
  907. int predicted = 0;
  908. if (!cluster)
  909. return;
  910. if (cluster->min_child_level > child_idx)
  911. return;
  912. spin_lock(&cluster->sync_lock);
  913. cpumask_or(&cluster->num_children_in_sync, cpu,
  914. &cluster->num_children_in_sync);
  915. for (i = 0; i < cluster->nlevels; i++) {
  916. struct lpm_cluster_level *lvl = &cluster->levels[i];
  917. if (child_idx >= lvl->min_child_level)
  918. cpumask_or(&lvl->num_cpu_votes, cpu,
  919. &lvl->num_cpu_votes);
  920. }
  921. /*
  922. * cluster_select() does not make any configuration changes. So its ok
  923. * to release the lock here. If a core wakes up for a rude request,
  924. * it need not wait for another to finish its cluster selection and
  925. * configuration process
  926. */
  927. if (!cpumask_equal(&cluster->num_children_in_sync,
  928. &cluster->child_cpus))
  929. goto failed;
  930. i = cluster_select(cluster, from_idle, &predicted);
  931. if (((i < 0) || (i == cluster->default_level))
  932. && predicted && from_idle) {
  933. update_cluster_history_time(&cluster->history,
  934. -1, ktime_to_us(ktime_get()));
  935. if (i < 0) {
  936. struct power_params *pwr_params =
  937. &cluster->levels[0].pwr;
  938. clusttimer_start(cluster,
  939. pwr_params->max_residency +
  940. cluster->tmr_add);
  941. goto failed;
  942. }
  943. }
  944. if (i < 0)
  945. goto failed;
  946. if (cluster_configure(cluster, i, from_idle, predicted))
  947. goto failed;
  948. cluster->stats->sleep_time = start_time;
  949. cluster_prepare(cluster->parent, &cluster->num_children_in_sync, i,
  950. from_idle, start_time);
  951. spin_unlock(&cluster->sync_lock);
  952. return;
  953. failed:
  954. spin_unlock(&cluster->sync_lock);
  955. cluster->stats->sleep_time = 0;
  956. }
  957. static void cluster_unprepare(struct lpm_cluster *cluster,
  958. const struct cpumask *cpu, int child_idx, bool from_idle,
  959. int64_t end_time)
  960. {
  961. struct lpm_cluster_level *level;
  962. bool first_cpu;
  963. int last_level, i;
  964. if (!cluster)
  965. return;
  966. if (cluster->min_child_level > child_idx)
  967. return;
  968. spin_lock(&cluster->sync_lock);
  969. last_level = cluster->default_level;
  970. first_cpu = cpumask_equal(&cluster->num_children_in_sync,
  971. &cluster->child_cpus);
  972. cpumask_andnot(&cluster->num_children_in_sync,
  973. &cluster->num_children_in_sync, cpu);
  974. for (i = 0; i < cluster->nlevels; i++) {
  975. struct lpm_cluster_level *lvl = &cluster->levels[i];
  976. if (child_idx >= lvl->min_child_level)
  977. cpumask_andnot(&lvl->num_cpu_votes,
  978. &lvl->num_cpu_votes, cpu);
  979. }
  980. if (from_idle && first_cpu &&
  981. (cluster->last_level == cluster->default_level))
  982. update_cluster_history(&cluster->history, cluster->last_level);
  983. if (!first_cpu || cluster->last_level == cluster->default_level)
  984. goto unlock_return;
  985. if (cluster->stats->sleep_time)
  986. cluster->stats->sleep_time = end_time -
  987. cluster->stats->sleep_time;
  988. lpm_stats_cluster_exit(cluster->stats, cluster->last_level, true);
  989. level = &cluster->levels[cluster->last_level];
  990. if (level->notify_rpm)
  991. if (sys_pm_ops && sys_pm_ops->exit) {
  992. spin_lock(&bc_timer_lock);
  993. sys_pm_ops->exit();
  994. spin_unlock(&bc_timer_lock);
  995. }
  996. update_debug_pc_event(CLUSTER_EXIT, cluster->last_level,
  997. cluster->num_children_in_sync.bits[0],
  998. cluster->child_cpus.bits[0], from_idle);
  999. trace_cluster_exit(cluster->cluster_name, cluster->last_level,
  1000. cluster->num_children_in_sync.bits[0],
  1001. cluster->child_cpus.bits[0], from_idle);
  1002. last_level = cluster->last_level;
  1003. cluster->last_level = cluster->default_level;
  1004. cluster_notify(cluster, &cluster->levels[last_level], false);
  1005. if (from_idle)
  1006. update_cluster_history(&cluster->history, last_level);
  1007. cluster_unprepare(cluster->parent, &cluster->child_cpus,
  1008. last_level, from_idle, end_time);
  1009. unlock_return:
  1010. spin_unlock(&cluster->sync_lock);
  1011. }
  1012. static inline void cpu_prepare(struct lpm_cpu *cpu, int cpu_index,
  1013. bool from_idle)
  1014. {
  1015. struct lpm_cpu_level *cpu_level = &cpu->levels[cpu_index];
  1016. /* Use broadcast timer for aggregating sleep mode within a cluster.
  1017. * A broadcast timer could be used in the following scenarios
  1018. * 1) The architected timer HW gets reset during certain low power
  1019. * modes and the core relies on a external(broadcast) timer to wake up
  1020. * from sleep. This information is passed through device tree.
  1021. * 2) The CPU low power mode could trigger a system low power mode.
  1022. * The low power module relies on Broadcast timer to aggregate the
  1023. * next wakeup within a cluster, in which case, CPU switches over to
  1024. * use broadcast timer.
  1025. */
  1026. if (from_idle && cpu_level->is_reset)
  1027. cpu_pm_enter();
  1028. }
  1029. static inline void cpu_unprepare(struct lpm_cpu *cpu, int cpu_index,
  1030. bool from_idle)
  1031. {
  1032. struct lpm_cpu_level *cpu_level = &cpu->levels[cpu_index];
  1033. if (from_idle && cpu_level->is_reset)
  1034. cpu_pm_exit();
  1035. }
  1036. static int get_cluster_id(struct lpm_cluster *cluster, int *aff_lvl,
  1037. bool from_idle)
  1038. {
  1039. int state_id = 0;
  1040. if (!cluster)
  1041. return 0;
  1042. spin_lock(&cluster->sync_lock);
  1043. if (!cpumask_equal(&cluster->num_children_in_sync,
  1044. &cluster->child_cpus))
  1045. goto unlock_and_return;
  1046. state_id += get_cluster_id(cluster->parent, aff_lvl, from_idle);
  1047. if (cluster->last_level != cluster->default_level) {
  1048. struct lpm_cluster_level *level
  1049. = &cluster->levels[cluster->last_level];
  1050. state_id += (level->psci_id & cluster->psci_mode_mask)
  1051. << cluster->psci_mode_shift;
  1052. /*
  1053. * We may have updated the broadcast timers, update
  1054. * the wakeup value by reading the bc timer directly.
  1055. */
  1056. if (level->notify_rpm)
  1057. if (sys_pm_ops && sys_pm_ops->update_wakeup)
  1058. sys_pm_ops->update_wakeup(from_idle);
  1059. if (cluster->psci_mode_shift)
  1060. (*aff_lvl)++;
  1061. }
  1062. unlock_and_return:
  1063. spin_unlock(&cluster->sync_lock);
  1064. return state_id;
  1065. }
  1066. static bool psci_enter_sleep(struct lpm_cpu *cpu, int idx, bool from_idle)
  1067. {
  1068. int affinity_level = 0, state_id = 0, power_state = 0;
  1069. bool success = false;
  1070. int ret = 0;
  1071. /*
  1072. * idx = 0 is the default LPM state
  1073. */
  1074. if (!idx) {
  1075. stop_critical_timings();
  1076. wfi();
  1077. start_critical_timings();
  1078. return 1;
  1079. }
  1080. if (from_idle && cpu->levels[idx].use_bc_timer) {
  1081. /*
  1082. * tick_broadcast_enter can change the affinity of the
  1083. * broadcast timer interrupt, during which interrupt will
  1084. * be disabled and enabled back. To avoid system pm ops
  1085. * doing any interrupt state save or restore in between
  1086. * this window hold the lock.
  1087. */
  1088. spin_lock(&bc_timer_lock);
  1089. ret = tick_broadcast_enter();
  1090. spin_unlock(&bc_timer_lock);
  1091. if (ret)
  1092. return success;
  1093. }
  1094. state_id = get_cluster_id(cpu->parent, &affinity_level, from_idle);
  1095. power_state = PSCI_POWER_STATE(cpu->levels[idx].is_reset);
  1096. affinity_level = PSCI_AFFINITY_LEVEL(affinity_level);
  1097. state_id += power_state + affinity_level + cpu->levels[idx].psci_id;
  1098. update_debug_pc_event(CPU_ENTER, state_id,
  1099. 0xdeaffeed, 0xdeaffeed, from_idle);
  1100. stop_critical_timings();
  1101. success = !arm_cpuidle_suspend(state_id);
  1102. start_critical_timings();
  1103. update_debug_pc_event(CPU_EXIT, state_id,
  1104. success, 0xdeaffeed, from_idle);
  1105. if (from_idle && cpu->levels[idx].use_bc_timer)
  1106. tick_broadcast_exit();
  1107. return success;
  1108. }
  1109. static int lpm_cpuidle_select(struct cpuidle_driver *drv,
  1110. struct cpuidle_device *dev)
  1111. {
  1112. struct lpm_cpu *cpu = per_cpu(cpu_lpm, dev->cpu);
  1113. if (!cpu)
  1114. return 0;
  1115. return cpu_power_select(dev, cpu);
  1116. }
  1117. static void update_history(struct cpuidle_device *dev, int idx)
  1118. {
  1119. struct lpm_history *history = &per_cpu(hist, dev->cpu);
  1120. uint32_t tmr = 0;
  1121. struct lpm_cpu *lpm_cpu = per_cpu(cpu_lpm, dev->cpu);
  1122. if (!lpm_prediction || !lpm_cpu->lpm_prediction)
  1123. return;
  1124. if (history->htmr_wkup) {
  1125. if (!history->hptr)
  1126. history->hptr = MAXSAMPLES-1;
  1127. else
  1128. history->hptr--;
  1129. history->resi[history->hptr] += dev->last_residency;
  1130. history->htmr_wkup = 0;
  1131. tmr = 1;
  1132. } else
  1133. history->resi[history->hptr] = dev->last_residency;
  1134. history->mode[history->hptr] = idx;
  1135. trace_cpu_pred_hist(history->mode[history->hptr],
  1136. history->resi[history->hptr], history->hptr, tmr);
  1137. if (history->nsamp < MAXSAMPLES)
  1138. history->nsamp++;
  1139. (history->hptr)++;
  1140. if (history->hptr >= MAXSAMPLES)
  1141. history->hptr = 0;
  1142. }
  1143. static int lpm_cpuidle_enter(struct cpuidle_device *dev,
  1144. struct cpuidle_driver *drv, int idx)
  1145. {
  1146. struct lpm_cpu *cpu = per_cpu(cpu_lpm, dev->cpu);
  1147. bool success = false;
  1148. const struct cpumask *cpumask = get_cpu_mask(dev->cpu);
  1149. ktime_t start = ktime_get();
  1150. uint64_t start_time = ktime_to_ns(start), end_time;
  1151. cpu_prepare(cpu, idx, true);
  1152. cluster_prepare(cpu->parent, cpumask, idx, true, start_time);
  1153. trace_cpu_idle_enter(idx);
  1154. lpm_stats_cpu_enter(idx, start_time);
  1155. if (need_resched())
  1156. goto exit;
  1157. success = psci_enter_sleep(cpu, idx, true);
  1158. exit:
  1159. end_time = ktime_to_ns(ktime_get());
  1160. lpm_stats_cpu_exit(idx, end_time, success);
  1161. cluster_unprepare(cpu->parent, cpumask, idx, true, end_time);
  1162. cpu_unprepare(cpu, idx, true);
  1163. dev->last_residency = ktime_us_delta(ktime_get(), start);
  1164. update_history(dev, idx);
  1165. trace_cpu_idle_exit(idx, success);
  1166. if (lpm_prediction && cpu->lpm_prediction) {
  1167. histtimer_cancel();
  1168. clusttimer_cancel();
  1169. }
  1170. local_irq_enable();
  1171. return idx;
  1172. }
  1173. static void lpm_cpuidle_freeze(struct cpuidle_device *dev,
  1174. struct cpuidle_driver *drv, int idx)
  1175. {
  1176. struct lpm_cpu *cpu = per_cpu(cpu_lpm, dev->cpu);
  1177. const struct cpumask *cpumask = get_cpu_mask(dev->cpu);
  1178. for (; idx >= 0; idx--) {
  1179. if (lpm_cpu_mode_allow(dev->cpu, idx, false))
  1180. break;
  1181. }
  1182. if (idx < 0) {
  1183. pr_err("Failed suspend\n");
  1184. return;
  1185. }
  1186. cpu_prepare(cpu, idx, true);
  1187. cluster_prepare(cpu->parent, cpumask, idx, false, 0);
  1188. psci_enter_sleep(cpu, idx, false);
  1189. cluster_unprepare(cpu->parent, cpumask, idx, false, 0);
  1190. cpu_unprepare(cpu, idx, true);
  1191. }
  1192. #ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS
  1193. static int cpuidle_register_cpu(struct cpuidle_driver *drv,
  1194. struct cpumask *mask)
  1195. {
  1196. struct cpuidle_device *device;
  1197. int cpu, ret;
  1198. if (!mask || !drv)
  1199. return -EINVAL;
  1200. drv->cpumask = mask;
  1201. ret = cpuidle_register_driver(drv);
  1202. if (ret) {
  1203. pr_err("Failed to register cpuidle driver %d\n", ret);
  1204. goto failed_driver_register;
  1205. }
  1206. for_each_cpu(cpu, mask) {
  1207. device = &per_cpu(cpuidle_dev, cpu);
  1208. device->cpu = cpu;
  1209. ret = cpuidle_register_device(device);
  1210. if (ret) {
  1211. pr_err("Failed to register cpuidle driver for cpu:%u\n",
  1212. cpu);
  1213. goto failed_driver_register;
  1214. }
  1215. }
  1216. return ret;
  1217. failed_driver_register:
  1218. for_each_cpu(cpu, mask)
  1219. cpuidle_unregister_driver(drv);
  1220. return ret;
  1221. }
  1222. #else
  1223. static int cpuidle_register_cpu(struct cpuidle_driver *drv,
  1224. struct cpumask *mask)
  1225. {
  1226. return cpuidle_register(drv, NULL);
  1227. }
  1228. #endif
  1229. static struct cpuidle_governor lpm_governor = {
  1230. .name = "qcom",
  1231. .rating = 30,
  1232. .select = lpm_cpuidle_select,
  1233. .owner = THIS_MODULE,
  1234. };
  1235. static int cluster_cpuidle_register(struct lpm_cluster *cl)
  1236. {
  1237. int i = 0, ret = 0;
  1238. unsigned int cpu;
  1239. struct lpm_cluster *p = NULL;
  1240. struct lpm_cpu *lpm_cpu;
  1241. if (list_empty(&cl->cpu)) {
  1242. struct lpm_cluster *n;
  1243. list_for_each_entry(n, &cl->child, list) {
  1244. ret = cluster_cpuidle_register(n);
  1245. if (ret)
  1246. break;
  1247. }
  1248. return ret;
  1249. }
  1250. list_for_each_entry(lpm_cpu, &cl->cpu, list) {
  1251. lpm_cpu->drv = kcalloc(1, sizeof(*lpm_cpu->drv), GFP_KERNEL);
  1252. if (!lpm_cpu->drv)
  1253. return -ENOMEM;
  1254. lpm_cpu->drv->name = "msm_idle";
  1255. for (i = 0; i < lpm_cpu->nlevels; i++) {
  1256. struct cpuidle_state *st = &lpm_cpu->drv->states[i];
  1257. struct lpm_cpu_level *cpu_level = &lpm_cpu->levels[i];
  1258. snprintf(st->name, CPUIDLE_NAME_LEN, "C%u\n", i);
  1259. snprintf(st->desc, CPUIDLE_DESC_LEN, "%s",
  1260. cpu_level->name);
  1261. st->flags = 0;
  1262. st->exit_latency = cpu_level->pwr.latency_us;
  1263. st->power_usage = cpu_level->pwr.ss_power;
  1264. st->target_residency = 0;
  1265. st->enter = lpm_cpuidle_enter;
  1266. if (i == lpm_cpu->nlevels - 1)
  1267. st->enter_freeze = lpm_cpuidle_freeze;
  1268. }
  1269. lpm_cpu->drv->state_count = lpm_cpu->nlevels;
  1270. lpm_cpu->drv->safe_state_index = 0;
  1271. for_each_cpu(cpu, &lpm_cpu->related_cpus)
  1272. per_cpu(cpu_lpm, cpu) = lpm_cpu;
  1273. for_each_possible_cpu(cpu) {
  1274. if (cpu_online(cpu))
  1275. continue;
  1276. if (per_cpu(cpu_lpm, cpu))
  1277. p = per_cpu(cpu_lpm, cpu)->parent;
  1278. while (p) {
  1279. int j;
  1280. spin_lock(&p->sync_lock);
  1281. cpumask_set_cpu(cpu, &p->num_children_in_sync);
  1282. for (j = 0; j < p->nlevels; j++)
  1283. cpumask_copy(
  1284. &p->levels[j].num_cpu_votes,
  1285. &p->num_children_in_sync);
  1286. spin_unlock(&p->sync_lock);
  1287. p = p->parent;
  1288. }
  1289. }
  1290. ret = cpuidle_register_cpu(lpm_cpu->drv,
  1291. &lpm_cpu->related_cpus);
  1292. if (ret) {
  1293. kfree(lpm_cpu->drv);
  1294. return -ENOMEM;
  1295. }
  1296. }
  1297. return 0;
  1298. }
  1299. /**
  1300. * init_lpm - initializes the governor
  1301. */
  1302. static int __init init_lpm(void)
  1303. {
  1304. return cpuidle_register_governor(&lpm_governor);
  1305. }
  1306. postcore_initcall(init_lpm);
  1307. static void register_cpu_lpm_stats(struct lpm_cpu *cpu,
  1308. struct lpm_cluster *parent)
  1309. {
  1310. const char **level_name;
  1311. int i;
  1312. level_name = kcalloc(cpu->nlevels, sizeof(*level_name), GFP_KERNEL);
  1313. if (!level_name)
  1314. return;
  1315. for (i = 0; i < cpu->nlevels; i++)
  1316. level_name[i] = cpu->levels[i].name;
  1317. lpm_stats_config_level("cpu", level_name, cpu->nlevels,
  1318. parent->stats, &cpu->related_cpus);
  1319. kfree(level_name);
  1320. }
  1321. static void register_cluster_lpm_stats(struct lpm_cluster *cl,
  1322. struct lpm_cluster *parent)
  1323. {
  1324. const char **level_name;
  1325. struct lpm_cluster *child;
  1326. struct lpm_cpu *cpu;
  1327. int i;
  1328. if (!cl)
  1329. return;
  1330. level_name = kcalloc(cl->nlevels, sizeof(*level_name), GFP_KERNEL);
  1331. if (!level_name)
  1332. return;
  1333. for (i = 0; i < cl->nlevels; i++)
  1334. level_name[i] = cl->levels[i].level_name;
  1335. cl->stats = lpm_stats_config_level(cl->cluster_name, level_name,
  1336. cl->nlevels, parent ? parent->stats : NULL, NULL);
  1337. kfree(level_name);
  1338. list_for_each_entry(cpu, &cl->cpu, list) {
  1339. pr_err("%s()\n", __func__);
  1340. register_cpu_lpm_stats(cpu, cl);
  1341. }
  1342. if (!list_empty(&cl->cpu))
  1343. return;
  1344. list_for_each_entry(child, &cl->child, list)
  1345. register_cluster_lpm_stats(child, cl);
  1346. }
  1347. static int lpm_suspend_prepare(void)
  1348. {
  1349. suspend_in_progress = true;
  1350. lpm_stats_suspend_enter();
  1351. return 0;
  1352. }
  1353. static void lpm_suspend_wake(void)
  1354. {
  1355. suspend_in_progress = false;
  1356. lpm_stats_suspend_exit();
  1357. }
  1358. static int lpm_suspend_enter(suspend_state_t state)
  1359. {
  1360. int cpu = raw_smp_processor_id();
  1361. struct lpm_cpu *lpm_cpu = per_cpu(cpu_lpm, cpu);
  1362. struct lpm_cluster *cluster = lpm_cpu->parent;
  1363. const struct cpumask *cpumask = get_cpu_mask(cpu);
  1364. int idx;
  1365. for (idx = lpm_cpu->nlevels - 1; idx >= 0; idx--) {
  1366. if (lpm_cpu_mode_allow(cpu, idx, false))
  1367. break;
  1368. }
  1369. if (idx < 0) {
  1370. pr_err("Failed suspend\n");
  1371. return 0;
  1372. }
  1373. cpu_prepare(lpm_cpu, idx, false);
  1374. cluster_prepare(cluster, cpumask, idx, false, 0);
  1375. psci_enter_sleep(lpm_cpu, idx, false);
  1376. cluster_unprepare(cluster, cpumask, idx, false, 0);
  1377. cpu_unprepare(lpm_cpu, idx, false);
  1378. return 0;
  1379. }
  1380. static const struct platform_suspend_ops lpm_suspend_ops = {
  1381. .enter = lpm_suspend_enter,
  1382. .valid = suspend_valid_only_mem,
  1383. .prepare_late = lpm_suspend_prepare,
  1384. .wake = lpm_suspend_wake,
  1385. };
  1386. static const struct platform_freeze_ops lpm_freeze_ops = {
  1387. .prepare = lpm_suspend_prepare,
  1388. .restore = lpm_suspend_wake,
  1389. };
  1390. static int lpm_probe(struct platform_device *pdev)
  1391. {
  1392. int ret;
  1393. int size;
  1394. unsigned int cpu;
  1395. struct hrtimer *cpu_histtimer;
  1396. struct kobject *module_kobj = NULL;
  1397. struct md_region md_entry;
  1398. get_online_cpus();
  1399. lpm_root_node = lpm_of_parse_cluster(pdev);
  1400. if (IS_ERR_OR_NULL(lpm_root_node)) {
  1401. pr_err("Failed to probe low power modes\n");
  1402. put_online_cpus();
  1403. return PTR_ERR(lpm_root_node);
  1404. }
  1405. if (print_parsed_dt)
  1406. cluster_dt_walkthrough(lpm_root_node);
  1407. /*
  1408. * Register hotplug notifier before broadcast time to ensure there
  1409. * to prevent race where a broadcast timer might not be setup on for a
  1410. * core. BUG in existing code but no known issues possibly because of
  1411. * how late lpm_levels gets initialized.
  1412. */
  1413. suspend_set_ops(&lpm_suspend_ops);
  1414. freeze_set_ops(&lpm_freeze_ops);
  1415. hrtimer_init(&lpm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1416. for_each_possible_cpu(cpu) {
  1417. cpu_histtimer = &per_cpu(histtimer, cpu);
  1418. hrtimer_init(cpu_histtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1419. }
  1420. cluster_timer_init(lpm_root_node);
  1421. size = num_dbg_elements * sizeof(struct lpm_debug);
  1422. lpm_debug = dma_alloc_coherent(&pdev->dev, size,
  1423. &lpm_debug_phys, GFP_KERNEL);
  1424. register_cluster_lpm_stats(lpm_root_node, NULL);
  1425. ret = cluster_cpuidle_register(lpm_root_node);
  1426. put_online_cpus();
  1427. if (ret) {
  1428. pr_err("Failed to register with cpuidle framework\n");
  1429. goto failed;
  1430. }
  1431. ret = cpuhp_setup_state(CPUHP_AP_QCOM_SLEEP_STARTING,
  1432. "AP_QCOM_SLEEP_STARTING",
  1433. lpm_starting_cpu, lpm_dying_cpu);
  1434. if (ret)
  1435. goto failed;
  1436. module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
  1437. if (!module_kobj) {
  1438. pr_err("Cannot find kobject for module %s\n", KBUILD_MODNAME);
  1439. ret = -ENOENT;
  1440. goto failed;
  1441. }
  1442. ret = create_cluster_lvl_nodes(lpm_root_node, module_kobj);
  1443. if (ret) {
  1444. pr_err("Failed to create cluster level nodes\n");
  1445. goto failed;
  1446. }
  1447. /* Add lpm_debug to Minidump*/
  1448. strlcpy(md_entry.name, "KLPMDEBUG", sizeof(md_entry.name));
  1449. md_entry.virt_addr = (uintptr_t)lpm_debug;
  1450. md_entry.phys_addr = lpm_debug_phys;
  1451. md_entry.size = size;
  1452. if (msm_minidump_add_region(&md_entry))
  1453. pr_info("Failed to add lpm_debug in Minidump\n");
  1454. return 0;
  1455. failed:
  1456. free_cluster_node(lpm_root_node);
  1457. lpm_root_node = NULL;
  1458. return ret;
  1459. }
  1460. static const struct of_device_id lpm_mtch_tbl[] = {
  1461. {.compatible = "qcom,lpm-levels"},
  1462. {},
  1463. };
  1464. static struct platform_driver lpm_driver = {
  1465. .probe = lpm_probe,
  1466. .driver = {
  1467. .name = "lpm-levels",
  1468. .owner = THIS_MODULE,
  1469. .of_match_table = lpm_mtch_tbl,
  1470. },
  1471. };
  1472. static int __init lpm_levels_module_init(void)
  1473. {
  1474. int rc;
  1475. #ifdef CONFIG_ARM
  1476. int cpu;
  1477. for_each_possible_cpu(cpu) {
  1478. rc = arm_cpuidle_init(cpu);
  1479. if (rc) {
  1480. pr_err("CPU%d ARM CPUidle init failed (%d)\n", cpu, rc);
  1481. return rc;
  1482. }
  1483. }
  1484. #endif
  1485. rc = platform_driver_register(&lpm_driver);
  1486. if (rc)
  1487. pr_info("Error registering %s rc=%d\n", lpm_driver.driver.name,
  1488. rc);
  1489. return rc;
  1490. }
  1491. late_initcall(lpm_levels_module_init);