etnaviv_gpu.c 54 KB

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