etnaviv_gpu.c 42 KB

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