sde_rsc.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "[sde_rsc:%s:%d]: " fmt, __func__, __LINE__
  6. #include <linux/kernel.h>
  7. #include <linux/debugfs.h>
  8. #include <linux/of.h>
  9. #include <linux/string.h>
  10. #include <linux/of_address.h>
  11. #include <linux/component.h>
  12. #include <linux/slab.h>
  13. #include <linux/mutex.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/module.h>
  16. #include <soc/qcom/rpmh.h>
  17. #include <drm/drmP.h>
  18. #include <drm/drm_irq.h>
  19. #include "sde_rsc_priv.h"
  20. #include "sde_dbg.h"
  21. #include "sde_trace.h"
  22. #define SDE_RSC_DRV_DBG_NAME "sde_rsc_drv"
  23. #define SDE_RSC_WRAPPER_DBG_NAME "sde_rsc_wrapper"
  24. #define SINGLE_TCS_EXECUTION_TIME_V1 1064000
  25. #define SINGLE_TCS_EXECUTION_TIME_V2 930000
  26. #define RSC_MODE_INSTRUCTION_TIME 100
  27. #define RSC_MODE_THRESHOLD_OVERHEAD 2700
  28. /**
  29. * rsc_min_threshold will be set to MIN_THRESHOLD_OVERHEAD_TIME which
  30. * takes into account back off time + overhead from RSC/RSC_WRAPPER. The
  31. * overhead buffer time is required to be greater than 14. Program it
  32. * with a higher value (3.3 ms), so it has sufficient time to complete
  33. * the sequence in rare cases.
  34. */
  35. #define MIN_THRESHOLD_OVERHEAD_TIME 64
  36. #define DEFAULT_PANEL_FPS 60
  37. #define DEFAULT_PANEL_JITTER_NUMERATOR 2
  38. #define DEFAULT_PANEL_JITTER_DENOMINATOR 1
  39. #define DEFAULT_PANEL_PREFILL_LINES 25
  40. #define DEFAULT_PANEL_VTOTAL (480 + DEFAULT_PANEL_PREFILL_LINES)
  41. #define TICKS_IN_NANO_SECOND 1000000000
  42. #define MAX_BUFFER_SIZE 256
  43. #define CMD_MODE_SWITCH_SUCCESS 0xFFFF
  44. #define VID_MODE_SWITCH_SUCCESS 0xFFFE
  45. #define CLK_MODE_SWITCH_SUCCESS 0xFFFD
  46. #define STATE_UPDATE_NOT_ALLOWED 0xFFFC
  47. /* Primary panel worst case VSYNC expected to be no less than 30fps */
  48. #define PRIMARY_VBLANK_WORST_CASE_MS 34
  49. #define DEFAULT_PANEL_MIN_V_PREFILL 35
  50. static struct sde_rsc_priv *rsc_prv_list[MAX_RSC_COUNT];
  51. static struct device *rpmh_dev[MAX_RSC_COUNT];
  52. static void sde_rsc_set_data_bus_mode(struct sde_power_handle *phandle, u32 tag)
  53. {
  54. int i = 0, j = 0;
  55. for (i = 0; i < SDE_POWER_HANDLE_DBUS_ID_MAX; i++) {
  56. if (!phandle->data_bus_handle[i].bus_active_only)
  57. continue;
  58. for (j = 0; j < phandle->data_bus_handle[i].data_paths_cnt; j++)
  59. icc_set_tag(phandle->data_bus_handle[i].data_bus_hdl[j],
  60. tag);
  61. }
  62. }
  63. /**
  64. * sde_rsc_client_create() - create the client for sde rsc.
  65. * Different displays like DSI, HDMI, DP, WB, etc should call this
  66. * api to register their vote for rpmh. They still need to vote for
  67. * power handle to get the clocks.
  68. * @rsc_index: A client will be created on this RSC. As of now only
  69. * SDE_RSC_INDEX is valid rsc index.
  70. * @name: Caller needs to provide some valid string to identify
  71. * the client. "primary", "dp", "hdmi" are suggested name.
  72. * @is_primary: Caller needs to provide information if client is primary
  73. * or not. Primary client votes will be redirected to
  74. * display rsc.
  75. * @vsync_source: This parameter is only valid for primary display. It provides
  76. * vsync source information
  77. *
  78. * Return: client node pointer.
  79. */
  80. struct sde_rsc_client *sde_rsc_client_create(u32 rsc_index, char *client_name,
  81. enum sde_rsc_client_type client_type, u32 vsync_source)
  82. {
  83. struct sde_rsc_client *client;
  84. struct sde_rsc_priv *rsc;
  85. static int id;
  86. if (!client_name) {
  87. pr_err("client name is null- not supported\n");
  88. return ERR_PTR(-EINVAL);
  89. } else if (rsc_index >= MAX_RSC_COUNT) {
  90. pr_err("invalid rsc index\n");
  91. return ERR_PTR(-EINVAL);
  92. } else if (!rsc_prv_list[rsc_index]) {
  93. pr_debug("rsc not probed yet or not available\n");
  94. return NULL;
  95. }
  96. rsc = rsc_prv_list[rsc_index];
  97. client = kzalloc(sizeof(struct sde_rsc_client), GFP_KERNEL);
  98. if (!client)
  99. return ERR_PTR(-ENOMEM);
  100. mutex_lock(&rsc->client_lock);
  101. strlcpy(client->name, client_name, MAX_RSC_CLIENT_NAME_LEN);
  102. client->current_state = SDE_RSC_IDLE_STATE;
  103. client->rsc_index = rsc_index;
  104. client->id = id;
  105. client->client_type = client_type;
  106. if (client->client_type == SDE_RSC_PRIMARY_DISP_CLIENT) {
  107. rsc->primary_client = client;
  108. rsc->vsync_source = vsync_source;
  109. }
  110. pr_debug("client %s rsc index:%d client_type:%d\n", client_name,
  111. rsc_index, client->client_type);
  112. list_add(&client->list, &rsc->client_list);
  113. id++;
  114. mutex_unlock(&rsc->client_lock);
  115. return client;
  116. }
  117. EXPORT_SYMBOL(sde_rsc_client_create);
  118. /**
  119. * sde_rsc_client_destroy() - Destroy the sde rsc client.
  120. *
  121. * @client: Client pointer provided by sde_rsc_client_create().
  122. *
  123. * Return: none
  124. */
  125. void sde_rsc_client_destroy(struct sde_rsc_client *client)
  126. {
  127. struct sde_rsc_priv *rsc;
  128. enum sde_rsc_state state;
  129. if (!client) {
  130. pr_debug("invalid client\n");
  131. goto end;
  132. } else if (client->rsc_index >= MAX_RSC_COUNT) {
  133. pr_err("invalid rsc index\n");
  134. goto end;
  135. }
  136. pr_debug("client %s destroyed\n", client->name);
  137. rsc = rsc_prv_list[client->rsc_index];
  138. if (!rsc)
  139. goto end;
  140. mutex_lock(&rsc->client_lock);
  141. state = client->current_state;
  142. mutex_unlock(&rsc->client_lock);
  143. if (state != SDE_RSC_IDLE_STATE) {
  144. int wait_vblank_crtc_id;
  145. sde_rsc_client_state_update(client, SDE_RSC_IDLE_STATE, NULL,
  146. SDE_RSC_INVALID_CRTC_ID, &wait_vblank_crtc_id);
  147. /* if vblank wait required at shutdown, use a simple sleep */
  148. if (wait_vblank_crtc_id != SDE_RSC_INVALID_CRTC_ID) {
  149. pr_err("unexpected sleep required on crtc %d at rsc client destroy\n",
  150. wait_vblank_crtc_id);
  151. SDE_EVT32(client->id, state, rsc->current_state,
  152. client->crtc_id, wait_vblank_crtc_id,
  153. SDE_EVTLOG_ERROR);
  154. msleep(PRIMARY_VBLANK_WORST_CASE_MS);
  155. }
  156. }
  157. mutex_lock(&rsc->client_lock);
  158. list_del_init(&client->list);
  159. mutex_unlock(&rsc->client_lock);
  160. kfree(client);
  161. end:
  162. return;
  163. }
  164. EXPORT_SYMBOL(sde_rsc_client_destroy);
  165. struct sde_rsc_event *sde_rsc_register_event(int rsc_index, uint32_t event_type,
  166. void (*cb_func)(uint32_t event_type, void *usr), void *usr)
  167. {
  168. struct sde_rsc_event *evt;
  169. struct sde_rsc_priv *rsc;
  170. if (rsc_index >= MAX_RSC_COUNT) {
  171. pr_err("invalid rsc index:%d\n", rsc_index);
  172. return ERR_PTR(-EINVAL);
  173. } else if (!rsc_prv_list[rsc_index]) {
  174. pr_err("rsc idx:%d not probed yet or not available\n",
  175. rsc_index);
  176. return ERR_PTR(-EINVAL);
  177. } else if (!cb_func || !event_type) {
  178. pr_err("no event or cb func\n");
  179. return ERR_PTR(-EINVAL);
  180. }
  181. rsc = rsc_prv_list[rsc_index];
  182. evt = kzalloc(sizeof(struct sde_rsc_event), GFP_KERNEL);
  183. if (!evt)
  184. return ERR_PTR(-ENOMEM);
  185. evt->event_type = event_type;
  186. evt->rsc_index = rsc_index;
  187. evt->usr = usr;
  188. evt->cb_func = cb_func;
  189. pr_debug("event register type:%d rsc index:%d\n",
  190. event_type, rsc_index);
  191. mutex_lock(&rsc->client_lock);
  192. list_add(&evt->list, &rsc->event_list);
  193. mutex_unlock(&rsc->client_lock);
  194. return evt;
  195. }
  196. EXPORT_SYMBOL(sde_rsc_register_event);
  197. void sde_rsc_unregister_event(struct sde_rsc_event *event)
  198. {
  199. struct sde_rsc_priv *rsc;
  200. if (!event) {
  201. pr_debug("invalid event client\n");
  202. goto end;
  203. } else if (event->rsc_index >= MAX_RSC_COUNT) {
  204. pr_err("invalid rsc index\n");
  205. goto end;
  206. }
  207. pr_debug("event client destroyed\n");
  208. rsc = rsc_prv_list[event->rsc_index];
  209. if (!rsc)
  210. goto end;
  211. mutex_lock(&rsc->client_lock);
  212. list_del_init(&event->list);
  213. mutex_unlock(&rsc->client_lock);
  214. kfree(event);
  215. end:
  216. return;
  217. }
  218. EXPORT_SYMBOL(sde_rsc_unregister_event);
  219. bool is_sde_rsc_available(int rsc_index)
  220. {
  221. if (rsc_index >= MAX_RSC_COUNT) {
  222. pr_err("invalid rsc index:%d\n", rsc_index);
  223. return false;
  224. } else if (!rsc_prv_list[rsc_index]) {
  225. pr_debug("rsc idx:%d not probed yet or not available\n",
  226. rsc_index);
  227. return false;
  228. }
  229. return true;
  230. }
  231. EXPORT_SYMBOL(is_sde_rsc_available);
  232. enum sde_rsc_state get_sde_rsc_current_state(int rsc_index)
  233. {
  234. struct sde_rsc_priv *rsc;
  235. if (rsc_index >= MAX_RSC_COUNT) {
  236. pr_err("invalid rsc index:%d\n", rsc_index);
  237. return SDE_RSC_IDLE_STATE;
  238. } else if (!rsc_prv_list[rsc_index]) {
  239. pr_err("rsc idx:%d not probed yet or not available\n",
  240. rsc_index);
  241. return SDE_RSC_IDLE_STATE;
  242. }
  243. rsc = rsc_prv_list[rsc_index];
  244. return rsc->current_state;
  245. }
  246. EXPORT_SYMBOL(get_sde_rsc_current_state);
  247. static u32 sde_rsc_timer_calculate(struct sde_rsc_priv *rsc,
  248. struct sde_rsc_cmd_config *cmd_config, enum sde_rsc_state state)
  249. {
  250. const u32 cxo_period_ns = 52;
  251. u64 rsc_backoff_time_ns = rsc->backoff_time_ns;
  252. u64 rsc_mode_threshold_time_ns = rsc->mode_threshold_time_ns;
  253. u64 rsc_time_slot_0_ns = rsc->time_slot_0_ns;
  254. u64 rsc_time_slot_1_ns;
  255. const u64 pdc_jitter = 20; /* 20% more */
  256. u64 frame_time_ns, frame_jitter;
  257. u64 line_time_ns, prefill_time_ns;
  258. u64 pdc_backoff_time_ns;
  259. s64 total;
  260. int ret = 0;
  261. u32 default_prefill_lines;
  262. if (cmd_config)
  263. memcpy(&rsc->cmd_config, cmd_config, sizeof(*cmd_config));
  264. /* calculate for 640x480 60 fps resolution by default */
  265. if (!rsc->cmd_config.fps)
  266. rsc->cmd_config.fps = DEFAULT_PANEL_FPS;
  267. if (!rsc->cmd_config.jitter_numer)
  268. rsc->cmd_config.jitter_numer = DEFAULT_PANEL_JITTER_NUMERATOR;
  269. if (!rsc->cmd_config.jitter_denom)
  270. rsc->cmd_config.jitter_denom = DEFAULT_PANEL_JITTER_DENOMINATOR;
  271. if (!rsc->cmd_config.vtotal)
  272. rsc->cmd_config.vtotal = DEFAULT_PANEL_VTOTAL;
  273. default_prefill_lines = (rsc->cmd_config.fps *
  274. DEFAULT_PANEL_MIN_V_PREFILL) / DEFAULT_PANEL_FPS;
  275. if ((state == SDE_RSC_CMD_STATE) || !rsc->cmd_config.prefill_lines)
  276. rsc->cmd_config.prefill_lines = default_prefill_lines;
  277. pr_debug("frame fps:%d jitter_numer:%d jitter_denom:%d vtotal:%d prefill lines:%d\n",
  278. rsc->cmd_config.fps, rsc->cmd_config.jitter_numer,
  279. rsc->cmd_config.jitter_denom, rsc->cmd_config.vtotal,
  280. rsc->cmd_config.prefill_lines);
  281. /* 1 nano second */
  282. frame_time_ns = TICKS_IN_NANO_SECOND;
  283. frame_time_ns = div_u64(frame_time_ns, rsc->cmd_config.fps);
  284. frame_jitter = frame_time_ns * rsc->cmd_config.jitter_numer;
  285. frame_jitter = div_u64(frame_jitter, rsc->cmd_config.jitter_denom);
  286. /* convert it to percentage */
  287. frame_jitter = div_u64(frame_jitter, 100);
  288. line_time_ns = frame_time_ns;
  289. line_time_ns = div_u64(line_time_ns, rsc->cmd_config.vtotal);
  290. prefill_time_ns = line_time_ns * rsc->cmd_config.prefill_lines;
  291. /* only take jitter into account for CMD mode */
  292. if (state == SDE_RSC_CMD_STATE)
  293. total = frame_time_ns - frame_jitter - prefill_time_ns;
  294. else
  295. total = frame_time_ns - prefill_time_ns;
  296. if (total < 0) {
  297. pr_err("invalid total time period time:%llu jiter_time:%llu blanking time:%llu\n",
  298. frame_time_ns, frame_jitter, prefill_time_ns);
  299. total = 0;
  300. }
  301. total = div_u64(total, cxo_period_ns);
  302. rsc->timer_config.static_wakeup_time_ns = total;
  303. pr_debug("frame time:%llu frame jiter_time:%llu\n",
  304. frame_time_ns, frame_jitter);
  305. pr_debug("line time:%llu prefill time ps:%llu\n",
  306. line_time_ns, prefill_time_ns);
  307. pr_debug("static wakeup time:%lld cxo:%u\n", total, cxo_period_ns);
  308. pdc_backoff_time_ns = rsc_backoff_time_ns;
  309. rsc_backoff_time_ns = div_u64(rsc_backoff_time_ns, cxo_period_ns);
  310. rsc->timer_config.rsc_backoff_time_ns = (u32) rsc_backoff_time_ns;
  311. pdc_backoff_time_ns *= pdc_jitter;
  312. pdc_backoff_time_ns = div_u64(pdc_backoff_time_ns, 100);
  313. rsc->timer_config.pdc_backoff_time_ns = (u32) pdc_backoff_time_ns;
  314. rsc_mode_threshold_time_ns =
  315. div_u64(rsc_mode_threshold_time_ns, cxo_period_ns);
  316. rsc->timer_config.rsc_mode_threshold_time_ns
  317. = (u32) rsc_mode_threshold_time_ns;
  318. /* time_slot_0 for mode0 latency */
  319. rsc_time_slot_0_ns = div_u64(rsc_time_slot_0_ns, cxo_period_ns);
  320. rsc->timer_config.rsc_time_slot_0_ns = (u32) rsc_time_slot_0_ns;
  321. /* time_slot_1 for mode1 latency */
  322. rsc_time_slot_1_ns = frame_time_ns;
  323. rsc_time_slot_1_ns = div_u64(rsc_time_slot_1_ns, cxo_period_ns);
  324. rsc->timer_config.rsc_time_slot_1_ns = (u32) rsc_time_slot_1_ns;
  325. /* mode 2 is infinite */
  326. rsc->timer_config.rsc_time_slot_2_ns = 0xFFFFFFFF;
  327. rsc->timer_config.min_threshold_time_ns = MIN_THRESHOLD_OVERHEAD_TIME;
  328. rsc->timer_config.bwi_threshold_time_ns =
  329. rsc->timer_config.rsc_time_slot_0_ns;
  330. /* timer update should be called with client call */
  331. if (cmd_config && rsc->hw_ops.timer_update) {
  332. ret = rsc->hw_ops.timer_update(rsc);
  333. if (ret)
  334. pr_err("sde rsc: hw timer update failed ret:%d\n", ret);
  335. /* rsc init should be called during rsc probe - one time only */
  336. } else if (rsc->hw_ops.init) {
  337. ret = rsc->hw_ops.init(rsc);
  338. if (ret)
  339. pr_err("sde rsc: hw init failed ret:%d\n", ret);
  340. }
  341. return ret;
  342. }
  343. static int sde_rsc_resource_disable(struct sde_rsc_priv *rsc)
  344. {
  345. struct sde_power_handle *phandle;
  346. struct dss_module_power *mp;
  347. if (!rsc) {
  348. pr_err("invalid drv data\n");
  349. return -EINVAL;
  350. }
  351. if (atomic_read(&rsc->resource_refcount) == 0) {
  352. pr_err("%pS: invalid rsc resource disable call\n",
  353. __builtin_return_address(0));
  354. return -EINVAL;
  355. }
  356. if (atomic_dec_return(&rsc->resource_refcount) != 0)
  357. return 0;
  358. phandle = &rsc->phandle;
  359. mp = &phandle->mp;
  360. msm_dss_enable_clk(mp->clk_config, mp->num_clk, false);
  361. sde_power_scale_reg_bus(phandle, VOTE_INDEX_DISABLE, false);
  362. msm_dss_enable_vreg(mp->vreg_config, mp->num_vreg, false);
  363. return 0;
  364. }
  365. static int sde_rsc_resource_enable(struct sde_rsc_priv *rsc)
  366. {
  367. struct sde_power_handle *phandle;
  368. struct dss_module_power *mp;
  369. int rc = 0;
  370. if (!rsc) {
  371. pr_err("invalid drv data\n");
  372. return -EINVAL;
  373. }
  374. if (atomic_inc_return(&rsc->resource_refcount) != 1)
  375. return 0;
  376. phandle = &rsc->phandle;
  377. mp = &phandle->mp;
  378. rc = msm_dss_enable_vreg(mp->vreg_config, mp->num_vreg, true);
  379. if (rc) {
  380. pr_err("failed to enable vregs rc=%d\n", rc);
  381. goto end;
  382. }
  383. rc = sde_power_scale_reg_bus(phandle, VOTE_INDEX_LOW, false);
  384. if (rc) {
  385. pr_err("failed to set reg bus vote rc=%d\n", rc);
  386. goto reg_bus_hdl_err;
  387. }
  388. rc = msm_dss_enable_clk(mp->clk_config, mp->num_clk, true);
  389. if (rc) {
  390. pr_err("clock enable failed rc:%d\n", rc);
  391. goto clk_err;
  392. }
  393. return rc;
  394. clk_err:
  395. sde_power_scale_reg_bus(phandle, VOTE_INDEX_DISABLE, false);
  396. reg_bus_hdl_err:
  397. msm_dss_enable_vreg(mp->vreg_config, mp->num_vreg, false);
  398. end:
  399. atomic_dec(&rsc->resource_refcount);
  400. return rc;
  401. }
  402. static int sde_rsc_switch_to_cmd(struct sde_rsc_priv *rsc,
  403. struct sde_rsc_cmd_config *config,
  404. struct sde_rsc_client *caller_client,
  405. int *wait_vblank_crtc_id)
  406. {
  407. struct sde_rsc_client *client;
  408. int rc = STATE_UPDATE_NOT_ALLOWED;
  409. if (!rsc->primary_client) {
  410. pr_err("primary client not available for cmd state switch\n");
  411. rc = -EINVAL;
  412. goto end;
  413. } else if (caller_client != rsc->primary_client) {
  414. pr_err("primary client state:%d not cmd state request\n",
  415. rsc->primary_client->current_state);
  416. rc = -EINVAL;
  417. goto end;
  418. }
  419. /* update timers - might not be available at next switch */
  420. if (config)
  421. sde_rsc_timer_calculate(rsc, config, SDE_RSC_CMD_STATE);
  422. /**
  423. * rsc clients can still send config at any time. If a config is
  424. * received during cmd_state then vsync_wait will execute with the logic
  425. * below. If a config is received when rsc is in AMC mode; A mode
  426. * switch will do the vsync wait. updated checks still support all cases
  427. * for dynamic mode switch and inline rotation.
  428. */
  429. if (rsc->current_state == SDE_RSC_CMD_STATE) {
  430. rc = 0;
  431. if (config && rsc->version < SDE_RSC_REV_3)
  432. goto vsync_wait;
  433. else
  434. goto end;
  435. }
  436. /* any non-primary clk state client blocks the cmd state switch */
  437. list_for_each_entry(client, &rsc->client_list, list)
  438. if (client->current_state == SDE_RSC_CLK_STATE &&
  439. client->client_type == SDE_RSC_EXTERNAL_DISP_CLIENT)
  440. goto end;
  441. if (rsc->hw_ops.state_update) {
  442. rc = rsc->hw_ops.state_update(rsc, SDE_RSC_CMD_STATE);
  443. if (!rc) {
  444. rpmh_mode_solver_set(rsc->rpmh_dev, true);
  445. sde_rsc_set_data_bus_mode(&rsc->phandle,
  446. QCOM_ICC_TAG_WAKE);
  447. }
  448. }
  449. /* vsync wait not needed during VID->CMD switch (rev 4+ HW only) */
  450. if (rsc->current_state == SDE_RSC_VID_STATE &&
  451. rsc->version >= SDE_RSC_REV_4) {
  452. rc = 0;
  453. goto end;
  454. }
  455. vsync_wait:
  456. /* indicate wait for vsync for vid to cmd state switch & cfg update */
  457. if (!rc && (rsc->current_state == SDE_RSC_VID_STATE ||
  458. rsc->current_state == SDE_RSC_CMD_STATE)) {
  459. rsc->post_poms = true;
  460. /* clear VSYNC timestamp for indication when update completes */
  461. if (rsc->hw_ops.hw_vsync)
  462. rsc->hw_ops.hw_vsync(rsc, VSYNC_ENABLE, NULL, 0, 0);
  463. if (!wait_vblank_crtc_id) {
  464. pr_err("invalid crtc id wait pointer, client %d\n",
  465. caller_client->id);
  466. SDE_EVT32(caller_client->id, rsc->current_state,
  467. caller_client->crtc_id,
  468. wait_vblank_crtc_id, SDE_EVTLOG_ERROR);
  469. msleep(PRIMARY_VBLANK_WORST_CASE_MS);
  470. } else {
  471. *wait_vblank_crtc_id = rsc->primary_client->crtc_id;
  472. }
  473. }
  474. end:
  475. return rc;
  476. }
  477. static int sde_rsc_switch_to_clk(struct sde_rsc_priv *rsc,
  478. int *wait_vblank_crtc_id)
  479. {
  480. struct sde_rsc_client *client;
  481. int rc = STATE_UPDATE_NOT_ALLOWED;
  482. bool multi_display_active = false;
  483. bool vid_display_active = false, cmd_display_active = false;
  484. list_for_each_entry(client, &rsc->client_list, list) {
  485. if (client->current_state == SDE_RSC_CLK_STATE &&
  486. client->client_type == SDE_RSC_EXTERNAL_DISP_CLIENT)
  487. multi_display_active = true;
  488. else if (client->current_state == SDE_RSC_VID_STATE)
  489. vid_display_active = true;
  490. else if (client->current_state == SDE_RSC_CMD_STATE)
  491. cmd_display_active = true;
  492. }
  493. pr_debug("multi_display:%d vid_display:%d cmd_display:%d\n",
  494. multi_display_active, vid_display_active, cmd_display_active);
  495. if (!multi_display_active && (vid_display_active || cmd_display_active))
  496. goto end;
  497. if (rsc->hw_ops.state_update) {
  498. rc = rsc->hw_ops.state_update(rsc, SDE_RSC_CLK_STATE);
  499. if (!rc) {
  500. rpmh_mode_solver_set(rsc->rpmh_dev, false);
  501. sde_rsc_set_data_bus_mode(&rsc->phandle,
  502. QCOM_ICC_TAG_AMC);
  503. }
  504. }
  505. /* indicate wait for vsync for cmd/vid to clk state switch */
  506. if (!rc && rsc->primary_client &&
  507. (rsc->current_state == SDE_RSC_CMD_STATE ||
  508. rsc->current_state == SDE_RSC_VID_STATE)) {
  509. /* clear VSYNC timestamp for indication when update completes */
  510. if (rsc->hw_ops.hw_vsync)
  511. rsc->hw_ops.hw_vsync(rsc, VSYNC_ENABLE, NULL, 0, 0);
  512. if (!wait_vblank_crtc_id) {
  513. pr_err("invalid crtc id wait pointer provided\n");
  514. msleep(PRIMARY_VBLANK_WORST_CASE_MS);
  515. } else {
  516. *wait_vblank_crtc_id = rsc->primary_client->crtc_id;
  517. /* increase refcount, so we wait for the next vsync */
  518. atomic_inc(&rsc->rsc_vsync_wait);
  519. SDE_EVT32(atomic_read(&rsc->rsc_vsync_wait));
  520. }
  521. } else if (atomic_read(&rsc->rsc_vsync_wait)) {
  522. SDE_EVT32(rsc->primary_client, rsc->current_state,
  523. atomic_read(&rsc->rsc_vsync_wait));
  524. /* Wait for the vsync, if the refcount is set */
  525. rc = wait_event_timeout(rsc->rsc_vsync_waitq,
  526. atomic_read(&rsc->rsc_vsync_wait) == 0,
  527. msecs_to_jiffies(PRIMARY_VBLANK_WORST_CASE_MS*2));
  528. if (!rc) {
  529. pr_err("Timeout waiting for vsync\n");
  530. rc = -ETIMEDOUT;
  531. SDE_EVT32(atomic_read(&rsc->rsc_vsync_wait), rc,
  532. SDE_EVTLOG_ERROR);
  533. } else {
  534. SDE_EVT32(atomic_read(&rsc->rsc_vsync_wait), rc);
  535. rc = 0;
  536. }
  537. }
  538. end:
  539. return rc;
  540. }
  541. static int sde_rsc_switch_to_vid(struct sde_rsc_priv *rsc,
  542. struct sde_rsc_cmd_config *config,
  543. struct sde_rsc_client *caller_client,
  544. int *wait_vblank_crtc_id)
  545. {
  546. struct sde_rsc_client *client;
  547. int rc = STATE_UPDATE_NOT_ALLOWED;
  548. if (!rsc->primary_client) {
  549. pr_err("primary client not available for vid state switch\n");
  550. rc = -EINVAL;
  551. goto end;
  552. } else if (caller_client != rsc->primary_client) {
  553. pr_err("primary client state:%d not vid state request\n",
  554. rsc->primary_client->current_state);
  555. rc = -EINVAL;
  556. goto end;
  557. }
  558. /* update timers - might not be available at next switch */
  559. if (config)
  560. sde_rsc_timer_calculate(rsc, config, SDE_RSC_VID_STATE);
  561. /**
  562. * rsc clients can still send config at any time. If a config is
  563. * received during vid_state then vsync_wait will execute with the logic
  564. * below.
  565. */
  566. if (rsc->current_state == SDE_RSC_VID_STATE) {
  567. rc = 0;
  568. if (config && rsc->version < SDE_RSC_REV_3)
  569. goto vsync_wait;
  570. else
  571. goto end;
  572. }
  573. /* any non-primary clk state client blocks the vid state switch */
  574. list_for_each_entry(client, &rsc->client_list, list)
  575. if (client->current_state == SDE_RSC_CLK_STATE &&
  576. client->client_type == SDE_RSC_EXTERNAL_DISP_CLIENT)
  577. goto end;
  578. if (rsc->hw_ops.state_update) {
  579. rc = rsc->hw_ops.state_update(rsc, SDE_RSC_VID_STATE);
  580. if (!rc) {
  581. rpmh_mode_solver_set(rsc->rpmh_dev,
  582. rsc->version >= SDE_RSC_REV_3);
  583. sde_rsc_set_data_bus_mode(&rsc->phandle,
  584. rsc->version >= SDE_RSC_REV_3 ?
  585. QCOM_ICC_TAG_WAKE : QCOM_ICC_TAG_AMC);
  586. }
  587. }
  588. /* vsync wait not needed during CMD->VID switch (rev 4+ HW only) */
  589. if (rsc->current_state == SDE_RSC_CMD_STATE &&
  590. rsc->version >= SDE_RSC_REV_4) {
  591. rc = 0;
  592. goto end;
  593. }
  594. vsync_wait:
  595. /* indicate wait for vsync for vid to cmd state switch & cfg update */
  596. if (!rc && (rsc->current_state == SDE_RSC_VID_STATE ||
  597. rsc->current_state == SDE_RSC_CMD_STATE)) {
  598. rsc->post_poms = true;
  599. /* clear VSYNC timestamp for indication when update completes */
  600. if (rsc->hw_ops.hw_vsync)
  601. rsc->hw_ops.hw_vsync(rsc, VSYNC_ENABLE, NULL, 0, 0);
  602. if (!wait_vblank_crtc_id) {
  603. pr_err("invalid crtc id wait pointer, client %d\n",
  604. caller_client->id);
  605. SDE_EVT32(caller_client->id, rsc->current_state,
  606. caller_client->crtc_id,
  607. wait_vblank_crtc_id, SDE_EVTLOG_ERROR);
  608. msleep(PRIMARY_VBLANK_WORST_CASE_MS);
  609. } else {
  610. *wait_vblank_crtc_id = rsc->primary_client->crtc_id;
  611. }
  612. }
  613. end:
  614. return rc;
  615. }
  616. static int sde_rsc_switch_to_idle(struct sde_rsc_priv *rsc,
  617. struct sde_rsc_cmd_config *config,
  618. struct sde_rsc_client *caller_client,
  619. int *wait_vblank_crtc_id)
  620. {
  621. struct sde_rsc_client *client;
  622. int rc = STATE_UPDATE_NOT_ALLOWED;
  623. bool clk_client_active = false, multi_display_active = false;
  624. bool vid_display_active = false, cmd_display_active = false;
  625. /*
  626. * following code needs to run the loop through each
  627. * client because they might be in different order
  628. * sorting is not possible; only preference is available
  629. */
  630. list_for_each_entry(client, &rsc->client_list, list) {
  631. if (client->current_state == SDE_RSC_CLK_STATE &&
  632. client->client_type == SDE_RSC_EXTERNAL_DISP_CLIENT)
  633. multi_display_active = true;
  634. else if (client->current_state == SDE_RSC_CLK_STATE &&
  635. client->client_type == SDE_RSC_CLK_CLIENT)
  636. clk_client_active = true;
  637. else if (client->current_state == SDE_RSC_VID_STATE)
  638. vid_display_active = true;
  639. else if (client->current_state == SDE_RSC_CMD_STATE)
  640. cmd_display_active = true;
  641. pr_debug("client state:%d type:%d\n",
  642. client->current_state, client->client_type);
  643. }
  644. pr_debug("multi_display:%d clk_client:%d vid_display:%d cmd_display:%d\n",
  645. multi_display_active, clk_client_active, vid_display_active,
  646. cmd_display_active);
  647. if (vid_display_active && !multi_display_active) {
  648. rc = sde_rsc_switch_to_vid(rsc, NULL, rsc->primary_client,
  649. wait_vblank_crtc_id);
  650. if (!rc)
  651. rc = VID_MODE_SWITCH_SUCCESS;
  652. } else if (cmd_display_active && !multi_display_active) {
  653. rc = sde_rsc_switch_to_cmd(rsc, NULL, rsc->primary_client,
  654. wait_vblank_crtc_id);
  655. if (!rc)
  656. rc = CMD_MODE_SWITCH_SUCCESS;
  657. } else if (clk_client_active) {
  658. rc = sde_rsc_switch_to_clk(rsc, wait_vblank_crtc_id);
  659. if (!rc)
  660. rc = CLK_MODE_SWITCH_SUCCESS;
  661. } else if (rsc->hw_ops.state_update) {
  662. rc = rsc->hw_ops.state_update(rsc, SDE_RSC_IDLE_STATE);
  663. rsc->post_poms = false;
  664. if (!rc) {
  665. rpmh_mode_solver_set(rsc->rpmh_dev, true);
  666. sde_rsc_set_data_bus_mode(&rsc->phandle,
  667. QCOM_ICC_TAG_WAKE);
  668. }
  669. }
  670. return rc;
  671. }
  672. /**
  673. * sde_rsc_client_get_vsync_refcount() - returns the status of the vsync
  674. * refcount, to signal if the client needs to reset the refcounting logic
  675. * @client: Client pointer provided by sde_rsc_client_create().
  676. *
  677. * Return: value of the vsync refcount.
  678. */
  679. int sde_rsc_client_get_vsync_refcount(
  680. struct sde_rsc_client *caller_client)
  681. {
  682. struct sde_rsc_priv *rsc;
  683. if (!caller_client) {
  684. pr_err("invalid client for rsc state update\n");
  685. return -EINVAL;
  686. } else if (caller_client->rsc_index >= MAX_RSC_COUNT) {
  687. pr_err("invalid rsc index\n");
  688. return -EINVAL;
  689. }
  690. rsc = rsc_prv_list[caller_client->rsc_index];
  691. if (!rsc)
  692. return 0;
  693. return atomic_read(&rsc->rsc_vsync_wait);
  694. }
  695. /**
  696. * sde_rsc_client_reset_vsync_refcount() - reduces the refcounting
  697. * logic that waits for the vsync.
  698. * @client: Client pointer provided by sde_rsc_client_create().
  699. *
  700. * Return: zero if refcount was already zero.
  701. */
  702. int sde_rsc_client_reset_vsync_refcount(
  703. struct sde_rsc_client *caller_client)
  704. {
  705. struct sde_rsc_priv *rsc;
  706. int ret;
  707. if (!caller_client) {
  708. pr_err("invalid client for rsc state update\n");
  709. return -EINVAL;
  710. } else if (caller_client->rsc_index >= MAX_RSC_COUNT) {
  711. pr_err("invalid rsc index\n");
  712. return -EINVAL;
  713. }
  714. rsc = rsc_prv_list[caller_client->rsc_index];
  715. if (!rsc)
  716. return 0;
  717. ret = atomic_add_unless(&rsc->rsc_vsync_wait, -1, 0);
  718. wake_up_all(&rsc->rsc_vsync_waitq);
  719. SDE_EVT32(atomic_read(&rsc->rsc_vsync_wait));
  720. return ret;
  721. }
  722. /**
  723. * sde_rsc_client_is_state_update_complete() - check if state update is complete
  724. * RSC state transition is not complete until HW receives VBLANK signal. This
  725. * function checks RSC HW to determine whether that signal has been received.
  726. * @client: Client pointer provided by sde_rsc_client_create().
  727. *
  728. * Return: true if the state update has completed.
  729. */
  730. bool sde_rsc_client_is_state_update_complete(
  731. struct sde_rsc_client *caller_client)
  732. {
  733. struct sde_rsc_priv *rsc;
  734. u32 vsync_timestamp0 = 0;
  735. if (!caller_client) {
  736. pr_err("invalid client for rsc state update\n");
  737. return false;
  738. } else if (caller_client->rsc_index >= MAX_RSC_COUNT) {
  739. pr_err("invalid rsc index\n");
  740. return false;
  741. }
  742. rsc = rsc_prv_list[caller_client->rsc_index];
  743. if (!rsc)
  744. return false;
  745. /**
  746. * state updates clear VSYNC timestamp, check if a new one arrived.
  747. * use VSYNC mode 0 (CMD TE) always for this, per HW recommendation.
  748. */
  749. if (rsc->hw_ops.hw_vsync)
  750. vsync_timestamp0 = rsc->hw_ops.hw_vsync(rsc, VSYNC_READ_VSYNC0,
  751. NULL, 0, 0);
  752. return vsync_timestamp0 != 0;
  753. }
  754. /**
  755. * sde_rsc_client_state_update() - rsc client state update
  756. * Video mode, cmd mode and clk state are suppoed as modes. A client need to
  757. * set this property during panel config time. A switching client can set the
  758. * property to change the state
  759. *
  760. * @client: Client pointer provided by sde_rsc_client_create().
  761. * @state: Client state - video/cmd
  762. * @config: fps, vtotal, porches, etc configuration for command mode
  763. * panel
  764. * @crtc_id: current client's crtc id
  765. * @wait_vblank_crtc_id: Output parameter. If set to non-zero, rsc hw
  766. * state update requires a wait for one vblank on
  767. * the primary crtc. In that case, this output
  768. * param will be set to the crtc on which to wait.
  769. * If SDE_RSC_INVALID_CRTC_ID, no wait necessary
  770. *
  771. * Return: error code.
  772. */
  773. int sde_rsc_client_state_update(struct sde_rsc_client *caller_client,
  774. enum sde_rsc_state state,
  775. struct sde_rsc_cmd_config *config, int crtc_id,
  776. int *wait_vblank_crtc_id)
  777. {
  778. int rc = 0;
  779. struct sde_rsc_priv *rsc;
  780. if (!caller_client) {
  781. pr_err("invalid client for rsc state update\n");
  782. return -EINVAL;
  783. } else if (caller_client->rsc_index >= MAX_RSC_COUNT) {
  784. pr_err("invalid rsc index\n");
  785. return -EINVAL;
  786. }
  787. rsc = rsc_prv_list[caller_client->rsc_index];
  788. if (!rsc)
  789. return -EINVAL;
  790. if (wait_vblank_crtc_id)
  791. *wait_vblank_crtc_id = SDE_RSC_INVALID_CRTC_ID;
  792. mutex_lock(&rsc->client_lock);
  793. SDE_EVT32_VERBOSE(caller_client->id, caller_client->current_state,
  794. state, rsc->current_state, SDE_EVTLOG_FUNC_ENTRY);
  795. caller_client->crtc_id = crtc_id;
  796. caller_client->current_state = state;
  797. if ((rsc->current_state == state) && !config) {
  798. pr_debug("no state change: %d\n", state);
  799. goto end;
  800. }
  801. pr_debug("%pS: rsc state:%d request client:%s state:%d\n",
  802. __builtin_return_address(0), rsc->current_state,
  803. caller_client->name, state);
  804. if ((state == SDE_RSC_VID_STATE) && (rsc->version >= SDE_RSC_REV_3))
  805. state = SDE_RSC_CLK_STATE;
  806. if (rsc->current_state == SDE_RSC_IDLE_STATE)
  807. sde_rsc_resource_enable(rsc);
  808. switch (state) {
  809. case SDE_RSC_IDLE_STATE:
  810. rc = sde_rsc_switch_to_idle(rsc, NULL, rsc->primary_client,
  811. wait_vblank_crtc_id);
  812. if (rc == CMD_MODE_SWITCH_SUCCESS) {
  813. state = SDE_RSC_CMD_STATE;
  814. rc = 0;
  815. } else if (rc == VID_MODE_SWITCH_SUCCESS) {
  816. state = SDE_RSC_VID_STATE;
  817. rc = 0;
  818. } else if (rc == CLK_MODE_SWITCH_SUCCESS) {
  819. state = SDE_RSC_CLK_STATE;
  820. rc = 0;
  821. }
  822. break;
  823. case SDE_RSC_CMD_STATE:
  824. rc = sde_rsc_switch_to_cmd(rsc, config, caller_client,
  825. wait_vblank_crtc_id);
  826. break;
  827. case SDE_RSC_VID_STATE:
  828. rc = sde_rsc_switch_to_vid(rsc, config, caller_client,
  829. wait_vblank_crtc_id);
  830. break;
  831. case SDE_RSC_CLK_STATE:
  832. rc = sde_rsc_switch_to_clk(rsc, wait_vblank_crtc_id);
  833. break;
  834. default:
  835. pr_err("invalid state handling %d\n", state);
  836. break;
  837. }
  838. if (rc == STATE_UPDATE_NOT_ALLOWED) {
  839. rc = 0;
  840. SDE_EVT32(caller_client->id, caller_client->current_state,
  841. state, rsc->current_state, rc, SDE_EVTLOG_FUNC_CASE1);
  842. goto clk_disable;
  843. } else if (rc) {
  844. pr_debug("state:%d update failed rc:%d\n", state, rc);
  845. SDE_EVT32(caller_client->id, caller_client->current_state,
  846. state, rsc->current_state, rc, SDE_EVTLOG_FUNC_CASE2);
  847. goto clk_disable;
  848. }
  849. pr_debug("state switch successfully complete: %d\n", state);
  850. SDE_ATRACE_INT("rsc_state", state);
  851. rsc->current_state = state;
  852. SDE_EVT32(caller_client->id, caller_client->current_state,
  853. state, rsc->current_state, SDE_EVTLOG_FUNC_EXIT);
  854. clk_disable:
  855. if (rsc->current_state == SDE_RSC_IDLE_STATE)
  856. sde_rsc_resource_disable(rsc);
  857. end:
  858. mutex_unlock(&rsc->client_lock);
  859. return rc;
  860. }
  861. EXPORT_SYMBOL(sde_rsc_client_state_update);
  862. /**
  863. * sde_rsc_client_vote() - ab/ib vote from rsc client
  864. *
  865. * @client: Client pointer provided by sde_rsc_client_create().
  866. * @bus_id: data bus for which to be voted
  867. * @ab: aggregated bandwidth vote from client.
  868. * @ib: instant bandwidth vote from client.
  869. *
  870. * Return: error code.
  871. */
  872. int sde_rsc_client_vote(struct sde_rsc_client *caller_client,
  873. u32 bus_id, u64 ab_vote, u64 ib_vote)
  874. {
  875. int rsc_index;
  876. struct sde_rsc_priv *rsc;
  877. if (caller_client && caller_client->rsc_index >= MAX_RSC_COUNT) {
  878. pr_err("invalid rsc client or client index\n");
  879. return -EINVAL;
  880. }
  881. rsc_index = caller_client ? caller_client->rsc_index : SDE_RSC_INDEX;
  882. rsc = rsc_prv_list[rsc_index];
  883. if (!rsc || bus_id >= SDE_POWER_HANDLE_DBUS_ID_MAX)
  884. return -EINVAL;
  885. pr_debug("client:%s ab:%llu ib:%llu\n",
  886. caller_client ? caller_client->name : "unknown",
  887. ab_vote, ib_vote);
  888. mutex_lock(&rsc->client_lock);
  889. rsc->bw_config.new_ab_vote[bus_id] = ab_vote;
  890. rsc->bw_config.new_ib_vote[bus_id] = ib_vote;
  891. mutex_unlock(&rsc->client_lock);
  892. return 0;
  893. }
  894. EXPORT_SYMBOL(sde_rsc_client_vote);
  895. int sde_rsc_client_trigger_vote(struct sde_rsc_client *caller_client,
  896. bool delta_vote)
  897. {
  898. int rc = 0, rsc_index, i;
  899. struct sde_rsc_priv *rsc;
  900. bool bw_increase = false;
  901. if (caller_client && caller_client->rsc_index >= MAX_RSC_COUNT) {
  902. pr_err("invalid rsc index\n");
  903. return -EINVAL;
  904. }
  905. rsc_index = caller_client ? caller_client->rsc_index : SDE_RSC_INDEX;
  906. rsc = rsc_prv_list[rsc_index];
  907. if (!rsc)
  908. return -EINVAL;
  909. pr_debug("client:%s trigger bw delta vote:%d\n",
  910. caller_client ? caller_client->name : "unknown", delta_vote);
  911. mutex_lock(&rsc->client_lock);
  912. for (i = 0; i < SDE_POWER_HANDLE_DBUS_ID_MAX && delta_vote; i++) {
  913. if (rsc->bw_config.new_ab_vote[i] > rsc->bw_config.ab_vote[i] ||
  914. rsc->bw_config.new_ib_vote[i] > rsc->bw_config.ib_vote[i])
  915. bw_increase = true;
  916. rsc->bw_config.ab_vote[i] = rsc->bw_config.new_ab_vote[i];
  917. rsc->bw_config.ib_vote[i] = rsc->bw_config.new_ib_vote[i];
  918. }
  919. rc = sde_rsc_resource_enable(rsc);
  920. if (rc < 0)
  921. goto clk_enable_fail;
  922. if (delta_vote) {
  923. if (rsc->hw_ops.tcs_wait) {
  924. rc = rsc->hw_ops.tcs_wait(rsc);
  925. if (rc) {
  926. pr_err("tcs is still busy; can't send command\n");
  927. if (rsc->hw_ops.tcs_use_ok)
  928. rsc->hw_ops.tcs_use_ok(rsc);
  929. goto end;
  930. }
  931. }
  932. rpmh_invalidate(rsc->rpmh_dev);
  933. for (i = 0; i < SDE_POWER_HANDLE_DBUS_ID_MAX; i++)
  934. sde_power_data_bus_set_quota(&rsc->phandle,
  935. i, rsc->bw_config.ab_vote[i],
  936. rsc->bw_config.ib_vote[i]);
  937. rpmh_flush(rsc->rpmh_dev);
  938. }
  939. if (rsc->hw_ops.bwi_status &&
  940. (rsc->current_state == SDE_RSC_CMD_STATE ||
  941. rsc->current_state == SDE_RSC_VID_STATE))
  942. rsc->hw_ops.bwi_status(rsc, bw_increase);
  943. else if (rsc->hw_ops.tcs_use_ok)
  944. rsc->hw_ops.tcs_use_ok(rsc);
  945. end:
  946. sde_rsc_resource_disable(rsc);
  947. clk_enable_fail:
  948. mutex_unlock(&rsc->client_lock);
  949. return rc;
  950. }
  951. EXPORT_SYMBOL(sde_rsc_client_trigger_vote);
  952. #if defined(CONFIG_DEBUG_FS)
  953. void sde_rsc_debug_dump(u32 mux_sel)
  954. {
  955. struct sde_rsc_priv *rsc;
  956. rsc = rsc_prv_list[SDE_RSC_INDEX];
  957. if (!rsc)
  958. return;
  959. /* this must be called with rsc clocks enabled */
  960. if (rsc->hw_ops.debug_dump)
  961. rsc->hw_ops.debug_dump(rsc, mux_sel);
  962. }
  963. static int _sde_debugfs_status_show(struct seq_file *s, void *data)
  964. {
  965. struct sde_rsc_priv *rsc;
  966. struct sde_rsc_client *client;
  967. int ret;
  968. if (!s || !s->private)
  969. return -EINVAL;
  970. rsc = s->private;
  971. mutex_lock(&rsc->client_lock);
  972. seq_printf(s, "rsc current state:%d\n", rsc->current_state);
  973. seq_printf(s, "wraper backoff time(ns):%d\n",
  974. rsc->timer_config.static_wakeup_time_ns);
  975. seq_printf(s, "rsc backoff time(ns):%d\n",
  976. rsc->timer_config.rsc_backoff_time_ns);
  977. seq_printf(s, "pdc backoff time(ns):%d\n",
  978. rsc->timer_config.pdc_backoff_time_ns);
  979. seq_printf(s, "rsc mode threshold time(ns):%d\n",
  980. rsc->timer_config.rsc_mode_threshold_time_ns);
  981. seq_printf(s, "rsc time slot 0(ns):%d\n",
  982. rsc->timer_config.rsc_time_slot_0_ns);
  983. seq_printf(s, "rsc time slot 1(ns):%d\n",
  984. rsc->timer_config.rsc_time_slot_1_ns);
  985. seq_printf(s, "frame fps:%d jitter_numer:%d jitter_denom:%d vtotal:%d prefill lines:%d\n",
  986. rsc->cmd_config.fps, rsc->cmd_config.jitter_numer,
  987. rsc->cmd_config.jitter_denom,
  988. rsc->cmd_config.vtotal, rsc->cmd_config.prefill_lines);
  989. seq_puts(s, "\n");
  990. list_for_each_entry(client, &rsc->client_list, list)
  991. seq_printf(s, "\t client:%s state:%d\n",
  992. client->name, client->current_state);
  993. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  994. pr_debug("debug node is not supported during idle state\n");
  995. seq_puts(s, "hw state is not supported during idle pc\n");
  996. goto end;
  997. }
  998. if (rsc->hw_ops.debug_show) {
  999. ret = rsc->hw_ops.debug_show(s, rsc);
  1000. if (ret)
  1001. pr_err("sde rsc: hw debug failed ret:%d\n", ret);
  1002. }
  1003. end:
  1004. mutex_unlock(&rsc->client_lock);
  1005. return 0;
  1006. }
  1007. static int _sde_debugfs_status_open(struct inode *inode, struct file *file)
  1008. {
  1009. return single_open(file, _sde_debugfs_status_show, inode->i_private);
  1010. }
  1011. static int _sde_debugfs_counters_show(struct seq_file *s, void *data)
  1012. {
  1013. struct sde_rsc_priv *rsc = s->private;
  1014. u32 counters[NUM_RSC_PROFILING_COUNTERS];
  1015. int i, ret;
  1016. if (!rsc)
  1017. return -EINVAL;
  1018. if (!rsc->hw_ops.get_counters) {
  1019. seq_puts(s, "counters are not supported on this target\n");
  1020. return 0;
  1021. }
  1022. memset(counters, 0, sizeof(counters));
  1023. mutex_lock(&rsc->client_lock);
  1024. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  1025. pr_debug("counters are not supported during idle state\n");
  1026. seq_puts(s, "no access to counters during idle pc\n");
  1027. goto end;
  1028. }
  1029. ret = rsc->hw_ops.get_counters(rsc, counters);
  1030. if (ret) {
  1031. pr_err("sde rsc: get_counters failed ret:%d\n", ret);
  1032. seq_puts(s, "failed to retrieve counts!\n");
  1033. goto end;
  1034. }
  1035. seq_puts(s, "rsc profiling counters:\n");
  1036. for (i = 0; i < NUM_RSC_PROFILING_COUNTERS; ++i)
  1037. seq_printf(s, "\tmode[%d] = 0x%08x:\n", i, counters[i]);
  1038. end:
  1039. mutex_unlock(&rsc->client_lock);
  1040. return 0;
  1041. }
  1042. static int _sde_debugfs_counters_open(struct inode *inode, struct file *file)
  1043. {
  1044. return single_open(file, _sde_debugfs_counters_show, inode->i_private);
  1045. }
  1046. static int _sde_debugfs_generic_noseek_open(struct inode *inode,
  1047. struct file *file)
  1048. {
  1049. /* non-seekable */
  1050. file->private_data = inode->i_private;
  1051. return nonseekable_open(inode, file);
  1052. }
  1053. static ssize_t _sde_debugfs_profiling_read(struct file *file, char __user *buf,
  1054. size_t count, loff_t *ppos)
  1055. {
  1056. struct sde_rsc_priv *rsc = file->private_data;
  1057. size_t max_size = min_t(size_t, count, MAX_BUFFER_SIZE);
  1058. char buffer[MAX_BUFFER_SIZE];
  1059. int blen = 0;
  1060. if (*ppos || !rsc)
  1061. return 0;
  1062. if (!rsc->hw_ops.setup_counters) {
  1063. blen += scnprintf(&buffer[blen], max_size - blen,
  1064. "counters are not supported on this target\n");
  1065. goto end;
  1066. }
  1067. mutex_lock(&rsc->client_lock);
  1068. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  1069. pr_debug("counters are not supported during idle state\n");
  1070. blen += scnprintf(&buffer[blen], max_size - blen,
  1071. "no access to counters during idle pc\n");
  1072. goto unlock;
  1073. }
  1074. blen += scnprintf(&buffer[blen], max_size - blen,
  1075. "%s\n", rsc->profiling_en ? "Y" : "N");
  1076. unlock:
  1077. mutex_unlock(&rsc->client_lock);
  1078. end:
  1079. if (copy_to_user(buf, buffer, blen))
  1080. return -EFAULT;
  1081. *ppos += blen;
  1082. return blen;
  1083. }
  1084. static ssize_t _sde_debugfs_profiling_write(struct file *file,
  1085. const char __user *p, size_t count, loff_t *ppos)
  1086. {
  1087. struct sde_rsc_priv *rsc = file->private_data;
  1088. bool input_valid, input_value;
  1089. char *input;
  1090. int rc;
  1091. if (!rsc || !rsc->hw_ops.setup_counters || !count ||
  1092. count > MAX_COUNT_SIZE_SUPPORTED)
  1093. return 0;
  1094. input = kmalloc(count + 1, GFP_KERNEL);
  1095. if (!input)
  1096. return -ENOMEM;
  1097. if (copy_from_user(input, p, count)) {
  1098. kfree(input);
  1099. return -EFAULT;
  1100. }
  1101. input[count] = '\0';
  1102. switch (input[0]) {
  1103. case 'y':
  1104. case 'Y':
  1105. case '1':
  1106. input_valid = true;
  1107. input_value = true;
  1108. break;
  1109. case 'n':
  1110. case 'N':
  1111. case '0':
  1112. input_valid = true;
  1113. input_value = false;
  1114. break;
  1115. default:
  1116. input_valid = false;
  1117. count = 0;
  1118. break;
  1119. }
  1120. mutex_lock(&rsc->client_lock);
  1121. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  1122. pr_debug("debug node is not supported during idle state\n");
  1123. count = 0;
  1124. goto end;
  1125. }
  1126. pr_debug("input %s, profiling_en: %d\n",
  1127. input_valid ? "valid" : "invalid", rsc->profiling_en);
  1128. if (input_valid) {
  1129. rsc->profiling_en = input_value;
  1130. rc = rsc->hw_ops.setup_counters(rsc, rsc->profiling_en);
  1131. if (rc)
  1132. pr_err("setup_counters failed, rc:%d\n", rc);
  1133. }
  1134. end:
  1135. mutex_unlock(&rsc->client_lock);
  1136. kfree(input);
  1137. return count;
  1138. }
  1139. static ssize_t _sde_debugfs_mode_ctrl_read(struct file *file, char __user *buf,
  1140. size_t count, loff_t *ppos)
  1141. {
  1142. struct sde_rsc_priv *rsc = file->private_data;
  1143. char buffer[MAX_BUFFER_SIZE];
  1144. int blen = 0;
  1145. size_t max_size = min_t(size_t, count, MAX_BUFFER_SIZE);
  1146. if (*ppos || !rsc || !rsc->hw_ops.mode_ctrl)
  1147. return 0;
  1148. mutex_lock(&rsc->client_lock);
  1149. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  1150. pr_debug("debug node is not supported during idle state\n");
  1151. blen = scnprintf(buffer, max_size,
  1152. "hw state is not supported during idle pc\n");
  1153. goto end;
  1154. }
  1155. blen = rsc->hw_ops.mode_ctrl(rsc, MODE_READ, buffer, max_size, 0);
  1156. end:
  1157. mutex_unlock(&rsc->client_lock);
  1158. if (blen <= 0)
  1159. return 0;
  1160. if (blen > count) {
  1161. blen = count;
  1162. buffer[count - 1] = '\0';
  1163. }
  1164. if (copy_to_user(buf, buffer, blen))
  1165. return -EFAULT;
  1166. *ppos += blen;
  1167. return blen;
  1168. }
  1169. static ssize_t _sde_debugfs_mode_ctrl_write(struct file *file,
  1170. const char __user *p, size_t count, loff_t *ppos)
  1171. {
  1172. struct sde_rsc_priv *rsc = file->private_data;
  1173. char *input;
  1174. u32 mode_state = 0;
  1175. int rc;
  1176. if (!rsc || !rsc->hw_ops.mode_ctrl || !count ||
  1177. count > MAX_COUNT_SIZE_SUPPORTED)
  1178. return 0;
  1179. input = kmalloc(count + 1, GFP_KERNEL);
  1180. if (!input)
  1181. return -ENOMEM;
  1182. if (copy_from_user(input, p, count)) {
  1183. kfree(input);
  1184. return -EFAULT;
  1185. }
  1186. input[count] = '\0';
  1187. rc = kstrtoint(input, 0, &mode_state);
  1188. if (rc) {
  1189. pr_err("mode_state: int conversion failed rc:%d\n", rc);
  1190. goto end;
  1191. }
  1192. pr_debug("mode_state: %d\n", mode_state);
  1193. mode_state &= 0x7;
  1194. if (mode_state != ALL_MODES_DISABLED &&
  1195. mode_state != ALL_MODES_ENABLED &&
  1196. mode_state != ONLY_MODE_0_ENABLED &&
  1197. mode_state != ONLY_MODE_0_1_ENABLED) {
  1198. pr_err("invalid mode:%d combination\n", mode_state);
  1199. goto end;
  1200. }
  1201. mutex_lock(&rsc->client_lock);
  1202. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  1203. pr_debug("debug node is not supported during idle state\n");
  1204. goto state_check;
  1205. }
  1206. rsc->hw_ops.mode_ctrl(rsc, MODE_UPDATE, NULL, 0, mode_state);
  1207. state_check:
  1208. mutex_unlock(&rsc->client_lock);
  1209. end:
  1210. kfree(input);
  1211. return count;
  1212. }
  1213. static ssize_t _sde_debugfs_vsync_mode_read(struct file *file, char __user *buf,
  1214. size_t count, loff_t *ppos)
  1215. {
  1216. struct sde_rsc_priv *rsc = file->private_data;
  1217. char buffer[MAX_BUFFER_SIZE];
  1218. int blen = 0;
  1219. size_t max_size = min_t(size_t, count, MAX_BUFFER_SIZE);
  1220. if (*ppos || !rsc || !rsc->hw_ops.hw_vsync)
  1221. return 0;
  1222. mutex_lock(&rsc->client_lock);
  1223. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  1224. pr_debug("debug node is not supported during idle state\n");
  1225. blen = scnprintf(buffer, max_size,
  1226. "hw state is not supported during idle pc\n");
  1227. goto end;
  1228. }
  1229. blen = rsc->hw_ops.hw_vsync(rsc, VSYNC_READ, buffer, max_size, 0);
  1230. end:
  1231. mutex_unlock(&rsc->client_lock);
  1232. if (blen <= 0)
  1233. return 0;
  1234. if (blen > count) {
  1235. blen = count;
  1236. buffer[count - 1] = '\0';
  1237. }
  1238. if (copy_to_user(buf, buffer, blen))
  1239. return -EFAULT;
  1240. *ppos += blen;
  1241. return blen;
  1242. }
  1243. static ssize_t _sde_debugfs_vsync_mode_write(struct file *file,
  1244. const char __user *p, size_t count, loff_t *ppos)
  1245. {
  1246. struct sde_rsc_priv *rsc = file->private_data;
  1247. char *input;
  1248. u32 vsync_state = 0;
  1249. int rc;
  1250. if (!rsc || !rsc->hw_ops.hw_vsync || !count ||
  1251. count > MAX_COUNT_SIZE_SUPPORTED)
  1252. return 0;
  1253. input = kmalloc(count + 1, GFP_KERNEL);
  1254. if (!input)
  1255. return -ENOMEM;
  1256. if (copy_from_user(input, p, count)) {
  1257. kfree(input);
  1258. return -EFAULT;
  1259. }
  1260. input[count] = '\0';
  1261. rc = kstrtoint(input, 0, &vsync_state);
  1262. if (rc) {
  1263. pr_err("vsync_state: int conversion failed rc:%d\n", rc);
  1264. goto end;
  1265. }
  1266. pr_debug("vsync_state: %d\n", vsync_state);
  1267. vsync_state &= 0x7;
  1268. mutex_lock(&rsc->client_lock);
  1269. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  1270. pr_debug("debug node is not supported during idle state\n");
  1271. goto state_check;
  1272. }
  1273. if (vsync_state)
  1274. rsc->hw_ops.hw_vsync(rsc, VSYNC_ENABLE, NULL,
  1275. 0, vsync_state - 1);
  1276. else
  1277. rsc->hw_ops.hw_vsync(rsc, VSYNC_DISABLE, NULL, 0, 0);
  1278. state_check:
  1279. mutex_unlock(&rsc->client_lock);
  1280. end:
  1281. kfree(input);
  1282. return count;
  1283. }
  1284. static const struct file_operations debugfs_status_fops = {
  1285. .open = _sde_debugfs_status_open,
  1286. .read = seq_read,
  1287. .llseek = seq_lseek,
  1288. .release = single_release,
  1289. };
  1290. static const struct file_operations mode_control_fops = {
  1291. .open = _sde_debugfs_generic_noseek_open,
  1292. .read = _sde_debugfs_mode_ctrl_read,
  1293. .write = _sde_debugfs_mode_ctrl_write,
  1294. };
  1295. static const struct file_operations vsync_status_fops = {
  1296. .open = _sde_debugfs_generic_noseek_open,
  1297. .read = _sde_debugfs_vsync_mode_read,
  1298. .write = _sde_debugfs_vsync_mode_write,
  1299. };
  1300. static const struct file_operations profiling_enable_fops = {
  1301. .open = _sde_debugfs_generic_noseek_open,
  1302. .read = _sde_debugfs_profiling_read,
  1303. .write = _sde_debugfs_profiling_write,
  1304. };
  1305. static const struct file_operations profiling_counts_fops = {
  1306. .open = _sde_debugfs_counters_open,
  1307. .read = seq_read,
  1308. .llseek = seq_lseek,
  1309. .release = single_release,
  1310. };
  1311. static void _sde_rsc_init_debugfs(struct sde_rsc_priv *rsc, char *name)
  1312. {
  1313. rsc->debugfs_root = debugfs_create_dir(name, NULL);
  1314. if (!rsc->debugfs_root)
  1315. return;
  1316. /* don't error check these */
  1317. debugfs_create_file("status", 0400, rsc->debugfs_root, rsc,
  1318. &debugfs_status_fops);
  1319. debugfs_create_file("mode_control", 0600, rsc->debugfs_root, rsc,
  1320. &mode_control_fops);
  1321. debugfs_create_file("vsync_mode", 0600, rsc->debugfs_root, rsc,
  1322. &vsync_status_fops);
  1323. if (rsc->profiling_supp) {
  1324. debugfs_create_file("profiling_en", 0600, rsc->debugfs_root,
  1325. rsc, &profiling_enable_fops);
  1326. debugfs_create_file("profiling_counts", 0400,
  1327. rsc->debugfs_root, rsc,
  1328. &profiling_counts_fops);
  1329. }
  1330. debugfs_create_x32("debug_mode", 0600, rsc->debugfs_root,
  1331. &rsc->debug_mode);
  1332. }
  1333. #else
  1334. static void _sde_rsc_init_debugfs(struct sde_rsc_priv *rsc, char *name)
  1335. {
  1336. }
  1337. #endif /* defined(CONFIG_DEBUG_FS) */
  1338. static void sde_rsc_deinit(struct platform_device *pdev,
  1339. struct sde_rsc_priv *rsc)
  1340. {
  1341. if (!rsc)
  1342. return;
  1343. sde_rsc_resource_disable(rsc);
  1344. if (rsc->sw_fs_enabled)
  1345. regulator_disable(rsc->fs);
  1346. if (rsc->fs)
  1347. devm_regulator_put(rsc->fs);
  1348. if (rsc->wrapper_io.base)
  1349. msm_dss_iounmap(&rsc->wrapper_io);
  1350. if (rsc->drv_io.base)
  1351. msm_dss_iounmap(&rsc->drv_io);
  1352. sde_power_resource_deinit(pdev, &rsc->phandle);
  1353. debugfs_remove_recursive(rsc->debugfs_root);
  1354. kfree(rsc);
  1355. }
  1356. /**
  1357. * sde_rsc_bind - bind rsc device with controlling device
  1358. * @dev: Pointer to base of platform device
  1359. * @master: Pointer to container of drm device
  1360. * @data: Pointer to private data
  1361. * Returns: Zero on success
  1362. */
  1363. static int sde_rsc_bind(struct device *dev,
  1364. struct device *master,
  1365. void *data)
  1366. {
  1367. struct sde_rsc_priv *rsc;
  1368. struct drm_device *drm;
  1369. struct platform_device *pdev = to_platform_device(dev);
  1370. if (!dev || !pdev || !master) {
  1371. pr_err("invalid param(s), dev %pK, pdev %pK, master %pK\n",
  1372. dev, pdev, master);
  1373. return -EINVAL;
  1374. }
  1375. drm = dev_get_drvdata(master);
  1376. rsc = platform_get_drvdata(pdev);
  1377. if (!drm || !rsc) {
  1378. pr_err("invalid param(s), drm %pK, rsc %pK\n",
  1379. drm, rsc);
  1380. return -EINVAL;
  1381. }
  1382. sde_dbg_reg_register_base(SDE_RSC_DRV_DBG_NAME, rsc->drv_io.base,
  1383. rsc->drv_io.len);
  1384. sde_dbg_reg_register_base(SDE_RSC_WRAPPER_DBG_NAME,
  1385. rsc->wrapper_io.base, rsc->wrapper_io.len);
  1386. return 0;
  1387. }
  1388. /**
  1389. * sde_rsc_unbind - unbind rsc from controlling device
  1390. * @dev: Pointer to base of platform device
  1391. * @master: Pointer to container of drm device
  1392. * @data: Pointer to private data
  1393. */
  1394. static void sde_rsc_unbind(struct device *dev,
  1395. struct device *master, void *data)
  1396. {
  1397. struct sde_rsc_priv *rsc;
  1398. struct platform_device *pdev = to_platform_device(dev);
  1399. if (!dev || !pdev) {
  1400. pr_err("invalid param(s)\n");
  1401. return;
  1402. }
  1403. rsc = platform_get_drvdata(pdev);
  1404. if (!rsc) {
  1405. pr_err("invalid display rsc\n");
  1406. return;
  1407. }
  1408. }
  1409. static const struct component_ops sde_rsc_comp_ops = {
  1410. .bind = sde_rsc_bind,
  1411. .unbind = sde_rsc_unbind,
  1412. };
  1413. static int sde_rsc_probe(struct platform_device *pdev)
  1414. {
  1415. int ret;
  1416. struct sde_rsc_priv *rsc;
  1417. static int counter;
  1418. char name[MAX_RSC_CLIENT_NAME_LEN];
  1419. if (counter >= MAX_RSC_COUNT) {
  1420. pr_err("sde rsc supports probe till MAX_RSC_COUNT=%d devices\n",
  1421. MAX_RSC_COUNT);
  1422. return -EINVAL;
  1423. }
  1424. rsc = kzalloc(sizeof(*rsc), GFP_KERNEL);
  1425. if (!rsc) {
  1426. ret = -ENOMEM;
  1427. goto rsc_alloc_fail;
  1428. }
  1429. platform_set_drvdata(pdev, rsc);
  1430. rsc->dev = &pdev->dev;
  1431. of_property_read_u32(pdev->dev.of_node, "qcom,sde-rsc-version",
  1432. &rsc->version);
  1433. if (rsc->version >= SDE_RSC_REV_2)
  1434. rsc->single_tcs_execution_time = SINGLE_TCS_EXECUTION_TIME_V2;
  1435. else
  1436. rsc->single_tcs_execution_time = SINGLE_TCS_EXECUTION_TIME_V1;
  1437. if (rsc->version >= SDE_RSC_REV_3) {
  1438. rsc->time_slot_0_ns = rsc->single_tcs_execution_time
  1439. + RSC_MODE_INSTRUCTION_TIME;
  1440. rsc->backoff_time_ns = RSC_MODE_INSTRUCTION_TIME;
  1441. rsc->mode_threshold_time_ns = rsc->time_slot_0_ns;
  1442. } else {
  1443. rsc->time_slot_0_ns = (rsc->single_tcs_execution_time * 2)
  1444. + RSC_MODE_INSTRUCTION_TIME;
  1445. rsc->backoff_time_ns = rsc->single_tcs_execution_time
  1446. + RSC_MODE_INSTRUCTION_TIME;
  1447. rsc->mode_threshold_time_ns = rsc->backoff_time_ns
  1448. + RSC_MODE_THRESHOLD_OVERHEAD;
  1449. }
  1450. if (rsc->version >= SDE_RSC_REV_4)
  1451. rsc->profiling_supp = true;
  1452. ret = sde_power_resource_init(pdev, &rsc->phandle);
  1453. if (ret) {
  1454. pr_err("sde rsc:power resource init failed ret:%d\n", ret);
  1455. goto sde_rsc_fail;
  1456. }
  1457. rsc->rpmh_dev = rpmh_dev[SDE_RSC_INDEX + counter];
  1458. if (IS_ERR_OR_NULL(rsc->rpmh_dev)) {
  1459. ret = !rsc->rpmh_dev ? -EINVAL : PTR_ERR(rsc->rpmh_dev);
  1460. rsc->rpmh_dev = NULL;
  1461. pr_err("rpmh device node is not available ret:%d\n", ret);
  1462. goto sde_rsc_fail;
  1463. }
  1464. ret = msm_dss_ioremap_byname(pdev, &rsc->wrapper_io, "wrapper");
  1465. if (ret) {
  1466. pr_err("sde rsc: wrapper io data mapping failed ret=%d\n", ret);
  1467. goto sde_rsc_fail;
  1468. }
  1469. ret = msm_dss_ioremap_byname(pdev, &rsc->drv_io, "drv");
  1470. if (ret) {
  1471. pr_err("sde rsc: drv io data mapping failed ret:%d\n", ret);
  1472. goto sde_rsc_fail;
  1473. }
  1474. rsc->fs = devm_regulator_get(&pdev->dev, "vdd");
  1475. if (IS_ERR_OR_NULL(rsc->fs)) {
  1476. rsc->fs = NULL;
  1477. pr_err("unable to get regulator\n");
  1478. goto sde_rsc_fail;
  1479. }
  1480. if (rsc->version >= SDE_RSC_REV_3)
  1481. ret = sde_rsc_hw_register_v3(rsc);
  1482. else
  1483. ret = sde_rsc_hw_register(rsc);
  1484. if (ret) {
  1485. pr_err("sde rsc: hw register failed ret:%d\n", ret);
  1486. goto sde_rsc_fail;
  1487. }
  1488. ret = regulator_enable(rsc->fs);
  1489. if (ret) {
  1490. pr_err("sde rsc: fs on failed ret:%d\n", ret);
  1491. goto sde_rsc_fail;
  1492. }
  1493. rsc->sw_fs_enabled = true;
  1494. ret = sde_rsc_resource_enable(rsc);
  1495. if (ret < 0) {
  1496. pr_err("failed to enable sde rsc power resources rc:%d\n", ret);
  1497. goto sde_rsc_fail;
  1498. }
  1499. if (sde_rsc_timer_calculate(rsc, NULL, SDE_RSC_IDLE_STATE))
  1500. goto sde_rsc_fail;
  1501. sde_rsc_resource_disable(rsc);
  1502. INIT_LIST_HEAD(&rsc->client_list);
  1503. INIT_LIST_HEAD(&rsc->event_list);
  1504. mutex_init(&rsc->client_lock);
  1505. init_waitqueue_head(&rsc->rsc_vsync_waitq);
  1506. atomic_set(&rsc->resource_refcount, 0);
  1507. pr_info("sde rsc index:%d probed successfully\n",
  1508. SDE_RSC_INDEX + counter);
  1509. rsc_prv_list[SDE_RSC_INDEX + counter] = rsc;
  1510. snprintf(name, MAX_RSC_CLIENT_NAME_LEN, "%s%d", "sde_rsc", counter);
  1511. _sde_rsc_init_debugfs(rsc, name);
  1512. counter++;
  1513. ret = component_add(&pdev->dev, &sde_rsc_comp_ops);
  1514. if (ret)
  1515. pr_debug("component add failed, ret=%d\n", ret);
  1516. ret = 0;
  1517. return ret;
  1518. sde_rsc_fail:
  1519. sde_rsc_deinit(pdev, rsc);
  1520. rsc_alloc_fail:
  1521. return ret;
  1522. }
  1523. static int sde_rsc_remove(struct platform_device *pdev)
  1524. {
  1525. struct sde_rsc_priv *rsc = platform_get_drvdata(pdev);
  1526. sde_rsc_deinit(pdev, rsc);
  1527. return 0;
  1528. }
  1529. static int sde_rsc_rpmh_probe(struct platform_device *pdev)
  1530. {
  1531. int ret = 0;
  1532. uint32_t index = 0;
  1533. ret = of_property_read_u32(pdev->dev.of_node, "cell-index", &index);
  1534. if (ret) {
  1535. pr_err("unable to find sde rsc cell index\n");
  1536. return ret;
  1537. } else if (index >= MAX_RSC_COUNT) {
  1538. pr_err("invalid cell index for sde rsc:%d\n", index);
  1539. return -EINVAL;
  1540. }
  1541. rpmh_dev[index] = &pdev->dev;
  1542. return 0;
  1543. }
  1544. int sde_rsc_rpmh_remove(struct platform_device *pdev)
  1545. {
  1546. int i;
  1547. for (i = 0; i < MAX_RSC_COUNT; i++)
  1548. rpmh_dev[i] = NULL;
  1549. return 0;
  1550. }
  1551. static const struct of_device_id dt_match[] = {
  1552. { .compatible = "qcom,sde-rsc"},
  1553. {},
  1554. };
  1555. static struct platform_driver sde_rsc_platform_driver = {
  1556. .probe = sde_rsc_probe,
  1557. .remove = sde_rsc_remove,
  1558. .driver = {
  1559. .name = "sde_rsc",
  1560. .of_match_table = dt_match,
  1561. .suppress_bind_attrs = true,
  1562. },
  1563. };
  1564. static const struct of_device_id sde_rsc_rpmh_match[] = {
  1565. {.compatible = "qcom,sde-rsc-rpmh"},
  1566. {},
  1567. };
  1568. static struct platform_driver sde_rsc_rpmh_driver = {
  1569. .probe = sde_rsc_rpmh_probe,
  1570. .remove = sde_rsc_rpmh_remove,
  1571. .driver = {
  1572. .name = "sde_rsc_rpmh",
  1573. .of_match_table = sde_rsc_rpmh_match,
  1574. },
  1575. };
  1576. void __init sde_rsc_register(void)
  1577. {
  1578. platform_driver_register(&sde_rsc_platform_driver);
  1579. }
  1580. void __exit sde_rsc_unregister(void)
  1581. {
  1582. platform_driver_unregister(&sde_rsc_platform_driver);
  1583. }
  1584. void __init sde_rsc_rpmh_register(void)
  1585. {
  1586. platform_driver_register(&sde_rsc_rpmh_driver);
  1587. }