sde_rsc.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  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. #endif /* defined(CONFIG_DEBUG_FS) */
  968. static int _sde_debugfs_status_show(struct seq_file *s, void *data)
  969. {
  970. struct sde_rsc_priv *rsc;
  971. struct sde_rsc_client *client;
  972. int ret;
  973. if (!s || !s->private)
  974. return -EINVAL;
  975. rsc = s->private;
  976. mutex_lock(&rsc->client_lock);
  977. seq_printf(s, "rsc current state:%d\n", rsc->current_state);
  978. seq_printf(s, "wraper backoff time(ns):%d\n",
  979. rsc->timer_config.static_wakeup_time_ns);
  980. seq_printf(s, "rsc backoff time(ns):%d\n",
  981. rsc->timer_config.rsc_backoff_time_ns);
  982. seq_printf(s, "pdc backoff time(ns):%d\n",
  983. rsc->timer_config.pdc_backoff_time_ns);
  984. seq_printf(s, "rsc mode threshold time(ns):%d\n",
  985. rsc->timer_config.rsc_mode_threshold_time_ns);
  986. seq_printf(s, "rsc time slot 0(ns):%d\n",
  987. rsc->timer_config.rsc_time_slot_0_ns);
  988. seq_printf(s, "rsc time slot 1(ns):%d\n",
  989. rsc->timer_config.rsc_time_slot_1_ns);
  990. seq_printf(s, "frame fps:%d jitter_numer:%d jitter_denom:%d vtotal:%d prefill lines:%d\n",
  991. rsc->cmd_config.fps, rsc->cmd_config.jitter_numer,
  992. rsc->cmd_config.jitter_denom,
  993. rsc->cmd_config.vtotal, rsc->cmd_config.prefill_lines);
  994. seq_puts(s, "\n");
  995. list_for_each_entry(client, &rsc->client_list, list)
  996. seq_printf(s, "\t client:%s state:%d\n",
  997. client->name, client->current_state);
  998. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  999. pr_debug("debug node is not supported during idle state\n");
  1000. seq_puts(s, "hw state is not supported during idle pc\n");
  1001. goto end;
  1002. }
  1003. if (rsc->hw_ops.debug_show) {
  1004. ret = rsc->hw_ops.debug_show(s, rsc);
  1005. if (ret)
  1006. pr_err("sde rsc: hw debug failed ret:%d\n", ret);
  1007. }
  1008. end:
  1009. mutex_unlock(&rsc->client_lock);
  1010. return 0;
  1011. }
  1012. static int _sde_debugfs_status_open(struct inode *inode, struct file *file)
  1013. {
  1014. return single_open(file, _sde_debugfs_status_show, inode->i_private);
  1015. }
  1016. static int _sde_debugfs_counters_show(struct seq_file *s, void *data)
  1017. {
  1018. struct sde_rsc_priv *rsc = s->private;
  1019. u32 counters[NUM_RSC_PROFILING_COUNTERS];
  1020. int i, ret;
  1021. if (!rsc)
  1022. return -EINVAL;
  1023. if (!rsc->hw_ops.get_counters) {
  1024. seq_puts(s, "counters are not supported on this target\n");
  1025. return 0;
  1026. }
  1027. memset(counters, 0, sizeof(counters));
  1028. mutex_lock(&rsc->client_lock);
  1029. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  1030. pr_debug("counters are not supported during idle state\n");
  1031. seq_puts(s, "no access to counters during idle pc\n");
  1032. goto end;
  1033. }
  1034. ret = rsc->hw_ops.get_counters(rsc, counters);
  1035. if (ret) {
  1036. pr_err("sde rsc: get_counters failed ret:%d\n", ret);
  1037. seq_puts(s, "failed to retrieve counts!\n");
  1038. goto end;
  1039. }
  1040. seq_puts(s, "rsc profiling counters:\n");
  1041. for (i = 0; i < NUM_RSC_PROFILING_COUNTERS; ++i)
  1042. seq_printf(s, "\tmode[%d] = 0x%08x:\n", i, counters[i]);
  1043. end:
  1044. mutex_unlock(&rsc->client_lock);
  1045. return 0;
  1046. }
  1047. static int _sde_debugfs_counters_open(struct inode *inode, struct file *file)
  1048. {
  1049. return single_open(file, _sde_debugfs_counters_show, inode->i_private);
  1050. }
  1051. static int _sde_debugfs_generic_noseek_open(struct inode *inode,
  1052. struct file *file)
  1053. {
  1054. /* non-seekable */
  1055. file->private_data = inode->i_private;
  1056. return nonseekable_open(inode, file);
  1057. }
  1058. static ssize_t _sde_debugfs_profiling_read(struct file *file, char __user *buf,
  1059. size_t count, loff_t *ppos)
  1060. {
  1061. struct sde_rsc_priv *rsc = file->private_data;
  1062. size_t max_size = min_t(size_t, count, MAX_BUFFER_SIZE);
  1063. char buffer[MAX_BUFFER_SIZE];
  1064. int blen = 0;
  1065. if (*ppos || !rsc)
  1066. return 0;
  1067. if (!rsc->hw_ops.setup_counters) {
  1068. blen += scnprintf(&buffer[blen], max_size - blen,
  1069. "counters are not supported on this target\n");
  1070. goto end;
  1071. }
  1072. mutex_lock(&rsc->client_lock);
  1073. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  1074. pr_debug("counters are not supported during idle state\n");
  1075. blen += scnprintf(&buffer[blen], max_size - blen,
  1076. "no access to counters during idle pc\n");
  1077. goto unlock;
  1078. }
  1079. blen += scnprintf(&buffer[blen], max_size - blen,
  1080. "%s\n", rsc->profiling_en ? "Y" : "N");
  1081. unlock:
  1082. mutex_unlock(&rsc->client_lock);
  1083. end:
  1084. if (copy_to_user(buf, buffer, blen))
  1085. return -EFAULT;
  1086. *ppos += blen;
  1087. return blen;
  1088. }
  1089. static ssize_t _sde_debugfs_profiling_write(struct file *file,
  1090. const char __user *p, size_t count, loff_t *ppos)
  1091. {
  1092. struct sde_rsc_priv *rsc = file->private_data;
  1093. bool input_valid, input_value;
  1094. char *input;
  1095. int rc;
  1096. if (!rsc || !rsc->hw_ops.setup_counters || !count ||
  1097. count > MAX_COUNT_SIZE_SUPPORTED)
  1098. return 0;
  1099. input = kmalloc(count + 1, GFP_KERNEL);
  1100. if (!input)
  1101. return -ENOMEM;
  1102. if (copy_from_user(input, p, count)) {
  1103. kfree(input);
  1104. return -EFAULT;
  1105. }
  1106. input[count] = '\0';
  1107. switch (input[0]) {
  1108. case 'y':
  1109. case 'Y':
  1110. case '1':
  1111. input_valid = true;
  1112. input_value = true;
  1113. break;
  1114. case 'n':
  1115. case 'N':
  1116. case '0':
  1117. input_valid = true;
  1118. input_value = false;
  1119. break;
  1120. default:
  1121. input_valid = false;
  1122. count = 0;
  1123. break;
  1124. }
  1125. mutex_lock(&rsc->client_lock);
  1126. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  1127. pr_debug("debug node is not supported during idle state\n");
  1128. count = 0;
  1129. goto end;
  1130. }
  1131. pr_debug("input %s, profiling_en: %d\n",
  1132. input_valid ? "valid" : "invalid", rsc->profiling_en);
  1133. if (input_valid) {
  1134. rsc->profiling_en = input_value;
  1135. rc = rsc->hw_ops.setup_counters(rsc, rsc->profiling_en);
  1136. if (rc)
  1137. pr_err("setup_counters failed, rc:%d\n", rc);
  1138. }
  1139. end:
  1140. mutex_unlock(&rsc->client_lock);
  1141. kfree(input);
  1142. return count;
  1143. }
  1144. static ssize_t _sde_debugfs_mode_ctrl_read(struct file *file, char __user *buf,
  1145. size_t count, loff_t *ppos)
  1146. {
  1147. struct sde_rsc_priv *rsc = file->private_data;
  1148. char buffer[MAX_BUFFER_SIZE];
  1149. int blen = 0;
  1150. size_t max_size = min_t(size_t, count, MAX_BUFFER_SIZE);
  1151. if (*ppos || !rsc || !rsc->hw_ops.mode_ctrl)
  1152. return 0;
  1153. mutex_lock(&rsc->client_lock);
  1154. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  1155. pr_debug("debug node is not supported during idle state\n");
  1156. blen = scnprintf(buffer, max_size,
  1157. "hw state is not supported during idle pc\n");
  1158. goto end;
  1159. }
  1160. blen = rsc->hw_ops.mode_ctrl(rsc, MODE_READ, buffer, max_size, 0);
  1161. end:
  1162. mutex_unlock(&rsc->client_lock);
  1163. if (blen <= 0)
  1164. return 0;
  1165. if (blen > count) {
  1166. blen = count;
  1167. buffer[count - 1] = '\0';
  1168. }
  1169. if (copy_to_user(buf, buffer, blen))
  1170. return -EFAULT;
  1171. *ppos += blen;
  1172. return blen;
  1173. }
  1174. static ssize_t _sde_debugfs_mode_ctrl_write(struct file *file,
  1175. const char __user *p, size_t count, loff_t *ppos)
  1176. {
  1177. struct sde_rsc_priv *rsc = file->private_data;
  1178. char *input;
  1179. u32 mode_state = 0;
  1180. int rc;
  1181. if (!rsc || !rsc->hw_ops.mode_ctrl || !count ||
  1182. count > MAX_COUNT_SIZE_SUPPORTED)
  1183. return 0;
  1184. input = kmalloc(count + 1, GFP_KERNEL);
  1185. if (!input)
  1186. return -ENOMEM;
  1187. if (copy_from_user(input, p, count)) {
  1188. kfree(input);
  1189. return -EFAULT;
  1190. }
  1191. input[count] = '\0';
  1192. rc = kstrtoint(input, 0, &mode_state);
  1193. if (rc) {
  1194. pr_err("mode_state: int conversion failed rc:%d\n", rc);
  1195. goto end;
  1196. }
  1197. pr_debug("mode_state: %d\n", mode_state);
  1198. mode_state &= 0x7;
  1199. if (mode_state != ALL_MODES_DISABLED &&
  1200. mode_state != ALL_MODES_ENABLED &&
  1201. mode_state != ONLY_MODE_0_ENABLED &&
  1202. mode_state != ONLY_MODE_0_1_ENABLED) {
  1203. pr_err("invalid mode:%d combination\n", mode_state);
  1204. goto end;
  1205. }
  1206. mutex_lock(&rsc->client_lock);
  1207. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  1208. pr_debug("debug node is not supported during idle state\n");
  1209. goto state_check;
  1210. }
  1211. rsc->hw_ops.mode_ctrl(rsc, MODE_UPDATE, NULL, 0, mode_state);
  1212. state_check:
  1213. mutex_unlock(&rsc->client_lock);
  1214. end:
  1215. kfree(input);
  1216. return count;
  1217. }
  1218. static ssize_t _sde_debugfs_vsync_mode_read(struct file *file, char __user *buf,
  1219. size_t count, loff_t *ppos)
  1220. {
  1221. struct sde_rsc_priv *rsc = file->private_data;
  1222. char buffer[MAX_BUFFER_SIZE];
  1223. int blen = 0;
  1224. size_t max_size = min_t(size_t, count, MAX_BUFFER_SIZE);
  1225. if (*ppos || !rsc || !rsc->hw_ops.hw_vsync)
  1226. return 0;
  1227. mutex_lock(&rsc->client_lock);
  1228. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  1229. pr_debug("debug node is not supported during idle state\n");
  1230. blen = scnprintf(buffer, max_size,
  1231. "hw state is not supported during idle pc\n");
  1232. goto end;
  1233. }
  1234. blen = rsc->hw_ops.hw_vsync(rsc, VSYNC_READ, buffer, max_size, 0);
  1235. end:
  1236. mutex_unlock(&rsc->client_lock);
  1237. if (blen <= 0)
  1238. return 0;
  1239. if (blen > count) {
  1240. blen = count;
  1241. buffer[count - 1] = '\0';
  1242. }
  1243. if (copy_to_user(buf, buffer, blen))
  1244. return -EFAULT;
  1245. *ppos += blen;
  1246. return blen;
  1247. }
  1248. static ssize_t _sde_debugfs_vsync_mode_write(struct file *file,
  1249. const char __user *p, size_t count, loff_t *ppos)
  1250. {
  1251. struct sde_rsc_priv *rsc = file->private_data;
  1252. char *input;
  1253. u32 vsync_state = 0;
  1254. int rc;
  1255. if (!rsc || !rsc->hw_ops.hw_vsync || !count ||
  1256. count > MAX_COUNT_SIZE_SUPPORTED)
  1257. return 0;
  1258. input = kmalloc(count + 1, GFP_KERNEL);
  1259. if (!input)
  1260. return -ENOMEM;
  1261. if (copy_from_user(input, p, count)) {
  1262. kfree(input);
  1263. return -EFAULT;
  1264. }
  1265. input[count] = '\0';
  1266. rc = kstrtoint(input, 0, &vsync_state);
  1267. if (rc) {
  1268. pr_err("vsync_state: int conversion failed rc:%d\n", rc);
  1269. goto end;
  1270. }
  1271. pr_debug("vsync_state: %d\n", vsync_state);
  1272. vsync_state &= 0x7;
  1273. mutex_lock(&rsc->client_lock);
  1274. if (rsc->current_state == SDE_RSC_IDLE_STATE) {
  1275. pr_debug("debug node is not supported during idle state\n");
  1276. goto state_check;
  1277. }
  1278. if (vsync_state)
  1279. rsc->hw_ops.hw_vsync(rsc, VSYNC_ENABLE, NULL,
  1280. 0, vsync_state - 1);
  1281. else
  1282. rsc->hw_ops.hw_vsync(rsc, VSYNC_DISABLE, NULL, 0, 0);
  1283. state_check:
  1284. mutex_unlock(&rsc->client_lock);
  1285. end:
  1286. kfree(input);
  1287. return count;
  1288. }
  1289. static const struct file_operations debugfs_status_fops = {
  1290. .open = _sde_debugfs_status_open,
  1291. .read = seq_read,
  1292. .llseek = seq_lseek,
  1293. .release = single_release,
  1294. };
  1295. static const struct file_operations mode_control_fops = {
  1296. .open = _sde_debugfs_generic_noseek_open,
  1297. .read = _sde_debugfs_mode_ctrl_read,
  1298. .write = _sde_debugfs_mode_ctrl_write,
  1299. };
  1300. static const struct file_operations vsync_status_fops = {
  1301. .open = _sde_debugfs_generic_noseek_open,
  1302. .read = _sde_debugfs_vsync_mode_read,
  1303. .write = _sde_debugfs_vsync_mode_write,
  1304. };
  1305. static const struct file_operations profiling_enable_fops = {
  1306. .open = _sde_debugfs_generic_noseek_open,
  1307. .read = _sde_debugfs_profiling_read,
  1308. .write = _sde_debugfs_profiling_write,
  1309. };
  1310. static const struct file_operations profiling_counts_fops = {
  1311. .open = _sde_debugfs_counters_open,
  1312. .read = seq_read,
  1313. .llseek = seq_lseek,
  1314. .release = single_release,
  1315. };
  1316. static void _sde_rsc_init_debugfs(struct sde_rsc_priv *rsc, char *name)
  1317. {
  1318. rsc->debugfs_root = debugfs_create_dir(name, NULL);
  1319. if (!rsc->debugfs_root)
  1320. return;
  1321. /* don't error check these */
  1322. debugfs_create_file("status", 0400, rsc->debugfs_root, rsc,
  1323. &debugfs_status_fops);
  1324. debugfs_create_file("mode_control", 0600, rsc->debugfs_root, rsc,
  1325. &mode_control_fops);
  1326. debugfs_create_file("vsync_mode", 0600, rsc->debugfs_root, rsc,
  1327. &vsync_status_fops);
  1328. if (rsc->profiling_supp) {
  1329. debugfs_create_file("profiling_en", 0600, rsc->debugfs_root,
  1330. rsc, &profiling_enable_fops);
  1331. debugfs_create_file("profiling_counts", 0400,
  1332. rsc->debugfs_root, rsc,
  1333. &profiling_counts_fops);
  1334. }
  1335. debugfs_create_x32("debug_mode", 0600, rsc->debugfs_root,
  1336. &rsc->debug_mode);
  1337. }
  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. mutex_lock(&rsc->client_lock);
  1383. rsc->master_drm = drm;
  1384. mutex_unlock(&rsc->client_lock);
  1385. sde_dbg_reg_register_base(SDE_RSC_DRV_DBG_NAME, rsc->drv_io.base,
  1386. rsc->drv_io.len);
  1387. sde_dbg_reg_register_base(SDE_RSC_WRAPPER_DBG_NAME,
  1388. rsc->wrapper_io.base, rsc->wrapper_io.len);
  1389. return 0;
  1390. }
  1391. /**
  1392. * sde_rsc_unbind - unbind rsc from controlling device
  1393. * @dev: Pointer to base of platform device
  1394. * @master: Pointer to container of drm device
  1395. * @data: Pointer to private data
  1396. */
  1397. static void sde_rsc_unbind(struct device *dev,
  1398. struct device *master, void *data)
  1399. {
  1400. struct sde_rsc_priv *rsc;
  1401. struct platform_device *pdev = to_platform_device(dev);
  1402. if (!dev || !pdev) {
  1403. pr_err("invalid param(s)\n");
  1404. return;
  1405. }
  1406. rsc = platform_get_drvdata(pdev);
  1407. if (!rsc) {
  1408. pr_err("invalid display rsc\n");
  1409. return;
  1410. }
  1411. mutex_lock(&rsc->client_lock);
  1412. rsc->master_drm = NULL;
  1413. mutex_unlock(&rsc->client_lock);
  1414. }
  1415. static const struct component_ops sde_rsc_comp_ops = {
  1416. .bind = sde_rsc_bind,
  1417. .unbind = sde_rsc_unbind,
  1418. };
  1419. static int sde_rsc_probe(struct platform_device *pdev)
  1420. {
  1421. int ret;
  1422. struct sde_rsc_priv *rsc;
  1423. static int counter;
  1424. char name[MAX_RSC_CLIENT_NAME_LEN];
  1425. if (counter >= MAX_RSC_COUNT) {
  1426. pr_err("sde rsc supports probe till MAX_RSC_COUNT=%d devices\n",
  1427. MAX_RSC_COUNT);
  1428. return -EINVAL;
  1429. }
  1430. rsc = kzalloc(sizeof(*rsc), GFP_KERNEL);
  1431. if (!rsc) {
  1432. ret = -ENOMEM;
  1433. goto rsc_alloc_fail;
  1434. }
  1435. platform_set_drvdata(pdev, rsc);
  1436. rsc->dev = &pdev->dev;
  1437. of_property_read_u32(pdev->dev.of_node, "qcom,sde-rsc-version",
  1438. &rsc->version);
  1439. if (rsc->version >= SDE_RSC_REV_2)
  1440. rsc->single_tcs_execution_time = SINGLE_TCS_EXECUTION_TIME_V2;
  1441. else
  1442. rsc->single_tcs_execution_time = SINGLE_TCS_EXECUTION_TIME_V1;
  1443. if (rsc->version >= SDE_RSC_REV_3) {
  1444. rsc->time_slot_0_ns = rsc->single_tcs_execution_time
  1445. + RSC_MODE_INSTRUCTION_TIME;
  1446. rsc->backoff_time_ns = RSC_MODE_INSTRUCTION_TIME;
  1447. rsc->mode_threshold_time_ns = rsc->time_slot_0_ns;
  1448. } else {
  1449. rsc->time_slot_0_ns = (rsc->single_tcs_execution_time * 2)
  1450. + RSC_MODE_INSTRUCTION_TIME;
  1451. rsc->backoff_time_ns = rsc->single_tcs_execution_time
  1452. + RSC_MODE_INSTRUCTION_TIME;
  1453. rsc->mode_threshold_time_ns = rsc->backoff_time_ns
  1454. + RSC_MODE_THRESHOLD_OVERHEAD;
  1455. }
  1456. if (rsc->version >= SDE_RSC_REV_4)
  1457. rsc->profiling_supp = true;
  1458. ret = sde_power_resource_init(pdev, &rsc->phandle);
  1459. if (ret) {
  1460. pr_err("sde rsc:power resource init failed ret:%d\n", ret);
  1461. goto sde_rsc_fail;
  1462. }
  1463. rsc->rpmh_dev = rpmh_dev[SDE_RSC_INDEX + counter];
  1464. if (IS_ERR_OR_NULL(rsc->rpmh_dev)) {
  1465. ret = !rsc->rpmh_dev ? -EINVAL : PTR_ERR(rsc->rpmh_dev);
  1466. rsc->rpmh_dev = NULL;
  1467. pr_err("rpmh device node is not available ret:%d\n", ret);
  1468. goto sde_rsc_fail;
  1469. }
  1470. ret = msm_dss_ioremap_byname(pdev, &rsc->wrapper_io, "wrapper");
  1471. if (ret) {
  1472. pr_err("sde rsc: wrapper io data mapping failed ret=%d\n", ret);
  1473. goto sde_rsc_fail;
  1474. }
  1475. ret = msm_dss_ioremap_byname(pdev, &rsc->drv_io, "drv");
  1476. if (ret) {
  1477. pr_err("sde rsc: drv io data mapping failed ret:%d\n", ret);
  1478. goto sde_rsc_fail;
  1479. }
  1480. rsc->fs = devm_regulator_get(&pdev->dev, "vdd");
  1481. if (IS_ERR_OR_NULL(rsc->fs)) {
  1482. rsc->fs = NULL;
  1483. pr_err("unable to get regulator\n");
  1484. goto sde_rsc_fail;
  1485. }
  1486. if (rsc->version >= SDE_RSC_REV_3)
  1487. ret = sde_rsc_hw_register_v3(rsc);
  1488. else
  1489. ret = sde_rsc_hw_register(rsc);
  1490. if (ret) {
  1491. pr_err("sde rsc: hw register failed ret:%d\n", ret);
  1492. goto sde_rsc_fail;
  1493. }
  1494. ret = regulator_enable(rsc->fs);
  1495. if (ret) {
  1496. pr_err("sde rsc: fs on failed ret:%d\n", ret);
  1497. goto sde_rsc_fail;
  1498. }
  1499. rsc->sw_fs_enabled = true;
  1500. ret = sde_rsc_resource_enable(rsc);
  1501. if (ret < 0) {
  1502. pr_err("failed to enable sde rsc power resources rc:%d\n", ret);
  1503. goto sde_rsc_fail;
  1504. }
  1505. if (sde_rsc_timer_calculate(rsc, NULL, SDE_RSC_IDLE_STATE))
  1506. goto sde_rsc_fail;
  1507. sde_rsc_resource_disable(rsc);
  1508. INIT_LIST_HEAD(&rsc->client_list);
  1509. INIT_LIST_HEAD(&rsc->event_list);
  1510. mutex_init(&rsc->client_lock);
  1511. init_waitqueue_head(&rsc->rsc_vsync_waitq);
  1512. atomic_set(&rsc->resource_refcount, 0);
  1513. pr_info("sde rsc index:%d probed successfully\n",
  1514. SDE_RSC_INDEX + counter);
  1515. rsc_prv_list[SDE_RSC_INDEX + counter] = rsc;
  1516. snprintf(name, MAX_RSC_CLIENT_NAME_LEN, "%s%d", "sde_rsc", counter);
  1517. _sde_rsc_init_debugfs(rsc, name);
  1518. counter++;
  1519. ret = component_add(&pdev->dev, &sde_rsc_comp_ops);
  1520. if (ret)
  1521. pr_debug("component add failed, ret=%d\n", ret);
  1522. ret = 0;
  1523. return ret;
  1524. sde_rsc_fail:
  1525. sde_rsc_deinit(pdev, rsc);
  1526. rsc_alloc_fail:
  1527. return ret;
  1528. }
  1529. static int sde_rsc_remove(struct platform_device *pdev)
  1530. {
  1531. struct sde_rsc_priv *rsc = platform_get_drvdata(pdev);
  1532. sde_rsc_deinit(pdev, rsc);
  1533. return 0;
  1534. }
  1535. static int sde_rsc_rpmh_probe(struct platform_device *pdev)
  1536. {
  1537. int ret = 0;
  1538. uint32_t index = 0;
  1539. ret = of_property_read_u32(pdev->dev.of_node, "cell-index", &index);
  1540. if (ret) {
  1541. pr_err("unable to find sde rsc cell index\n");
  1542. return ret;
  1543. } else if (index >= MAX_RSC_COUNT) {
  1544. pr_err("invalid cell index for sde rsc:%d\n", index);
  1545. return -EINVAL;
  1546. }
  1547. rpmh_dev[index] = &pdev->dev;
  1548. return 0;
  1549. }
  1550. int sde_rsc_rpmh_remove(struct platform_device *pdev)
  1551. {
  1552. int i;
  1553. for (i = 0; i < MAX_RSC_COUNT; i++)
  1554. rpmh_dev[i] = NULL;
  1555. return 0;
  1556. }
  1557. static const struct of_device_id dt_match[] = {
  1558. { .compatible = "qcom,sde-rsc"},
  1559. {},
  1560. };
  1561. MODULE_DEVICE_TABLE(of, dt_match);
  1562. static struct platform_driver sde_rsc_platform_driver = {
  1563. .probe = sde_rsc_probe,
  1564. .remove = sde_rsc_remove,
  1565. .driver = {
  1566. .name = "sde_rsc",
  1567. .of_match_table = dt_match,
  1568. .suppress_bind_attrs = true,
  1569. },
  1570. };
  1571. static const struct of_device_id sde_rsc_rpmh_match[] = {
  1572. {.compatible = "qcom,sde-rsc-rpmh"},
  1573. {},
  1574. };
  1575. static struct platform_driver sde_rsc_rpmh_driver = {
  1576. .probe = sde_rsc_rpmh_probe,
  1577. .remove = sde_rsc_rpmh_remove,
  1578. .driver = {
  1579. .name = "sde_rsc_rpmh",
  1580. .of_match_table = sde_rsc_rpmh_match,
  1581. },
  1582. };
  1583. static int __init sde_rsc_register(void)
  1584. {
  1585. return platform_driver_register(&sde_rsc_platform_driver);
  1586. }
  1587. static void __exit sde_rsc_unregister(void)
  1588. {
  1589. platform_driver_unregister(&sde_rsc_platform_driver);
  1590. }
  1591. static int __init sde_rsc_rpmh_register(void)
  1592. {
  1593. return platform_driver_register(&sde_rsc_rpmh_driver);
  1594. }
  1595. subsys_initcall(sde_rsc_rpmh_register);
  1596. module_init(sde_rsc_register);
  1597. module_exit(sde_rsc_unregister);