etnaviv_gpu.c 43 KB

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