etnaviv_gpu.c 50 KB

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