eas_plus.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973
  1. /*
  2. * Copyright (C) 2016 MediaTek Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See http://www.gnu.org/licenses/gpl-2.0.html for more details.
  12. */
  13. #include <linux/stop_machine.h>
  14. static inline unsigned long task_util(struct task_struct *p);
  15. static int select_max_spare_capacity(struct task_struct *p, int target);
  16. int cpu_eff_tp = 1024;
  17. unsigned long long big_cpu_eff_tp = 1024;
  18. #ifndef cpu_isolated
  19. #define cpu_isolated(cpu) 0
  20. #endif
  21. #if defined(CONFIG_MACH_MT6763) || defined(CONFIG_MACH_MT6758)
  22. /* cpu7 is L+ */
  23. int l_plus_cpu = 7;
  24. #else
  25. int l_plus_cpu = -1;
  26. #endif
  27. #ifdef CONFIG_MTK_SCHED_EAS_POWER_SUPPORT
  28. #if defined(CONFIG_MACH_MT6763) || defined(CONFIG_MACH_MT6758)
  29. /* MT6763: 2 gears. cluster 0 & 1 is buck shared. */
  30. static int share_buck[3] = {1, 0, 2};
  31. #elif defined(CONFIG_MACH_MT6799)
  32. /* MT6799: 3 gears. cluster 0 & 2 is buck shared. */
  33. static int share_buck[3] = {2, 1, 0};
  34. #elif defined(CONFIG_MACH_MT6765) || defined(CONFIG_MACH_MT6762)
  35. static int share_buck[3] = {1, 0, 2};
  36. #elif defined(CONFIG_MACH_MT6779)
  37. static int share_buck[2] = {2, 1};
  38. #define ARM_V8_2
  39. int l_plus_cpu = -1;
  40. #elif defined(CONFIG_MACH_MT6893) || \
  41. (defined(CONFIG_MACH_MT6885) && defined(CONFIG_MTK_SCHED_MULTI_GEARS))
  42. static int share_buck[3] = {0, 2, 1};
  43. #else
  44. /* no buck shared */
  45. static int share_buck[3] = {0, 1, 2};
  46. #endif
  47. #endif
  48. #define CCI_ID (arch_get_nr_clusters())
  49. static void
  50. update_system_overutilized(struct lb_env *env)
  51. {
  52. unsigned long group_util;
  53. bool intra_overutil = false;
  54. unsigned long min_capacity;
  55. struct sched_group *group = env->sd->groups;
  56. int this_cpu;
  57. int min_cap_orig_cpu;
  58. bool overutilized = sd_overutilized(env->sd);
  59. int i;
  60. if (!sched_feat(SCHED_MTK_EAS))
  61. return;
  62. this_cpu = smp_processor_id();
  63. min_cap_orig_cpu = cpu_rq(this_cpu)->rd->min_cap_orig_cpu;
  64. if (min_cap_orig_cpu > -1)
  65. min_capacity = capacity_orig_of(min_cap_orig_cpu);
  66. else
  67. return;
  68. do {
  69. group_util = 0;
  70. for_each_cpu_and(i, sched_group_span(group), env->cpus) {
  71. if (cpu_isolated(i))
  72. continue;
  73. group_util += cpu_util(i);
  74. if (cpu_overutilized(i)) {
  75. if (capacity_orig_of(i) == min_capacity) {
  76. intra_overutil = true;
  77. break;
  78. }
  79. }
  80. }
  81. /*
  82. * A capacity base hint for over-utilization.
  83. * Not to trigger system overutiled if heavy tasks
  84. * in Big.cluster, so
  85. * add the free room(20%) of Big.cluster is impacted which means
  86. * system-wide over-utilization,
  87. * that considers whole cluster not single cpu
  88. */
  89. if (group->group_weight > 1 && (group->sgc->capacity * 1024 <
  90. group_util * capacity_margin)) {
  91. intra_overutil = true;
  92. break;
  93. }
  94. group = group->next;
  95. } while (group != env->sd->groups && !intra_overutil);
  96. if (overutilized != intra_overutil) {
  97. if (intra_overutil == true)
  98. set_sd_overutilized(env->sd);
  99. else
  100. clear_sd_overutilized(env->sd);
  101. }
  102. }
  103. bool is_intra_domain(int prev, int target)
  104. {
  105. #ifdef CONFIG_ARM64
  106. return (cpu_topology[prev].cluster_id ==
  107. cpu_topology[target].cluster_id);
  108. #else
  109. return (cpu_topology[prev].socket_id ==
  110. cpu_topology[target].socket_id);
  111. #endif
  112. }
  113. static int
  114. ___select_idle_sibling(struct task_struct *p, int prev_cpu, int new_cpu)
  115. {
  116. if (sched_feat(SCHED_MTK_EAS)) {
  117. #ifdef CONFIG_SCHED_TUNE
  118. bool prefer_idle = schedtune_prefer_idle(p) > 0;
  119. #else
  120. bool prefer_idle = true;
  121. #endif
  122. int idle_cpu;
  123. idle_cpu = find_best_idle_cpu(p, prefer_idle);
  124. if (idle_cpu >= 0)
  125. new_cpu = idle_cpu;
  126. else
  127. new_cpu = select_max_spare_capacity(p, new_cpu);
  128. } else
  129. new_cpu = select_idle_sibling(p, prev_cpu, new_cpu);
  130. return new_cpu;
  131. }
  132. /* To find a CPU with max spare capacity in the same cluster with target */
  133. static
  134. int select_max_spare_capacity(struct task_struct *p, int target)
  135. {
  136. unsigned long int max_spare_capacity = 0;
  137. int max_spare_cpu = -1;
  138. struct cpumask cls_cpus;
  139. int cid = arch_get_cluster_id(target); /* cid of target CPU */
  140. int cpu = task_cpu(p);
  141. struct cpumask *tsk_cpus_allow = &p->cpus_allowed;
  142. /* If the prevous cpu is cache affine and idle, choose it first. */
  143. if (cpu != l_plus_cpu && cpu != target &&
  144. cpus_share_cache(cpu, target) &&
  145. idle_cpu(cpu) && !cpu_isolated(cpu))
  146. return cpu;
  147. arch_get_cluster_cpus(&cls_cpus, cid);
  148. /* Otherwise, find a CPU with max spare-capacity in cluster */
  149. for_each_cpu_and(cpu, tsk_cpus_allow, &cls_cpus) {
  150. unsigned long int new_usage;
  151. unsigned long int spare_cap;
  152. if (!cpu_online(cpu))
  153. continue;
  154. if (cpu_isolated(cpu))
  155. continue;
  156. #ifdef CONFIG_MTK_SCHED_INTEROP
  157. if (cpu_rq(cpu)->rt.rt_nr_running &&
  158. likely(!is_rt_throttle(cpu)))
  159. continue;
  160. #endif
  161. #ifdef CONFIG_SCHED_WALT
  162. if (walt_cpu_high_irqload(cpu))
  163. continue;
  164. #endif
  165. if (idle_cpu(cpu))
  166. return cpu;
  167. new_usage = cpu_util(cpu) + task_util(p);
  168. if (new_usage >= capacity_of(cpu))
  169. spare_cap = 0;
  170. else /* consider RT/IRQ capacity reduction */
  171. spare_cap = (capacity_of(cpu) - new_usage);
  172. /* update CPU with max spare capacity */
  173. if ((long int)spare_cap > (long int)max_spare_capacity) {
  174. max_spare_cpu = cpu;
  175. max_spare_capacity = spare_cap;
  176. }
  177. }
  178. /* if max_spare_cpu exist, choose it. */
  179. if (max_spare_cpu > -1)
  180. return max_spare_cpu;
  181. else
  182. return task_cpu(p);
  183. }
  184. /*
  185. * @p: the task want to be located at.
  186. *
  187. * Return:
  188. *
  189. * cpu id or
  190. * -1 if target CPU is not found
  191. */
  192. int find_best_idle_cpu(struct task_struct *p, bool prefer_idle)
  193. {
  194. int i;
  195. int best_idle_cpu = -1;
  196. struct cpumask *tsk_cpus_allow = &p->cpus_allowed;
  197. struct hmp_domain *domain;
  198. int domain_order = 0;
  199. int prefer_big = prefer_idle && (task_util(p) > stune_task_threshold);
  200. for_each_hmp_domain_L_first(domain) {
  201. for_each_cpu(i, &domain->possible_cpus) {
  202. /* tsk with prefer idle to find bigger idle cpu */
  203. if (!cpu_online(i) || cpu_isolated(i) ||
  204. !cpumask_test_cpu(i, tsk_cpus_allow))
  205. continue;
  206. #ifdef CONFIG_MTK_SCHED_INTEROP
  207. if (cpu_rq(i)->rt.rt_nr_running &&
  208. likely(!is_rt_throttle(i)))
  209. continue;
  210. #endif
  211. /* favoring tasks that prefer idle cpus
  212. * to improve latency.
  213. */
  214. if (idle_cpu(i)) {
  215. best_idle_cpu = i;
  216. if (!prefer_big) {
  217. goto find_idle_cpu;
  218. } else {
  219. #ifdef CONFIG_MTK_SCHED_BL_FIRST
  220. if (domain_order == 1)
  221. goto find_idle_cpu;
  222. #endif
  223. }
  224. }
  225. }
  226. domain_order++;
  227. }
  228. find_idle_cpu:
  229. return best_idle_cpu;
  230. }
  231. static int init_cpu_info(void)
  232. {
  233. int i;
  234. for (i = 0; i < nr_cpu_ids; i++) {
  235. unsigned long capacity = SCHED_CAPACITY_SCALE;
  236. if (cpu_core_energy(i)) {
  237. int idx = cpu_core_energy(i)->nr_cap_states - 1;
  238. capacity = cpu_core_energy(i)->cap_states[idx].cap;
  239. }
  240. }
  241. return 0;
  242. }
  243. late_initcall_sync(init_cpu_info)
  244. #ifdef CONFIG_MTK_UNIFY_POWER
  245. void set_sched_turn_point_cap(void)
  246. {
  247. int turn_point_idx;
  248. struct hmp_domain *domain;
  249. int cpu;
  250. const struct sched_group_energy *sge_core;
  251. domain = list_entry(hmp_domains.prev, struct hmp_domain, hmp_domains);
  252. cpu = cpumask_first(&domain->possible_cpus);
  253. sge_core = cpu_core_energy(cpu);
  254. turn_point_idx = max(upower_get_turn_point() - 1, 0);
  255. cpu_eff_tp = sge_core->cap_states[turn_point_idx].cap;
  256. }
  257. #else
  258. void set_sched_turn_point_cap(void)
  259. {
  260. return;
  261. }
  262. #endif
  263. #if defined(CONFIG_SCHED_HMP) || defined(CONFIG_MTK_IDLE_BALANCE_ENHANCEMENT)
  264. /*
  265. * Heterogenous Multi-Processor (HMP) Global Load Balance
  266. */
  267. static DEFINE_SPINLOCK(hmp_force_migration);
  268. /*
  269. * For debugging purpose,
  270. * to depart functions of cpu_stop to make call_stack clear.
  271. */
  272. static int hmp_idle_pull_cpu_stop(void *data)
  273. {
  274. int ret;
  275. struct task_struct *p = ((struct rq *)data)->migrate_task;
  276. ret = active_load_balance_cpu_stop(data);
  277. put_task_struct(p);
  278. return ret;
  279. }
  280. static int
  281. migrate_running_task(int this_cpu, struct task_struct *p, struct rq *target)
  282. {
  283. unsigned long flags;
  284. unsigned int force = 0;
  285. /* now we have a candidate */
  286. raw_spin_lock_irqsave(&target->lock, flags);
  287. if (!target->active_balance &&
  288. (task_rq(p) == target) && !cpu_park(cpu_of(target)) &&
  289. p->state != TASK_DEAD) {
  290. get_task_struct(p);
  291. target->push_cpu = this_cpu;
  292. target->migrate_task = p;
  293. trace_sched_hmp_migrate(p, target->push_cpu, MIGR_IDLE_RUNNING);
  294. #ifdef CONFIG_SCHED_HMP
  295. hmp_next_up_delay(&p->se, target->push_cpu);
  296. #endif
  297. target->active_balance = MIGR_IDLE_RUNNING; /* idle pull */
  298. force = 1;
  299. }
  300. raw_spin_unlock_irqrestore(&target->lock, flags);
  301. if (force) {
  302. if (!stop_one_cpu_nowait(cpu_of(target),
  303. hmp_idle_pull_cpu_stop,
  304. target, &target->active_balance_work)) {
  305. put_task_struct(p); /* out of rq->lock */
  306. raw_spin_lock_irqsave(&target->lock, flags);
  307. target->active_balance = 0;
  308. target->migrate_task = NULL;
  309. force = 0;
  310. raw_spin_unlock_irqrestore(&target->lock, flags);
  311. }
  312. }
  313. return force;
  314. }
  315. #endif
  316. unsigned long cluster_max_capacity(void)
  317. {
  318. struct hmp_domain *domain;
  319. unsigned int max_capacity = 0;
  320. for_each_hmp_domain_L_first(domain) {
  321. int cpu;
  322. unsigned long capacity;
  323. cpu = cpumask_first(&domain->possible_cpus);
  324. capacity = capacity_of(cpu);
  325. if (capacity > max_capacity)
  326. max_capacity = capacity;
  327. }
  328. return max_capacity;
  329. }
  330. inline unsigned long task_uclamped_min_w_ceiling(struct task_struct *p)
  331. {
  332. unsigned long max_capacity = cluster_max_capacity();
  333. return min_t(unsigned int, uclamp_task_effective_util(p, UCLAMP_MIN),
  334. max_capacity);
  335. }
  336. /* Calculte util with DVFS margin */
  337. inline unsigned int freq_util(unsigned long util)
  338. {
  339. return util * capacity_margin / SCHED_CAPACITY_SCALE;
  340. }
  341. #ifdef CONFIG_MTK_IDLE_BALANCE_ENHANCEMENT
  342. bool idle_lb_enhance(struct task_struct *p, int cpu)
  343. {
  344. int target_capacity = capacity_orig_of(cpu);
  345. if (schedtune_prefer_idle(p))
  346. return 1;
  347. if (uclamp_task_effective_util(p, UCLAMP_MIN) > target_capacity)
  348. return 1;
  349. return 0;
  350. }
  351. /* must hold runqueue lock for queue se is currently on */
  352. static const int idle_prefer_max_tasks = 5;
  353. static struct sched_entity
  354. *get_idle_prefer_task(int cpu, int target_cpu, int check_min_cap,
  355. struct task_struct **backup_task, int *backup_cpu)
  356. {
  357. int num_tasks = idle_prefer_max_tasks;
  358. const struct cpumask *hmp_target_mask = NULL;
  359. int src_capacity;
  360. unsigned int util_min;
  361. struct cfs_rq *cfs_rq;
  362. struct sched_entity *se;
  363. if (target_cpu >= 0)
  364. hmp_target_mask = cpumask_of(target_cpu);
  365. else
  366. return NULL;
  367. /* The currently running task is not on the runqueue
  368. * a. idle prefer
  369. * b. task_capacity > belonged CPU
  370. */
  371. src_capacity = capacity_orig_of(cpu);
  372. cfs_rq = &cpu_rq(cpu)->cfs;
  373. se = __pick_first_entity(cfs_rq);
  374. while (num_tasks && se) {
  375. if (entity_is_task(se) &&
  376. cpumask_intersects(hmp_target_mask,
  377. &(task_of(se)->cpus_allowed))) {
  378. struct task_struct *p;
  379. p = task_of(se);
  380. util_min = uclamp_task_effective_util(p, UCLAMP_MIN);
  381. #ifdef CONFIG_MTK_SCHED_BOOST
  382. if (!task_prefer_match_on_cpu(p, cpu, target_cpu))
  383. return se;
  384. #endif
  385. if (check_min_cap && util_min >= src_capacity)
  386. return se;
  387. if (schedtune_prefer_idle(task_of(se)) &&
  388. cpu_rq(cpu)->nr_running > 1) {
  389. if (!check_min_cap)
  390. return se;
  391. if (backup_task && !*backup_task) {
  392. *backup_cpu = cpu;
  393. /* get task and selection inside
  394. * rq lock
  395. */
  396. *backup_task = task_of(se);
  397. get_task_struct(*backup_task);
  398. }
  399. }
  400. }
  401. se = __pick_next_entity(se);
  402. num_tasks--;
  403. }
  404. return NULL;
  405. }
  406. static void
  407. hmp_slowest_idle_prefer_pull(int this_cpu, struct task_struct **p,
  408. struct rq **target)
  409. {
  410. int cpu, backup_cpu;
  411. struct sched_entity *se = NULL;
  412. struct task_struct *backup_task = NULL;
  413. struct hmp_domain *domain;
  414. struct list_head *pos;
  415. int selected = 0;
  416. struct rq *rq;
  417. unsigned long flags;
  418. int check_min_cap;
  419. /* 1. select a runnable task
  420. * idle prefer
  421. *
  422. * order: fast to slow hmp domain
  423. */
  424. check_min_cap = 0;
  425. list_for_each(pos, &hmp_domains) {
  426. domain = list_entry(pos, struct hmp_domain, hmp_domains);
  427. for_each_cpu(cpu, &domain->cpus) {
  428. if (cpu == this_cpu)
  429. continue;
  430. rq = cpu_rq(cpu);
  431. raw_spin_lock_irqsave(&rq->lock, flags);
  432. se = get_idle_prefer_task(cpu, this_cpu,
  433. check_min_cap, &backup_task, &backup_cpu);
  434. if (se && entity_is_task(se) &&
  435. cpumask_test_cpu(this_cpu,
  436. &(task_of(se))->cpus_allowed)) {
  437. selected = 1;
  438. /* get task and selection inside rq lock */
  439. *p = task_of(se);
  440. get_task_struct(*p);
  441. *target = rq;
  442. }
  443. raw_spin_unlock_irqrestore(&rq->lock, flags);
  444. if (selected) {
  445. /* To put task out of rq lock */
  446. if (backup_task)
  447. put_task_struct(backup_task);
  448. return;
  449. }
  450. }
  451. }
  452. if (backup_task) {
  453. *target = cpu_rq(backup_cpu);
  454. return;
  455. }
  456. }
  457. DECLARE_PER_CPU(struct hmp_domain *, hmp_cpu_domain);
  458. static void
  459. hmp_fastest_idle_prefer_pull(int this_cpu, struct task_struct **p,
  460. struct rq **target)
  461. {
  462. int cpu, backup_cpu;
  463. struct sched_entity *se = NULL;
  464. struct task_struct *backup_task = NULL;
  465. struct hmp_domain *hmp_domain = NULL, *domain;
  466. struct list_head *pos;
  467. int selected = 0;
  468. struct rq *rq;
  469. unsigned long flags;
  470. int target_capacity;
  471. int check_min_cap;
  472. int turning;
  473. hmp_domain = per_cpu(hmp_cpu_domain, this_cpu);
  474. /* 1. select a runnable task
  475. *
  476. * first candidate:
  477. * capacity_min in slow domain
  478. *
  479. * order: target->next to slow hmp domain
  480. */
  481. check_min_cap = 1;
  482. list_for_each(pos, &hmp_domain->hmp_domains) {
  483. domain = list_entry(pos, struct hmp_domain, hmp_domains);
  484. for_each_cpu(cpu, &domain->cpus) {
  485. if (cpu == this_cpu)
  486. continue;
  487. rq = cpu_rq(cpu);
  488. raw_spin_lock_irqsave(&rq->lock, flags);
  489. se = get_idle_prefer_task(cpu, this_cpu,
  490. check_min_cap, &backup_task, &backup_cpu);
  491. if (se && entity_is_task(se) &&
  492. cpumask_test_cpu(this_cpu,
  493. &(task_of(se))->cpus_allowed)) {
  494. selected = 1;
  495. /* get task and selection inside rq lock */
  496. *p = task_of(se);
  497. get_task_struct(*p);
  498. *target = rq;
  499. }
  500. raw_spin_unlock_irqrestore(&rq->lock, flags);
  501. if (selected) {
  502. /* To put task out of rq lock */
  503. if (backup_task)
  504. put_task_struct(backup_task);
  505. return;
  506. }
  507. }
  508. if (list_is_last(pos, &hmp_domains))
  509. break;
  510. }
  511. /* backup candidate:
  512. * idle prefer
  513. *
  514. * order: fastest to target hmp domain
  515. */
  516. check_min_cap = 0;
  517. list_for_each(pos, &hmp_domains) {
  518. domain = list_entry(pos, struct hmp_domain, hmp_domains);
  519. for_each_cpu(cpu, &domain->cpus) {
  520. if (cpu == this_cpu)
  521. continue;
  522. rq = cpu_rq(cpu);
  523. raw_spin_lock_irqsave(&rq->lock, flags);
  524. se = get_idle_prefer_task(cpu, this_cpu,
  525. check_min_cap, &backup_task, &backup_cpu);
  526. if (se && entity_is_task(se) &&
  527. cpumask_test_cpu(this_cpu,
  528. &(task_of(se)->cpus_allowed))) {
  529. selected = 1;
  530. /* get task and selection inside rq lock */
  531. *p = task_of(se);
  532. get_task_struct(*p);
  533. *target = rq;
  534. }
  535. raw_spin_unlock_irqrestore(&rq->lock, flags);
  536. if (selected) {
  537. /* To put task out of rq lock */
  538. if (backup_task)
  539. put_task_struct(backup_task);
  540. return;
  541. }
  542. }
  543. if (cpumask_test_cpu(this_cpu, &domain->cpus))
  544. break;
  545. }
  546. if (backup_task) {
  547. *p = backup_task;
  548. *target = cpu_rq(backup_cpu);
  549. return;
  550. }
  551. /* 2. select a running task
  552. * order: target->next to slow hmp domain
  553. * 3. turning = true, pick a runnable task from slower domain
  554. */
  555. turning = check_freq_turning();
  556. list_for_each(pos, &hmp_domain->hmp_domains) {
  557. domain = list_entry(pos, struct hmp_domain, hmp_domains);
  558. for_each_cpu(cpu, &domain->cpus) {
  559. if (cpu == this_cpu)
  560. continue;
  561. rq = cpu_rq(cpu);
  562. raw_spin_lock_irqsave(&rq->lock, flags);
  563. se = rq->cfs.curr;
  564. if (!se) {
  565. raw_spin_unlock_irqrestore(&rq->lock, flags);
  566. continue;
  567. }
  568. if (!entity_is_task(se)) {
  569. struct cfs_rq *cfs_rq;
  570. cfs_rq = group_cfs_rq(se);
  571. while (cfs_rq) {
  572. se = cfs_rq->curr;
  573. if (!entity_is_task(se))
  574. cfs_rq = group_cfs_rq(se);
  575. else
  576. cfs_rq = NULL;
  577. }
  578. }
  579. target_capacity = capacity_orig_of(cpu);
  580. if (se && entity_is_task(se) &&
  581. (uclamp_task_effective_util(task_of(se),
  582. UCLAMP_MIN) >= target_capacity) &&
  583. cpumask_test_cpu(this_cpu,
  584. &((task_of(se))->cpus_allowed))) {
  585. selected = 1;
  586. /* get task and selection inside rq lock */
  587. *p = task_of(se);
  588. get_task_struct(*p);
  589. *target = rq;
  590. }
  591. raw_spin_unlock_irqrestore(&rq->lock, flags);
  592. if (selected) {
  593. /* To put task out of rq lock */
  594. if (backup_task)
  595. put_task_struct(backup_task);
  596. return;
  597. }
  598. if (turning && !backup_task) {
  599. const struct cpumask *hmp_target_mask = NULL;
  600. struct cfs_rq *cfs_rq;
  601. struct sched_entity *se;
  602. raw_spin_lock_irqsave(&rq->lock, flags);
  603. hmp_target_mask = cpumask_of(this_cpu);
  604. cfs_rq = &rq->cfs;
  605. se = __pick_first_entity(cfs_rq);
  606. if (se && entity_is_task(se) &&
  607. cpumask_intersects(hmp_target_mask,
  608. &(task_of(se)->cpus_allowed))) {
  609. backup_cpu = cpu;
  610. /* get task and selection inside
  611. * rq lock
  612. */
  613. backup_task = task_of(se);
  614. get_task_struct(backup_task);
  615. }
  616. raw_spin_unlock_irqrestore(&rq->lock, flags);
  617. }
  618. }
  619. if (list_is_last(pos, &hmp_domains))
  620. break;
  621. }
  622. if (backup_task) {
  623. *p = backup_task;
  624. *target = cpu_rq(backup_cpu);
  625. return;
  626. }
  627. }
  628. /*
  629. * rq: src rq
  630. */
  631. static int
  632. migrate_runnable_task(struct task_struct *p, int dst_cpu,
  633. struct rq *rq)
  634. {
  635. struct rq_flags rf;
  636. int moved = 0;
  637. int src_cpu = cpu_of(rq);
  638. if (!raw_spin_trylock(&p->pi_lock))
  639. return moved;
  640. rq_lock(rq, &rf);
  641. /* Are both target and busiest cpu online */
  642. if (!cpu_online(src_cpu) || !cpu_online(dst_cpu) ||
  643. cpu_isolated(src_cpu) || cpu_isolated(dst_cpu))
  644. goto out_unlock;
  645. /* Task has migrated meanwhile, abort forced migration */
  646. /* can't migrate running task */
  647. if (task_running(rq, p))
  648. goto out_unlock;
  649. /*
  650. * If task_rq(p) != rq, it cannot be migrated here, because we're
  651. * holding rq->lock, if p->on_rq == 0 it cannot get enqueued because
  652. * we're holding p->pi_lock.
  653. */
  654. if (task_rq(p) == rq) {
  655. if (task_on_rq_queued(p)) {
  656. rq = __migrate_task(rq, &rf, p, dst_cpu);
  657. moved = 1;
  658. }
  659. }
  660. out_unlock:
  661. rq_unlock(rq, &rf);
  662. raw_spin_unlock(&p->pi_lock);
  663. return moved;
  664. }
  665. static unsigned int aggressive_idle_pull(int this_cpu)
  666. {
  667. int moved = 0;
  668. struct rq *target = NULL;
  669. struct task_struct *p = NULL;
  670. if (!sched_smp_initialized)
  671. return 0;
  672. if (!spin_trylock(&hmp_force_migration))
  673. return 0;
  674. /*
  675. * aggressive idle balance for min_cap/idle_prefer
  676. */
  677. if (hmp_cpu_is_slowest(this_cpu)) {
  678. hmp_slowest_idle_prefer_pull(this_cpu, &p, &target);
  679. if (p) {
  680. trace_sched_hmp_migrate(p, this_cpu, 0x10);
  681. moved = migrate_runnable_task(p, this_cpu, target);
  682. if (moved)
  683. goto done;
  684. }
  685. } else {
  686. hmp_fastest_idle_prefer_pull(this_cpu, &p, &target);
  687. if (p) {
  688. trace_sched_hmp_migrate(p, this_cpu, 0x10);
  689. moved = migrate_runnable_task(p, this_cpu, target);
  690. if (moved)
  691. goto done;
  692. moved = migrate_running_task(this_cpu, p, target);
  693. }
  694. }
  695. done:
  696. spin_unlock(&hmp_force_migration);
  697. if (p)
  698. put_task_struct(p);
  699. return moved;
  700. }
  701. #else
  702. bool idle_lb_enhance(struct task_struct *p, int cpu)
  703. {
  704. return 0;
  705. }
  706. static unsigned int aggressive_idle_pull(int this_cpu)
  707. {
  708. return 0;
  709. }
  710. #endif
  711. #ifdef CONFIG_UCLAMP_TASK
  712. static __always_inline
  713. unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util,
  714. struct task_struct *p)
  715. {
  716. unsigned long min_util = rq->uclamp.value[UCLAMP_MIN];
  717. unsigned long max_util = rq->uclamp.value[UCLAMP_MAX];
  718. if (p) {
  719. min_util = max_t(unsigned long, min_util,
  720. (unsigned long)uclamp_task_effective_util(p, UCLAMP_MIN));
  721. max_util = max_t(unsigned long, max_util,
  722. (unsigned long)uclamp_task_effective_util(p, UCLAMP_MAX));
  723. }
  724. /*
  725. * Since CPU's {min,max}_util clamps are MAX aggregated considering
  726. * RUNNABLE tasks with_different_ clamps, we can end up with an
  727. * inversion. Fix it now when the clamps are applied.
  728. */
  729. if (unlikely(min_util >= max_util))
  730. return min_util;
  731. return clamp(util, min_util, max_util);
  732. }
  733. #endif
  734. #ifdef CONFIG_MTK_SCHED_EAS_POWER_SUPPORT
  735. #define fits_capacity(cap, max) ((cap) * capacity_margin < (max) * 1024)
  736. static unsigned long __cpu_norm_sumutil(unsigned long util,
  737. unsigned long capacity)
  738. {
  739. return (util << SCHED_CAPACITY_SHIFT)/capacity;
  740. }
  741. struct sg_state {
  742. int cid;
  743. int cap_idx;
  744. unsigned long cap;
  745. unsigned long volt;
  746. unsigned long max_util;
  747. unsigned long sum_util;
  748. };
  749. /*
  750. * compute_energy(): Estimates the energy that @pd would consume if @p was
  751. * migrated to @dst_cpu. compute_energy() predicts what will be the utilization
  752. * landscape of @pd's CPUs after the task migration, and uses the Energy Model
  753. * to compute what would be the energy if we decided to actually migrate that
  754. * task.
  755. */
  756. static int
  757. update_sg_util(struct task_struct *p, int dst_cpu,
  758. const struct cpumask *sg_mask, struct sg_state *sg_env)
  759. {
  760. int cpu = cpumask_first(sg_mask);
  761. struct sched_domain *sd;
  762. const struct sched_group *sg;
  763. const struct sched_group_energy *sge;
  764. unsigned long new_util;
  765. int idx, max_idx;
  766. sg_env->sum_util = 0;
  767. sg_env->max_util = 0;
  768. sge = cpu_core_energy(cpu); /* for CPU */
  769. /*
  770. * The capacity state of CPUs of the current rd can be driven by CPUs
  771. * of another rd if they belong to the same pd. So, account for the
  772. * utilization of these CPUs too by masking pd with cpu_online_mask
  773. * instead of the rd span.
  774. *
  775. * If an entire pd is outside of the current rd, it will not appear in
  776. * its pd list and will not be accounted by compute_energy().
  777. */
  778. for_each_cpu_and(cpu, sg_mask, cpu_online_mask) {
  779. unsigned long cpu_util, cpu_boosted_util;
  780. struct task_struct *tsk = cpu == dst_cpu ? p : NULL;
  781. cpu_util = cpu_util_without(cpu, p);
  782. cpu_boosted_util = uclamp_rq_util_with(cpu_rq(cpu), cpu_util, p);
  783. if (tsk)
  784. cpu_util += task_util_est(p);
  785. sg_env->sum_util += cpu_util;
  786. sg_env->max_util = max(sg_env->max_util, cpu_boosted_util);
  787. }
  788. /* default is max_cap if we don't find a match */
  789. max_idx = sge->nr_cap_states - 1;
  790. sg_env->cap_idx = max_idx;
  791. sg_env->cap = sge->cap_states[max_idx].cap;
  792. new_util = sg_env->max_util * capacity_margin >> SCHED_CAPACITY_SHIFT;
  793. new_util = min_t(unsigned long, new_util,
  794. (unsigned long) sge->cap_states[sge->nr_cap_states-1].cap);
  795. for (idx = 0; idx < sge->nr_cap_states; idx++) {
  796. if (sge->cap_states[idx].cap >= new_util) {
  797. /* Keep track of SG's capacity */
  798. sg_env->cap_idx = idx;
  799. sg_env->cap = sge->cap_states[idx].cap;
  800. sg_env->volt = sge->cap_states[idx].volt;
  801. break;
  802. }
  803. }
  804. mt_sched_printf(sched_eas_energy_calc,
  805. "dst_cpu=%d mask=0x%lx sum_util=%lu max_util=%lu new_util=%lu (idx=%d cap=%ld volt=%ld)",
  806. dst_cpu, sg_mask->bits[0], sg_env->sum_util, sg_env->max_util,
  807. new_util, sg_env->cap_idx, sg_env->cap, sg_env->volt);
  808. return 1;
  809. }
  810. unsigned int share_buck_lkg_idx(const struct sched_group_energy *_sge,
  811. int cpu_idx, unsigned long v_max)
  812. {
  813. int co_buck_lkg_idx = _sge->nr_cap_states - 1;
  814. int idx;
  815. for (idx = cpu_idx; idx < _sge->nr_cap_states; idx++) {
  816. if (_sge->cap_states[idx].volt >= v_max) {
  817. co_buck_lkg_idx = idx;
  818. break;
  819. }
  820. }
  821. return co_buck_lkg_idx;
  822. }
  823. #define VOLT_SCALE 10
  824. void calc_pwr(int sd_level, const struct sched_group_energy *_sge,
  825. int cap_idx, unsigned long volt, unsigned long co_volt,
  826. unsigned long *dyn_pwr, unsigned long *lkg_pwr)
  827. {
  828. unsigned long int volt_factor = 1;
  829. if (co_volt > volt) {
  830. /*
  831. * calculated power with share-buck impact
  832. *
  833. * dynamic power = F*V^2
  834. *
  835. * dyn_pwr = current_power * (v_max/v_min)^2
  836. * lkg_pwr = tlb[idx of v_max].leak;
  837. */
  838. unsigned long v_max = co_volt;
  839. unsigned long v_min = volt;
  840. int lkg_idx = _sge->lkg_idx;
  841. int co_buck_lkg_idx;
  842. volt_factor = ((v_max*v_max) << VOLT_SCALE) /
  843. (v_min*v_min);
  844. *dyn_pwr = (_sge->cap_states[cap_idx].dyn_pwr *
  845. volt_factor) >> VOLT_SCALE;
  846. co_buck_lkg_idx = share_buck_lkg_idx(_sge, cap_idx, v_max);
  847. *lkg_pwr = _sge->cap_states[co_buck_lkg_idx].lkg_pwr[lkg_idx];
  848. trace_sched_busy_power(sd_level, cap_idx,
  849. _sge->cap_states[cap_idx].dyn_pwr, volt_factor,
  850. *dyn_pwr, co_buck_lkg_idx, *lkg_pwr,
  851. *dyn_pwr + *lkg_pwr);
  852. } else {
  853. /* No share buck impact */
  854. int lkg_idx = _sge->lkg_idx;
  855. *dyn_pwr = _sge->cap_states[cap_idx].dyn_pwr;
  856. *lkg_pwr = _sge->cap_states[cap_idx].lkg_pwr[lkg_idx];
  857. trace_sched_busy_power(sd_level, cap_idx, *dyn_pwr,
  858. volt_factor, *dyn_pwr, cap_idx,
  859. *lkg_pwr, *dyn_pwr + *lkg_pwr);
  860. }
  861. }
  862. /**
  863. * em_sg_energy() - Estimates the energy consumed by the CPUs of a perf. domain
  864. * @sd : performance domain for which energy has to be estimated
  865. * @max_util : highest utilization among CPUs of the domain
  866. * @sum_util : sum of the utilization of all CPUs in the domain
  867. *
  868. * Return: the sum of the energy consumed by the CPUs of the domain assuming
  869. * a capacity state satisfying the max utilization of the domain.
  870. */
  871. static inline unsigned long compute_energy_sg(const struct cpumask *sg_cpus,
  872. struct sg_state *sg_env, struct sg_state *share_env)
  873. {
  874. int cpu;
  875. const struct sched_group_energy *_sge;
  876. unsigned long dyn_pwr, lkg_pwr;
  877. unsigned long dyn_egy, lkg_egy;
  878. unsigned long total_energy;
  879. unsigned long sg_util;
  880. cpu = cpumask_first(sg_cpus);
  881. _sge = cpu_core_energy(cpu); /* for CPU */
  882. calc_pwr(0, _sge,
  883. sg_env->cap_idx, sg_env->volt, share_env->volt,
  884. &dyn_pwr, &lkg_pwr);
  885. sg_util = __cpu_norm_sumutil(sg_env->sum_util, sg_env->cap);
  886. dyn_egy = sg_util * dyn_pwr;
  887. lkg_egy = SCHED_CAPACITY_SCALE * lkg_pwr;
  888. total_energy = dyn_egy + lkg_egy;
  889. mt_sched_printf(sched_eas_energy_calc,
  890. "sg_util=%lu dyn_egy=%d lkg_egy=%d (cost=%d) mask=0x%lx",
  891. sg_util,
  892. (int)dyn_egy, (int)lkg_egy, (int)total_energy,
  893. sg_cpus->bits[0]);
  894. return total_energy;
  895. }
  896. bool is_share_buck(int cid, int *co_buck_cid)
  897. {
  898. bool ret = false;
  899. if (share_buck[cid] != cid) {
  900. *co_buck_cid = share_buck[cid];
  901. ret = true;
  902. }
  903. return ret;
  904. }
  905. static long
  906. compute_energy_enhanced(struct task_struct *p, int dst_cpu,
  907. struct sched_group *sg)
  908. {
  909. int cid, share_cid, cpu;
  910. struct sg_state sg_env, share_env;
  911. const struct cpumask *sg_cpus;
  912. struct cpumask share_cpus;
  913. unsigned long total_energy = 0;
  914. share_env.volt = 0;
  915. sg_cpus = sched_group_span(sg);
  916. cpu = cpumask_first(sg_cpus);
  917. #ifdef CONFIG_ARM64
  918. cid = cpu_topology[cpu].cluster_id;
  919. #else
  920. cid = cpu_topology[cpu].socket_id;
  921. #endif
  922. if (!update_sg_util(p, dst_cpu, sg_cpus, &sg_env))
  923. return 0;
  924. if (is_share_buck(cid, &share_cid)) {
  925. arch_get_cluster_cpus(&share_cpus, share_cid);
  926. if (!update_sg_util(p, dst_cpu, &share_cpus, &share_env))
  927. return 0;
  928. total_energy += compute_energy_sg(&share_cpus, &share_env,
  929. &sg_env);
  930. }
  931. total_energy += compute_energy_sg(sg_cpus, &sg_env, &share_env);
  932. return total_energy;
  933. }
  934. static int find_energy_efficient_cpu_enhanced(struct task_struct *p,
  935. int this_cpu, int prev_cpu, int sync)
  936. {
  937. unsigned long prev_energy = 0;
  938. unsigned long prev_delta = ULONG_MAX, best_delta = ULONG_MAX;
  939. int max_spare_cap_cpu_ls = prev_cpu;
  940. unsigned long max_spare_cap_ls = 0, target_cap;
  941. unsigned long sys_max_spare_cap = 0;
  942. unsigned long cpu_cap, util, wake_util;
  943. bool boosted, prefer_idle = false;
  944. unsigned int min_exit_lat = UINT_MAX;
  945. int sys_max_spare_cap_cpu = -1;
  946. int best_energy_cpu = prev_cpu;
  947. struct cpuidle_state *idle;
  948. struct sched_domain *sd;
  949. struct sched_group *sg;
  950. if (sysctl_sched_sync_hint_enable && sync) {
  951. if (cpumask_test_cpu(this_cpu, &p->cpus_allowed) &&
  952. !cpu_isolated(this_cpu)) {
  953. return this_cpu;
  954. }
  955. }
  956. sd = rcu_dereference(per_cpu(sd_ea, this_cpu));
  957. if (!sd)
  958. return -1;
  959. if (!boosted_task_util(p))
  960. return -1;
  961. prefer_idle = schedtune_prefer_idle(p);
  962. boosted = (schedtune_task_boost(p) > 0) || (uclamp_task_effective_util(p, UCLAMP_MIN) > 0);
  963. target_cap = boosted ? 0 : ULONG_MAX;
  964. sg = sd->groups;
  965. do {
  966. unsigned long cur_energy = 0, cur_delta = 0;
  967. unsigned long spare_cap, max_spare_cap = 0;
  968. unsigned long base_energy_sg;
  969. int max_spare_cap_cpu = -1, best_idle_cpu = -1;
  970. int cpu;
  971. /* compute the ''base' energy of the sg, without @p*/
  972. base_energy_sg = compute_energy_enhanced(p, -1, sg);
  973. for_each_cpu_and(cpu, &p->cpus_allowed, sched_group_span(sg)) {
  974. if (cpu_isolated(cpu))
  975. continue;
  976. #ifdef CONFIG_MTK_SCHED_INTEROP
  977. if (cpu_rq(cpu)->rt.rt_nr_running &&
  978. likely(!is_rt_throttle(cpu)))
  979. continue;
  980. #endif
  981. /* Skip CPUs that will be overutilized. */
  982. wake_util = cpu_util_without(cpu, p);
  983. util = wake_util + task_util_est(p);
  984. cpu_cap = capacity_of(cpu);
  985. spare_cap = cpu_cap - util;
  986. if (spare_cap > sys_max_spare_cap) {
  987. sys_max_spare_cap = spare_cap;
  988. sys_max_spare_cap_cpu = cpu;
  989. }
  990. /*
  991. * Skip CPUs that cannot satisfy the capacity request.
  992. * IOW, placing the task there would make the CPU
  993. * overutilized. Take uclamp into account to see how
  994. * much capacity we can get out of the CPU; this is
  995. * aligned with schedutil_cpu_util().
  996. */
  997. util = uclamp_rq_util_with(cpu_rq(cpu), util, p);
  998. if (!fits_capacity(util, cpu_cap))
  999. continue;
  1000. /* Always use prev_cpu as a candidate. */
  1001. if (cpu == prev_cpu) {
  1002. prev_energy = compute_energy_enhanced(p,
  1003. prev_cpu, sg);
  1004. prev_delta = prev_energy - base_energy_sg;
  1005. best_delta = min(best_delta, prev_delta);
  1006. }
  1007. /*
  1008. * Find the CPU with the maximum spare capacity in
  1009. * the performance domain
  1010. */
  1011. spare_cap = cpu_cap - util;
  1012. if (spare_cap > max_spare_cap) {
  1013. max_spare_cap = spare_cap;
  1014. max_spare_cap_cpu = cpu;
  1015. }
  1016. if (!prefer_idle)
  1017. continue;
  1018. if (idle_cpu(cpu)) {
  1019. cpu_cap = capacity_orig_of(cpu);
  1020. if (boosted && cpu_cap < target_cap)
  1021. continue;
  1022. if (!boosted && cpu_cap > target_cap)
  1023. continue;
  1024. idle = idle_get_state(cpu_rq(cpu));
  1025. if (idle && idle->exit_latency > min_exit_lat &&
  1026. cpu_cap == target_cap)
  1027. continue;
  1028. if (idle)
  1029. min_exit_lat = idle->exit_latency;
  1030. target_cap = cpu_cap;
  1031. best_idle_cpu = cpu;
  1032. } else if (spare_cap > max_spare_cap_ls) {
  1033. max_spare_cap_ls = spare_cap;
  1034. max_spare_cap_cpu_ls = cpu;
  1035. }
  1036. }
  1037. if (!prefer_idle && max_spare_cap_cpu >= 0 &&
  1038. max_spare_cap_cpu != prev_cpu) {
  1039. cur_energy = compute_energy_enhanced(p,
  1040. max_spare_cap_cpu, sg);
  1041. cur_delta = cur_energy - base_energy_sg;
  1042. if (cur_delta < best_delta) {
  1043. best_delta = cur_delta;
  1044. best_energy_cpu = max_spare_cap_cpu;
  1045. }
  1046. }
  1047. if (prefer_idle && best_idle_cpu >= 0 &&
  1048. best_idle_cpu != prev_cpu) {
  1049. cur_energy = compute_energy_enhanced(p,
  1050. best_idle_cpu, sg);
  1051. cur_delta = cur_energy - base_energy_sg;
  1052. if (cur_delta < best_delta) {
  1053. best_delta = cur_delta;
  1054. best_energy_cpu = best_idle_cpu;
  1055. }
  1056. }
  1057. mt_sched_printf(sched_eas_energy_calc,
  1058. "prev_cpu=%d base_energy=%lu prev_energy=%lu prev_delta=%d",
  1059. prev_cpu, base_energy_sg, prev_energy, (int)prev_delta);
  1060. mt_sched_printf(sched_eas_energy_calc,
  1061. "max_spare_cap_cpu=%d best_idle_cpu=%d cur_energy=%lu cur_delta=%d",
  1062. max_spare_cap_cpu, best_idle_cpu, cur_energy, (int)cur_delta);
  1063. } while (sg = sg->next, sg != sd->groups);
  1064. /*
  1065. * Pick the best CPU if prev_cpu cannot be used, or it it saves energy
  1066. * used by prev_cpu.
  1067. */
  1068. if (prev_delta == ULONG_MAX) {
  1069. /* All cpu failed on !fit_capacity, use sys_max_spare_cap_cpu */
  1070. if (best_energy_cpu == prev_cpu)
  1071. return sys_max_spare_cap_cpu;
  1072. else
  1073. return best_energy_cpu;
  1074. }
  1075. if ((prev_delta - best_delta) > 0)
  1076. return best_energy_cpu;
  1077. return prev_cpu;
  1078. }
  1079. static int __find_energy_efficient_cpu(struct sched_domain *sd,
  1080. struct task_struct *p,
  1081. int cpu, int prev_cpu,
  1082. int sync)
  1083. {
  1084. int num_cluster = arch_get_nr_clusters();
  1085. if (num_cluster <= 2)
  1086. return find_energy_efficient_cpu(sd, p, cpu, prev_cpu, sync);
  1087. else
  1088. return find_energy_efficient_cpu_enhanced(p, cpu, prev_cpu, sync);
  1089. }
  1090. /*
  1091. * group_norm_util() returns the approximated group util relative to it's
  1092. * current capacity (busy ratio) in the range [0..SCHED_CAPACITY_SCALE] for use
  1093. * in energy calculations. Since task executions may or may not overlap in time
  1094. * in the group the true normalized util is between max(cpu_norm_util(i)) and
  1095. * sum(cpu_norm_util(i)) when iterating over all cpus in the group, i. The
  1096. * latter is used as the estimate as it leads to a more pessimistic energy
  1097. * estimate (more busy).
  1098. */
  1099. static unsigned
  1100. long group_norm_util(struct energy_env *eenv, int cpu_idx)
  1101. {
  1102. struct sched_group *sg = eenv->sg;
  1103. int cpu_id = group_first_cpu(sg);
  1104. #ifdef CONFIG_ARM64
  1105. int cid = cpu_topology[cpu_id].cluster_id;
  1106. #else
  1107. int cid = cpu_topology[cpu_id].socket_id;
  1108. #endif
  1109. unsigned long capacity = eenv->cpu[cpu_idx].cap[cid];
  1110. unsigned long util, util_sum = 0;
  1111. int cpu;
  1112. for_each_cpu(cpu, sched_group_span(eenv->sg)) {
  1113. util = cpu_util_without(cpu, eenv->p);
  1114. /*
  1115. * If we are looking at the target CPU specified by the eenv,
  1116. * then we should add the (estimated) utilization of the task
  1117. * assuming we will wake it up on that CPU.
  1118. */
  1119. if (unlikely(cpu == eenv->cpu[cpu_idx].cpu_id))
  1120. util += eenv->util_delta;
  1121. util_sum += __cpu_norm_util(util, capacity);
  1122. trace_group_norm_util(cpu_idx, cpu, cid, util_sum,
  1123. __cpu_norm_util(util, capacity), eenv->util_delta,
  1124. util, capacity);
  1125. }
  1126. if (util_sum > SCHED_CAPACITY_SCALE)
  1127. return SCHED_CAPACITY_SCALE;
  1128. return util_sum;
  1129. }
  1130. #endif
  1131. #ifdef CONFIG_MTK_SCHED_EAS_POWER_SUPPORT
  1132. static unsigned long
  1133. mtk_cluster_max_usage(int cid, struct energy_env *eenv, int cpu_idx,
  1134. int *max_cpu)
  1135. {
  1136. unsigned long util, max_util = 0;
  1137. int cpu = -1;
  1138. struct cpumask cls_cpus;
  1139. *max_cpu = -1;
  1140. arch_get_cluster_cpus(&cls_cpus, cid);
  1141. for_each_cpu(cpu, &cls_cpus) {
  1142. if (!cpu_online(cpu))
  1143. continue;
  1144. util = cpu_util_without(cpu, eenv->p);
  1145. /*
  1146. * If we are looking at the target CPU specified by the eenv,
  1147. * then we should add the (estimated) utilization of the task
  1148. * assuming we will wake it up on that CPU.
  1149. */
  1150. if (unlikely(cpu == eenv->cpu[cpu_idx].cpu_id))
  1151. util += eenv->util_delta;
  1152. if (util >= max_util) {
  1153. max_util = util;
  1154. *max_cpu = cpu;
  1155. }
  1156. }
  1157. return max_util;
  1158. }
  1159. void mtk_cluster_capacity_idx(int cid, struct energy_env *eenv, int cpu_idx)
  1160. {
  1161. int cpu;
  1162. unsigned long util = mtk_cluster_max_usage(cid, eenv, cpu_idx, &cpu);
  1163. unsigned long new_capacity = util;
  1164. struct sched_domain *sd;
  1165. struct sched_group *sg;
  1166. const struct sched_group_energy *sge;
  1167. int idx, max_idx;
  1168. if (cpu == -1) { /* maybe no online CPU */
  1169. printk_deferred("sched: %s no online CPU", __func__);
  1170. return;
  1171. }
  1172. sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd);
  1173. if (sd) {
  1174. sg = sd->groups;
  1175. sge = sg->sge;
  1176. } else{
  1177. printk_deferred("sched: %s no sd", __func__);
  1178. return;
  1179. }
  1180. max_idx = sge->nr_cap_states - 1;
  1181. /* default is max_cap if we don't find a match */
  1182. eenv->cpu[cpu_idx].cap_idx[cid] = max_idx;
  1183. eenv->cpu[cpu_idx].cap[cid] = sge->cap_states[max_idx].cap;
  1184. /* OPP idx to refer capacity margin */
  1185. new_capacity = util * capacity_margin >> SCHED_CAPACITY_SHIFT;
  1186. new_capacity = min(new_capacity,
  1187. (unsigned long) sge->cap_states[sge->nr_cap_states-1].cap);
  1188. for (idx = 0; idx < sge->nr_cap_states; idx++) {
  1189. if (sge->cap_states[idx].cap >= new_capacity) {
  1190. /* Keep track of SG's capacity */
  1191. eenv->cpu[cpu_idx].cap_idx[cid] = idx;
  1192. eenv->cpu[cpu_idx].cap[cid] = sge->cap_states[idx].cap;
  1193. break;
  1194. }
  1195. }
  1196. mt_sched_printf(sched_eas_energy_calc,
  1197. "cpu_idx=%d dst_cpu=%d cid=%d max_cpu=%d (util=%ld new=%ld) max_opp=%d (cap=%d)",
  1198. cpu_idx, eenv->cpu[cpu_idx].cpu_id,
  1199. cid, cpu, util, new_capacity,
  1200. eenv->cpu[cpu_idx].cap_idx[cid],
  1201. eenv->cpu[cpu_idx].cap[cid]);
  1202. }
  1203. #if defined(ARM_V8_2) && defined(CONFIG_MTK_UNIFY_POWER)
  1204. struct sched_group_energy cci_tbl;
  1205. const struct sched_group_energy * const cci_energy(void)
  1206. {
  1207. struct sched_group_energy *sge = &cci_tbl;
  1208. struct upower_tbl_info **addr_ptr_tbl_info;
  1209. struct upower_tbl_info *ptr_tbl_info;
  1210. struct upower_tbl *ptr_tbl;
  1211. addr_ptr_tbl_info = upower_get_tbl();
  1212. ptr_tbl_info = *addr_ptr_tbl_info;
  1213. ptr_tbl = ptr_tbl_info[UPOWER_BANK_CCI].p_upower_tbl;
  1214. sge->nr_cap_states = ptr_tbl->row_num;
  1215. sge->cap_states = ptr_tbl->row;
  1216. sge->lkg_idx = ptr_tbl->lkg_idx;
  1217. return sge;
  1218. }
  1219. extern unsigned int mt_cpufreq_get_cur_cci_freq_idx(void);
  1220. void get_cci_volt(struct sg_state *cci)
  1221. {
  1222. const struct sched_group_energy *_sge;
  1223. static int CCI_nr_cap_stats;
  1224. _sge = cci_energy();
  1225. if (CCI_nr_cap_stats == 0) {
  1226. CCI_nr_cap_stats = _sge->nr_cap_states;
  1227. }
  1228. cci->cap_idx = CCI_nr_cap_stats - mt_cpufreq_get_cur_cci_freq_idx();
  1229. cci->volt = _sge->cap_states[cci->cap_idx].volt;
  1230. }
  1231. #else
  1232. void get_cci_volt(struct sg_state *cci)
  1233. {
  1234. }
  1235. #endif
  1236. void share_buck_volt(struct energy_env *eenv, int cpu_idx, int cid,
  1237. struct sg_state *co_buck)
  1238. {
  1239. if (is_share_buck(cid, &(co_buck->cid))) {
  1240. int num_cluster = arch_get_nr_clusters();
  1241. int cap_idx = eenv->cpu[cpu_idx].cap_idx[cid];
  1242. if (co_buck->cid < num_cluster) {
  1243. struct cpumask cls_cpus;
  1244. const struct sched_group_energy *sge_core;
  1245. int cpu;
  1246. arch_get_cluster_cpus(&cls_cpus, co_buck->cid);
  1247. cpu = cpumask_first(&cls_cpus);
  1248. sge_core = cpu_core_energy(cpu);
  1249. co_buck->cap_idx =
  1250. eenv->cpu[cpu_idx].cap_idx[co_buck->cid];
  1251. co_buck->volt =
  1252. sge_core->cap_states[co_buck->cap_idx].volt;
  1253. #if defined(ARM_V8_2) && defined(CONFIG_MTK_UNIFY_POWER)
  1254. } else if (co_buck->cid == CCI_ID) { /* CCI + DSU */
  1255. get_cci_volt(co_buck);
  1256. #endif
  1257. }
  1258. trace_sched_share_buck(cpu_idx, cid, cap_idx, co_buck->cid,
  1259. co_buck->cap_idx, co_buck->volt);
  1260. }
  1261. }
  1262. int
  1263. mtk_idle_power(int cpu_idx, int idle_state, int cpu, void *argu, int sd_level)
  1264. {
  1265. struct energy_env *eenv = (struct energy_env *)argu;
  1266. const struct sched_group_energy *_sge, *sge_core, *sge_clus;
  1267. struct sched_domain *sd;
  1268. unsigned long volt;
  1269. int energy_cost = 0;
  1270. #ifdef CONFIG_ARM64
  1271. int cid = cpu_topology[cpu].cluster_id;
  1272. #else
  1273. int cid = cpu_topology[cpu].socket_id;
  1274. #endif
  1275. int cap_idx = eenv->cpu[cpu_idx].cap_idx[cid];
  1276. struct sg_state co_buck = {-1, -1, 0};
  1277. sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd);
  1278. /* [FIXME] racing with hotplug */
  1279. if (!sd)
  1280. return 0;
  1281. /* [FIXME] racing with hotplug */
  1282. if (cap_idx == -1)
  1283. return 0;
  1284. _sge = cpu_core_energy(cpu);
  1285. volt = _sge->cap_states[cap_idx].volt;
  1286. share_buck_volt(eenv, cpu_idx, cid, &co_buck);
  1287. if (co_buck.volt > volt)
  1288. cap_idx = share_buck_lkg_idx(_sge, cap_idx, co_buck.volt);
  1289. _sge = sge_core = sge_clus = NULL;
  1290. /* To handle only 1 CPU in cluster by HPS */
  1291. if (unlikely(!sd->child &&
  1292. (rcu_dereference(per_cpu(sd_scs, cpu)) == NULL))) {
  1293. struct upower_tbl_row *cpu_pwr_tbl, *clu_pwr_tbl;
  1294. sge_core = cpu_core_energy(cpu);
  1295. sge_clus = cpu_cluster_energy(cpu);
  1296. cpu_pwr_tbl = &sge_core->cap_states[cap_idx];
  1297. clu_pwr_tbl = &sge_clus->cap_states[cap_idx];
  1298. /* idle: core->leask_power + cluster->lkg_pwr */
  1299. energy_cost = cpu_pwr_tbl->lkg_pwr[sge_core->lkg_idx] +
  1300. clu_pwr_tbl->lkg_pwr[sge_clus->lkg_idx];
  1301. mt_sched_printf(sched_eas_energy_calc,
  1302. "%s: %s lv=%d tlb_cpu[%d].leak=%d tlb_clu[%d].leak=%d total=%d",
  1303. __func__, "WFI", sd_level,
  1304. cap_idx,
  1305. cpu_pwr_tbl->lkg_pwr[sge_core->lkg_idx],
  1306. cap_idx,
  1307. clu_pwr_tbl->lkg_pwr[sge_clus->lkg_idx],
  1308. energy_cost);
  1309. } else {
  1310. struct upower_tbl_row *pwr_tbl;
  1311. unsigned long lkg_pwr;
  1312. if (sd_level == 0)
  1313. _sge = cpu_core_energy(cpu); /* for cpu */
  1314. else
  1315. _sge = cpu_cluster_energy(cpu); /* for cluster */
  1316. pwr_tbl = &_sge->cap_states[cap_idx];
  1317. lkg_pwr = pwr_tbl->lkg_pwr[_sge->lkg_idx];
  1318. energy_cost = lkg_pwr;
  1319. trace_sched_idle_power(sd_level, cap_idx, lkg_pwr, energy_cost);
  1320. }
  1321. idle_state = 0;
  1322. #if defined(ARM_V8_2) && defined(CONFIG_MTK_UNIFY_POWER)
  1323. if ((sd_level != 0) && (co_buck.cid == CCI_ID)) {
  1324. struct upower_tbl_row *CCI_pwr_tbl;
  1325. unsigned long lkg_pwr;
  1326. _sge = cci_energy();
  1327. CCI_pwr_tbl = &_sge->cap_states[cap_idx];
  1328. lkg_pwr = CCI_pwr_tbl->lkg_pwr[_sge->lkg_idx];
  1329. energy_cost += lkg_pwr;
  1330. trace_sched_idle_power(sd_level, cap_idx, lkg_pwr, energy_cost);
  1331. }
  1332. #endif
  1333. return energy_cost;
  1334. }
  1335. int calc_busy_power(const struct sched_group_energy *_sge, int cap_idx,
  1336. unsigned long co_volt, int sd_level)
  1337. {
  1338. unsigned long dyn_pwr, lkg_pwr;
  1339. unsigned long volt;
  1340. volt = _sge->cap_states[cap_idx].volt;
  1341. calc_pwr(sd_level, _sge, cap_idx, volt, co_volt, &dyn_pwr, &lkg_pwr);
  1342. return dyn_pwr + lkg_pwr;
  1343. }
  1344. int mtk_busy_power(int cpu_idx, int cpu, void *argu, int sd_level)
  1345. {
  1346. struct energy_env *eenv = (struct energy_env *)argu;
  1347. const struct sched_group_energy *_sge;
  1348. struct sched_domain *sd;
  1349. int energy_cost = 0;
  1350. #ifdef CONFIG_ARM64
  1351. int cid = cpu_topology[cpu].cluster_id;
  1352. #else
  1353. int cid = cpu_topology[cpu].socket_id;
  1354. #endif
  1355. int cap_idx = eenv->cpu[cpu_idx].cap_idx[cid];
  1356. struct sg_state co_buck = {-1, -1, 0};
  1357. sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd);
  1358. /* [FIXME] racing with hotplug */
  1359. if (!sd)
  1360. return 0;
  1361. /* [FIXME] racing with hotplug */
  1362. if (cap_idx == -1)
  1363. return 0;
  1364. share_buck_volt(eenv, cpu_idx, cid, &co_buck);
  1365. /* To handle only 1 CPU in cluster by HPS */
  1366. if (unlikely(!sd->child &&
  1367. (rcu_dereference(per_cpu(sd_scs, cpu)) == NULL))) {
  1368. /* fix HPS defeats: only one CPU in this cluster */
  1369. _sge = cpu_core_energy(cpu); /* for CPU */
  1370. energy_cost = calc_busy_power(_sge, cap_idx, co_buck.volt,
  1371. 0);
  1372. _sge = cpu_cluster_energy(cpu); /* for cluster */
  1373. energy_cost += calc_busy_power(_sge, cap_idx, co_buck.volt,
  1374. 1);
  1375. } else {
  1376. if (sd_level == 0)
  1377. _sge = cpu_core_energy(cpu); /* for CPU */
  1378. else
  1379. _sge = cpu_cluster_energy(cpu); /* for cluster */
  1380. energy_cost = calc_busy_power(_sge, cap_idx, co_buck.volt,
  1381. sd_level);
  1382. }
  1383. #if defined(ARM_V8_2) && defined(CONFIG_MTK_UNIFY_POWER)
  1384. if ((sd_level != 0) && (co_buck.cid == CCI_ID)) {
  1385. /* CCI + DSU */
  1386. unsigned long volt;
  1387. _sge = cpu_core_energy(cpu); /* for CPU */
  1388. volt = _sge->cap_states[cap_idx].volt;
  1389. _sge = cci_energy();
  1390. energy_cost += calc_busy_power(_sge, co_buck.cap_idx, volt,
  1391. sd_level);
  1392. }
  1393. #endif
  1394. return energy_cost;
  1395. }
  1396. #endif
  1397. #ifdef CONFIG_MTK_SCHED_EAS_POWER_SUPPORT
  1398. void mtk_update_new_capacity(struct energy_env *eenv)
  1399. {
  1400. int i, cpu_idx;
  1401. /* To get max opp index of every cluster for power estimation of
  1402. * share buck
  1403. */
  1404. for (cpu_idx = EAS_CPU_PRV; cpu_idx < eenv->max_cpu_count ; ++cpu_idx) {
  1405. if (eenv->cpu[cpu_idx].cpu_id == -1)
  1406. continue;
  1407. for (i = 0; i < arch_get_nr_clusters(); i++)
  1408. mtk_cluster_capacity_idx(i, eenv, cpu_idx);
  1409. }
  1410. }
  1411. #else
  1412. void mtk_update_new_capacity(struct energy_env *eenv)
  1413. {
  1414. }
  1415. static int __find_energy_efficient_cpu(struct sched_domain *sd,
  1416. struct task_struct *p,
  1417. int cpu, int prev_cpu,
  1418. int sync)
  1419. {
  1420. return find_energy_efficient_cpu(sd, p, cpu, prev_cpu, sync);
  1421. }
  1422. #endif
  1423. #ifdef CONFIG_MTK_SCHED_BOOST
  1424. static void select_task_prefer_cpu_fair(struct task_struct *p, int *result)
  1425. {
  1426. int task_prefer;
  1427. int cpu, new_cpu;
  1428. task_prefer = cpu_prefer(p);
  1429. cpu = (*result & LB_CPU_MASK);
  1430. new_cpu = select_task_prefer_cpu(p, cpu);
  1431. if ((new_cpu >= 0) && (new_cpu != cpu)) {
  1432. if (task_prefer_match(p, cpu))
  1433. *result = new_cpu | LB_THERMAL;
  1434. else
  1435. *result = new_cpu | LB_HINT;
  1436. }
  1437. }
  1438. #else
  1439. static void select_task_prefer_cpu_fair(struct task_struct *p, int *result)
  1440. {
  1441. }
  1442. #endif
  1443. inline int
  1444. task_match_on_dst_cpu(struct task_struct *p, int src_cpu, int target_cpu)
  1445. {
  1446. struct task_struct *target_tsk;
  1447. struct rq *rq = cpu_rq(target_cpu);
  1448. #ifdef CONFIG_MTK_SCHED_BOOST
  1449. if (task_prefer_match(p, src_cpu))
  1450. return 0;
  1451. target_tsk = rq->curr;
  1452. if (task_prefer_fit(target_tsk, target_cpu))
  1453. return 0;
  1454. #endif
  1455. return 1;
  1456. }
  1457. static int check_freq_turning(void)
  1458. {
  1459. struct root_domain *rd = cpu_rq(smp_processor_id())->rd;
  1460. unsigned long capacity_curr_little, capacity_curr_big;
  1461. if (rd->min_cap_orig_cpu < 0 || rd->max_cap_orig_cpu < 0)
  1462. return false;
  1463. capacity_curr_little = capacity_curr_of(rd->min_cap_orig_cpu);
  1464. capacity_curr_big = capacity_curr_of(rd->max_cap_orig_cpu);
  1465. if ((capacity_curr_little > cpu_eff_tp) &&
  1466. (capacity_curr_big <= big_cpu_eff_tp))
  1467. return true;
  1468. return false;
  1469. }
  1470. struct task_rotate_work {
  1471. struct work_struct w;
  1472. struct task_struct *src_task;
  1473. struct task_struct *dst_task;
  1474. int src_cpu;
  1475. int dst_cpu;
  1476. };
  1477. static DEFINE_PER_CPU(struct task_rotate_work, task_rotate_works);
  1478. struct task_rotate_reset_uclamp_work task_rotate_reset_uclamp_works;
  1479. unsigned int sysctl_sched_rotation_enable;
  1480. bool set_uclamp;
  1481. void set_sched_rotation_enable(bool enable)
  1482. {
  1483. sysctl_sched_rotation_enable = enable;
  1484. }
  1485. bool is_min_capacity_cpu(int cpu)
  1486. {
  1487. struct root_domain *rd = cpu_rq(smp_processor_id())->rd;
  1488. if (rd->min_cap_orig_cpu < 0)
  1489. return false;
  1490. if (capacity_orig_of(cpu) == capacity_orig_of(rd->min_cap_orig_cpu))
  1491. return true;
  1492. return false;
  1493. }
  1494. static void task_rotate_work_func(struct work_struct *work)
  1495. {
  1496. struct task_rotate_work *wr = container_of(work,
  1497. struct task_rotate_work, w);
  1498. int ret = -1;
  1499. struct rq *src_rq, *dst_rq;
  1500. ret = migrate_swap(wr->src_task, wr->dst_task);
  1501. if (ret == 0) {
  1502. update_eas_uclamp_min(EAS_UCLAMP_KIR_BIG_TASK, CGROUP_TA,
  1503. scale_to_percent(SCHED_CAPACITY_SCALE));
  1504. set_uclamp = true;
  1505. trace_sched_big_task_rotation(wr->src_cpu, wr->dst_cpu,
  1506. wr->src_task->pid,
  1507. wr->dst_task->pid,
  1508. true, set_uclamp);
  1509. }
  1510. put_task_struct(wr->src_task);
  1511. put_task_struct(wr->dst_task);
  1512. src_rq = cpu_rq(wr->src_cpu);
  1513. dst_rq = cpu_rq(wr->dst_cpu);
  1514. local_irq_disable();
  1515. double_rq_lock(src_rq, dst_rq);
  1516. src_rq->active_balance = 0;
  1517. dst_rq->active_balance = 0;
  1518. double_rq_unlock(src_rq, dst_rq);
  1519. local_irq_enable();
  1520. }
  1521. static void task_rotate_reset_uclamp_work_func(struct work_struct *work)
  1522. {
  1523. update_eas_uclamp_min(EAS_UCLAMP_KIR_BIG_TASK, CGROUP_TA, 0);
  1524. set_uclamp = false;
  1525. trace_sched_big_task_rotation_reset(set_uclamp);
  1526. }
  1527. void task_rotate_work_init(void)
  1528. {
  1529. int i;
  1530. for_each_possible_cpu(i) {
  1531. struct task_rotate_work *wr = &per_cpu(task_rotate_works, i);
  1532. INIT_WORK(&wr->w, task_rotate_work_func);
  1533. }
  1534. INIT_WORK(&task_rotate_reset_uclamp_works.w,
  1535. task_rotate_reset_uclamp_work_func);
  1536. }
  1537. void task_check_for_rotation(struct rq *src_rq)
  1538. {
  1539. u64 wc, wait, max_wait = 0, run, max_run = 0;
  1540. int deserved_cpu = nr_cpu_ids, dst_cpu = nr_cpu_ids;
  1541. int i, src_cpu = cpu_of(src_rq);
  1542. struct rq *dst_rq;
  1543. struct task_rotate_work *wr = NULL;
  1544. int heavy_task = 0;
  1545. int force = 0;
  1546. if (!sysctl_sched_rotation_enable)
  1547. return;
  1548. if (is_max_capacity_cpu(src_cpu))
  1549. return;
  1550. for_each_possible_cpu(i) {
  1551. struct rq *rq = cpu_rq(i);
  1552. struct task_struct *curr_task = rq->curr;
  1553. if (curr_task &&
  1554. !task_fits_capacity(curr_task, capacity_of(i)))
  1555. heavy_task += 1;
  1556. }
  1557. if (heavy_task < HEAVY_TASK_NUM)
  1558. return;
  1559. wc = ktime_get_ns();
  1560. for_each_possible_cpu(i) {
  1561. struct rq *rq = cpu_rq(i);
  1562. if (!is_min_capacity_cpu(i))
  1563. continue;
  1564. if (is_reserved(i))
  1565. continue;
  1566. if (!rq->misfit_task_load || rq->curr->sched_class !=
  1567. &fair_sched_class)
  1568. continue;
  1569. wait = wc - rq->curr->last_enqueued_ts;
  1570. if (wait > max_wait) {
  1571. max_wait = wait;
  1572. deserved_cpu = i;
  1573. }
  1574. }
  1575. if (deserved_cpu != src_cpu)
  1576. return;
  1577. for_each_possible_cpu(i) {
  1578. struct rq *rq = cpu_rq(i);
  1579. if (capacity_orig_of(i) <= capacity_orig_of(src_cpu))
  1580. continue;
  1581. if (is_reserved(i))
  1582. continue;
  1583. if (rq->curr->sched_class != &fair_sched_class)
  1584. continue;
  1585. if (rq->nr_running > 1)
  1586. continue;
  1587. run = wc - rq->curr->last_enqueued_ts;
  1588. if (run < TASK_ROTATION_THRESHOLD_NS)
  1589. continue;
  1590. if (run > max_run) {
  1591. max_run = run;
  1592. dst_cpu = i;
  1593. }
  1594. }
  1595. if (dst_cpu == nr_cpu_ids)
  1596. return;
  1597. dst_rq = cpu_rq(dst_cpu);
  1598. double_rq_lock(src_rq, dst_rq);
  1599. if (dst_rq->curr->sched_class == &fair_sched_class) {
  1600. if (!cpumask_test_cpu(dst_cpu,
  1601. &(src_rq->curr)->cpus_allowed) ||
  1602. !cpumask_test_cpu(src_cpu,
  1603. &(dst_rq->curr)->cpus_allowed)) {
  1604. double_rq_unlock(src_rq, dst_rq);
  1605. return;
  1606. }
  1607. if (!src_rq->active_balance && !dst_rq->active_balance) {
  1608. src_rq->active_balance = MIGR_ROTATION;
  1609. dst_rq->active_balance = MIGR_ROTATION;
  1610. get_task_struct(src_rq->curr);
  1611. get_task_struct(dst_rq->curr);
  1612. wr = &per_cpu(task_rotate_works, src_cpu);
  1613. wr->src_task = src_rq->curr;
  1614. wr->dst_task = dst_rq->curr;
  1615. wr->src_cpu = src_rq->cpu;
  1616. wr->dst_cpu = dst_rq->cpu;
  1617. force = 1;
  1618. }
  1619. }
  1620. double_rq_unlock(src_rq, dst_rq);
  1621. if (force) {
  1622. queue_work_on(src_cpu, system_highpri_wq, &wr->w);
  1623. trace_sched_big_task_rotation(wr->src_cpu, wr->dst_cpu,
  1624. wr->src_task->pid, wr->dst_task->pid,
  1625. false, set_uclamp);
  1626. }
  1627. }