sde_rsc.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901
  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->master_drm == NULL) {
  798. pr_err("invalid master component binding\n");
  799. rc = -EINVAL;
  800. goto end;
  801. } else if ((rsc->current_state == state) && !config) {
  802. pr_debug("no state change: %d\n", state);
  803. goto end;
  804. }
  805. pr_debug("%pS: rsc state:%d request client:%s state:%d\n",
  806. __builtin_return_address(0), rsc->current_state,
  807. caller_client->name, state);
  808. if ((state == SDE_RSC_VID_STATE) && (rsc->version >= SDE_RSC_REV_3))
  809. state = SDE_RSC_CLK_STATE;
  810. if (rsc->current_state == SDE_RSC_IDLE_STATE)
  811. sde_rsc_resource_enable(rsc);
  812. switch (state) {
  813. case SDE_RSC_IDLE_STATE:
  814. rc = sde_rsc_switch_to_idle(rsc, NULL, rsc->primary_client,
  815. wait_vblank_crtc_id);
  816. if (rc == CMD_MODE_SWITCH_SUCCESS) {
  817. state = SDE_RSC_CMD_STATE;
  818. rc = 0;
  819. } else if (rc == VID_MODE_SWITCH_SUCCESS) {
  820. state = SDE_RSC_VID_STATE;
  821. rc = 0;
  822. } else if (rc == CLK_MODE_SWITCH_SUCCESS) {
  823. state = SDE_RSC_CLK_STATE;
  824. rc = 0;
  825. }
  826. break;
  827. case SDE_RSC_CMD_STATE:
  828. rc = sde_rsc_switch_to_cmd(rsc, config, caller_client,
  829. wait_vblank_crtc_id);
  830. break;
  831. case SDE_RSC_VID_STATE:
  832. rc = sde_rsc_switch_to_vid(rsc, config, caller_client,
  833. wait_vblank_crtc_id);
  834. break;
  835. case SDE_RSC_CLK_STATE:
  836. rc = sde_rsc_switch_to_clk(rsc, wait_vblank_crtc_id);
  837. break;
  838. default:
  839. pr_err("invalid state handling %d\n", state);
  840. break;
  841. }
  842. if (rc == STATE_UPDATE_NOT_ALLOWED) {
  843. rc = 0;
  844. SDE_EVT32(caller_client->id, caller_client->current_state,
  845. state, rsc->current_state, rc, SDE_EVTLOG_FUNC_CASE1);
  846. goto clk_disable;
  847. } else if (rc) {
  848. pr_debug("state:%d update failed rc:%d\n", state, rc);
  849. SDE_EVT32(caller_client->id, caller_client->current_state,
  850. state, rsc->current_state, rc, SDE_EVTLOG_FUNC_CASE2);
  851. goto clk_disable;
  852. }
  853. pr_debug("state switch successfully complete: %d\n", state);
  854. SDE_ATRACE_INT("rsc_state", state);
  855. rsc->current_state = state;
  856. SDE_EVT32(caller_client->id, caller_client->current_state,
  857. state, rsc->current_state, SDE_EVTLOG_FUNC_EXIT);
  858. clk_disable:
  859. if (rsc->current_state == SDE_RSC_IDLE_STATE)
  860. sde_rsc_resource_disable(rsc);
  861. end:
  862. mutex_unlock(&rsc->client_lock);
  863. return rc;
  864. }
  865. EXPORT_SYMBOL(sde_rsc_client_state_update);
  866. /**
  867. * sde_rsc_client_vote() - ab/ib vote from rsc client
  868. *
  869. * @client: Client pointer provided by sde_rsc_client_create().
  870. * @bus_id: data bus for which to be voted
  871. * @ab: aggregated bandwidth vote from client.
  872. * @ib: instant bandwidth vote from client.
  873. *
  874. * Return: error code.
  875. */
  876. int sde_rsc_client_vote(struct sde_rsc_client *caller_client,
  877. u32 bus_id, u64 ab_vote, u64 ib_vote)
  878. {
  879. int rsc_index;
  880. struct sde_rsc_priv *rsc;
  881. if (caller_client && caller_client->rsc_index >= MAX_RSC_COUNT) {
  882. pr_err("invalid rsc client or client index\n");
  883. return -EINVAL;
  884. }
  885. rsc_index = caller_client ? caller_client->rsc_index : SDE_RSC_INDEX;
  886. rsc = rsc_prv_list[rsc_index];
  887. if (!rsc || bus_id >= SDE_POWER_HANDLE_DBUS_ID_MAX)
  888. return -EINVAL;
  889. pr_debug("client:%s ab:%llu ib:%llu\n",
  890. caller_client ? caller_client->name : "unknown",
  891. ab_vote, ib_vote);
  892. mutex_lock(&rsc->client_lock);
  893. rsc->bw_config.new_ab_vote[bus_id] = ab_vote;
  894. rsc->bw_config.new_ib_vote[bus_id] = ib_vote;
  895. mutex_unlock(&rsc->client_lock);
  896. return 0;
  897. }
  898. EXPORT_SYMBOL(sde_rsc_client_vote);
  899. int sde_rsc_client_trigger_vote(struct sde_rsc_client *caller_client,
  900. bool delta_vote)
  901. {
  902. int rc = 0, rsc_index, i;
  903. struct sde_rsc_priv *rsc;
  904. bool bw_increase = false;
  905. if (caller_client && caller_client->rsc_index >= MAX_RSC_COUNT) {
  906. pr_err("invalid rsc index\n");
  907. return -EINVAL;
  908. }
  909. rsc_index = caller_client ? caller_client->rsc_index : SDE_RSC_INDEX;
  910. rsc = rsc_prv_list[rsc_index];
  911. if (!rsc)
  912. return -EINVAL;
  913. pr_debug("client:%s trigger bw delta vote:%d\n",
  914. caller_client ? caller_client->name : "unknown", delta_vote);
  915. mutex_lock(&rsc->client_lock);
  916. for (i = 0; i < SDE_POWER_HANDLE_DBUS_ID_MAX && delta_vote; i++) {
  917. if (rsc->bw_config.new_ab_vote[i] > rsc->bw_config.ab_vote[i] ||
  918. rsc->bw_config.new_ib_vote[i] > rsc->bw_config.ib_vote[i])
  919. bw_increase = true;
  920. rsc->bw_config.ab_vote[i] = rsc->bw_config.new_ab_vote[i];
  921. rsc->bw_config.ib_vote[i] = rsc->bw_config.new_ib_vote[i];
  922. }
  923. rc = sde_rsc_resource_enable(rsc);
  924. if (rc < 0)
  925. goto clk_enable_fail;
  926. if (delta_vote) {
  927. if (rsc->hw_ops.tcs_wait) {
  928. rc = rsc->hw_ops.tcs_wait(rsc);
  929. if (rc) {
  930. pr_err("tcs is still busy; can't send command\n");
  931. if (rsc->hw_ops.tcs_use_ok)
  932. rsc->hw_ops.tcs_use_ok(rsc);
  933. goto end;
  934. }
  935. }
  936. rpmh_invalidate(rsc->rpmh_dev);
  937. for (i = 0; i < SDE_POWER_HANDLE_DBUS_ID_MAX; i++)
  938. sde_power_data_bus_set_quota(&rsc->phandle,
  939. i, rsc->bw_config.ab_vote[i],
  940. rsc->bw_config.ib_vote[i]);
  941. rpmh_flush(rsc->rpmh_dev);
  942. }
  943. if (rsc->hw_ops.bwi_status &&
  944. (rsc->current_state == SDE_RSC_CMD_STATE ||
  945. rsc->current_state == SDE_RSC_VID_STATE))
  946. rsc->hw_ops.bwi_status(rsc, bw_increase);
  947. else if (rsc->hw_ops.tcs_use_ok)
  948. rsc->hw_ops.tcs_use_ok(rsc);
  949. end:
  950. sde_rsc_resource_disable(rsc);
  951. clk_enable_fail:
  952. mutex_unlock(&rsc->client_lock);
  953. return rc;
  954. }
  955. EXPORT_SYMBOL(sde_rsc_client_trigger_vote);
  956. #if defined(CONFIG_DEBUG_FS)
  957. void sde_rsc_debug_dump(u32 mux_sel)
  958. {
  959. struct sde_rsc_priv *rsc;
  960. rsc = rsc_prv_list[SDE_RSC_INDEX];
  961. if (!rsc)
  962. return;
  963. /* this must be called with rsc clocks enabled */
  964. if (rsc->hw_ops.debug_dump)
  965. rsc->hw_ops.debug_dump(rsc, mux_sel);
  966. }
  967. static int _sde_debugfs_status_show(struct seq_file *s, void *data)
  968. {
  969. struct sde_rsc_priv *rsc;
  970. struct sde_rsc_client *client;
  971. int ret;
  972. if (!s || !s->private)
  973. return -EINVAL;
  974. rsc = s->private;
  975. mutex_lock(&rsc->client_lock);
  976. seq_printf(s, "rsc current state:%d\n", rsc->current_state);
  977. seq_printf(s, "wraper backoff time(ns):%d\n",
  978. rsc->timer_config.static_wakeup_time_ns);
  979. seq_printf(s, "rsc backoff time(ns):%d\n",
  980. rsc->timer_config.rsc_backoff_time_ns);
  981. seq_printf(s, "pdc backoff time(ns):%d\n",
  982. rsc->timer_config.pdc_backoff_time_ns);
  983. seq_printf(s, "rsc mode threshold time(ns):%d\n",
  984. rsc->timer_config.rsc_mode_threshold_time_ns);
  985. seq_printf(s, "rsc time slot 0(ns):%d\n",
  986. rsc->timer_config.rsc_time_slot_0_ns);
  987. seq_printf(s, "rsc time slot 1(ns):%d\n",
  988. rsc->timer_config.rsc_time_slot_1_ns);
  989. seq_printf(s, "frame fps:%d jitter_numer:%d jitter_denom:%d vtotal:%d prefill lines:%d\n",
  990. rsc->cmd_config.fps, rsc->cmd_config.jitter_numer,
  991. rsc->cmd_config.jitter_denom,
  992. rsc->cmd_config.vtotal, rsc->cmd_config.prefill_lines);
  993. seq_puts(s, "\n");
  994. list_for_each_entry(client, &rsc->client_list, list)
  995. seq_printf(s, "\t client:%s state:%d\n",
  996. client->name, client->current_state);
  997. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  998. pr_debug("debug node is not supported during idle state\n");
  999. seq_puts(s, "hw state is not supported during idle pc\n");
  1000. goto end;
  1001. }
  1002. if (rsc->hw_ops.debug_show) {
  1003. ret = rsc->hw_ops.debug_show(s, rsc);
  1004. if (ret)
  1005. pr_err("sde rsc: hw debug failed ret:%d\n", ret);
  1006. }
  1007. end:
  1008. mutex_unlock(&rsc->client_lock);
  1009. return 0;
  1010. }
  1011. static int _sde_debugfs_status_open(struct inode *inode, struct file *file)
  1012. {
  1013. return single_open(file, _sde_debugfs_status_show, inode->i_private);
  1014. }
  1015. static int _sde_debugfs_counters_show(struct seq_file *s, void *data)
  1016. {
  1017. struct sde_rsc_priv *rsc = s->private;
  1018. u32 counters[NUM_RSC_PROFILING_COUNTERS];
  1019. int i, ret;
  1020. if (!rsc)
  1021. return -EINVAL;
  1022. if (!rsc->hw_ops.get_counters) {
  1023. seq_puts(s, "counters are not supported on this target\n");
  1024. return 0;
  1025. }
  1026. memset(counters, 0, sizeof(counters));
  1027. mutex_lock(&rsc->client_lock);
  1028. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  1029. pr_debug("counters are not supported during idle state\n");
  1030. seq_puts(s, "no access to counters during idle pc\n");
  1031. goto end;
  1032. }
  1033. ret = rsc->hw_ops.get_counters(rsc, counters);
  1034. if (ret) {
  1035. pr_err("sde rsc: get_counters failed ret:%d\n", ret);
  1036. seq_puts(s, "failed to retrieve counts!\n");
  1037. goto end;
  1038. }
  1039. seq_puts(s, "rsc profiling counters:\n");
  1040. for (i = 0; i < NUM_RSC_PROFILING_COUNTERS; ++i)
  1041. seq_printf(s, "\tmode[%d] = 0x%08x:\n", i, counters[i]);
  1042. end:
  1043. mutex_unlock(&rsc->client_lock);
  1044. return 0;
  1045. }
  1046. static int _sde_debugfs_counters_open(struct inode *inode, struct file *file)
  1047. {
  1048. return single_open(file, _sde_debugfs_counters_show, inode->i_private);
  1049. }
  1050. static int _sde_debugfs_generic_noseek_open(struct inode *inode,
  1051. struct file *file)
  1052. {
  1053. /* non-seekable */
  1054. file->private_data = inode->i_private;
  1055. return nonseekable_open(inode, file);
  1056. }
  1057. static ssize_t _sde_debugfs_profiling_read(struct file *file, char __user *buf,
  1058. size_t count, loff_t *ppos)
  1059. {
  1060. struct sde_rsc_priv *rsc = file->private_data;
  1061. size_t max_size = min_t(size_t, count, MAX_BUFFER_SIZE);
  1062. char buffer[MAX_BUFFER_SIZE];
  1063. int blen = 0;
  1064. if (*ppos || !rsc)
  1065. return 0;
  1066. if (!rsc->hw_ops.setup_counters) {
  1067. blen += scnprintf(&buffer[blen], max_size - blen,
  1068. "counters are not supported on this target\n");
  1069. goto end;
  1070. }
  1071. mutex_lock(&rsc->client_lock);
  1072. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  1073. pr_debug("counters are not supported during idle state\n");
  1074. blen += scnprintf(&buffer[blen], max_size - blen,
  1075. "no access to counters during idle pc\n");
  1076. goto unlock;
  1077. }
  1078. blen += scnprintf(&buffer[blen], max_size - blen,
  1079. "%s\n", rsc->profiling_en ? "Y" : "N");
  1080. unlock:
  1081. mutex_unlock(&rsc->client_lock);
  1082. end:
  1083. if (copy_to_user(buf, buffer, blen))
  1084. return -EFAULT;
  1085. *ppos += blen;
  1086. return blen;
  1087. }
  1088. static ssize_t _sde_debugfs_profiling_write(struct file *file,
  1089. const char __user *p, size_t count, loff_t *ppos)
  1090. {
  1091. struct sde_rsc_priv *rsc = file->private_data;
  1092. bool input_valid, input_value;
  1093. char *input;
  1094. int rc;
  1095. if (!rsc || !rsc->hw_ops.setup_counters || !count ||
  1096. count > MAX_COUNT_SIZE_SUPPORTED)
  1097. return 0;
  1098. input = kmalloc(count + 1, GFP_KERNEL);
  1099. if (!input)
  1100. return -ENOMEM;
  1101. if (copy_from_user(input, p, count)) {
  1102. kfree(input);
  1103. return -EFAULT;
  1104. }
  1105. input[count] = '\0';
  1106. switch (input[0]) {
  1107. case 'y':
  1108. case 'Y':
  1109. case '1':
  1110. input_valid = true;
  1111. input_value = true;
  1112. break;
  1113. case 'n':
  1114. case 'N':
  1115. case '0':
  1116. input_valid = true;
  1117. input_value = false;
  1118. break;
  1119. default:
  1120. input_valid = false;
  1121. count = 0;
  1122. break;
  1123. }
  1124. mutex_lock(&rsc->client_lock);
  1125. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  1126. pr_debug("debug node is not supported during idle state\n");
  1127. count = 0;
  1128. goto end;
  1129. }
  1130. pr_debug("input %s, profiling_en: %d\n",
  1131. input_valid ? "valid" : "invalid", rsc->profiling_en);
  1132. if (input_valid) {
  1133. rsc->profiling_en = input_value;
  1134. rc = rsc->hw_ops.setup_counters(rsc, rsc->profiling_en);
  1135. if (rc)
  1136. pr_err("setup_counters failed, rc:%d\n", rc);
  1137. }
  1138. end:
  1139. mutex_unlock(&rsc->client_lock);
  1140. kfree(input);
  1141. return count;
  1142. }
  1143. static ssize_t _sde_debugfs_mode_ctrl_read(struct file *file, char __user *buf,
  1144. size_t count, loff_t *ppos)
  1145. {
  1146. struct sde_rsc_priv *rsc = file->private_data;
  1147. char buffer[MAX_BUFFER_SIZE];
  1148. int blen = 0;
  1149. size_t max_size = min_t(size_t, count, MAX_BUFFER_SIZE);
  1150. if (*ppos || !rsc || !rsc->hw_ops.mode_ctrl)
  1151. return 0;
  1152. mutex_lock(&rsc->client_lock);
  1153. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  1154. pr_debug("debug node is not supported during idle state\n");
  1155. blen = scnprintf(buffer, max_size,
  1156. "hw state is not supported during idle pc\n");
  1157. goto end;
  1158. }
  1159. blen = rsc->hw_ops.mode_ctrl(rsc, MODE_READ, buffer, max_size, 0);
  1160. end:
  1161. mutex_unlock(&rsc->client_lock);
  1162. if (blen <= 0)
  1163. return 0;
  1164. if (blen > count) {
  1165. blen = count;
  1166. buffer[count - 1] = '\0';
  1167. }
  1168. if (copy_to_user(buf, buffer, blen))
  1169. return -EFAULT;
  1170. *ppos += blen;
  1171. return blen;
  1172. }
  1173. static ssize_t _sde_debugfs_mode_ctrl_write(struct file *file,
  1174. const char __user *p, size_t count, loff_t *ppos)
  1175. {
  1176. struct sde_rsc_priv *rsc = file->private_data;
  1177. char *input;
  1178. u32 mode_state = 0;
  1179. int rc;
  1180. if (!rsc || !rsc->hw_ops.mode_ctrl || !count ||
  1181. count > MAX_COUNT_SIZE_SUPPORTED)
  1182. return 0;
  1183. input = kmalloc(count + 1, GFP_KERNEL);
  1184. if (!input)
  1185. return -ENOMEM;
  1186. if (copy_from_user(input, p, count)) {
  1187. kfree(input);
  1188. return -EFAULT;
  1189. }
  1190. input[count] = '\0';
  1191. rc = kstrtoint(input, 0, &mode_state);
  1192. if (rc) {
  1193. pr_err("mode_state: int conversion failed rc:%d\n", rc);
  1194. goto end;
  1195. }
  1196. pr_debug("mode_state: %d\n", mode_state);
  1197. mode_state &= 0x7;
  1198. if (mode_state != ALL_MODES_DISABLED &&
  1199. mode_state != ALL_MODES_ENABLED &&
  1200. mode_state != ONLY_MODE_0_ENABLED &&
  1201. mode_state != ONLY_MODE_0_1_ENABLED) {
  1202. pr_err("invalid mode:%d combination\n", mode_state);
  1203. goto end;
  1204. }
  1205. mutex_lock(&rsc->client_lock);
  1206. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  1207. pr_debug("debug node is not supported during idle state\n");
  1208. goto state_check;
  1209. }
  1210. rsc->hw_ops.mode_ctrl(rsc, MODE_UPDATE, NULL, 0, mode_state);
  1211. state_check:
  1212. mutex_unlock(&rsc->client_lock);
  1213. end:
  1214. kfree(input);
  1215. return count;
  1216. }
  1217. static ssize_t _sde_debugfs_vsync_mode_read(struct file *file, char __user *buf,
  1218. size_t count, loff_t *ppos)
  1219. {
  1220. struct sde_rsc_priv *rsc = file->private_data;
  1221. char buffer[MAX_BUFFER_SIZE];
  1222. int blen = 0;
  1223. size_t max_size = min_t(size_t, count, MAX_BUFFER_SIZE);
  1224. if (*ppos || !rsc || !rsc->hw_ops.hw_vsync)
  1225. return 0;
  1226. mutex_lock(&rsc->client_lock);
  1227. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  1228. pr_debug("debug node is not supported during idle state\n");
  1229. blen = scnprintf(buffer, max_size,
  1230. "hw state is not supported during idle pc\n");
  1231. goto end;
  1232. }
  1233. blen = rsc->hw_ops.hw_vsync(rsc, VSYNC_READ, buffer, max_size, 0);
  1234. end:
  1235. mutex_unlock(&rsc->client_lock);
  1236. if (blen <= 0)
  1237. return 0;
  1238. if (blen > count) {
  1239. blen = count;
  1240. buffer[count - 1] = '\0';
  1241. }
  1242. if (copy_to_user(buf, buffer, blen))
  1243. return -EFAULT;
  1244. *ppos += blen;
  1245. return blen;
  1246. }
  1247. static ssize_t _sde_debugfs_vsync_mode_write(struct file *file,
  1248. const char __user *p, size_t count, loff_t *ppos)
  1249. {
  1250. struct sde_rsc_priv *rsc = file->private_data;
  1251. char *input;
  1252. u32 vsync_state = 0;
  1253. int rc;
  1254. if (!rsc || !rsc->hw_ops.hw_vsync || !count ||
  1255. count > MAX_COUNT_SIZE_SUPPORTED)
  1256. return 0;
  1257. input = kmalloc(count + 1, GFP_KERNEL);
  1258. if (!input)
  1259. return -ENOMEM;
  1260. if (copy_from_user(input, p, count)) {
  1261. kfree(input);
  1262. return -EFAULT;
  1263. }
  1264. input[count] = '\0';
  1265. rc = kstrtoint(input, 0, &vsync_state);
  1266. if (rc) {
  1267. pr_err("vsync_state: int conversion failed rc:%d\n", rc);
  1268. goto end;
  1269. }
  1270. pr_debug("vsync_state: %d\n", vsync_state);
  1271. vsync_state &= 0x7;
  1272. mutex_lock(&rsc->client_lock);
  1273. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  1274. pr_debug("debug node is not supported during idle state\n");
  1275. goto state_check;
  1276. }
  1277. if (vsync_state)
  1278. rsc->hw_ops.hw_vsync(rsc, VSYNC_ENABLE, NULL,
  1279. 0, vsync_state - 1);
  1280. else
  1281. rsc->hw_ops.hw_vsync(rsc, VSYNC_DISABLE, NULL, 0, 0);
  1282. state_check:
  1283. mutex_unlock(&rsc->client_lock);
  1284. end:
  1285. kfree(input);
  1286. return count;
  1287. }
  1288. static const struct file_operations debugfs_status_fops = {
  1289. .open = _sde_debugfs_status_open,
  1290. .read = seq_read,
  1291. .llseek = seq_lseek,
  1292. .release = single_release,
  1293. };
  1294. static const struct file_operations mode_control_fops = {
  1295. .open = _sde_debugfs_generic_noseek_open,
  1296. .read = _sde_debugfs_mode_ctrl_read,
  1297. .write = _sde_debugfs_mode_ctrl_write,
  1298. };
  1299. static const struct file_operations vsync_status_fops = {
  1300. .open = _sde_debugfs_generic_noseek_open,
  1301. .read = _sde_debugfs_vsync_mode_read,
  1302. .write = _sde_debugfs_vsync_mode_write,
  1303. };
  1304. static const struct file_operations profiling_enable_fops = {
  1305. .open = _sde_debugfs_generic_noseek_open,
  1306. .read = _sde_debugfs_profiling_read,
  1307. .write = _sde_debugfs_profiling_write,
  1308. };
  1309. static const struct file_operations profiling_counts_fops = {
  1310. .open = _sde_debugfs_counters_open,
  1311. .read = seq_read,
  1312. .llseek = seq_lseek,
  1313. .release = single_release,
  1314. };
  1315. static void _sde_rsc_init_debugfs(struct sde_rsc_priv *rsc, char *name)
  1316. {
  1317. rsc->debugfs_root = debugfs_create_dir(name, NULL);
  1318. if (!rsc->debugfs_root)
  1319. return;
  1320. /* don't error check these */
  1321. debugfs_create_file("status", 0400, rsc->debugfs_root, rsc,
  1322. &debugfs_status_fops);
  1323. debugfs_create_file("mode_control", 0600, rsc->debugfs_root, rsc,
  1324. &mode_control_fops);
  1325. debugfs_create_file("vsync_mode", 0600, rsc->debugfs_root, rsc,
  1326. &vsync_status_fops);
  1327. if (rsc->profiling_supp) {
  1328. debugfs_create_file("profiling_en", 0600, rsc->debugfs_root,
  1329. rsc, &profiling_enable_fops);
  1330. debugfs_create_file("profiling_counts", 0400,
  1331. rsc->debugfs_root, rsc,
  1332. &profiling_counts_fops);
  1333. }
  1334. debugfs_create_x32("debug_mode", 0600, rsc->debugfs_root,
  1335. &rsc->debug_mode);
  1336. }
  1337. #else
  1338. static void _sde_rsc_init_debugfs(struct sde_rsc_priv *rsc, char *name)
  1339. {
  1340. }
  1341. #endif /* defined(CONFIG_DEBUG_FS) */
  1342. static void sde_rsc_deinit(struct platform_device *pdev,
  1343. struct sde_rsc_priv *rsc)
  1344. {
  1345. if (!rsc)
  1346. return;
  1347. sde_rsc_resource_disable(rsc);
  1348. if (rsc->sw_fs_enabled)
  1349. regulator_disable(rsc->fs);
  1350. if (rsc->fs)
  1351. devm_regulator_put(rsc->fs);
  1352. if (rsc->wrapper_io.base)
  1353. msm_dss_iounmap(&rsc->wrapper_io);
  1354. if (rsc->drv_io.base)
  1355. msm_dss_iounmap(&rsc->drv_io);
  1356. sde_power_resource_deinit(pdev, &rsc->phandle);
  1357. debugfs_remove_recursive(rsc->debugfs_root);
  1358. kfree(rsc);
  1359. }
  1360. /**
  1361. * sde_rsc_bind - bind rsc device with controlling device
  1362. * @dev: Pointer to base of platform device
  1363. * @master: Pointer to container of drm device
  1364. * @data: Pointer to private data
  1365. * Returns: Zero on success
  1366. */
  1367. static int sde_rsc_bind(struct device *dev,
  1368. struct device *master,
  1369. void *data)
  1370. {
  1371. struct sde_rsc_priv *rsc;
  1372. struct drm_device *drm;
  1373. struct platform_device *pdev = to_platform_device(dev);
  1374. if (!dev || !pdev || !master) {
  1375. pr_err("invalid param(s), dev %pK, pdev %pK, master %pK\n",
  1376. dev, pdev, master);
  1377. return -EINVAL;
  1378. }
  1379. drm = dev_get_drvdata(master);
  1380. rsc = platform_get_drvdata(pdev);
  1381. if (!drm || !rsc) {
  1382. pr_err("invalid param(s), drm %pK, rsc %pK\n",
  1383. drm, rsc);
  1384. return -EINVAL;
  1385. }
  1386. mutex_lock(&rsc->client_lock);
  1387. rsc->master_drm = drm;
  1388. mutex_unlock(&rsc->client_lock);
  1389. sde_dbg_reg_register_base(SDE_RSC_DRV_DBG_NAME, rsc->drv_io.base,
  1390. rsc->drv_io.len);
  1391. sde_dbg_reg_register_base(SDE_RSC_WRAPPER_DBG_NAME,
  1392. rsc->wrapper_io.base, rsc->wrapper_io.len);
  1393. return 0;
  1394. }
  1395. /**
  1396. * sde_rsc_unbind - unbind rsc from controlling device
  1397. * @dev: Pointer to base of platform device
  1398. * @master: Pointer to container of drm device
  1399. * @data: Pointer to private data
  1400. */
  1401. static void sde_rsc_unbind(struct device *dev,
  1402. struct device *master, void *data)
  1403. {
  1404. struct sde_rsc_priv *rsc;
  1405. struct platform_device *pdev = to_platform_device(dev);
  1406. if (!dev || !pdev) {
  1407. pr_err("invalid param(s)\n");
  1408. return;
  1409. }
  1410. rsc = platform_get_drvdata(pdev);
  1411. if (!rsc) {
  1412. pr_err("invalid display rsc\n");
  1413. return;
  1414. }
  1415. mutex_lock(&rsc->client_lock);
  1416. rsc->master_drm = NULL;
  1417. mutex_unlock(&rsc->client_lock);
  1418. }
  1419. static const struct component_ops sde_rsc_comp_ops = {
  1420. .bind = sde_rsc_bind,
  1421. .unbind = sde_rsc_unbind,
  1422. };
  1423. static int sde_rsc_probe(struct platform_device *pdev)
  1424. {
  1425. int ret;
  1426. struct sde_rsc_priv *rsc;
  1427. static int counter;
  1428. char name[MAX_RSC_CLIENT_NAME_LEN];
  1429. if (counter >= MAX_RSC_COUNT) {
  1430. pr_err("sde rsc supports probe till MAX_RSC_COUNT=%d devices\n",
  1431. MAX_RSC_COUNT);
  1432. return -EINVAL;
  1433. }
  1434. rsc = kzalloc(sizeof(*rsc), GFP_KERNEL);
  1435. if (!rsc) {
  1436. ret = -ENOMEM;
  1437. goto rsc_alloc_fail;
  1438. }
  1439. platform_set_drvdata(pdev, rsc);
  1440. rsc->dev = &pdev->dev;
  1441. of_property_read_u32(pdev->dev.of_node, "qcom,sde-rsc-version",
  1442. &rsc->version);
  1443. if (rsc->version >= SDE_RSC_REV_2)
  1444. rsc->single_tcs_execution_time = SINGLE_TCS_EXECUTION_TIME_V2;
  1445. else
  1446. rsc->single_tcs_execution_time = SINGLE_TCS_EXECUTION_TIME_V1;
  1447. if (rsc->version >= SDE_RSC_REV_3) {
  1448. rsc->time_slot_0_ns = rsc->single_tcs_execution_time
  1449. + RSC_MODE_INSTRUCTION_TIME;
  1450. rsc->backoff_time_ns = RSC_MODE_INSTRUCTION_TIME;
  1451. rsc->mode_threshold_time_ns = rsc->time_slot_0_ns;
  1452. } else {
  1453. rsc->time_slot_0_ns = (rsc->single_tcs_execution_time * 2)
  1454. + RSC_MODE_INSTRUCTION_TIME;
  1455. rsc->backoff_time_ns = rsc->single_tcs_execution_time
  1456. + RSC_MODE_INSTRUCTION_TIME;
  1457. rsc->mode_threshold_time_ns = rsc->backoff_time_ns
  1458. + RSC_MODE_THRESHOLD_OVERHEAD;
  1459. }
  1460. if (rsc->version >= SDE_RSC_REV_4)
  1461. rsc->profiling_supp = true;
  1462. ret = sde_power_resource_init(pdev, &rsc->phandle);
  1463. if (ret) {
  1464. pr_err("sde rsc:power resource init failed ret:%d\n", ret);
  1465. goto sde_rsc_fail;
  1466. }
  1467. rsc->rpmh_dev = rpmh_dev[SDE_RSC_INDEX + counter];
  1468. if (IS_ERR_OR_NULL(rsc->rpmh_dev)) {
  1469. ret = !rsc->rpmh_dev ? -EINVAL : PTR_ERR(rsc->rpmh_dev);
  1470. rsc->rpmh_dev = NULL;
  1471. pr_err("rpmh device node is not available ret:%d\n", ret);
  1472. goto sde_rsc_fail;
  1473. }
  1474. ret = msm_dss_ioremap_byname(pdev, &rsc->wrapper_io, "wrapper");
  1475. if (ret) {
  1476. pr_err("sde rsc: wrapper io data mapping failed ret=%d\n", ret);
  1477. goto sde_rsc_fail;
  1478. }
  1479. ret = msm_dss_ioremap_byname(pdev, &rsc->drv_io, "drv");
  1480. if (ret) {
  1481. pr_err("sde rsc: drv io data mapping failed ret:%d\n", ret);
  1482. goto sde_rsc_fail;
  1483. }
  1484. rsc->fs = devm_regulator_get(&pdev->dev, "vdd");
  1485. if (IS_ERR_OR_NULL(rsc->fs)) {
  1486. rsc->fs = NULL;
  1487. pr_err("unable to get regulator\n");
  1488. goto sde_rsc_fail;
  1489. }
  1490. if (rsc->version >= SDE_RSC_REV_3)
  1491. ret = sde_rsc_hw_register_v3(rsc);
  1492. else
  1493. ret = sde_rsc_hw_register(rsc);
  1494. if (ret) {
  1495. pr_err("sde rsc: hw register failed ret:%d\n", ret);
  1496. goto sde_rsc_fail;
  1497. }
  1498. ret = regulator_enable(rsc->fs);
  1499. if (ret) {
  1500. pr_err("sde rsc: fs on failed ret:%d\n", ret);
  1501. goto sde_rsc_fail;
  1502. }
  1503. rsc->sw_fs_enabled = true;
  1504. ret = sde_rsc_resource_enable(rsc);
  1505. if (ret < 0) {
  1506. pr_err("failed to enable sde rsc power resources rc:%d\n", ret);
  1507. goto sde_rsc_fail;
  1508. }
  1509. if (sde_rsc_timer_calculate(rsc, NULL, SDE_RSC_IDLE_STATE))
  1510. goto sde_rsc_fail;
  1511. sde_rsc_resource_disable(rsc);
  1512. INIT_LIST_HEAD(&rsc->client_list);
  1513. INIT_LIST_HEAD(&rsc->event_list);
  1514. mutex_init(&rsc->client_lock);
  1515. init_waitqueue_head(&rsc->rsc_vsync_waitq);
  1516. atomic_set(&rsc->resource_refcount, 0);
  1517. pr_info("sde rsc index:%d probed successfully\n",
  1518. SDE_RSC_INDEX + counter);
  1519. rsc_prv_list[SDE_RSC_INDEX + counter] = rsc;
  1520. snprintf(name, MAX_RSC_CLIENT_NAME_LEN, "%s%d", "sde_rsc", counter);
  1521. _sde_rsc_init_debugfs(rsc, name);
  1522. counter++;
  1523. ret = component_add(&pdev->dev, &sde_rsc_comp_ops);
  1524. if (ret)
  1525. pr_debug("component add failed, ret=%d\n", ret);
  1526. ret = 0;
  1527. return ret;
  1528. sde_rsc_fail:
  1529. sde_rsc_deinit(pdev, rsc);
  1530. rsc_alloc_fail:
  1531. return ret;
  1532. }
  1533. static int sde_rsc_remove(struct platform_device *pdev)
  1534. {
  1535. struct sde_rsc_priv *rsc = platform_get_drvdata(pdev);
  1536. sde_rsc_deinit(pdev, rsc);
  1537. return 0;
  1538. }
  1539. static int sde_rsc_rpmh_probe(struct platform_device *pdev)
  1540. {
  1541. int ret = 0;
  1542. uint32_t index = 0;
  1543. ret = of_property_read_u32(pdev->dev.of_node, "cell-index", &index);
  1544. if (ret) {
  1545. pr_err("unable to find sde rsc cell index\n");
  1546. return ret;
  1547. } else if (index >= MAX_RSC_COUNT) {
  1548. pr_err("invalid cell index for sde rsc:%d\n", index);
  1549. return -EINVAL;
  1550. }
  1551. rpmh_dev[index] = &pdev->dev;
  1552. return 0;
  1553. }
  1554. int sde_rsc_rpmh_remove(struct platform_device *pdev)
  1555. {
  1556. int i;
  1557. for (i = 0; i < MAX_RSC_COUNT; i++)
  1558. rpmh_dev[i] = NULL;
  1559. return 0;
  1560. }
  1561. static const struct of_device_id dt_match[] = {
  1562. { .compatible = "qcom,sde-rsc"},
  1563. {},
  1564. };
  1565. MODULE_DEVICE_TABLE(of, dt_match);
  1566. static struct platform_driver sde_rsc_platform_driver = {
  1567. .probe = sde_rsc_probe,
  1568. .remove = sde_rsc_remove,
  1569. .driver = {
  1570. .name = "sde_rsc",
  1571. .of_match_table = dt_match,
  1572. .suppress_bind_attrs = true,
  1573. },
  1574. };
  1575. static const struct of_device_id sde_rsc_rpmh_match[] = {
  1576. {.compatible = "qcom,sde-rsc-rpmh"},
  1577. {},
  1578. };
  1579. static struct platform_driver sde_rsc_rpmh_driver = {
  1580. .probe = sde_rsc_rpmh_probe,
  1581. .remove = sde_rsc_rpmh_remove,
  1582. .driver = {
  1583. .name = "sde_rsc_rpmh",
  1584. .of_match_table = sde_rsc_rpmh_match,
  1585. },
  1586. };
  1587. static int __init sde_rsc_register(void)
  1588. {
  1589. return platform_driver_register(&sde_rsc_platform_driver);
  1590. }
  1591. static void __exit sde_rsc_unregister(void)
  1592. {
  1593. platform_driver_unregister(&sde_rsc_platform_driver);
  1594. }
  1595. static int __init sde_rsc_rpmh_register(void)
  1596. {
  1597. return platform_driver_register(&sde_rsc_rpmh_driver);
  1598. }
  1599. subsys_initcall(sde_rsc_rpmh_register);
  1600. module_init(sde_rsc_register);
  1601. module_exit(sde_rsc_unregister);