etnaviv_gpu.c 48 KB

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