etnaviv_gpu.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2015-2018 Etnaviv Project
  4. */
  5. #include <linux/component.h>
  6. #include <linux/dma-fence.h>
  7. #include <linux/moduleparam.h>
  8. #include <linux/of_device.h>
  9. #include <linux/thermal.h>
  10. #include "etnaviv_cmdbuf.h"
  11. #include "etnaviv_dump.h"
  12. #include "etnaviv_gpu.h"
  13. #include "etnaviv_gem.h"
  14. #include "etnaviv_mmu.h"
  15. #include "etnaviv_perfmon.h"
  16. #include "etnaviv_sched.h"
  17. #include "common.xml.h"
  18. #include "state.xml.h"
  19. #include "state_hi.xml.h"
  20. #include "cmdstream.xml.h"
  21. #ifndef PHYS_OFFSET
  22. #define PHYS_OFFSET 0
  23. #endif
  24. static const struct platform_device_id gpu_ids[] = {
  25. { .name = "etnaviv-gpu,2d" },
  26. { },
  27. };
  28. /*
  29. * Driver functions:
  30. */
  31. int etnaviv_gpu_get_param(struct etnaviv_gpu *gpu, u32 param, u64 *value)
  32. {
  33. switch (param) {
  34. case ETNAVIV_PARAM_GPU_MODEL:
  35. *value = gpu->identity.model;
  36. break;
  37. case ETNAVIV_PARAM_GPU_REVISION:
  38. *value = gpu->identity.revision;
  39. break;
  40. case ETNAVIV_PARAM_GPU_FEATURES_0:
  41. *value = gpu->identity.features;
  42. break;
  43. case ETNAVIV_PARAM_GPU_FEATURES_1:
  44. *value = gpu->identity.minor_features0;
  45. break;
  46. case ETNAVIV_PARAM_GPU_FEATURES_2:
  47. *value = gpu->identity.minor_features1;
  48. break;
  49. case ETNAVIV_PARAM_GPU_FEATURES_3:
  50. *value = gpu->identity.minor_features2;
  51. break;
  52. case ETNAVIV_PARAM_GPU_FEATURES_4:
  53. *value = gpu->identity.minor_features3;
  54. break;
  55. case ETNAVIV_PARAM_GPU_FEATURES_5:
  56. *value = gpu->identity.minor_features4;
  57. break;
  58. case ETNAVIV_PARAM_GPU_FEATURES_6:
  59. *value = gpu->identity.minor_features5;
  60. break;
  61. case ETNAVIV_PARAM_GPU_FEATURES_7:
  62. *value = gpu->identity.minor_features6;
  63. break;
  64. case ETNAVIV_PARAM_GPU_FEATURES_8:
  65. *value = gpu->identity.minor_features7;
  66. break;
  67. case ETNAVIV_PARAM_GPU_FEATURES_9:
  68. *value = gpu->identity.minor_features8;
  69. break;
  70. case ETNAVIV_PARAM_GPU_FEATURES_10:
  71. *value = gpu->identity.minor_features9;
  72. break;
  73. case ETNAVIV_PARAM_GPU_FEATURES_11:
  74. *value = gpu->identity.minor_features10;
  75. break;
  76. case ETNAVIV_PARAM_GPU_FEATURES_12:
  77. *value = gpu->identity.minor_features11;
  78. break;
  79. case ETNAVIV_PARAM_GPU_STREAM_COUNT:
  80. *value = gpu->identity.stream_count;
  81. break;
  82. case ETNAVIV_PARAM_GPU_REGISTER_MAX:
  83. *value = gpu->identity.register_max;
  84. break;
  85. case ETNAVIV_PARAM_GPU_THREAD_COUNT:
  86. *value = gpu->identity.thread_count;
  87. break;
  88. case ETNAVIV_PARAM_GPU_VERTEX_CACHE_SIZE:
  89. *value = gpu->identity.vertex_cache_size;
  90. break;
  91. case ETNAVIV_PARAM_GPU_SHADER_CORE_COUNT:
  92. *value = gpu->identity.shader_core_count;
  93. break;
  94. case ETNAVIV_PARAM_GPU_PIXEL_PIPES:
  95. *value = gpu->identity.pixel_pipes;
  96. break;
  97. case ETNAVIV_PARAM_GPU_VERTEX_OUTPUT_BUFFER_SIZE:
  98. *value = gpu->identity.vertex_output_buffer_size;
  99. break;
  100. case ETNAVIV_PARAM_GPU_BUFFER_SIZE:
  101. *value = gpu->identity.buffer_size;
  102. break;
  103. case ETNAVIV_PARAM_GPU_INSTRUCTION_COUNT:
  104. *value = gpu->identity.instruction_count;
  105. break;
  106. case ETNAVIV_PARAM_GPU_NUM_CONSTANTS:
  107. *value = gpu->identity.num_constants;
  108. break;
  109. case ETNAVIV_PARAM_GPU_NUM_VARYINGS:
  110. *value = gpu->identity.varyings_count;
  111. break;
  112. default:
  113. DBG("%s: invalid param: %u", dev_name(gpu->dev), param);
  114. return -EINVAL;
  115. }
  116. return 0;
  117. }
  118. #define etnaviv_is_model_rev(gpu, mod, rev) \
  119. ((gpu)->identity.model == chipModel_##mod && \
  120. (gpu)->identity.revision == rev)
  121. #define etnaviv_field(val, field) \
  122. (((val) & field##__MASK) >> field##__SHIFT)
  123. static void etnaviv_hw_specs(struct etnaviv_gpu *gpu)
  124. {
  125. if (gpu->identity.minor_features0 &
  126. chipMinorFeatures0_MORE_MINOR_FEATURES) {
  127. u32 specs[4];
  128. unsigned int streams;
  129. specs[0] = gpu_read(gpu, VIVS_HI_CHIP_SPECS);
  130. specs[1] = gpu_read(gpu, VIVS_HI_CHIP_SPECS_2);
  131. specs[2] = gpu_read(gpu, VIVS_HI_CHIP_SPECS_3);
  132. specs[3] = gpu_read(gpu, VIVS_HI_CHIP_SPECS_4);
  133. gpu->identity.stream_count = etnaviv_field(specs[0],
  134. VIVS_HI_CHIP_SPECS_STREAM_COUNT);
  135. gpu->identity.register_max = etnaviv_field(specs[0],
  136. VIVS_HI_CHIP_SPECS_REGISTER_MAX);
  137. gpu->identity.thread_count = etnaviv_field(specs[0],
  138. VIVS_HI_CHIP_SPECS_THREAD_COUNT);
  139. gpu->identity.vertex_cache_size = etnaviv_field(specs[0],
  140. VIVS_HI_CHIP_SPECS_VERTEX_CACHE_SIZE);
  141. gpu->identity.shader_core_count = etnaviv_field(specs[0],
  142. VIVS_HI_CHIP_SPECS_SHADER_CORE_COUNT);
  143. gpu->identity.pixel_pipes = etnaviv_field(specs[0],
  144. VIVS_HI_CHIP_SPECS_PIXEL_PIPES);
  145. gpu->identity.vertex_output_buffer_size =
  146. etnaviv_field(specs[0],
  147. VIVS_HI_CHIP_SPECS_VERTEX_OUTPUT_BUFFER_SIZE);
  148. gpu->identity.buffer_size = etnaviv_field(specs[1],
  149. VIVS_HI_CHIP_SPECS_2_BUFFER_SIZE);
  150. gpu->identity.instruction_count = etnaviv_field(specs[1],
  151. VIVS_HI_CHIP_SPECS_2_INSTRUCTION_COUNT);
  152. gpu->identity.num_constants = etnaviv_field(specs[1],
  153. VIVS_HI_CHIP_SPECS_2_NUM_CONSTANTS);
  154. gpu->identity.varyings_count = etnaviv_field(specs[2],
  155. VIVS_HI_CHIP_SPECS_3_VARYINGS_COUNT);
  156. /* This overrides the value from older register if non-zero */
  157. streams = etnaviv_field(specs[3],
  158. VIVS_HI_CHIP_SPECS_4_STREAM_COUNT);
  159. if (streams)
  160. gpu->identity.stream_count = streams;
  161. }
  162. /* Fill in the stream count if not specified */
  163. if (gpu->identity.stream_count == 0) {
  164. if (gpu->identity.model >= 0x1000)
  165. gpu->identity.stream_count = 4;
  166. else
  167. gpu->identity.stream_count = 1;
  168. }
  169. /* Convert the register max value */
  170. if (gpu->identity.register_max)
  171. gpu->identity.register_max = 1 << gpu->identity.register_max;
  172. else if (gpu->identity.model == chipModel_GC400)
  173. gpu->identity.register_max = 32;
  174. else
  175. gpu->identity.register_max = 64;
  176. /* Convert thread count */
  177. if (gpu->identity.thread_count)
  178. gpu->identity.thread_count = 1 << gpu->identity.thread_count;
  179. else if (gpu->identity.model == chipModel_GC400)
  180. gpu->identity.thread_count = 64;
  181. else if (gpu->identity.model == chipModel_GC500 ||
  182. gpu->identity.model == chipModel_GC530)
  183. gpu->identity.thread_count = 128;
  184. else
  185. gpu->identity.thread_count = 256;
  186. if (gpu->identity.vertex_cache_size == 0)
  187. gpu->identity.vertex_cache_size = 8;
  188. if (gpu->identity.shader_core_count == 0) {
  189. if (gpu->identity.model >= 0x1000)
  190. gpu->identity.shader_core_count = 2;
  191. else
  192. gpu->identity.shader_core_count = 1;
  193. }
  194. if (gpu->identity.pixel_pipes == 0)
  195. gpu->identity.pixel_pipes = 1;
  196. /* Convert virtex buffer size */
  197. if (gpu->identity.vertex_output_buffer_size) {
  198. gpu->identity.vertex_output_buffer_size =
  199. 1 << gpu->identity.vertex_output_buffer_size;
  200. } else if (gpu->identity.model == chipModel_GC400) {
  201. if (gpu->identity.revision < 0x4000)
  202. gpu->identity.vertex_output_buffer_size = 512;
  203. else if (gpu->identity.revision < 0x4200)
  204. gpu->identity.vertex_output_buffer_size = 256;
  205. else
  206. gpu->identity.vertex_output_buffer_size = 128;
  207. } else {
  208. gpu->identity.vertex_output_buffer_size = 512;
  209. }
  210. switch (gpu->identity.instruction_count) {
  211. case 0:
  212. if (etnaviv_is_model_rev(gpu, GC2000, 0x5108) ||
  213. gpu->identity.model == chipModel_GC880)
  214. gpu->identity.instruction_count = 512;
  215. else
  216. gpu->identity.instruction_count = 256;
  217. break;
  218. case 1:
  219. gpu->identity.instruction_count = 1024;
  220. break;
  221. case 2:
  222. gpu->identity.instruction_count = 2048;
  223. break;
  224. default:
  225. gpu->identity.instruction_count = 256;
  226. break;
  227. }
  228. if (gpu->identity.num_constants == 0)
  229. gpu->identity.num_constants = 168;
  230. if (gpu->identity.varyings_count == 0) {
  231. if (gpu->identity.minor_features1 & chipMinorFeatures1_HALTI0)
  232. gpu->identity.varyings_count = 12;
  233. else
  234. gpu->identity.varyings_count = 8;
  235. }
  236. /*
  237. * For some cores, two varyings are consumed for position, so the
  238. * maximum varying count needs to be reduced by one.
  239. */
  240. if (etnaviv_is_model_rev(gpu, GC5000, 0x5434) ||
  241. etnaviv_is_model_rev(gpu, GC4000, 0x5222) ||
  242. etnaviv_is_model_rev(gpu, GC4000, 0x5245) ||
  243. etnaviv_is_model_rev(gpu, GC4000, 0x5208) ||
  244. etnaviv_is_model_rev(gpu, GC3000, 0x5435) ||
  245. etnaviv_is_model_rev(gpu, GC2200, 0x5244) ||
  246. etnaviv_is_model_rev(gpu, GC2100, 0x5108) ||
  247. etnaviv_is_model_rev(gpu, GC2000, 0x5108) ||
  248. etnaviv_is_model_rev(gpu, GC1500, 0x5246) ||
  249. etnaviv_is_model_rev(gpu, GC880, 0x5107) ||
  250. etnaviv_is_model_rev(gpu, GC880, 0x5106))
  251. gpu->identity.varyings_count -= 1;
  252. }
  253. static void etnaviv_hw_identify(struct etnaviv_gpu *gpu)
  254. {
  255. u32 chipIdentity;
  256. chipIdentity = gpu_read(gpu, VIVS_HI_CHIP_IDENTITY);
  257. /* Special case for older graphic cores. */
  258. if (etnaviv_field(chipIdentity, VIVS_HI_CHIP_IDENTITY_FAMILY) == 0x01) {
  259. gpu->identity.model = chipModel_GC500;
  260. gpu->identity.revision = etnaviv_field(chipIdentity,
  261. VIVS_HI_CHIP_IDENTITY_REVISION);
  262. } else {
  263. gpu->identity.model = gpu_read(gpu, VIVS_HI_CHIP_MODEL);
  264. gpu->identity.revision = gpu_read(gpu, VIVS_HI_CHIP_REV);
  265. /*
  266. * !!!! HACK ALERT !!!!
  267. * Because people change device IDs without letting software
  268. * know about it - here is the hack to make it all look the
  269. * same. Only for GC400 family.
  270. */
  271. if ((gpu->identity.model & 0xff00) == 0x0400 &&
  272. gpu->identity.model != chipModel_GC420) {
  273. gpu->identity.model = gpu->identity.model & 0x0400;
  274. }
  275. /* Another special case */
  276. if (etnaviv_is_model_rev(gpu, GC300, 0x2201)) {
  277. u32 chipDate = gpu_read(gpu, VIVS_HI_CHIP_DATE);
  278. u32 chipTime = gpu_read(gpu, VIVS_HI_CHIP_TIME);
  279. if (chipDate == 0x20080814 && chipTime == 0x12051100) {
  280. /*
  281. * This IP has an ECO; put the correct
  282. * revision in it.
  283. */
  284. gpu->identity.revision = 0x1051;
  285. }
  286. }
  287. /*
  288. * NXP likes to call the GPU on the i.MX6QP GC2000+, but in
  289. * reality it's just a re-branded GC3000. We can identify this
  290. * core by the upper half of the revision register being all 1.
  291. * Fix model/rev here, so all other places can refer to this
  292. * core by its real identity.
  293. */
  294. if (etnaviv_is_model_rev(gpu, GC2000, 0xffff5450)) {
  295. gpu->identity.model = chipModel_GC3000;
  296. gpu->identity.revision &= 0xffff;
  297. }
  298. }
  299. dev_info(gpu->dev, "model: GC%x, revision: %x\n",
  300. gpu->identity.model, gpu->identity.revision);
  301. /*
  302. * If there is a match in the HWDB, we aren't interested in the
  303. * remaining register values, as they might be wrong.
  304. */
  305. if (etnaviv_fill_identity_from_hwdb(gpu))
  306. return;
  307. gpu->identity.features = gpu_read(gpu, VIVS_HI_CHIP_FEATURE);
  308. /* Disable fast clear on GC700. */
  309. if (gpu->identity.model == chipModel_GC700)
  310. gpu->identity.features &= ~chipFeatures_FAST_CLEAR;
  311. if ((gpu->identity.model == chipModel_GC500 &&
  312. gpu->identity.revision < 2) ||
  313. (gpu->identity.model == chipModel_GC300 &&
  314. gpu->identity.revision < 0x2000)) {
  315. /*
  316. * GC500 rev 1.x and GC300 rev < 2.0 doesn't have these
  317. * registers.
  318. */
  319. gpu->identity.minor_features0 = 0;
  320. gpu->identity.minor_features1 = 0;
  321. gpu->identity.minor_features2 = 0;
  322. gpu->identity.minor_features3 = 0;
  323. gpu->identity.minor_features4 = 0;
  324. gpu->identity.minor_features5 = 0;
  325. } else
  326. gpu->identity.minor_features0 =
  327. gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_0);
  328. if (gpu->identity.minor_features0 &
  329. chipMinorFeatures0_MORE_MINOR_FEATURES) {
  330. gpu->identity.minor_features1 =
  331. gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_1);
  332. gpu->identity.minor_features2 =
  333. gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_2);
  334. gpu->identity.minor_features3 =
  335. gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_3);
  336. gpu->identity.minor_features4 =
  337. gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_4);
  338. gpu->identity.minor_features5 =
  339. gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_5);
  340. }
  341. /* GC600 idle register reports zero bits where modules aren't present */
  342. if (gpu->identity.model == chipModel_GC600) {
  343. gpu->idle_mask = VIVS_HI_IDLE_STATE_TX |
  344. VIVS_HI_IDLE_STATE_RA |
  345. VIVS_HI_IDLE_STATE_SE |
  346. VIVS_HI_IDLE_STATE_PA |
  347. VIVS_HI_IDLE_STATE_SH |
  348. VIVS_HI_IDLE_STATE_PE |
  349. VIVS_HI_IDLE_STATE_DE |
  350. VIVS_HI_IDLE_STATE_FE;
  351. } else {
  352. gpu->idle_mask = ~VIVS_HI_IDLE_STATE_AXI_LP;
  353. }
  354. etnaviv_hw_specs(gpu);
  355. }
  356. static void etnaviv_gpu_load_clock(struct etnaviv_gpu *gpu, u32 clock)
  357. {
  358. gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, clock |
  359. VIVS_HI_CLOCK_CONTROL_FSCALE_CMD_LOAD);
  360. gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, clock);
  361. }
  362. static void etnaviv_gpu_update_clock(struct etnaviv_gpu *gpu)
  363. {
  364. if (gpu->identity.minor_features2 &
  365. chipMinorFeatures2_DYNAMIC_FREQUENCY_SCALING) {
  366. clk_set_rate(gpu->clk_core,
  367. gpu->base_rate_core >> gpu->freq_scale);
  368. clk_set_rate(gpu->clk_shader,
  369. gpu->base_rate_shader >> gpu->freq_scale);
  370. } else {
  371. unsigned int fscale = 1 << (6 - gpu->freq_scale);
  372. u32 clock = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
  373. clock &= ~VIVS_HI_CLOCK_CONTROL_FSCALE_VAL__MASK;
  374. clock |= VIVS_HI_CLOCK_CONTROL_FSCALE_VAL(fscale);
  375. etnaviv_gpu_load_clock(gpu, clock);
  376. }
  377. }
  378. static int etnaviv_hw_reset(struct etnaviv_gpu *gpu)
  379. {
  380. u32 control, idle;
  381. unsigned long timeout;
  382. bool failed = true;
  383. /* We hope that the GPU resets in under one second */
  384. timeout = jiffies + msecs_to_jiffies(1000);
  385. while (time_is_after_jiffies(timeout)) {
  386. /* enable clock */
  387. unsigned int fscale = 1 << (6 - gpu->freq_scale);
  388. control = VIVS_HI_CLOCK_CONTROL_FSCALE_VAL(fscale);
  389. etnaviv_gpu_load_clock(gpu, control);
  390. /* isolate the GPU. */
  391. control |= VIVS_HI_CLOCK_CONTROL_ISOLATE_GPU;
  392. gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
  393. if (gpu->sec_mode == ETNA_SEC_KERNEL) {
  394. gpu_write(gpu, VIVS_MMUv2_AHB_CONTROL,
  395. VIVS_MMUv2_AHB_CONTROL_RESET);
  396. } else {
  397. /* set soft reset. */
  398. control |= VIVS_HI_CLOCK_CONTROL_SOFT_RESET;
  399. gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
  400. }
  401. /* wait for reset. */
  402. usleep_range(10, 20);
  403. /* reset soft reset bit. */
  404. control &= ~VIVS_HI_CLOCK_CONTROL_SOFT_RESET;
  405. gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
  406. /* reset GPU isolation. */
  407. control &= ~VIVS_HI_CLOCK_CONTROL_ISOLATE_GPU;
  408. gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
  409. /* read idle register. */
  410. idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
  411. /* try reseting again if FE it not idle */
  412. if ((idle & VIVS_HI_IDLE_STATE_FE) == 0) {
  413. dev_dbg(gpu->dev, "FE is not idle\n");
  414. continue;
  415. }
  416. /* read reset register. */
  417. control = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
  418. /* is the GPU idle? */
  419. if (((control & VIVS_HI_CLOCK_CONTROL_IDLE_3D) == 0) ||
  420. ((control & VIVS_HI_CLOCK_CONTROL_IDLE_2D) == 0)) {
  421. dev_dbg(gpu->dev, "GPU is not idle\n");
  422. continue;
  423. }
  424. /* disable debug registers, as they are not normally needed */
  425. control |= VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS;
  426. gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
  427. failed = false;
  428. break;
  429. }
  430. if (failed) {
  431. idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
  432. control = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
  433. dev_err(gpu->dev, "GPU failed to reset: FE %sidle, 3D %sidle, 2D %sidle\n",
  434. idle & VIVS_HI_IDLE_STATE_FE ? "" : "not ",
  435. control & VIVS_HI_CLOCK_CONTROL_IDLE_3D ? "" : "not ",
  436. control & VIVS_HI_CLOCK_CONTROL_IDLE_2D ? "" : "not ");
  437. return -EBUSY;
  438. }
  439. /* We rely on the GPU running, so program the clock */
  440. etnaviv_gpu_update_clock(gpu);
  441. return 0;
  442. }
  443. static void etnaviv_gpu_enable_mlcg(struct etnaviv_gpu *gpu)
  444. {
  445. u32 pmc, ppc;
  446. /* enable clock gating */
  447. ppc = gpu_read(gpu, VIVS_PM_POWER_CONTROLS);
  448. ppc |= VIVS_PM_POWER_CONTROLS_ENABLE_MODULE_CLOCK_GATING;
  449. /* Disable stall module clock gating for 4.3.0.1 and 4.3.0.2 revs */
  450. if (gpu->identity.revision == 0x4301 ||
  451. gpu->identity.revision == 0x4302)
  452. ppc |= VIVS_PM_POWER_CONTROLS_DISABLE_STALL_MODULE_CLOCK_GATING;
  453. gpu_write(gpu, VIVS_PM_POWER_CONTROLS, ppc);
  454. pmc = gpu_read(gpu, VIVS_PM_MODULE_CONTROLS);
  455. /* Disable PA clock gating for GC400+ without bugfix except for GC420 */
  456. if (gpu->identity.model >= chipModel_GC400 &&
  457. gpu->identity.model != chipModel_GC420 &&
  458. !(gpu->identity.minor_features3 & chipMinorFeatures3_BUG_FIXES12))
  459. pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_PA;
  460. /*
  461. * Disable PE clock gating on revs < 5.0.0.0 when HZ is
  462. * present without a bug fix.
  463. */
  464. if (gpu->identity.revision < 0x5000 &&
  465. gpu->identity.minor_features0 & chipMinorFeatures0_HZ &&
  466. !(gpu->identity.minor_features1 &
  467. chipMinorFeatures1_DISABLE_PE_GATING))
  468. pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_PE;
  469. if (gpu->identity.revision < 0x5422)
  470. pmc |= BIT(15); /* Unknown bit */
  471. /* Disable TX clock gating on affected core revisions. */
  472. if (etnaviv_is_model_rev(gpu, GC4000, 0x5222) ||
  473. etnaviv_is_model_rev(gpu, GC2000, 0x5108))
  474. pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_TX;
  475. pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_RA_HZ;
  476. pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_RA_EZ;
  477. gpu_write(gpu, VIVS_PM_MODULE_CONTROLS, pmc);
  478. }
  479. void etnaviv_gpu_start_fe(struct etnaviv_gpu *gpu, u32 address, u16 prefetch)
  480. {
  481. gpu_write(gpu, VIVS_FE_COMMAND_ADDRESS, address);
  482. gpu_write(gpu, VIVS_FE_COMMAND_CONTROL,
  483. VIVS_FE_COMMAND_CONTROL_ENABLE |
  484. VIVS_FE_COMMAND_CONTROL_PREFETCH(prefetch));
  485. if (gpu->sec_mode == ETNA_SEC_KERNEL) {
  486. gpu_write(gpu, VIVS_MMUv2_SEC_COMMAND_CONTROL,
  487. VIVS_MMUv2_SEC_COMMAND_CONTROL_ENABLE |
  488. VIVS_MMUv2_SEC_COMMAND_CONTROL_PREFETCH(prefetch));
  489. }
  490. }
  491. static void etnaviv_gpu_setup_pulse_eater(struct etnaviv_gpu *gpu)
  492. {
  493. /*
  494. * Base value for VIVS_PM_PULSE_EATER register on models where it
  495. * cannot be read, extracted from vivante kernel driver.
  496. */
  497. u32 pulse_eater = 0x01590880;
  498. if (etnaviv_is_model_rev(gpu, GC4000, 0x5208) ||
  499. etnaviv_is_model_rev(gpu, GC4000, 0x5222)) {
  500. pulse_eater |= BIT(23);
  501. }
  502. if (etnaviv_is_model_rev(gpu, GC1000, 0x5039) ||
  503. etnaviv_is_model_rev(gpu, GC1000, 0x5040)) {
  504. pulse_eater &= ~BIT(16);
  505. pulse_eater |= BIT(17);
  506. }
  507. if ((gpu->identity.revision > 0x5420) &&
  508. (gpu->identity.features & chipFeatures_PIPE_3D))
  509. {
  510. /* Performance fix: disable internal DFS */
  511. pulse_eater = gpu_read(gpu, VIVS_PM_PULSE_EATER);
  512. pulse_eater |= BIT(18);
  513. }
  514. gpu_write(gpu, VIVS_PM_PULSE_EATER, pulse_eater);
  515. }
  516. static void etnaviv_gpu_hw_init(struct etnaviv_gpu *gpu)
  517. {
  518. u16 prefetch;
  519. if ((etnaviv_is_model_rev(gpu, GC320, 0x5007) ||
  520. etnaviv_is_model_rev(gpu, GC320, 0x5220)) &&
  521. gpu_read(gpu, VIVS_HI_CHIP_TIME) != 0x2062400) {
  522. u32 mc_memory_debug;
  523. mc_memory_debug = gpu_read(gpu, VIVS_MC_DEBUG_MEMORY) & ~0xff;
  524. if (gpu->identity.revision == 0x5007)
  525. mc_memory_debug |= 0x0c;
  526. else
  527. mc_memory_debug |= 0x08;
  528. gpu_write(gpu, VIVS_MC_DEBUG_MEMORY, mc_memory_debug);
  529. }
  530. /* enable module-level clock gating */
  531. etnaviv_gpu_enable_mlcg(gpu);
  532. /*
  533. * Update GPU AXI cache atttribute to "cacheable, no allocate".
  534. * This is necessary to prevent the iMX6 SoC locking up.
  535. */
  536. gpu_write(gpu, VIVS_HI_AXI_CONFIG,
  537. VIVS_HI_AXI_CONFIG_AWCACHE(2) |
  538. VIVS_HI_AXI_CONFIG_ARCACHE(2));
  539. /* GC2000 rev 5108 needs a special bus config */
  540. if (etnaviv_is_model_rev(gpu, GC2000, 0x5108)) {
  541. u32 bus_config = gpu_read(gpu, VIVS_MC_BUS_CONFIG);
  542. bus_config &= ~(VIVS_MC_BUS_CONFIG_FE_BUS_CONFIG__MASK |
  543. VIVS_MC_BUS_CONFIG_TX_BUS_CONFIG__MASK);
  544. bus_config |= VIVS_MC_BUS_CONFIG_FE_BUS_CONFIG(1) |
  545. VIVS_MC_BUS_CONFIG_TX_BUS_CONFIG(0);
  546. gpu_write(gpu, VIVS_MC_BUS_CONFIG, bus_config);
  547. }
  548. if (gpu->sec_mode == ETNA_SEC_KERNEL) {
  549. u32 val = gpu_read(gpu, VIVS_MMUv2_AHB_CONTROL);
  550. val |= VIVS_MMUv2_AHB_CONTROL_NONSEC_ACCESS;
  551. gpu_write(gpu, VIVS_MMUv2_AHB_CONTROL, val);
  552. }
  553. /* setup the pulse eater */
  554. etnaviv_gpu_setup_pulse_eater(gpu);
  555. /* setup the MMU */
  556. etnaviv_iommu_restore(gpu);
  557. /* Start command processor */
  558. prefetch = etnaviv_buffer_init(gpu);
  559. gpu_write(gpu, VIVS_HI_INTR_ENBL, ~0U);
  560. etnaviv_gpu_start_fe(gpu, etnaviv_cmdbuf_get_va(&gpu->buffer),
  561. prefetch);
  562. }
  563. int etnaviv_gpu_init(struct etnaviv_gpu *gpu)
  564. {
  565. int ret, i;
  566. ret = pm_runtime_get_sync(gpu->dev);
  567. if (ret < 0) {
  568. dev_err(gpu->dev, "Failed to enable GPU power domain\n");
  569. goto pm_put;
  570. }
  571. etnaviv_hw_identify(gpu);
  572. if (gpu->identity.model == 0) {
  573. dev_err(gpu->dev, "Unknown GPU model\n");
  574. ret = -ENXIO;
  575. goto fail;
  576. }
  577. /* Exclude VG cores with FE2.0 */
  578. if (gpu->identity.features & chipFeatures_PIPE_VG &&
  579. gpu->identity.features & chipFeatures_FE20) {
  580. dev_info(gpu->dev, "Ignoring GPU with VG and FE2.0\n");
  581. ret = -ENXIO;
  582. goto fail;
  583. }
  584. /*
  585. * Set the GPU linear window to be at the end of the DMA window, where
  586. * the CMA area is likely to reside. This ensures that we are able to
  587. * map the command buffers while having the linear window overlap as
  588. * much RAM as possible, so we can optimize mappings for other buffers.
  589. *
  590. * For 3D cores only do this if MC2.0 is present, as with MC1.0 it leads
  591. * to different views of the memory on the individual engines.
  592. */
  593. if (!(gpu->identity.features & chipFeatures_PIPE_3D) ||
  594. (gpu->identity.minor_features0 & chipMinorFeatures0_MC20)) {
  595. u32 dma_mask = (u32)dma_get_required_mask(gpu->dev);
  596. if (dma_mask < PHYS_OFFSET + SZ_2G)
  597. gpu->memory_base = PHYS_OFFSET;
  598. else
  599. gpu->memory_base = dma_mask - SZ_2G + 1;
  600. } else if (PHYS_OFFSET >= SZ_2G) {
  601. dev_info(gpu->dev, "Need to move linear window on MC1.0, disabling TS\n");
  602. gpu->memory_base = PHYS_OFFSET;
  603. gpu->identity.features &= ~chipFeatures_FAST_CLEAR;
  604. }
  605. /*
  606. * On cores with security features supported, we claim control over the
  607. * security states.
  608. */
  609. if ((gpu->identity.minor_features7 & chipMinorFeatures7_BIT_SECURITY) &&
  610. (gpu->identity.minor_features10 & chipMinorFeatures10_SECURITY_AHB))
  611. gpu->sec_mode = ETNA_SEC_KERNEL;
  612. ret = etnaviv_hw_reset(gpu);
  613. if (ret) {
  614. dev_err(gpu->dev, "GPU reset failed\n");
  615. goto fail;
  616. }
  617. gpu->mmu = etnaviv_iommu_new(gpu);
  618. if (IS_ERR(gpu->mmu)) {
  619. dev_err(gpu->dev, "Failed to instantiate GPU IOMMU\n");
  620. ret = PTR_ERR(gpu->mmu);
  621. goto fail;
  622. }
  623. gpu->cmdbuf_suballoc = etnaviv_cmdbuf_suballoc_new(gpu);
  624. if (IS_ERR(gpu->cmdbuf_suballoc)) {
  625. dev_err(gpu->dev, "Failed to create cmdbuf suballocator\n");
  626. ret = PTR_ERR(gpu->cmdbuf_suballoc);
  627. goto destroy_iommu;
  628. }
  629. /* Create buffer: */
  630. ret = etnaviv_cmdbuf_init(gpu->cmdbuf_suballoc, &gpu->buffer,
  631. PAGE_SIZE);
  632. if (ret) {
  633. dev_err(gpu->dev, "could not create command buffer\n");
  634. goto destroy_suballoc;
  635. }
  636. if (gpu->mmu->version == ETNAVIV_IOMMU_V1 &&
  637. etnaviv_cmdbuf_get_va(&gpu->buffer) > 0x80000000) {
  638. ret = -EINVAL;
  639. dev_err(gpu->dev,
  640. "command buffer outside valid memory window\n");
  641. goto free_buffer;
  642. }
  643. /* Setup event management */
  644. spin_lock_init(&gpu->event_spinlock);
  645. init_completion(&gpu->event_free);
  646. bitmap_zero(gpu->event_bitmap, ETNA_NR_EVENTS);
  647. for (i = 0; i < ARRAY_SIZE(gpu->event); i++)
  648. complete(&gpu->event_free);
  649. /* Now program the hardware */
  650. mutex_lock(&gpu->lock);
  651. etnaviv_gpu_hw_init(gpu);
  652. gpu->exec_state = -1;
  653. mutex_unlock(&gpu->lock);
  654. pm_runtime_mark_last_busy(gpu->dev);
  655. pm_runtime_put_autosuspend(gpu->dev);
  656. return 0;
  657. free_buffer:
  658. etnaviv_cmdbuf_free(&gpu->buffer);
  659. gpu->buffer.suballoc = NULL;
  660. destroy_suballoc:
  661. etnaviv_cmdbuf_suballoc_destroy(gpu->cmdbuf_suballoc);
  662. gpu->cmdbuf_suballoc = NULL;
  663. destroy_iommu:
  664. etnaviv_iommu_destroy(gpu->mmu);
  665. gpu->mmu = NULL;
  666. fail:
  667. pm_runtime_mark_last_busy(gpu->dev);
  668. pm_put:
  669. pm_runtime_put_autosuspend(gpu->dev);
  670. return ret;
  671. }
  672. #ifdef CONFIG_DEBUG_FS
  673. struct dma_debug {
  674. u32 address[2];
  675. u32 state[2];
  676. };
  677. static void verify_dma(struct etnaviv_gpu *gpu, struct dma_debug *debug)
  678. {
  679. u32 i;
  680. debug->address[0] = gpu_read(gpu, VIVS_FE_DMA_ADDRESS);
  681. debug->state[0] = gpu_read(gpu, VIVS_FE_DMA_DEBUG_STATE);
  682. for (i = 0; i < 500; i++) {
  683. debug->address[1] = gpu_read(gpu, VIVS_FE_DMA_ADDRESS);
  684. debug->state[1] = gpu_read(gpu, VIVS_FE_DMA_DEBUG_STATE);
  685. if (debug->address[0] != debug->address[1])
  686. break;
  687. if (debug->state[0] != debug->state[1])
  688. break;
  689. }
  690. }
  691. int etnaviv_gpu_debugfs(struct etnaviv_gpu *gpu, struct seq_file *m)
  692. {
  693. struct dma_debug debug;
  694. u32 dma_lo, dma_hi, axi, idle;
  695. int ret;
  696. seq_printf(m, "%s Status:\n", dev_name(gpu->dev));
  697. ret = pm_runtime_get_sync(gpu->dev);
  698. if (ret < 0)
  699. goto pm_put;
  700. dma_lo = gpu_read(gpu, VIVS_FE_DMA_LOW);
  701. dma_hi = gpu_read(gpu, VIVS_FE_DMA_HIGH);
  702. axi = gpu_read(gpu, VIVS_HI_AXI_STATUS);
  703. idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
  704. verify_dma(gpu, &debug);
  705. seq_puts(m, "\tfeatures\n");
  706. seq_printf(m, "\t major_features: 0x%08x\n",
  707. gpu->identity.features);
  708. seq_printf(m, "\t minor_features0: 0x%08x\n",
  709. gpu->identity.minor_features0);
  710. seq_printf(m, "\t minor_features1: 0x%08x\n",
  711. gpu->identity.minor_features1);
  712. seq_printf(m, "\t minor_features2: 0x%08x\n",
  713. gpu->identity.minor_features2);
  714. seq_printf(m, "\t minor_features3: 0x%08x\n",
  715. gpu->identity.minor_features3);
  716. seq_printf(m, "\t minor_features4: 0x%08x\n",
  717. gpu->identity.minor_features4);
  718. seq_printf(m, "\t minor_features5: 0x%08x\n",
  719. gpu->identity.minor_features5);
  720. seq_printf(m, "\t minor_features6: 0x%08x\n",
  721. gpu->identity.minor_features6);
  722. seq_printf(m, "\t minor_features7: 0x%08x\n",
  723. gpu->identity.minor_features7);
  724. seq_printf(m, "\t minor_features8: 0x%08x\n",
  725. gpu->identity.minor_features8);
  726. seq_printf(m, "\t minor_features9: 0x%08x\n",
  727. gpu->identity.minor_features9);
  728. seq_printf(m, "\t minor_features10: 0x%08x\n",
  729. gpu->identity.minor_features10);
  730. seq_printf(m, "\t minor_features11: 0x%08x\n",
  731. gpu->identity.minor_features11);
  732. seq_puts(m, "\tspecs\n");
  733. seq_printf(m, "\t stream_count: %d\n",
  734. gpu->identity.stream_count);
  735. seq_printf(m, "\t register_max: %d\n",
  736. gpu->identity.register_max);
  737. seq_printf(m, "\t thread_count: %d\n",
  738. gpu->identity.thread_count);
  739. seq_printf(m, "\t vertex_cache_size: %d\n",
  740. gpu->identity.vertex_cache_size);
  741. seq_printf(m, "\t shader_core_count: %d\n",
  742. gpu->identity.shader_core_count);
  743. seq_printf(m, "\t pixel_pipes: %d\n",
  744. gpu->identity.pixel_pipes);
  745. seq_printf(m, "\t vertex_output_buffer_size: %d\n",
  746. gpu->identity.vertex_output_buffer_size);
  747. seq_printf(m, "\t buffer_size: %d\n",
  748. gpu->identity.buffer_size);
  749. seq_printf(m, "\t instruction_count: %d\n",
  750. gpu->identity.instruction_count);
  751. seq_printf(m, "\t num_constants: %d\n",
  752. gpu->identity.num_constants);
  753. seq_printf(m, "\t varyings_count: %d\n",
  754. gpu->identity.varyings_count);
  755. seq_printf(m, "\taxi: 0x%08x\n", axi);
  756. seq_printf(m, "\tidle: 0x%08x\n", idle);
  757. idle |= ~gpu->idle_mask & ~VIVS_HI_IDLE_STATE_AXI_LP;
  758. if ((idle & VIVS_HI_IDLE_STATE_FE) == 0)
  759. seq_puts(m, "\t FE is not idle\n");
  760. if ((idle & VIVS_HI_IDLE_STATE_DE) == 0)
  761. seq_puts(m, "\t DE is not idle\n");
  762. if ((idle & VIVS_HI_IDLE_STATE_PE) == 0)
  763. seq_puts(m, "\t PE is not idle\n");
  764. if ((idle & VIVS_HI_IDLE_STATE_SH) == 0)
  765. seq_puts(m, "\t SH is not idle\n");
  766. if ((idle & VIVS_HI_IDLE_STATE_PA) == 0)
  767. seq_puts(m, "\t PA is not idle\n");
  768. if ((idle & VIVS_HI_IDLE_STATE_SE) == 0)
  769. seq_puts(m, "\t SE is not idle\n");
  770. if ((idle & VIVS_HI_IDLE_STATE_RA) == 0)
  771. seq_puts(m, "\t RA is not idle\n");
  772. if ((idle & VIVS_HI_IDLE_STATE_TX) == 0)
  773. seq_puts(m, "\t TX is not idle\n");
  774. if ((idle & VIVS_HI_IDLE_STATE_VG) == 0)
  775. seq_puts(m, "\t VG is not idle\n");
  776. if ((idle & VIVS_HI_IDLE_STATE_IM) == 0)
  777. seq_puts(m, "\t IM is not idle\n");
  778. if ((idle & VIVS_HI_IDLE_STATE_FP) == 0)
  779. seq_puts(m, "\t FP is not idle\n");
  780. if ((idle & VIVS_HI_IDLE_STATE_TS) == 0)
  781. seq_puts(m, "\t TS is not idle\n");
  782. if (idle & VIVS_HI_IDLE_STATE_AXI_LP)
  783. seq_puts(m, "\t AXI low power mode\n");
  784. if (gpu->identity.features & chipFeatures_DEBUG_MODE) {
  785. u32 read0 = gpu_read(gpu, VIVS_MC_DEBUG_READ0);
  786. u32 read1 = gpu_read(gpu, VIVS_MC_DEBUG_READ1);
  787. u32 write = gpu_read(gpu, VIVS_MC_DEBUG_WRITE);
  788. seq_puts(m, "\tMC\n");
  789. seq_printf(m, "\t read0: 0x%08x\n", read0);
  790. seq_printf(m, "\t read1: 0x%08x\n", read1);
  791. seq_printf(m, "\t write: 0x%08x\n", write);
  792. }
  793. seq_puts(m, "\tDMA ");
  794. if (debug.address[0] == debug.address[1] &&
  795. debug.state[0] == debug.state[1]) {
  796. seq_puts(m, "seems to be stuck\n");
  797. } else if (debug.address[0] == debug.address[1]) {
  798. seq_puts(m, "address is constant\n");
  799. } else {
  800. seq_puts(m, "is running\n");
  801. }
  802. seq_printf(m, "\t address 0: 0x%08x\n", debug.address[0]);
  803. seq_printf(m, "\t address 1: 0x%08x\n", debug.address[1]);
  804. seq_printf(m, "\t state 0: 0x%08x\n", debug.state[0]);
  805. seq_printf(m, "\t state 1: 0x%08x\n", debug.state[1]);
  806. seq_printf(m, "\t last fetch 64 bit word: 0x%08x 0x%08x\n",
  807. dma_lo, dma_hi);
  808. ret = 0;
  809. pm_runtime_mark_last_busy(gpu->dev);
  810. pm_put:
  811. pm_runtime_put_autosuspend(gpu->dev);
  812. return ret;
  813. }
  814. #endif
  815. void etnaviv_gpu_recover_hang(struct etnaviv_gpu *gpu)
  816. {
  817. unsigned long flags;
  818. unsigned int i = 0;
  819. dev_err(gpu->dev, "recover hung GPU!\n");
  820. if (pm_runtime_get_sync(gpu->dev) < 0)
  821. goto pm_put;
  822. mutex_lock(&gpu->lock);
  823. etnaviv_hw_reset(gpu);
  824. /* complete all events, the GPU won't do it after the reset */
  825. spin_lock_irqsave(&gpu->event_spinlock, flags);
  826. for_each_set_bit_from(i, gpu->event_bitmap, ETNA_NR_EVENTS)
  827. complete(&gpu->event_free);
  828. bitmap_zero(gpu->event_bitmap, ETNA_NR_EVENTS);
  829. spin_unlock_irqrestore(&gpu->event_spinlock, flags);
  830. gpu->completed_fence = gpu->active_fence;
  831. etnaviv_gpu_hw_init(gpu);
  832. gpu->lastctx = NULL;
  833. gpu->exec_state = -1;
  834. mutex_unlock(&gpu->lock);
  835. pm_runtime_mark_last_busy(gpu->dev);
  836. pm_put:
  837. pm_runtime_put_autosuspend(gpu->dev);
  838. }
  839. /* fence object management */
  840. struct etnaviv_fence {
  841. struct etnaviv_gpu *gpu;
  842. struct dma_fence base;
  843. };
  844. static inline struct etnaviv_fence *to_etnaviv_fence(struct dma_fence *fence)
  845. {
  846. return container_of(fence, struct etnaviv_fence, base);
  847. }
  848. static const char *etnaviv_fence_get_driver_name(struct dma_fence *fence)
  849. {
  850. return "etnaviv";
  851. }
  852. static const char *etnaviv_fence_get_timeline_name(struct dma_fence *fence)
  853. {
  854. struct etnaviv_fence *f = to_etnaviv_fence(fence);
  855. return dev_name(f->gpu->dev);
  856. }
  857. static bool etnaviv_fence_signaled(struct dma_fence *fence)
  858. {
  859. struct etnaviv_fence *f = to_etnaviv_fence(fence);
  860. return fence_completed(f->gpu, f->base.seqno);
  861. }
  862. static void etnaviv_fence_release(struct dma_fence *fence)
  863. {
  864. struct etnaviv_fence *f = to_etnaviv_fence(fence);
  865. kfree_rcu(f, base.rcu);
  866. }
  867. static const struct dma_fence_ops etnaviv_fence_ops = {
  868. .get_driver_name = etnaviv_fence_get_driver_name,
  869. .get_timeline_name = etnaviv_fence_get_timeline_name,
  870. .signaled = etnaviv_fence_signaled,
  871. .release = etnaviv_fence_release,
  872. };
  873. static struct dma_fence *etnaviv_gpu_fence_alloc(struct etnaviv_gpu *gpu)
  874. {
  875. struct etnaviv_fence *f;
  876. /*
  877. * GPU lock must already be held, otherwise fence completion order might
  878. * not match the seqno order assigned here.
  879. */
  880. lockdep_assert_held(&gpu->lock);
  881. f = kzalloc(sizeof(*f), GFP_KERNEL);
  882. if (!f)
  883. return NULL;
  884. f->gpu = gpu;
  885. dma_fence_init(&f->base, &etnaviv_fence_ops, &gpu->fence_spinlock,
  886. gpu->fence_context, ++gpu->next_fence);
  887. return &f->base;
  888. }
  889. /*
  890. * event management:
  891. */
  892. static int event_alloc(struct etnaviv_gpu *gpu, unsigned nr_events,
  893. unsigned int *events)
  894. {
  895. unsigned long flags, timeout = msecs_to_jiffies(10 * 10000);
  896. unsigned i, acquired = 0;
  897. for (i = 0; i < nr_events; i++) {
  898. unsigned long ret;
  899. ret = wait_for_completion_timeout(&gpu->event_free, timeout);
  900. if (!ret) {
  901. dev_err(gpu->dev, "wait_for_completion_timeout failed");
  902. goto out;
  903. }
  904. acquired++;
  905. timeout = ret;
  906. }
  907. spin_lock_irqsave(&gpu->event_spinlock, flags);
  908. for (i = 0; i < nr_events; i++) {
  909. int event = find_first_zero_bit(gpu->event_bitmap, ETNA_NR_EVENTS);
  910. events[i] = event;
  911. memset(&gpu->event[event], 0, sizeof(struct etnaviv_event));
  912. set_bit(event, gpu->event_bitmap);
  913. }
  914. spin_unlock_irqrestore(&gpu->event_spinlock, flags);
  915. return 0;
  916. out:
  917. for (i = 0; i < acquired; i++)
  918. complete(&gpu->event_free);
  919. return -EBUSY;
  920. }
  921. static void event_free(struct etnaviv_gpu *gpu, unsigned int event)
  922. {
  923. unsigned long flags;
  924. spin_lock_irqsave(&gpu->event_spinlock, flags);
  925. if (!test_bit(event, gpu->event_bitmap)) {
  926. dev_warn(gpu->dev, "event %u is already marked as free",
  927. event);
  928. spin_unlock_irqrestore(&gpu->event_spinlock, flags);
  929. } else {
  930. clear_bit(event, gpu->event_bitmap);
  931. spin_unlock_irqrestore(&gpu->event_spinlock, flags);
  932. complete(&gpu->event_free);
  933. }
  934. }
  935. /*
  936. * Cmdstream submission/retirement:
  937. */
  938. int etnaviv_gpu_wait_fence_interruptible(struct etnaviv_gpu *gpu,
  939. u32 id, struct timespec *timeout)
  940. {
  941. struct dma_fence *fence;
  942. int ret;
  943. /*
  944. * Look up the fence and take a reference. We might still find a fence
  945. * whose refcount has already dropped to zero. dma_fence_get_rcu
  946. * pretends we didn't find a fence in that case.
  947. */
  948. rcu_read_lock();
  949. fence = idr_find(&gpu->fence_idr, id);
  950. if (fence)
  951. fence = dma_fence_get_rcu(fence);
  952. rcu_read_unlock();
  953. if (!fence)
  954. return 0;
  955. if (!timeout) {
  956. /* No timeout was requested: just test for completion */
  957. ret = dma_fence_is_signaled(fence) ? 0 : -EBUSY;
  958. } else {
  959. unsigned long remaining = etnaviv_timeout_to_jiffies(timeout);
  960. ret = dma_fence_wait_timeout(fence, true, remaining);
  961. if (ret == 0)
  962. ret = -ETIMEDOUT;
  963. else if (ret != -ERESTARTSYS)
  964. ret = 0;
  965. }
  966. dma_fence_put(fence);
  967. return ret;
  968. }
  969. /*
  970. * Wait for an object to become inactive. This, on it's own, is not race
  971. * free: the object is moved by the scheduler off the active list, and
  972. * then the iova is put. Moreover, the object could be re-submitted just
  973. * after we notice that it's become inactive.
  974. *
  975. * Although the retirement happens under the gpu lock, we don't want to hold
  976. * that lock in this function while waiting.
  977. */
  978. int etnaviv_gpu_wait_obj_inactive(struct etnaviv_gpu *gpu,
  979. struct etnaviv_gem_object *etnaviv_obj, struct timespec *timeout)
  980. {
  981. unsigned long remaining;
  982. long ret;
  983. if (!timeout)
  984. return !is_active(etnaviv_obj) ? 0 : -EBUSY;
  985. remaining = etnaviv_timeout_to_jiffies(timeout);
  986. ret = wait_event_interruptible_timeout(gpu->fence_event,
  987. !is_active(etnaviv_obj),
  988. remaining);
  989. if (ret > 0)
  990. return 0;
  991. else if (ret == -ERESTARTSYS)
  992. return -ERESTARTSYS;
  993. else
  994. return -ETIMEDOUT;
  995. }
  996. static void sync_point_perfmon_sample(struct etnaviv_gpu *gpu,
  997. struct etnaviv_event *event, unsigned int flags)
  998. {
  999. const struct etnaviv_gem_submit *submit = event->submit;
  1000. unsigned int i;
  1001. for (i = 0; i < submit->nr_pmrs; i++) {
  1002. const struct etnaviv_perfmon_request *pmr = submit->pmrs + i;
  1003. if (pmr->flags == flags)
  1004. etnaviv_perfmon_process(gpu, pmr, submit->exec_state);
  1005. }
  1006. }
  1007. static void sync_point_perfmon_sample_pre(struct etnaviv_gpu *gpu,
  1008. struct etnaviv_event *event)
  1009. {
  1010. u32 val;
  1011. /* disable clock gating */
  1012. val = gpu_read(gpu, VIVS_PM_POWER_CONTROLS);
  1013. val &= ~VIVS_PM_POWER_CONTROLS_ENABLE_MODULE_CLOCK_GATING;
  1014. gpu_write(gpu, VIVS_PM_POWER_CONTROLS, val);
  1015. /* enable debug register */
  1016. val = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
  1017. val &= ~VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS;
  1018. gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, val);
  1019. sync_point_perfmon_sample(gpu, event, ETNA_PM_PROCESS_PRE);
  1020. }
  1021. static void sync_point_perfmon_sample_post(struct etnaviv_gpu *gpu,
  1022. struct etnaviv_event *event)
  1023. {
  1024. const struct etnaviv_gem_submit *submit = event->submit;
  1025. unsigned int i;
  1026. u32 val;
  1027. sync_point_perfmon_sample(gpu, event, ETNA_PM_PROCESS_POST);
  1028. for (i = 0; i < submit->nr_pmrs; i++) {
  1029. const struct etnaviv_perfmon_request *pmr = submit->pmrs + i;
  1030. *pmr->bo_vma = pmr->sequence;
  1031. }
  1032. /* disable debug register */
  1033. val = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
  1034. val |= VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS;
  1035. gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, val);
  1036. /* enable clock gating */
  1037. val = gpu_read(gpu, VIVS_PM_POWER_CONTROLS);
  1038. val |= VIVS_PM_POWER_CONTROLS_ENABLE_MODULE_CLOCK_GATING;
  1039. gpu_write(gpu, VIVS_PM_POWER_CONTROLS, val);
  1040. }
  1041. /* add bo's to gpu's ring, and kick gpu: */
  1042. struct dma_fence *etnaviv_gpu_submit(struct etnaviv_gem_submit *submit)
  1043. {
  1044. struct etnaviv_gpu *gpu = submit->gpu;
  1045. struct dma_fence *gpu_fence;
  1046. unsigned int i, nr_events = 1, event[3];
  1047. int ret;
  1048. if (!submit->runtime_resumed) {
  1049. ret = pm_runtime_get_sync(gpu->dev);
  1050. if (ret < 0) {
  1051. pm_runtime_put_noidle(gpu->dev);
  1052. return NULL;
  1053. }
  1054. submit->runtime_resumed = true;
  1055. }
  1056. /*
  1057. * if there are performance monitor requests we need to have
  1058. * - a sync point to re-configure gpu and process ETNA_PM_PROCESS_PRE
  1059. * requests.
  1060. * - a sync point to re-configure gpu, process ETNA_PM_PROCESS_POST requests
  1061. * and update the sequence number for userspace.
  1062. */
  1063. if (submit->nr_pmrs)
  1064. nr_events = 3;
  1065. ret = event_alloc(gpu, nr_events, event);
  1066. if (ret) {
  1067. DRM_ERROR("no free events\n");
  1068. pm_runtime_put_noidle(gpu->dev);
  1069. return NULL;
  1070. }
  1071. mutex_lock(&gpu->lock);
  1072. gpu_fence = etnaviv_gpu_fence_alloc(gpu);
  1073. if (!gpu_fence) {
  1074. for (i = 0; i < nr_events; i++)
  1075. event_free(gpu, event[i]);
  1076. goto out_unlock;
  1077. }
  1078. gpu->active_fence = gpu_fence->seqno;
  1079. if (submit->nr_pmrs) {
  1080. gpu->event[event[1]].sync_point = &sync_point_perfmon_sample_pre;
  1081. kref_get(&submit->refcount);
  1082. gpu->event[event[1]].submit = submit;
  1083. etnaviv_sync_point_queue(gpu, event[1]);
  1084. }
  1085. gpu->event[event[0]].fence = gpu_fence;
  1086. submit->cmdbuf.user_size = submit->cmdbuf.size - 8;
  1087. etnaviv_buffer_queue(gpu, submit->exec_state, event[0],
  1088. &submit->cmdbuf);
  1089. if (submit->nr_pmrs) {
  1090. gpu->event[event[2]].sync_point = &sync_point_perfmon_sample_post;
  1091. kref_get(&submit->refcount);
  1092. gpu->event[event[2]].submit = submit;
  1093. etnaviv_sync_point_queue(gpu, event[2]);
  1094. }
  1095. out_unlock:
  1096. mutex_unlock(&gpu->lock);
  1097. return gpu_fence;
  1098. }
  1099. static void sync_point_worker(struct work_struct *work)
  1100. {
  1101. struct etnaviv_gpu *gpu = container_of(work, struct etnaviv_gpu,
  1102. sync_point_work);
  1103. struct etnaviv_event *event = &gpu->event[gpu->sync_point_event];
  1104. u32 addr = gpu_read(gpu, VIVS_FE_DMA_ADDRESS);
  1105. event->sync_point(gpu, event);
  1106. etnaviv_submit_put(event->submit);
  1107. event_free(gpu, gpu->sync_point_event);
  1108. /* restart FE last to avoid GPU and IRQ racing against this worker */
  1109. etnaviv_gpu_start_fe(gpu, addr + 2, 2);
  1110. }
  1111. static void dump_mmu_fault(struct etnaviv_gpu *gpu)
  1112. {
  1113. u32 status_reg, status;
  1114. int i;
  1115. if (gpu->sec_mode == ETNA_SEC_NONE)
  1116. status_reg = VIVS_MMUv2_STATUS;
  1117. else
  1118. status_reg = VIVS_MMUv2_SEC_STATUS;
  1119. status = gpu_read(gpu, status_reg);
  1120. dev_err_ratelimited(gpu->dev, "MMU fault status 0x%08x\n", status);
  1121. for (i = 0; i < 4; i++) {
  1122. u32 address_reg;
  1123. if (!(status & (VIVS_MMUv2_STATUS_EXCEPTION0__MASK << (i * 4))))
  1124. continue;
  1125. if (gpu->sec_mode == ETNA_SEC_NONE)
  1126. address_reg = VIVS_MMUv2_EXCEPTION_ADDR(i);
  1127. else
  1128. address_reg = VIVS_MMUv2_SEC_EXCEPTION_ADDR;
  1129. dev_err_ratelimited(gpu->dev, "MMU %d fault addr 0x%08x\n", i,
  1130. gpu_read(gpu, address_reg));
  1131. }
  1132. }
  1133. static irqreturn_t irq_handler(int irq, void *data)
  1134. {
  1135. struct etnaviv_gpu *gpu = data;
  1136. irqreturn_t ret = IRQ_NONE;
  1137. u32 intr = gpu_read(gpu, VIVS_HI_INTR_ACKNOWLEDGE);
  1138. if (intr != 0) {
  1139. int event;
  1140. pm_runtime_mark_last_busy(gpu->dev);
  1141. dev_dbg(gpu->dev, "intr 0x%08x\n", intr);
  1142. if (intr & VIVS_HI_INTR_ACKNOWLEDGE_AXI_BUS_ERROR) {
  1143. dev_err(gpu->dev, "AXI bus error\n");
  1144. intr &= ~VIVS_HI_INTR_ACKNOWLEDGE_AXI_BUS_ERROR;
  1145. }
  1146. if (intr & VIVS_HI_INTR_ACKNOWLEDGE_MMU_EXCEPTION) {
  1147. dump_mmu_fault(gpu);
  1148. intr &= ~VIVS_HI_INTR_ACKNOWLEDGE_MMU_EXCEPTION;
  1149. }
  1150. while ((event = ffs(intr)) != 0) {
  1151. struct dma_fence *fence;
  1152. event -= 1;
  1153. intr &= ~(1 << event);
  1154. dev_dbg(gpu->dev, "event %u\n", event);
  1155. if (gpu->event[event].sync_point) {
  1156. gpu->sync_point_event = event;
  1157. queue_work(gpu->wq, &gpu->sync_point_work);
  1158. }
  1159. fence = gpu->event[event].fence;
  1160. if (!fence)
  1161. continue;
  1162. gpu->event[event].fence = NULL;
  1163. /*
  1164. * Events can be processed out of order. Eg,
  1165. * - allocate and queue event 0
  1166. * - allocate event 1
  1167. * - event 0 completes, we process it
  1168. * - allocate and queue event 0
  1169. * - event 1 and event 0 complete
  1170. * we can end up processing event 0 first, then 1.
  1171. */
  1172. if (fence_after(fence->seqno, gpu->completed_fence))
  1173. gpu->completed_fence = fence->seqno;
  1174. dma_fence_signal(fence);
  1175. event_free(gpu, event);
  1176. }
  1177. ret = IRQ_HANDLED;
  1178. }
  1179. return ret;
  1180. }
  1181. static int etnaviv_gpu_clk_enable(struct etnaviv_gpu *gpu)
  1182. {
  1183. int ret;
  1184. if (gpu->clk_reg) {
  1185. ret = clk_prepare_enable(gpu->clk_reg);
  1186. if (ret)
  1187. return ret;
  1188. }
  1189. if (gpu->clk_bus) {
  1190. ret = clk_prepare_enable(gpu->clk_bus);
  1191. if (ret)
  1192. goto disable_clk_reg;
  1193. }
  1194. if (gpu->clk_core) {
  1195. ret = clk_prepare_enable(gpu->clk_core);
  1196. if (ret)
  1197. goto disable_clk_bus;
  1198. }
  1199. if (gpu->clk_shader) {
  1200. ret = clk_prepare_enable(gpu->clk_shader);
  1201. if (ret)
  1202. goto disable_clk_core;
  1203. }
  1204. return 0;
  1205. disable_clk_core:
  1206. if (gpu->clk_core)
  1207. clk_disable_unprepare(gpu->clk_core);
  1208. disable_clk_bus:
  1209. if (gpu->clk_bus)
  1210. clk_disable_unprepare(gpu->clk_bus);
  1211. disable_clk_reg:
  1212. if (gpu->clk_reg)
  1213. clk_disable_unprepare(gpu->clk_reg);
  1214. return ret;
  1215. }
  1216. static int etnaviv_gpu_clk_disable(struct etnaviv_gpu *gpu)
  1217. {
  1218. if (gpu->clk_shader)
  1219. clk_disable_unprepare(gpu->clk_shader);
  1220. if (gpu->clk_core)
  1221. clk_disable_unprepare(gpu->clk_core);
  1222. if (gpu->clk_bus)
  1223. clk_disable_unprepare(gpu->clk_bus);
  1224. if (gpu->clk_reg)
  1225. clk_disable_unprepare(gpu->clk_reg);
  1226. return 0;
  1227. }
  1228. int etnaviv_gpu_wait_idle(struct etnaviv_gpu *gpu, unsigned int timeout_ms)
  1229. {
  1230. unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
  1231. do {
  1232. u32 idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
  1233. if ((idle & gpu->idle_mask) == gpu->idle_mask)
  1234. return 0;
  1235. if (time_is_before_jiffies(timeout)) {
  1236. dev_warn(gpu->dev,
  1237. "timed out waiting for idle: idle=0x%x\n",
  1238. idle);
  1239. return -ETIMEDOUT;
  1240. }
  1241. udelay(5);
  1242. } while (1);
  1243. }
  1244. static int etnaviv_gpu_hw_suspend(struct etnaviv_gpu *gpu)
  1245. {
  1246. if (gpu->buffer.suballoc) {
  1247. /* Replace the last WAIT with END */
  1248. mutex_lock(&gpu->lock);
  1249. etnaviv_buffer_end(gpu);
  1250. mutex_unlock(&gpu->lock);
  1251. /*
  1252. * We know that only the FE is busy here, this should
  1253. * happen quickly (as the WAIT is only 200 cycles). If
  1254. * we fail, just warn and continue.
  1255. */
  1256. etnaviv_gpu_wait_idle(gpu, 100);
  1257. }
  1258. return etnaviv_gpu_clk_disable(gpu);
  1259. }
  1260. #ifdef CONFIG_PM
  1261. static int etnaviv_gpu_hw_resume(struct etnaviv_gpu *gpu)
  1262. {
  1263. int ret;
  1264. ret = mutex_lock_killable(&gpu->lock);
  1265. if (ret)
  1266. return ret;
  1267. etnaviv_gpu_update_clock(gpu);
  1268. etnaviv_gpu_hw_init(gpu);
  1269. gpu->lastctx = NULL;
  1270. gpu->exec_state = -1;
  1271. mutex_unlock(&gpu->lock);
  1272. return 0;
  1273. }
  1274. #endif
  1275. static int
  1276. etnaviv_gpu_cooling_get_max_state(struct thermal_cooling_device *cdev,
  1277. unsigned long *state)
  1278. {
  1279. *state = 6;
  1280. return 0;
  1281. }
  1282. static int
  1283. etnaviv_gpu_cooling_get_cur_state(struct thermal_cooling_device *cdev,
  1284. unsigned long *state)
  1285. {
  1286. struct etnaviv_gpu *gpu = cdev->devdata;
  1287. *state = gpu->freq_scale;
  1288. return 0;
  1289. }
  1290. static int
  1291. etnaviv_gpu_cooling_set_cur_state(struct thermal_cooling_device *cdev,
  1292. unsigned long state)
  1293. {
  1294. struct etnaviv_gpu *gpu = cdev->devdata;
  1295. mutex_lock(&gpu->lock);
  1296. gpu->freq_scale = state;
  1297. if (!pm_runtime_suspended(gpu->dev))
  1298. etnaviv_gpu_update_clock(gpu);
  1299. mutex_unlock(&gpu->lock);
  1300. return 0;
  1301. }
  1302. static struct thermal_cooling_device_ops cooling_ops = {
  1303. .get_max_state = etnaviv_gpu_cooling_get_max_state,
  1304. .get_cur_state = etnaviv_gpu_cooling_get_cur_state,
  1305. .set_cur_state = etnaviv_gpu_cooling_set_cur_state,
  1306. };
  1307. static int etnaviv_gpu_bind(struct device *dev, struct device *master,
  1308. void *data)
  1309. {
  1310. struct drm_device *drm = data;
  1311. struct etnaviv_drm_private *priv = drm->dev_private;
  1312. struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
  1313. int ret;
  1314. if (IS_ENABLED(CONFIG_DRM_ETNAVIV_THERMAL)) {
  1315. gpu->cooling = thermal_of_cooling_device_register(dev->of_node,
  1316. (char *)dev_name(dev), gpu, &cooling_ops);
  1317. if (IS_ERR(gpu->cooling))
  1318. return PTR_ERR(gpu->cooling);
  1319. }
  1320. gpu->wq = alloc_ordered_workqueue(dev_name(dev), 0);
  1321. if (!gpu->wq) {
  1322. ret = -ENOMEM;
  1323. goto out_thermal;
  1324. }
  1325. ret = etnaviv_sched_init(gpu);
  1326. if (ret)
  1327. goto out_workqueue;
  1328. #ifdef CONFIG_PM
  1329. ret = pm_runtime_get_sync(gpu->dev);
  1330. #else
  1331. ret = etnaviv_gpu_clk_enable(gpu);
  1332. #endif
  1333. if (ret < 0)
  1334. goto out_sched;
  1335. gpu->drm = drm;
  1336. gpu->fence_context = dma_fence_context_alloc(1);
  1337. idr_init(&gpu->fence_idr);
  1338. spin_lock_init(&gpu->fence_spinlock);
  1339. INIT_WORK(&gpu->sync_point_work, sync_point_worker);
  1340. init_waitqueue_head(&gpu->fence_event);
  1341. priv->gpu[priv->num_gpus++] = gpu;
  1342. pm_runtime_mark_last_busy(gpu->dev);
  1343. pm_runtime_put_autosuspend(gpu->dev);
  1344. return 0;
  1345. out_sched:
  1346. etnaviv_sched_fini(gpu);
  1347. out_workqueue:
  1348. destroy_workqueue(gpu->wq);
  1349. out_thermal:
  1350. if (IS_ENABLED(CONFIG_DRM_ETNAVIV_THERMAL))
  1351. thermal_cooling_device_unregister(gpu->cooling);
  1352. return ret;
  1353. }
  1354. static void etnaviv_gpu_unbind(struct device *dev, struct device *master,
  1355. void *data)
  1356. {
  1357. struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
  1358. DBG("%s", dev_name(gpu->dev));
  1359. flush_workqueue(gpu->wq);
  1360. destroy_workqueue(gpu->wq);
  1361. etnaviv_sched_fini(gpu);
  1362. #ifdef CONFIG_PM
  1363. pm_runtime_get_sync(gpu->dev);
  1364. pm_runtime_put_sync_suspend(gpu->dev);
  1365. #else
  1366. etnaviv_gpu_hw_suspend(gpu);
  1367. #endif
  1368. if (gpu->buffer.suballoc)
  1369. etnaviv_cmdbuf_free(&gpu->buffer);
  1370. if (gpu->cmdbuf_suballoc) {
  1371. etnaviv_cmdbuf_suballoc_destroy(gpu->cmdbuf_suballoc);
  1372. gpu->cmdbuf_suballoc = NULL;
  1373. }
  1374. if (gpu->mmu) {
  1375. etnaviv_iommu_destroy(gpu->mmu);
  1376. gpu->mmu = NULL;
  1377. }
  1378. gpu->drm = NULL;
  1379. idr_destroy(&gpu->fence_idr);
  1380. if (IS_ENABLED(CONFIG_DRM_ETNAVIV_THERMAL))
  1381. thermal_cooling_device_unregister(gpu->cooling);
  1382. gpu->cooling = NULL;
  1383. }
  1384. static const struct component_ops gpu_ops = {
  1385. .bind = etnaviv_gpu_bind,
  1386. .unbind = etnaviv_gpu_unbind,
  1387. };
  1388. static const struct of_device_id etnaviv_gpu_match[] = {
  1389. {
  1390. .compatible = "vivante,gc"
  1391. },
  1392. { /* sentinel */ }
  1393. };
  1394. MODULE_DEVICE_TABLE(of, etnaviv_gpu_match);
  1395. static int etnaviv_gpu_platform_probe(struct platform_device *pdev)
  1396. {
  1397. struct device *dev = &pdev->dev;
  1398. struct etnaviv_gpu *gpu;
  1399. struct resource *res;
  1400. int err;
  1401. gpu = devm_kzalloc(dev, sizeof(*gpu), GFP_KERNEL);
  1402. if (!gpu)
  1403. return -ENOMEM;
  1404. gpu->dev = &pdev->dev;
  1405. mutex_init(&gpu->lock);
  1406. mutex_init(&gpu->fence_lock);
  1407. /* Map registers: */
  1408. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1409. gpu->mmio = devm_ioremap_resource(&pdev->dev, res);
  1410. if (IS_ERR(gpu->mmio))
  1411. return PTR_ERR(gpu->mmio);
  1412. /* Get Interrupt: */
  1413. gpu->irq = platform_get_irq(pdev, 0);
  1414. if (gpu->irq < 0) {
  1415. dev_err(dev, "failed to get irq: %d\n", gpu->irq);
  1416. return gpu->irq;
  1417. }
  1418. err = devm_request_irq(&pdev->dev, gpu->irq, irq_handler, 0,
  1419. dev_name(gpu->dev), gpu);
  1420. if (err) {
  1421. dev_err(dev, "failed to request IRQ%u: %d\n", gpu->irq, err);
  1422. return err;
  1423. }
  1424. /* Get Clocks: */
  1425. gpu->clk_reg = devm_clk_get(&pdev->dev, "reg");
  1426. DBG("clk_reg: %p", gpu->clk_reg);
  1427. if (IS_ERR(gpu->clk_reg))
  1428. gpu->clk_reg = NULL;
  1429. gpu->clk_bus = devm_clk_get(&pdev->dev, "bus");
  1430. DBG("clk_bus: %p", gpu->clk_bus);
  1431. if (IS_ERR(gpu->clk_bus))
  1432. gpu->clk_bus = NULL;
  1433. gpu->clk_core = devm_clk_get(&pdev->dev, "core");
  1434. DBG("clk_core: %p", gpu->clk_core);
  1435. if (IS_ERR(gpu->clk_core))
  1436. gpu->clk_core = NULL;
  1437. gpu->base_rate_core = clk_get_rate(gpu->clk_core);
  1438. gpu->clk_shader = devm_clk_get(&pdev->dev, "shader");
  1439. DBG("clk_shader: %p", gpu->clk_shader);
  1440. if (IS_ERR(gpu->clk_shader))
  1441. gpu->clk_shader = NULL;
  1442. gpu->base_rate_shader = clk_get_rate(gpu->clk_shader);
  1443. /* TODO: figure out max mapped size */
  1444. dev_set_drvdata(dev, gpu);
  1445. /*
  1446. * We treat the device as initially suspended. The runtime PM
  1447. * autosuspend delay is rather arbitary: no measurements have
  1448. * yet been performed to determine an appropriate value.
  1449. */
  1450. pm_runtime_use_autosuspend(gpu->dev);
  1451. pm_runtime_set_autosuspend_delay(gpu->dev, 200);
  1452. pm_runtime_enable(gpu->dev);
  1453. err = component_add(&pdev->dev, &gpu_ops);
  1454. if (err < 0) {
  1455. dev_err(&pdev->dev, "failed to register component: %d\n", err);
  1456. return err;
  1457. }
  1458. return 0;
  1459. }
  1460. static int etnaviv_gpu_platform_remove(struct platform_device *pdev)
  1461. {
  1462. component_del(&pdev->dev, &gpu_ops);
  1463. pm_runtime_disable(&pdev->dev);
  1464. return 0;
  1465. }
  1466. #ifdef CONFIG_PM
  1467. static int etnaviv_gpu_rpm_suspend(struct device *dev)
  1468. {
  1469. struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
  1470. u32 idle, mask;
  1471. /* If we have outstanding fences, we're not idle */
  1472. if (gpu->completed_fence != gpu->active_fence)
  1473. return -EBUSY;
  1474. /* Check whether the hardware (except FE) is idle */
  1475. mask = gpu->idle_mask & ~VIVS_HI_IDLE_STATE_FE;
  1476. idle = gpu_read(gpu, VIVS_HI_IDLE_STATE) & mask;
  1477. if (idle != mask)
  1478. return -EBUSY;
  1479. return etnaviv_gpu_hw_suspend(gpu);
  1480. }
  1481. static int etnaviv_gpu_rpm_resume(struct device *dev)
  1482. {
  1483. struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
  1484. int ret;
  1485. ret = etnaviv_gpu_clk_enable(gpu);
  1486. if (ret)
  1487. return ret;
  1488. /* Re-initialise the basic hardware state */
  1489. if (gpu->drm && gpu->buffer.suballoc) {
  1490. ret = etnaviv_gpu_hw_resume(gpu);
  1491. if (ret) {
  1492. etnaviv_gpu_clk_disable(gpu);
  1493. return ret;
  1494. }
  1495. }
  1496. return 0;
  1497. }
  1498. #endif
  1499. static const struct dev_pm_ops etnaviv_gpu_pm_ops = {
  1500. SET_RUNTIME_PM_OPS(etnaviv_gpu_rpm_suspend, etnaviv_gpu_rpm_resume,
  1501. NULL)
  1502. };
  1503. struct platform_driver etnaviv_gpu_driver = {
  1504. .driver = {
  1505. .name = "etnaviv-gpu",
  1506. .owner = THIS_MODULE,
  1507. .pm = &etnaviv_gpu_pm_ops,
  1508. .of_match_table = etnaviv_gpu_match,
  1509. },
  1510. .probe = etnaviv_gpu_platform_probe,
  1511. .remove = etnaviv_gpu_platform_remove,
  1512. .id_table = gpu_ids,
  1513. };