etnaviv_gpu.c 47 KB

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