etnaviv_gpu.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  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. unsigned int fscale = 1 << (6 - gpu->freq_scale);
  349. u32 clock;
  350. clock = VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS |
  351. VIVS_HI_CLOCK_CONTROL_FSCALE_VAL(fscale);
  352. etnaviv_gpu_load_clock(gpu, clock);
  353. }
  354. static int etnaviv_hw_reset(struct etnaviv_gpu *gpu)
  355. {
  356. u32 control, idle;
  357. unsigned long timeout;
  358. bool failed = true;
  359. /* TODO
  360. *
  361. * - clock gating
  362. * - puls eater
  363. * - what about VG?
  364. */
  365. /* We hope that the GPU resets in under one second */
  366. timeout = jiffies + msecs_to_jiffies(1000);
  367. while (time_is_after_jiffies(timeout)) {
  368. /* enable clock */
  369. etnaviv_gpu_update_clock(gpu);
  370. control = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
  371. /* Wait for stable clock. Vivante's code waited for 1ms */
  372. usleep_range(1000, 10000);
  373. /* isolate the GPU. */
  374. control |= VIVS_HI_CLOCK_CONTROL_ISOLATE_GPU;
  375. gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
  376. /* set soft reset. */
  377. control |= VIVS_HI_CLOCK_CONTROL_SOFT_RESET;
  378. gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
  379. /* wait for reset. */
  380. msleep(1);
  381. /* reset soft reset bit. */
  382. control &= ~VIVS_HI_CLOCK_CONTROL_SOFT_RESET;
  383. gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
  384. /* reset GPU isolation. */
  385. control &= ~VIVS_HI_CLOCK_CONTROL_ISOLATE_GPU;
  386. gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
  387. /* read idle register. */
  388. idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
  389. /* try reseting again if FE it not idle */
  390. if ((idle & VIVS_HI_IDLE_STATE_FE) == 0) {
  391. dev_dbg(gpu->dev, "FE is not idle\n");
  392. continue;
  393. }
  394. /* read reset register. */
  395. control = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
  396. /* is the GPU idle? */
  397. if (((control & VIVS_HI_CLOCK_CONTROL_IDLE_3D) == 0) ||
  398. ((control & VIVS_HI_CLOCK_CONTROL_IDLE_2D) == 0)) {
  399. dev_dbg(gpu->dev, "GPU is not idle\n");
  400. continue;
  401. }
  402. failed = false;
  403. break;
  404. }
  405. if (failed) {
  406. idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
  407. control = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
  408. dev_err(gpu->dev, "GPU failed to reset: FE %sidle, 3D %sidle, 2D %sidle\n",
  409. idle & VIVS_HI_IDLE_STATE_FE ? "" : "not ",
  410. control & VIVS_HI_CLOCK_CONTROL_IDLE_3D ? "" : "not ",
  411. control & VIVS_HI_CLOCK_CONTROL_IDLE_2D ? "" : "not ");
  412. return -EBUSY;
  413. }
  414. /* We rely on the GPU running, so program the clock */
  415. etnaviv_gpu_update_clock(gpu);
  416. return 0;
  417. }
  418. static void etnaviv_gpu_enable_mlcg(struct etnaviv_gpu *gpu)
  419. {
  420. u32 pmc, ppc;
  421. /* enable clock gating */
  422. ppc = gpu_read(gpu, VIVS_PM_POWER_CONTROLS);
  423. ppc |= VIVS_PM_POWER_CONTROLS_ENABLE_MODULE_CLOCK_GATING;
  424. /* Disable stall module clock gating for 4.3.0.1 and 4.3.0.2 revs */
  425. if (gpu->identity.revision == 0x4301 ||
  426. gpu->identity.revision == 0x4302)
  427. ppc |= VIVS_PM_POWER_CONTROLS_DISABLE_STALL_MODULE_CLOCK_GATING;
  428. gpu_write(gpu, VIVS_PM_POWER_CONTROLS, ppc);
  429. pmc = gpu_read(gpu, VIVS_PM_MODULE_CONTROLS);
  430. /* Disable PA clock gating for GC400+ except for GC420 */
  431. if (gpu->identity.model >= chipModel_GC400 &&
  432. gpu->identity.model != chipModel_GC420)
  433. pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_PA;
  434. /*
  435. * Disable PE clock gating on revs < 5.0.0.0 when HZ is
  436. * present without a bug fix.
  437. */
  438. if (gpu->identity.revision < 0x5000 &&
  439. gpu->identity.minor_features0 & chipMinorFeatures0_HZ &&
  440. !(gpu->identity.minor_features1 &
  441. chipMinorFeatures1_DISABLE_PE_GATING))
  442. pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_PE;
  443. if (gpu->identity.revision < 0x5422)
  444. pmc |= BIT(15); /* Unknown bit */
  445. pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_RA_HZ;
  446. pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_RA_EZ;
  447. gpu_write(gpu, VIVS_PM_MODULE_CONTROLS, pmc);
  448. }
  449. void etnaviv_gpu_start_fe(struct etnaviv_gpu *gpu, u32 address, u16 prefetch)
  450. {
  451. gpu_write(gpu, VIVS_FE_COMMAND_ADDRESS, address);
  452. gpu_write(gpu, VIVS_FE_COMMAND_CONTROL,
  453. VIVS_FE_COMMAND_CONTROL_ENABLE |
  454. VIVS_FE_COMMAND_CONTROL_PREFETCH(prefetch));
  455. }
  456. static void etnaviv_gpu_setup_pulse_eater(struct etnaviv_gpu *gpu)
  457. {
  458. /*
  459. * Base value for VIVS_PM_PULSE_EATER register on models where it
  460. * cannot be read, extracted from vivante kernel driver.
  461. */
  462. u32 pulse_eater = 0x01590880;
  463. if (etnaviv_is_model_rev(gpu, GC4000, 0x5208) ||
  464. etnaviv_is_model_rev(gpu, GC4000, 0x5222)) {
  465. pulse_eater |= BIT(23);
  466. }
  467. if (etnaviv_is_model_rev(gpu, GC1000, 0x5039) ||
  468. etnaviv_is_model_rev(gpu, GC1000, 0x5040)) {
  469. pulse_eater &= ~BIT(16);
  470. pulse_eater |= BIT(17);
  471. }
  472. if ((gpu->identity.revision > 0x5420) &&
  473. (gpu->identity.features & chipFeatures_PIPE_3D))
  474. {
  475. /* Performance fix: disable internal DFS */
  476. pulse_eater = gpu_read(gpu, VIVS_PM_PULSE_EATER);
  477. pulse_eater |= BIT(18);
  478. }
  479. gpu_write(gpu, VIVS_PM_PULSE_EATER, pulse_eater);
  480. }
  481. static void etnaviv_gpu_hw_init(struct etnaviv_gpu *gpu)
  482. {
  483. u16 prefetch;
  484. if ((etnaviv_is_model_rev(gpu, GC320, 0x5007) ||
  485. etnaviv_is_model_rev(gpu, GC320, 0x5220)) &&
  486. gpu_read(gpu, VIVS_HI_CHIP_TIME) != 0x2062400) {
  487. u32 mc_memory_debug;
  488. mc_memory_debug = gpu_read(gpu, VIVS_MC_DEBUG_MEMORY) & ~0xff;
  489. if (gpu->identity.revision == 0x5007)
  490. mc_memory_debug |= 0x0c;
  491. else
  492. mc_memory_debug |= 0x08;
  493. gpu_write(gpu, VIVS_MC_DEBUG_MEMORY, mc_memory_debug);
  494. }
  495. /* enable module-level clock gating */
  496. etnaviv_gpu_enable_mlcg(gpu);
  497. /*
  498. * Update GPU AXI cache atttribute to "cacheable, no allocate".
  499. * This is necessary to prevent the iMX6 SoC locking up.
  500. */
  501. gpu_write(gpu, VIVS_HI_AXI_CONFIG,
  502. VIVS_HI_AXI_CONFIG_AWCACHE(2) |
  503. VIVS_HI_AXI_CONFIG_ARCACHE(2));
  504. /* GC2000 rev 5108 needs a special bus config */
  505. if (etnaviv_is_model_rev(gpu, GC2000, 0x5108)) {
  506. u32 bus_config = gpu_read(gpu, VIVS_MC_BUS_CONFIG);
  507. bus_config &= ~(VIVS_MC_BUS_CONFIG_FE_BUS_CONFIG__MASK |
  508. VIVS_MC_BUS_CONFIG_TX_BUS_CONFIG__MASK);
  509. bus_config |= VIVS_MC_BUS_CONFIG_FE_BUS_CONFIG(1) |
  510. VIVS_MC_BUS_CONFIG_TX_BUS_CONFIG(0);
  511. gpu_write(gpu, VIVS_MC_BUS_CONFIG, bus_config);
  512. }
  513. /* setup the pulse eater */
  514. etnaviv_gpu_setup_pulse_eater(gpu);
  515. /* setup the MMU */
  516. etnaviv_iommu_restore(gpu);
  517. /* Start command processor */
  518. prefetch = etnaviv_buffer_init(gpu);
  519. gpu_write(gpu, VIVS_HI_INTR_ENBL, ~0U);
  520. etnaviv_gpu_start_fe(gpu, etnaviv_cmdbuf_get_va(gpu->buffer),
  521. prefetch);
  522. }
  523. int etnaviv_gpu_init(struct etnaviv_gpu *gpu)
  524. {
  525. int ret, i;
  526. ret = pm_runtime_get_sync(gpu->dev);
  527. if (ret < 0) {
  528. dev_err(gpu->dev, "Failed to enable GPU power domain\n");
  529. return ret;
  530. }
  531. etnaviv_hw_identify(gpu);
  532. if (gpu->identity.model == 0) {
  533. dev_err(gpu->dev, "Unknown GPU model\n");
  534. ret = -ENXIO;
  535. goto fail;
  536. }
  537. /* Exclude VG cores with FE2.0 */
  538. if (gpu->identity.features & chipFeatures_PIPE_VG &&
  539. gpu->identity.features & chipFeatures_FE20) {
  540. dev_info(gpu->dev, "Ignoring GPU with VG and FE2.0\n");
  541. ret = -ENXIO;
  542. goto fail;
  543. }
  544. /*
  545. * Set the GPU linear window to be at the end of the DMA window, where
  546. * the CMA area is likely to reside. This ensures that we are able to
  547. * map the command buffers while having the linear window overlap as
  548. * much RAM as possible, so we can optimize mappings for other buffers.
  549. *
  550. * For 3D cores only do this if MC2.0 is present, as with MC1.0 it leads
  551. * to different views of the memory on the individual engines.
  552. */
  553. if (!(gpu->identity.features & chipFeatures_PIPE_3D) ||
  554. (gpu->identity.minor_features0 & chipMinorFeatures0_MC20)) {
  555. u32 dma_mask = (u32)dma_get_required_mask(gpu->dev);
  556. if (dma_mask < PHYS_OFFSET + SZ_2G)
  557. gpu->memory_base = PHYS_OFFSET;
  558. else
  559. gpu->memory_base = dma_mask - SZ_2G + 1;
  560. } else if (PHYS_OFFSET >= SZ_2G) {
  561. dev_info(gpu->dev, "Need to move linear window on MC1.0, disabling TS\n");
  562. gpu->memory_base = PHYS_OFFSET;
  563. gpu->identity.features &= ~chipFeatures_FAST_CLEAR;
  564. }
  565. ret = etnaviv_hw_reset(gpu);
  566. if (ret) {
  567. dev_err(gpu->dev, "GPU reset failed\n");
  568. goto fail;
  569. }
  570. gpu->mmu = etnaviv_iommu_new(gpu);
  571. if (IS_ERR(gpu->mmu)) {
  572. dev_err(gpu->dev, "Failed to instantiate GPU IOMMU\n");
  573. ret = PTR_ERR(gpu->mmu);
  574. goto fail;
  575. }
  576. gpu->cmdbuf_suballoc = etnaviv_cmdbuf_suballoc_new(gpu);
  577. if (IS_ERR(gpu->cmdbuf_suballoc)) {
  578. dev_err(gpu->dev, "Failed to create cmdbuf suballocator\n");
  579. ret = PTR_ERR(gpu->cmdbuf_suballoc);
  580. goto fail;
  581. }
  582. /* Create buffer: */
  583. gpu->buffer = etnaviv_cmdbuf_new(gpu->cmdbuf_suballoc, PAGE_SIZE, 0);
  584. if (!gpu->buffer) {
  585. ret = -ENOMEM;
  586. dev_err(gpu->dev, "could not create command buffer\n");
  587. goto destroy_iommu;
  588. }
  589. if (gpu->mmu->version == ETNAVIV_IOMMU_V1 &&
  590. etnaviv_cmdbuf_get_va(gpu->buffer) > 0x80000000) {
  591. ret = -EINVAL;
  592. dev_err(gpu->dev,
  593. "command buffer outside valid memory window\n");
  594. goto free_buffer;
  595. }
  596. /* Setup event management */
  597. spin_lock_init(&gpu->event_spinlock);
  598. init_completion(&gpu->event_free);
  599. for (i = 0; i < ARRAY_SIZE(gpu->event); i++) {
  600. gpu->event[i].used = false;
  601. complete(&gpu->event_free);
  602. }
  603. /* Now program the hardware */
  604. mutex_lock(&gpu->lock);
  605. etnaviv_gpu_hw_init(gpu);
  606. gpu->exec_state = -1;
  607. mutex_unlock(&gpu->lock);
  608. pm_runtime_mark_last_busy(gpu->dev);
  609. pm_runtime_put_autosuspend(gpu->dev);
  610. return 0;
  611. free_buffer:
  612. etnaviv_cmdbuf_free(gpu->buffer);
  613. gpu->buffer = NULL;
  614. destroy_iommu:
  615. etnaviv_iommu_destroy(gpu->mmu);
  616. gpu->mmu = NULL;
  617. fail:
  618. pm_runtime_mark_last_busy(gpu->dev);
  619. pm_runtime_put_autosuspend(gpu->dev);
  620. return ret;
  621. }
  622. #ifdef CONFIG_DEBUG_FS
  623. struct dma_debug {
  624. u32 address[2];
  625. u32 state[2];
  626. };
  627. static void verify_dma(struct etnaviv_gpu *gpu, struct dma_debug *debug)
  628. {
  629. u32 i;
  630. debug->address[0] = gpu_read(gpu, VIVS_FE_DMA_ADDRESS);
  631. debug->state[0] = gpu_read(gpu, VIVS_FE_DMA_DEBUG_STATE);
  632. for (i = 0; i < 500; i++) {
  633. debug->address[1] = gpu_read(gpu, VIVS_FE_DMA_ADDRESS);
  634. debug->state[1] = gpu_read(gpu, VIVS_FE_DMA_DEBUG_STATE);
  635. if (debug->address[0] != debug->address[1])
  636. break;
  637. if (debug->state[0] != debug->state[1])
  638. break;
  639. }
  640. }
  641. int etnaviv_gpu_debugfs(struct etnaviv_gpu *gpu, struct seq_file *m)
  642. {
  643. struct dma_debug debug;
  644. u32 dma_lo, dma_hi, axi, idle;
  645. int ret;
  646. seq_printf(m, "%s Status:\n", dev_name(gpu->dev));
  647. ret = pm_runtime_get_sync(gpu->dev);
  648. if (ret < 0)
  649. return ret;
  650. dma_lo = gpu_read(gpu, VIVS_FE_DMA_LOW);
  651. dma_hi = gpu_read(gpu, VIVS_FE_DMA_HIGH);
  652. axi = gpu_read(gpu, VIVS_HI_AXI_STATUS);
  653. idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
  654. verify_dma(gpu, &debug);
  655. seq_puts(m, "\tfeatures\n");
  656. seq_printf(m, "\t minor_features0: 0x%08x\n",
  657. gpu->identity.minor_features0);
  658. seq_printf(m, "\t minor_features1: 0x%08x\n",
  659. gpu->identity.minor_features1);
  660. seq_printf(m, "\t minor_features2: 0x%08x\n",
  661. gpu->identity.minor_features2);
  662. seq_printf(m, "\t minor_features3: 0x%08x\n",
  663. gpu->identity.minor_features3);
  664. seq_printf(m, "\t minor_features4: 0x%08x\n",
  665. gpu->identity.minor_features4);
  666. seq_printf(m, "\t minor_features5: 0x%08x\n",
  667. gpu->identity.minor_features5);
  668. seq_puts(m, "\tspecs\n");
  669. seq_printf(m, "\t stream_count: %d\n",
  670. gpu->identity.stream_count);
  671. seq_printf(m, "\t register_max: %d\n",
  672. gpu->identity.register_max);
  673. seq_printf(m, "\t thread_count: %d\n",
  674. gpu->identity.thread_count);
  675. seq_printf(m, "\t vertex_cache_size: %d\n",
  676. gpu->identity.vertex_cache_size);
  677. seq_printf(m, "\t shader_core_count: %d\n",
  678. gpu->identity.shader_core_count);
  679. seq_printf(m, "\t pixel_pipes: %d\n",
  680. gpu->identity.pixel_pipes);
  681. seq_printf(m, "\t vertex_output_buffer_size: %d\n",
  682. gpu->identity.vertex_output_buffer_size);
  683. seq_printf(m, "\t buffer_size: %d\n",
  684. gpu->identity.buffer_size);
  685. seq_printf(m, "\t instruction_count: %d\n",
  686. gpu->identity.instruction_count);
  687. seq_printf(m, "\t num_constants: %d\n",
  688. gpu->identity.num_constants);
  689. seq_printf(m, "\t varyings_count: %d\n",
  690. gpu->identity.varyings_count);
  691. seq_printf(m, "\taxi: 0x%08x\n", axi);
  692. seq_printf(m, "\tidle: 0x%08x\n", idle);
  693. idle |= ~gpu->idle_mask & ~VIVS_HI_IDLE_STATE_AXI_LP;
  694. if ((idle & VIVS_HI_IDLE_STATE_FE) == 0)
  695. seq_puts(m, "\t FE is not idle\n");
  696. if ((idle & VIVS_HI_IDLE_STATE_DE) == 0)
  697. seq_puts(m, "\t DE is not idle\n");
  698. if ((idle & VIVS_HI_IDLE_STATE_PE) == 0)
  699. seq_puts(m, "\t PE is not idle\n");
  700. if ((idle & VIVS_HI_IDLE_STATE_SH) == 0)
  701. seq_puts(m, "\t SH is not idle\n");
  702. if ((idle & VIVS_HI_IDLE_STATE_PA) == 0)
  703. seq_puts(m, "\t PA is not idle\n");
  704. if ((idle & VIVS_HI_IDLE_STATE_SE) == 0)
  705. seq_puts(m, "\t SE is not idle\n");
  706. if ((idle & VIVS_HI_IDLE_STATE_RA) == 0)
  707. seq_puts(m, "\t RA is not idle\n");
  708. if ((idle & VIVS_HI_IDLE_STATE_TX) == 0)
  709. seq_puts(m, "\t TX is not idle\n");
  710. if ((idle & VIVS_HI_IDLE_STATE_VG) == 0)
  711. seq_puts(m, "\t VG is not idle\n");
  712. if ((idle & VIVS_HI_IDLE_STATE_IM) == 0)
  713. seq_puts(m, "\t IM is not idle\n");
  714. if ((idle & VIVS_HI_IDLE_STATE_FP) == 0)
  715. seq_puts(m, "\t FP is not idle\n");
  716. if ((idle & VIVS_HI_IDLE_STATE_TS) == 0)
  717. seq_puts(m, "\t TS is not idle\n");
  718. if (idle & VIVS_HI_IDLE_STATE_AXI_LP)
  719. seq_puts(m, "\t AXI low power mode\n");
  720. if (gpu->identity.features & chipFeatures_DEBUG_MODE) {
  721. u32 read0 = gpu_read(gpu, VIVS_MC_DEBUG_READ0);
  722. u32 read1 = gpu_read(gpu, VIVS_MC_DEBUG_READ1);
  723. u32 write = gpu_read(gpu, VIVS_MC_DEBUG_WRITE);
  724. seq_puts(m, "\tMC\n");
  725. seq_printf(m, "\t read0: 0x%08x\n", read0);
  726. seq_printf(m, "\t read1: 0x%08x\n", read1);
  727. seq_printf(m, "\t write: 0x%08x\n", write);
  728. }
  729. seq_puts(m, "\tDMA ");
  730. if (debug.address[0] == debug.address[1] &&
  731. debug.state[0] == debug.state[1]) {
  732. seq_puts(m, "seems to be stuck\n");
  733. } else if (debug.address[0] == debug.address[1]) {
  734. seq_puts(m, "address is constant\n");
  735. } else {
  736. seq_puts(m, "is running\n");
  737. }
  738. seq_printf(m, "\t address 0: 0x%08x\n", debug.address[0]);
  739. seq_printf(m, "\t address 1: 0x%08x\n", debug.address[1]);
  740. seq_printf(m, "\t state 0: 0x%08x\n", debug.state[0]);
  741. seq_printf(m, "\t state 1: 0x%08x\n", debug.state[1]);
  742. seq_printf(m, "\t last fetch 64 bit word: 0x%08x 0x%08x\n",
  743. dma_lo, dma_hi);
  744. ret = 0;
  745. pm_runtime_mark_last_busy(gpu->dev);
  746. pm_runtime_put_autosuspend(gpu->dev);
  747. return ret;
  748. }
  749. #endif
  750. /*
  751. * Hangcheck detection for locked gpu:
  752. */
  753. static void recover_worker(struct work_struct *work)
  754. {
  755. struct etnaviv_gpu *gpu = container_of(work, struct etnaviv_gpu,
  756. recover_work);
  757. unsigned long flags;
  758. unsigned int i;
  759. dev_err(gpu->dev, "hangcheck recover!\n");
  760. if (pm_runtime_get_sync(gpu->dev) < 0)
  761. return;
  762. mutex_lock(&gpu->lock);
  763. /* Only catch the first event, or when manually re-armed */
  764. if (etnaviv_dump_core) {
  765. etnaviv_core_dump(gpu);
  766. etnaviv_dump_core = false;
  767. }
  768. etnaviv_hw_reset(gpu);
  769. /* complete all events, the GPU won't do it after the reset */
  770. spin_lock_irqsave(&gpu->event_spinlock, flags);
  771. for (i = 0; i < ARRAY_SIZE(gpu->event); i++) {
  772. if (!gpu->event[i].used)
  773. continue;
  774. dma_fence_signal(gpu->event[i].fence);
  775. gpu->event[i].fence = NULL;
  776. gpu->event[i].used = false;
  777. complete(&gpu->event_free);
  778. }
  779. spin_unlock_irqrestore(&gpu->event_spinlock, flags);
  780. gpu->completed_fence = gpu->active_fence;
  781. etnaviv_gpu_hw_init(gpu);
  782. gpu->lastctx = NULL;
  783. gpu->exec_state = -1;
  784. mutex_unlock(&gpu->lock);
  785. pm_runtime_mark_last_busy(gpu->dev);
  786. pm_runtime_put_autosuspend(gpu->dev);
  787. /* Retire the buffer objects in a work */
  788. etnaviv_queue_work(gpu->drm, &gpu->retire_work);
  789. }
  790. static void hangcheck_timer_reset(struct etnaviv_gpu *gpu)
  791. {
  792. DBG("%s", dev_name(gpu->dev));
  793. mod_timer(&gpu->hangcheck_timer,
  794. round_jiffies_up(jiffies + DRM_ETNAVIV_HANGCHECK_JIFFIES));
  795. }
  796. static void hangcheck_handler(unsigned long data)
  797. {
  798. struct etnaviv_gpu *gpu = (struct etnaviv_gpu *)data;
  799. u32 fence = gpu->completed_fence;
  800. bool progress = false;
  801. if (fence != gpu->hangcheck_fence) {
  802. gpu->hangcheck_fence = fence;
  803. progress = true;
  804. }
  805. if (!progress) {
  806. u32 dma_addr = gpu_read(gpu, VIVS_FE_DMA_ADDRESS);
  807. int change = dma_addr - gpu->hangcheck_dma_addr;
  808. if (change < 0 || change > 16) {
  809. gpu->hangcheck_dma_addr = dma_addr;
  810. progress = true;
  811. }
  812. }
  813. if (!progress && fence_after(gpu->active_fence, fence)) {
  814. dev_err(gpu->dev, "hangcheck detected gpu lockup!\n");
  815. dev_err(gpu->dev, " completed fence: %u\n", fence);
  816. dev_err(gpu->dev, " active fence: %u\n",
  817. gpu->active_fence);
  818. etnaviv_queue_work(gpu->drm, &gpu->recover_work);
  819. }
  820. /* if still more pending work, reset the hangcheck timer: */
  821. if (fence_after(gpu->active_fence, gpu->hangcheck_fence))
  822. hangcheck_timer_reset(gpu);
  823. }
  824. static void hangcheck_disable(struct etnaviv_gpu *gpu)
  825. {
  826. del_timer_sync(&gpu->hangcheck_timer);
  827. cancel_work_sync(&gpu->recover_work);
  828. }
  829. /* fence object management */
  830. struct etnaviv_fence {
  831. struct etnaviv_gpu *gpu;
  832. struct dma_fence base;
  833. };
  834. static inline struct etnaviv_fence *to_etnaviv_fence(struct dma_fence *fence)
  835. {
  836. return container_of(fence, struct etnaviv_fence, base);
  837. }
  838. static const char *etnaviv_fence_get_driver_name(struct dma_fence *fence)
  839. {
  840. return "etnaviv";
  841. }
  842. static const char *etnaviv_fence_get_timeline_name(struct dma_fence *fence)
  843. {
  844. struct etnaviv_fence *f = to_etnaviv_fence(fence);
  845. return dev_name(f->gpu->dev);
  846. }
  847. static bool etnaviv_fence_enable_signaling(struct dma_fence *fence)
  848. {
  849. return true;
  850. }
  851. static bool etnaviv_fence_signaled(struct dma_fence *fence)
  852. {
  853. struct etnaviv_fence *f = to_etnaviv_fence(fence);
  854. return fence_completed(f->gpu, f->base.seqno);
  855. }
  856. static void etnaviv_fence_release(struct dma_fence *fence)
  857. {
  858. struct etnaviv_fence *f = to_etnaviv_fence(fence);
  859. kfree_rcu(f, base.rcu);
  860. }
  861. static const struct dma_fence_ops etnaviv_fence_ops = {
  862. .get_driver_name = etnaviv_fence_get_driver_name,
  863. .get_timeline_name = etnaviv_fence_get_timeline_name,
  864. .enable_signaling = etnaviv_fence_enable_signaling,
  865. .signaled = etnaviv_fence_signaled,
  866. .wait = dma_fence_default_wait,
  867. .release = etnaviv_fence_release,
  868. };
  869. static struct dma_fence *etnaviv_gpu_fence_alloc(struct etnaviv_gpu *gpu)
  870. {
  871. struct etnaviv_fence *f;
  872. /*
  873. * GPU lock must already be held, otherwise fence completion order might
  874. * not match the seqno order assigned here.
  875. */
  876. lockdep_assert_held(&gpu->lock);
  877. f = kzalloc(sizeof(*f), GFP_KERNEL);
  878. if (!f)
  879. return NULL;
  880. f->gpu = gpu;
  881. dma_fence_init(&f->base, &etnaviv_fence_ops, &gpu->fence_spinlock,
  882. gpu->fence_context, ++gpu->next_fence);
  883. return &f->base;
  884. }
  885. int etnaviv_gpu_fence_sync_obj(struct etnaviv_gem_object *etnaviv_obj,
  886. unsigned int context, bool exclusive, bool explicit)
  887. {
  888. struct reservation_object *robj = etnaviv_obj->resv;
  889. struct reservation_object_list *fobj;
  890. struct dma_fence *fence;
  891. int i, ret;
  892. if (!exclusive) {
  893. ret = reservation_object_reserve_shared(robj);
  894. if (ret)
  895. return ret;
  896. }
  897. if (explicit)
  898. return 0;
  899. /*
  900. * If we have any shared fences, then the exclusive fence
  901. * should be ignored as it will already have been signalled.
  902. */
  903. fobj = reservation_object_get_list(robj);
  904. if (!fobj || fobj->shared_count == 0) {
  905. /* Wait on any existing exclusive fence which isn't our own */
  906. fence = reservation_object_get_excl(robj);
  907. if (fence && fence->context != context) {
  908. ret = dma_fence_wait(fence, true);
  909. if (ret)
  910. return ret;
  911. }
  912. }
  913. if (!exclusive || !fobj)
  914. return 0;
  915. for (i = 0; i < fobj->shared_count; i++) {
  916. fence = rcu_dereference_protected(fobj->shared[i],
  917. reservation_object_held(robj));
  918. if (fence->context != context) {
  919. ret = dma_fence_wait(fence, true);
  920. if (ret)
  921. return ret;
  922. }
  923. }
  924. return 0;
  925. }
  926. /*
  927. * event management:
  928. */
  929. static unsigned int event_alloc(struct etnaviv_gpu *gpu)
  930. {
  931. unsigned long ret, flags;
  932. unsigned int i, event = ~0U;
  933. ret = wait_for_completion_timeout(&gpu->event_free,
  934. msecs_to_jiffies(10 * 10000));
  935. if (!ret)
  936. dev_err(gpu->dev, "wait_for_completion_timeout failed");
  937. spin_lock_irqsave(&gpu->event_spinlock, flags);
  938. /* find first free event */
  939. for (i = 0; i < ARRAY_SIZE(gpu->event); i++) {
  940. if (gpu->event[i].used == false) {
  941. gpu->event[i].used = true;
  942. event = i;
  943. break;
  944. }
  945. }
  946. spin_unlock_irqrestore(&gpu->event_spinlock, flags);
  947. return event;
  948. }
  949. static void event_free(struct etnaviv_gpu *gpu, unsigned int event)
  950. {
  951. unsigned long flags;
  952. spin_lock_irqsave(&gpu->event_spinlock, flags);
  953. if (gpu->event[event].used == false) {
  954. dev_warn(gpu->dev, "event %u is already marked as free",
  955. event);
  956. spin_unlock_irqrestore(&gpu->event_spinlock, flags);
  957. } else {
  958. gpu->event[event].used = false;
  959. spin_unlock_irqrestore(&gpu->event_spinlock, flags);
  960. complete(&gpu->event_free);
  961. }
  962. }
  963. /*
  964. * Cmdstream submission/retirement:
  965. */
  966. static void retire_worker(struct work_struct *work)
  967. {
  968. struct etnaviv_gpu *gpu = container_of(work, struct etnaviv_gpu,
  969. retire_work);
  970. u32 fence = gpu->completed_fence;
  971. struct etnaviv_cmdbuf *cmdbuf, *tmp;
  972. unsigned int i;
  973. mutex_lock(&gpu->lock);
  974. list_for_each_entry_safe(cmdbuf, tmp, &gpu->active_cmd_list, node) {
  975. if (!dma_fence_is_signaled(cmdbuf->fence))
  976. break;
  977. list_del(&cmdbuf->node);
  978. dma_fence_put(cmdbuf->fence);
  979. for (i = 0; i < cmdbuf->nr_bos; i++) {
  980. struct etnaviv_vram_mapping *mapping = cmdbuf->bo_map[i];
  981. struct etnaviv_gem_object *etnaviv_obj = mapping->object;
  982. atomic_dec(&etnaviv_obj->gpu_active);
  983. /* drop the refcount taken in etnaviv_gpu_submit */
  984. etnaviv_gem_mapping_unreference(mapping);
  985. }
  986. etnaviv_cmdbuf_free(cmdbuf);
  987. /*
  988. * We need to balance the runtime PM count caused by
  989. * each submission. Upon submission, we increment
  990. * the runtime PM counter, and allocate one event.
  991. * So here, we put the runtime PM count for each
  992. * completed event.
  993. */
  994. pm_runtime_put_autosuspend(gpu->dev);
  995. }
  996. gpu->retired_fence = fence;
  997. mutex_unlock(&gpu->lock);
  998. wake_up_all(&gpu->fence_event);
  999. }
  1000. int etnaviv_gpu_wait_fence_interruptible(struct etnaviv_gpu *gpu,
  1001. u32 fence, struct timespec *timeout)
  1002. {
  1003. int ret;
  1004. if (fence_after(fence, gpu->next_fence)) {
  1005. DRM_ERROR("waiting on invalid fence: %u (of %u)\n",
  1006. fence, gpu->next_fence);
  1007. return -EINVAL;
  1008. }
  1009. if (!timeout) {
  1010. /* No timeout was requested: just test for completion */
  1011. ret = fence_completed(gpu, fence) ? 0 : -EBUSY;
  1012. } else {
  1013. unsigned long remaining = etnaviv_timeout_to_jiffies(timeout);
  1014. ret = wait_event_interruptible_timeout(gpu->fence_event,
  1015. fence_completed(gpu, fence),
  1016. remaining);
  1017. if (ret == 0) {
  1018. DBG("timeout waiting for fence: %u (retired: %u completed: %u)",
  1019. fence, gpu->retired_fence,
  1020. gpu->completed_fence);
  1021. ret = -ETIMEDOUT;
  1022. } else if (ret != -ERESTARTSYS) {
  1023. ret = 0;
  1024. }
  1025. }
  1026. return ret;
  1027. }
  1028. /*
  1029. * Wait for an object to become inactive. This, on it's own, is not race
  1030. * free: the object is moved by the retire worker off the active list, and
  1031. * then the iova is put. Moreover, the object could be re-submitted just
  1032. * after we notice that it's become inactive.
  1033. *
  1034. * Although the retirement happens under the gpu lock, we don't want to hold
  1035. * that lock in this function while waiting.
  1036. */
  1037. int etnaviv_gpu_wait_obj_inactive(struct etnaviv_gpu *gpu,
  1038. struct etnaviv_gem_object *etnaviv_obj, struct timespec *timeout)
  1039. {
  1040. unsigned long remaining;
  1041. long ret;
  1042. if (!timeout)
  1043. return !is_active(etnaviv_obj) ? 0 : -EBUSY;
  1044. remaining = etnaviv_timeout_to_jiffies(timeout);
  1045. ret = wait_event_interruptible_timeout(gpu->fence_event,
  1046. !is_active(etnaviv_obj),
  1047. remaining);
  1048. if (ret > 0) {
  1049. struct etnaviv_drm_private *priv = gpu->drm->dev_private;
  1050. /* Synchronise with the retire worker */
  1051. flush_workqueue(priv->wq);
  1052. return 0;
  1053. } else if (ret == -ERESTARTSYS) {
  1054. return -ERESTARTSYS;
  1055. } else {
  1056. return -ETIMEDOUT;
  1057. }
  1058. }
  1059. int etnaviv_gpu_pm_get_sync(struct etnaviv_gpu *gpu)
  1060. {
  1061. return pm_runtime_get_sync(gpu->dev);
  1062. }
  1063. void etnaviv_gpu_pm_put(struct etnaviv_gpu *gpu)
  1064. {
  1065. pm_runtime_mark_last_busy(gpu->dev);
  1066. pm_runtime_put_autosuspend(gpu->dev);
  1067. }
  1068. /* add bo's to gpu's ring, and kick gpu: */
  1069. int etnaviv_gpu_submit(struct etnaviv_gpu *gpu,
  1070. struct etnaviv_gem_submit *submit, struct etnaviv_cmdbuf *cmdbuf)
  1071. {
  1072. struct dma_fence *fence;
  1073. unsigned int event, i;
  1074. int ret;
  1075. ret = etnaviv_gpu_pm_get_sync(gpu);
  1076. if (ret < 0)
  1077. return ret;
  1078. /*
  1079. * TODO
  1080. *
  1081. * - flush
  1082. * - data endian
  1083. * - prefetch
  1084. *
  1085. */
  1086. event = event_alloc(gpu);
  1087. if (unlikely(event == ~0U)) {
  1088. DRM_ERROR("no free event\n");
  1089. ret = -EBUSY;
  1090. goto out_pm_put;
  1091. }
  1092. mutex_lock(&gpu->lock);
  1093. fence = etnaviv_gpu_fence_alloc(gpu);
  1094. if (!fence) {
  1095. event_free(gpu, event);
  1096. ret = -ENOMEM;
  1097. goto out_unlock;
  1098. }
  1099. gpu->event[event].fence = fence;
  1100. submit->fence = dma_fence_get(fence);
  1101. gpu->active_fence = submit->fence->seqno;
  1102. if (gpu->lastctx != cmdbuf->ctx) {
  1103. gpu->mmu->need_flush = true;
  1104. gpu->switch_context = true;
  1105. gpu->lastctx = cmdbuf->ctx;
  1106. }
  1107. etnaviv_buffer_queue(gpu, event, cmdbuf);
  1108. cmdbuf->fence = fence;
  1109. list_add_tail(&cmdbuf->node, &gpu->active_cmd_list);
  1110. /* We're committed to adding this command buffer, hold a PM reference */
  1111. pm_runtime_get_noresume(gpu->dev);
  1112. for (i = 0; i < submit->nr_bos; i++) {
  1113. struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
  1114. /* Each cmdbuf takes a refcount on the mapping */
  1115. etnaviv_gem_mapping_reference(submit->bos[i].mapping);
  1116. cmdbuf->bo_map[i] = submit->bos[i].mapping;
  1117. atomic_inc(&etnaviv_obj->gpu_active);
  1118. if (submit->bos[i].flags & ETNA_SUBMIT_BO_WRITE)
  1119. reservation_object_add_excl_fence(etnaviv_obj->resv,
  1120. fence);
  1121. else
  1122. reservation_object_add_shared_fence(etnaviv_obj->resv,
  1123. fence);
  1124. }
  1125. cmdbuf->nr_bos = submit->nr_bos;
  1126. hangcheck_timer_reset(gpu);
  1127. ret = 0;
  1128. out_unlock:
  1129. mutex_unlock(&gpu->lock);
  1130. out_pm_put:
  1131. etnaviv_gpu_pm_put(gpu);
  1132. return ret;
  1133. }
  1134. /*
  1135. * Init/Cleanup:
  1136. */
  1137. static irqreturn_t irq_handler(int irq, void *data)
  1138. {
  1139. struct etnaviv_gpu *gpu = data;
  1140. irqreturn_t ret = IRQ_NONE;
  1141. u32 intr = gpu_read(gpu, VIVS_HI_INTR_ACKNOWLEDGE);
  1142. if (intr != 0) {
  1143. int event;
  1144. pm_runtime_mark_last_busy(gpu->dev);
  1145. dev_dbg(gpu->dev, "intr 0x%08x\n", intr);
  1146. if (intr & VIVS_HI_INTR_ACKNOWLEDGE_AXI_BUS_ERROR) {
  1147. dev_err(gpu->dev, "AXI bus error\n");
  1148. intr &= ~VIVS_HI_INTR_ACKNOWLEDGE_AXI_BUS_ERROR;
  1149. }
  1150. if (intr & VIVS_HI_INTR_ACKNOWLEDGE_MMU_EXCEPTION) {
  1151. int i;
  1152. dev_err_ratelimited(gpu->dev,
  1153. "MMU fault status 0x%08x\n",
  1154. gpu_read(gpu, VIVS_MMUv2_STATUS));
  1155. for (i = 0; i < 4; i++) {
  1156. dev_err_ratelimited(gpu->dev,
  1157. "MMU %d fault addr 0x%08x\n",
  1158. i, gpu_read(gpu,
  1159. VIVS_MMUv2_EXCEPTION_ADDR(i)));
  1160. }
  1161. intr &= ~VIVS_HI_INTR_ACKNOWLEDGE_MMU_EXCEPTION;
  1162. }
  1163. while ((event = ffs(intr)) != 0) {
  1164. struct dma_fence *fence;
  1165. event -= 1;
  1166. intr &= ~(1 << event);
  1167. dev_dbg(gpu->dev, "event %u\n", event);
  1168. fence = gpu->event[event].fence;
  1169. gpu->event[event].fence = NULL;
  1170. dma_fence_signal(fence);
  1171. /*
  1172. * Events can be processed out of order. Eg,
  1173. * - allocate and queue event 0
  1174. * - allocate event 1
  1175. * - event 0 completes, we process it
  1176. * - allocate and queue event 0
  1177. * - event 1 and event 0 complete
  1178. * we can end up processing event 0 first, then 1.
  1179. */
  1180. if (fence_after(fence->seqno, gpu->completed_fence))
  1181. gpu->completed_fence = fence->seqno;
  1182. event_free(gpu, event);
  1183. }
  1184. /* Retire the buffer objects in a work */
  1185. etnaviv_queue_work(gpu->drm, &gpu->retire_work);
  1186. ret = IRQ_HANDLED;
  1187. }
  1188. return ret;
  1189. }
  1190. static int etnaviv_gpu_clk_enable(struct etnaviv_gpu *gpu)
  1191. {
  1192. int ret;
  1193. if (gpu->clk_bus) {
  1194. ret = clk_prepare_enable(gpu->clk_bus);
  1195. if (ret)
  1196. return ret;
  1197. }
  1198. if (gpu->clk_core) {
  1199. ret = clk_prepare_enable(gpu->clk_core);
  1200. if (ret)
  1201. goto disable_clk_bus;
  1202. }
  1203. if (gpu->clk_shader) {
  1204. ret = clk_prepare_enable(gpu->clk_shader);
  1205. if (ret)
  1206. goto disable_clk_core;
  1207. }
  1208. return 0;
  1209. disable_clk_core:
  1210. if (gpu->clk_core)
  1211. clk_disable_unprepare(gpu->clk_core);
  1212. disable_clk_bus:
  1213. if (gpu->clk_bus)
  1214. clk_disable_unprepare(gpu->clk_bus);
  1215. return ret;
  1216. }
  1217. static int etnaviv_gpu_clk_disable(struct etnaviv_gpu *gpu)
  1218. {
  1219. if (gpu->clk_shader)
  1220. clk_disable_unprepare(gpu->clk_shader);
  1221. if (gpu->clk_core)
  1222. clk_disable_unprepare(gpu->clk_core);
  1223. if (gpu->clk_bus)
  1224. clk_disable_unprepare(gpu->clk_bus);
  1225. return 0;
  1226. }
  1227. int etnaviv_gpu_wait_idle(struct etnaviv_gpu *gpu, unsigned int timeout_ms)
  1228. {
  1229. unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
  1230. do {
  1231. u32 idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
  1232. if ((idle & gpu->idle_mask) == gpu->idle_mask)
  1233. return 0;
  1234. if (time_is_before_jiffies(timeout)) {
  1235. dev_warn(gpu->dev,
  1236. "timed out waiting for idle: idle=0x%x\n",
  1237. idle);
  1238. return -ETIMEDOUT;
  1239. }
  1240. udelay(5);
  1241. } while (1);
  1242. }
  1243. static int etnaviv_gpu_hw_suspend(struct etnaviv_gpu *gpu)
  1244. {
  1245. if (gpu->buffer) {
  1246. /* Replace the last WAIT with END */
  1247. etnaviv_buffer_end(gpu);
  1248. /*
  1249. * We know that only the FE is busy here, this should
  1250. * happen quickly (as the WAIT is only 200 cycles). If
  1251. * we fail, just warn and continue.
  1252. */
  1253. etnaviv_gpu_wait_idle(gpu, 100);
  1254. }
  1255. return etnaviv_gpu_clk_disable(gpu);
  1256. }
  1257. #ifdef CONFIG_PM
  1258. static int etnaviv_gpu_hw_resume(struct etnaviv_gpu *gpu)
  1259. {
  1260. int ret;
  1261. ret = mutex_lock_killable(&gpu->lock);
  1262. if (ret)
  1263. return ret;
  1264. etnaviv_gpu_update_clock(gpu);
  1265. etnaviv_gpu_hw_init(gpu);
  1266. gpu->switch_context = true;
  1267. gpu->exec_state = -1;
  1268. mutex_unlock(&gpu->lock);
  1269. return 0;
  1270. }
  1271. #endif
  1272. static int
  1273. etnaviv_gpu_cooling_get_max_state(struct thermal_cooling_device *cdev,
  1274. unsigned long *state)
  1275. {
  1276. *state = 6;
  1277. return 0;
  1278. }
  1279. static int
  1280. etnaviv_gpu_cooling_get_cur_state(struct thermal_cooling_device *cdev,
  1281. unsigned long *state)
  1282. {
  1283. struct etnaviv_gpu *gpu = cdev->devdata;
  1284. *state = gpu->freq_scale;
  1285. return 0;
  1286. }
  1287. static int
  1288. etnaviv_gpu_cooling_set_cur_state(struct thermal_cooling_device *cdev,
  1289. unsigned long state)
  1290. {
  1291. struct etnaviv_gpu *gpu = cdev->devdata;
  1292. mutex_lock(&gpu->lock);
  1293. gpu->freq_scale = state;
  1294. if (!pm_runtime_suspended(gpu->dev))
  1295. etnaviv_gpu_update_clock(gpu);
  1296. mutex_unlock(&gpu->lock);
  1297. return 0;
  1298. }
  1299. static struct thermal_cooling_device_ops cooling_ops = {
  1300. .get_max_state = etnaviv_gpu_cooling_get_max_state,
  1301. .get_cur_state = etnaviv_gpu_cooling_get_cur_state,
  1302. .set_cur_state = etnaviv_gpu_cooling_set_cur_state,
  1303. };
  1304. static int etnaviv_gpu_bind(struct device *dev, struct device *master,
  1305. void *data)
  1306. {
  1307. struct drm_device *drm = data;
  1308. struct etnaviv_drm_private *priv = drm->dev_private;
  1309. struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
  1310. int ret;
  1311. gpu->cooling = thermal_of_cooling_device_register(dev->of_node,
  1312. (char *)dev_name(dev), gpu, &cooling_ops);
  1313. if (IS_ERR(gpu->cooling))
  1314. return PTR_ERR(gpu->cooling);
  1315. #ifdef CONFIG_PM
  1316. ret = pm_runtime_get_sync(gpu->dev);
  1317. #else
  1318. ret = etnaviv_gpu_clk_enable(gpu);
  1319. #endif
  1320. if (ret < 0) {
  1321. thermal_cooling_device_unregister(gpu->cooling);
  1322. return ret;
  1323. }
  1324. gpu->drm = drm;
  1325. gpu->fence_context = dma_fence_context_alloc(1);
  1326. spin_lock_init(&gpu->fence_spinlock);
  1327. INIT_LIST_HEAD(&gpu->active_cmd_list);
  1328. INIT_WORK(&gpu->retire_work, retire_worker);
  1329. INIT_WORK(&gpu->recover_work, recover_worker);
  1330. init_waitqueue_head(&gpu->fence_event);
  1331. setup_deferrable_timer(&gpu->hangcheck_timer, hangcheck_handler,
  1332. (unsigned long)gpu);
  1333. priv->gpu[priv->num_gpus++] = gpu;
  1334. pm_runtime_mark_last_busy(gpu->dev);
  1335. pm_runtime_put_autosuspend(gpu->dev);
  1336. return 0;
  1337. }
  1338. static void etnaviv_gpu_unbind(struct device *dev, struct device *master,
  1339. void *data)
  1340. {
  1341. struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
  1342. DBG("%s", dev_name(gpu->dev));
  1343. hangcheck_disable(gpu);
  1344. #ifdef CONFIG_PM
  1345. pm_runtime_get_sync(gpu->dev);
  1346. pm_runtime_put_sync_suspend(gpu->dev);
  1347. #else
  1348. etnaviv_gpu_hw_suspend(gpu);
  1349. #endif
  1350. if (gpu->buffer) {
  1351. etnaviv_cmdbuf_free(gpu->buffer);
  1352. gpu->buffer = NULL;
  1353. }
  1354. if (gpu->cmdbuf_suballoc) {
  1355. etnaviv_cmdbuf_suballoc_destroy(gpu->cmdbuf_suballoc);
  1356. gpu->cmdbuf_suballoc = NULL;
  1357. }
  1358. if (gpu->mmu) {
  1359. etnaviv_iommu_destroy(gpu->mmu);
  1360. gpu->mmu = NULL;
  1361. }
  1362. gpu->drm = NULL;
  1363. thermal_cooling_device_unregister(gpu->cooling);
  1364. gpu->cooling = NULL;
  1365. }
  1366. static const struct component_ops gpu_ops = {
  1367. .bind = etnaviv_gpu_bind,
  1368. .unbind = etnaviv_gpu_unbind,
  1369. };
  1370. static const struct of_device_id etnaviv_gpu_match[] = {
  1371. {
  1372. .compatible = "vivante,gc"
  1373. },
  1374. { /* sentinel */ }
  1375. };
  1376. static int etnaviv_gpu_platform_probe(struct platform_device *pdev)
  1377. {
  1378. struct device *dev = &pdev->dev;
  1379. struct etnaviv_gpu *gpu;
  1380. int err;
  1381. gpu = devm_kzalloc(dev, sizeof(*gpu), GFP_KERNEL);
  1382. if (!gpu)
  1383. return -ENOMEM;
  1384. gpu->dev = &pdev->dev;
  1385. mutex_init(&gpu->lock);
  1386. /* Map registers: */
  1387. gpu->mmio = etnaviv_ioremap(pdev, NULL, dev_name(gpu->dev));
  1388. if (IS_ERR(gpu->mmio))
  1389. return PTR_ERR(gpu->mmio);
  1390. /* Get Interrupt: */
  1391. gpu->irq = platform_get_irq(pdev, 0);
  1392. if (gpu->irq < 0) {
  1393. dev_err(dev, "failed to get irq: %d\n", gpu->irq);
  1394. return gpu->irq;
  1395. }
  1396. err = devm_request_irq(&pdev->dev, gpu->irq, irq_handler, 0,
  1397. dev_name(gpu->dev), gpu);
  1398. if (err) {
  1399. dev_err(dev, "failed to request IRQ%u: %d\n", gpu->irq, err);
  1400. return err;
  1401. }
  1402. /* Get Clocks: */
  1403. gpu->clk_bus = devm_clk_get(&pdev->dev, "bus");
  1404. DBG("clk_bus: %p", gpu->clk_bus);
  1405. if (IS_ERR(gpu->clk_bus))
  1406. gpu->clk_bus = NULL;
  1407. gpu->clk_core = devm_clk_get(&pdev->dev, "core");
  1408. DBG("clk_core: %p", gpu->clk_core);
  1409. if (IS_ERR(gpu->clk_core))
  1410. gpu->clk_core = NULL;
  1411. gpu->clk_shader = devm_clk_get(&pdev->dev, "shader");
  1412. DBG("clk_shader: %p", gpu->clk_shader);
  1413. if (IS_ERR(gpu->clk_shader))
  1414. gpu->clk_shader = NULL;
  1415. /* TODO: figure out max mapped size */
  1416. dev_set_drvdata(dev, gpu);
  1417. /*
  1418. * We treat the device as initially suspended. The runtime PM
  1419. * autosuspend delay is rather arbitary: no measurements have
  1420. * yet been performed to determine an appropriate value.
  1421. */
  1422. pm_runtime_use_autosuspend(gpu->dev);
  1423. pm_runtime_set_autosuspend_delay(gpu->dev, 200);
  1424. pm_runtime_enable(gpu->dev);
  1425. err = component_add(&pdev->dev, &gpu_ops);
  1426. if (err < 0) {
  1427. dev_err(&pdev->dev, "failed to register component: %d\n", err);
  1428. return err;
  1429. }
  1430. return 0;
  1431. }
  1432. static int etnaviv_gpu_platform_remove(struct platform_device *pdev)
  1433. {
  1434. component_del(&pdev->dev, &gpu_ops);
  1435. pm_runtime_disable(&pdev->dev);
  1436. return 0;
  1437. }
  1438. #ifdef CONFIG_PM
  1439. static int etnaviv_gpu_rpm_suspend(struct device *dev)
  1440. {
  1441. struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
  1442. u32 idle, mask;
  1443. /* If we have outstanding fences, we're not idle */
  1444. if (gpu->completed_fence != gpu->active_fence)
  1445. return -EBUSY;
  1446. /* Check whether the hardware (except FE) is idle */
  1447. mask = gpu->idle_mask & ~VIVS_HI_IDLE_STATE_FE;
  1448. idle = gpu_read(gpu, VIVS_HI_IDLE_STATE) & mask;
  1449. if (idle != mask)
  1450. return -EBUSY;
  1451. return etnaviv_gpu_hw_suspend(gpu);
  1452. }
  1453. static int etnaviv_gpu_rpm_resume(struct device *dev)
  1454. {
  1455. struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
  1456. int ret;
  1457. ret = etnaviv_gpu_clk_enable(gpu);
  1458. if (ret)
  1459. return ret;
  1460. /* Re-initialise the basic hardware state */
  1461. if (gpu->drm && gpu->buffer) {
  1462. ret = etnaviv_gpu_hw_resume(gpu);
  1463. if (ret) {
  1464. etnaviv_gpu_clk_disable(gpu);
  1465. return ret;
  1466. }
  1467. }
  1468. return 0;
  1469. }
  1470. #endif
  1471. static const struct dev_pm_ops etnaviv_gpu_pm_ops = {
  1472. SET_RUNTIME_PM_OPS(etnaviv_gpu_rpm_suspend, etnaviv_gpu_rpm_resume,
  1473. NULL)
  1474. };
  1475. struct platform_driver etnaviv_gpu_driver = {
  1476. .driver = {
  1477. .name = "etnaviv-gpu",
  1478. .owner = THIS_MODULE,
  1479. .pm = &etnaviv_gpu_pm_ops,
  1480. .of_match_table = etnaviv_gpu_match,
  1481. },
  1482. .probe = etnaviv_gpu_platform_probe,
  1483. .remove = etnaviv_gpu_platform_remove,
  1484. .id_table = gpu_ids,
  1485. };