etnaviv_gpu.c 45 KB

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