adreno_ringbuffer.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. /* Copyright (c) 2002,2007-2014,2017, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. #include <linux/firmware.h>
  14. #include <linux/slab.h>
  15. #include <linux/sched.h>
  16. #include <linux/log2.h>
  17. #include <linux/time.h>
  18. #include <linux/delay.h>
  19. #include "kgsl.h"
  20. #include "kgsl_sharedmem.h"
  21. #include "kgsl_cffdump.h"
  22. #include "adreno.h"
  23. #include "adreno_pm4types.h"
  24. #include "adreno_ringbuffer.h"
  25. #include "a2xx_reg.h"
  26. #include "a3xx_reg.h"
  27. #define GSL_RB_NOP_SIZEDWORDS 2
  28. void adreno_ringbuffer_submit(struct adreno_ringbuffer *rb)
  29. {
  30. struct adreno_device *adreno_dev = ADRENO_DEVICE(rb->device);
  31. BUG_ON(rb->wptr == 0);
  32. /* Let the pwrscale policy know that new commands have
  33. been submitted. */
  34. kgsl_pwrscale_busy(rb->device);
  35. /*synchronize memory before informing the hardware of the
  36. *new commands.
  37. */
  38. mb();
  39. adreno_writereg(adreno_dev, ADRENO_REG_CP_RB_WPTR, rb->wptr);
  40. }
  41. static int
  42. adreno_ringbuffer_waitspace(struct adreno_ringbuffer *rb,
  43. struct adreno_context *context,
  44. unsigned int numcmds, int wptr_ahead)
  45. {
  46. int nopcount;
  47. unsigned int freecmds;
  48. unsigned int *cmds;
  49. uint cmds_gpu;
  50. unsigned long wait_time;
  51. unsigned long wait_timeout = msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
  52. unsigned long wait_time_part;
  53. unsigned int rptr;
  54. /* if wptr ahead, fill the remaining with NOPs */
  55. if (wptr_ahead) {
  56. /* -1 for header */
  57. nopcount = rb->sizedwords - rb->wptr - 1;
  58. cmds = (unsigned int *)rb->buffer_desc.hostptr + rb->wptr;
  59. cmds_gpu = rb->buffer_desc.gpuaddr + sizeof(uint)*rb->wptr;
  60. GSL_RB_WRITE(rb->device, cmds, cmds_gpu,
  61. cp_nop_packet(nopcount));
  62. /* Make sure that rptr is not 0 before submitting
  63. * commands at the end of ringbuffer. We do not
  64. * want the rptr and wptr to become equal when
  65. * the ringbuffer is not empty */
  66. do {
  67. rptr = adreno_get_rptr(rb);
  68. } while (!rptr);
  69. rb->wptr = 0;
  70. }
  71. wait_time = jiffies + wait_timeout;
  72. wait_time_part = jiffies + msecs_to_jiffies(KGSL_TIMEOUT_PART);
  73. /* wait for space in ringbuffer */
  74. while (1) {
  75. rptr = adreno_get_rptr(rb);
  76. freecmds = rptr - rb->wptr;
  77. if (freecmds == 0 || freecmds > numcmds)
  78. break;
  79. if (time_after(jiffies, wait_time)) {
  80. KGSL_DRV_ERR(rb->device,
  81. "Timed out while waiting for freespace in ringbuffer "
  82. "rptr: 0x%x, wptr: 0x%x\n", rptr, rb->wptr);
  83. return -ETIMEDOUT;
  84. }
  85. }
  86. return 0;
  87. }
  88. unsigned int *adreno_ringbuffer_allocspace(struct adreno_ringbuffer *rb,
  89. struct adreno_context *context,
  90. unsigned int numcmds)
  91. {
  92. unsigned int *ptr = NULL;
  93. int ret = 0;
  94. unsigned int rptr;
  95. BUG_ON(numcmds >= rb->sizedwords);
  96. rptr = adreno_get_rptr(rb);
  97. /* check for available space */
  98. if (rb->wptr >= rptr) {
  99. /* wptr ahead or equal to rptr */
  100. /* reserve dwords for nop packet */
  101. if ((rb->wptr + numcmds) > (rb->sizedwords -
  102. GSL_RB_NOP_SIZEDWORDS))
  103. ret = adreno_ringbuffer_waitspace(rb, context,
  104. numcmds, 1);
  105. } else {
  106. /* wptr behind rptr */
  107. if ((rb->wptr + numcmds) >= rptr)
  108. ret = adreno_ringbuffer_waitspace(rb, context,
  109. numcmds, 0);
  110. /* check for remaining space */
  111. /* reserve dwords for nop packet */
  112. if (!ret && (rb->wptr + numcmds) > (rb->sizedwords -
  113. GSL_RB_NOP_SIZEDWORDS))
  114. ret = adreno_ringbuffer_waitspace(rb, context,
  115. numcmds, 1);
  116. }
  117. if (!ret) {
  118. ptr = (unsigned int *)rb->buffer_desc.hostptr + rb->wptr;
  119. rb->wptr += numcmds;
  120. } else
  121. ptr = ERR_PTR(ret);
  122. return ptr;
  123. }
  124. static int _load_firmware(struct kgsl_device *device, const char *fwfile,
  125. void **data, int *len)
  126. {
  127. const struct firmware *fw = NULL;
  128. int ret;
  129. ret = request_firmware(&fw, fwfile, device->dev);
  130. if (ret) {
  131. KGSL_DRV_ERR(device, "request_firmware(%s) failed: %d\n",
  132. fwfile, ret);
  133. return ret;
  134. }
  135. *data = kmalloc(fw->size, GFP_KERNEL);
  136. if (*data) {
  137. memcpy(*data, fw->data, fw->size);
  138. *len = fw->size;
  139. } else
  140. KGSL_MEM_ERR(device, "kmalloc(%d) failed\n", fw->size);
  141. release_firmware(fw);
  142. return (*data != NULL) ? 0 : -ENOMEM;
  143. }
  144. int adreno_ringbuffer_read_pm4_ucode(struct kgsl_device *device)
  145. {
  146. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  147. int ret = 0;
  148. if (adreno_dev->pm4_fw == NULL) {
  149. int len;
  150. void *ptr;
  151. ret = _load_firmware(device, adreno_dev->pm4_fwfile,
  152. &ptr, &len);
  153. if (ret)
  154. goto err;
  155. /* PM4 size is 3 dword aligned plus 1 dword of version */
  156. if (len % ((sizeof(uint32_t) * 3)) != sizeof(uint32_t)) {
  157. KGSL_DRV_ERR(device, "Bad firmware size: %d\n", len);
  158. ret = -EINVAL;
  159. kfree(ptr);
  160. goto err;
  161. }
  162. adreno_dev->pm4_fw_size = len / sizeof(uint32_t);
  163. adreno_dev->pm4_fw = ptr;
  164. adreno_dev->pm4_fw_version = adreno_dev->pm4_fw[1];
  165. }
  166. err:
  167. return ret;
  168. }
  169. /**
  170. * adreno_ringbuffer_load_pm4_ucode() - Load pm4 ucode
  171. * @device: Pointer to a KGSL device
  172. * @start: Starting index in pm4 ucode to load
  173. * @end: Ending index of pm4 ucode to load
  174. * @addr: Address to load the pm4 ucode
  175. *
  176. * Load the pm4 ucode from @start at @addr.
  177. */
  178. inline int adreno_ringbuffer_load_pm4_ucode(struct kgsl_device *device,
  179. unsigned int start, unsigned int end, unsigned int addr)
  180. {
  181. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  182. int i;
  183. adreno_writereg(adreno_dev, ADRENO_REG_CP_ME_RAM_WADDR, addr);
  184. for (i = start; i < end; i++)
  185. adreno_writereg(adreno_dev, ADRENO_REG_CP_ME_RAM_DATA,
  186. adreno_dev->pm4_fw[i]);
  187. return 0;
  188. }
  189. int adreno_ringbuffer_read_pfp_ucode(struct kgsl_device *device)
  190. {
  191. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  192. int ret = 0;
  193. if (adreno_dev->pfp_fw == NULL) {
  194. int len;
  195. void *ptr;
  196. ret = _load_firmware(device, adreno_dev->pfp_fwfile,
  197. &ptr, &len);
  198. if (ret)
  199. goto err;
  200. /* PFP size shold be dword aligned */
  201. if (len % sizeof(uint32_t) != 0) {
  202. KGSL_DRV_ERR(device, "Bad firmware size: %d\n", len);
  203. ret = -EINVAL;
  204. kfree(ptr);
  205. goto err;
  206. }
  207. adreno_dev->pfp_fw_size = len / sizeof(uint32_t);
  208. adreno_dev->pfp_fw = ptr;
  209. adreno_dev->pfp_fw_version = adreno_dev->pfp_fw[5];
  210. }
  211. err:
  212. return ret;
  213. }
  214. /**
  215. * adreno_ringbuffer_load_pfp_ucode() - Load pfp ucode
  216. * @device: Pointer to a KGSL device
  217. * @start: Starting index in pfp ucode to load
  218. * @end: Ending index of pfp ucode to load
  219. * @addr: Address to load the pfp ucode
  220. *
  221. * Load the pfp ucode from @start at @addr.
  222. */
  223. inline int adreno_ringbuffer_load_pfp_ucode(struct kgsl_device *device,
  224. unsigned int start, unsigned int end, unsigned int addr)
  225. {
  226. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  227. int i;
  228. adreno_writereg(adreno_dev, ADRENO_REG_CP_PFP_UCODE_ADDR, addr);
  229. for (i = start; i < end; i++)
  230. adreno_writereg(adreno_dev, ADRENO_REG_CP_PFP_UCODE_DATA,
  231. adreno_dev->pfp_fw[i]);
  232. return 0;
  233. }
  234. /**
  235. * _ringbuffer_bootstrap_ucode() - Bootstrap GPU Ucode
  236. * @rb: Pointer to adreno ringbuffer
  237. * @load_jt: If non zero only load Jump tables
  238. *
  239. * Bootstrap ucode for GPU
  240. * load_jt == 0, bootstrap full microcode
  241. * load_jt == 1, bootstrap jump tables of microcode
  242. *
  243. * For example a bootstrap packet would like below
  244. * Setup a type3 bootstrap packet
  245. * PFP size to bootstrap
  246. * PFP addr to write the PFP data
  247. * PM4 size to bootstrap
  248. * PM4 addr to write the PM4 data
  249. * PFP dwords from microcode to bootstrap
  250. * PM4 size dwords from microcode to bootstrap
  251. */
  252. static int _ringbuffer_bootstrap_ucode(struct adreno_ringbuffer *rb,
  253. unsigned int load_jt)
  254. {
  255. unsigned int *cmds, cmds_gpu, bootstrap_size;
  256. int i = 0;
  257. struct kgsl_device *device = rb->device;
  258. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  259. unsigned int pm4_size, pm4_idx, pm4_addr, pfp_size, pfp_idx, pfp_addr;
  260. /* Only bootstrap jump tables of ucode */
  261. if (load_jt) {
  262. pm4_idx = adreno_dev->pm4_jt_idx;
  263. pm4_addr = adreno_dev->pm4_jt_addr;
  264. pfp_idx = adreno_dev->pfp_jt_idx;
  265. pfp_addr = adreno_dev->pfp_jt_addr;
  266. } else {
  267. /* Bootstrap full ucode */
  268. pm4_idx = 1;
  269. pm4_addr = 0;
  270. pfp_idx = 1;
  271. pfp_addr = 0;
  272. }
  273. pm4_size = (adreno_dev->pm4_fw_size - pm4_idx);
  274. pfp_size = (adreno_dev->pfp_fw_size - pfp_idx);
  275. /*
  276. * Below set of commands register with PFP that 6f is the
  277. * opcode for bootstrapping
  278. */
  279. adreno_writereg(adreno_dev, ADRENO_REG_CP_PFP_UCODE_ADDR, 0x200);
  280. adreno_writereg(adreno_dev, ADRENO_REG_CP_PFP_UCODE_DATA, 0x6f0005);
  281. /* clear ME_HALT to start micro engine */
  282. adreno_writereg(adreno_dev, ADRENO_REG_CP_ME_CNTL, 0);
  283. bootstrap_size = (pm4_size + pfp_size + 5);
  284. cmds = adreno_ringbuffer_allocspace(rb, NULL, bootstrap_size);
  285. if (cmds == NULL)
  286. return -ENOMEM;
  287. cmds_gpu = rb->buffer_desc.gpuaddr +
  288. sizeof(uint) * (rb->wptr - bootstrap_size);
  289. /* Construct the packet that bootsraps the ucode */
  290. GSL_RB_WRITE(rb->device, cmds, cmds_gpu,
  291. cp_type3_packet(CP_BOOTSTRAP_UCODE,
  292. (bootstrap_size - 1)));
  293. GSL_RB_WRITE(rb->device, cmds, cmds_gpu, pfp_size);
  294. GSL_RB_WRITE(rb->device, cmds, cmds_gpu, pfp_addr);
  295. GSL_RB_WRITE(rb->device, cmds, cmds_gpu, pm4_size);
  296. GSL_RB_WRITE(rb->device, cmds, cmds_gpu, pm4_addr);
  297. for (i = pfp_idx; i < adreno_dev->pfp_fw_size; i++)
  298. GSL_RB_WRITE(rb->device, cmds, cmds_gpu, adreno_dev->pfp_fw[i]);
  299. for (i = pm4_idx; i < adreno_dev->pm4_fw_size; i++)
  300. GSL_RB_WRITE(rb->device, cmds, cmds_gpu, adreno_dev->pm4_fw[i]);
  301. adreno_ringbuffer_submit(rb);
  302. /* idle device to validate bootstrap */
  303. return adreno_idle(device);
  304. }
  305. /**
  306. * _ringbuffer_setup_common() - Ringbuffer start
  307. * @rb: Pointer to adreno ringbuffer
  308. *
  309. * Setup ringbuffer for GPU.
  310. */
  311. void _ringbuffer_setup_common(struct adreno_ringbuffer *rb)
  312. {
  313. struct kgsl_device *device = rb->device;
  314. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  315. kgsl_sharedmem_set(rb->device, &rb->buffer_desc, 0, 0xAA,
  316. (rb->sizedwords << 2));
  317. /*
  318. * The size of the ringbuffer in the hardware is the log2
  319. * representation of the size in quadwords (sizedwords / 2).
  320. * Also disable the host RPTR shadow register as it might be unreliable
  321. * in certain circumstances.
  322. */
  323. adreno_writereg(adreno_dev, ADRENO_REG_CP_RB_CNTL,
  324. (ilog2(rb->sizedwords >> 1) & 0x3F) |
  325. (1 << 27));
  326. adreno_writereg(adreno_dev, ADRENO_REG_CP_RB_BASE,
  327. rb->buffer_desc.gpuaddr);
  328. if (adreno_is_a2xx(adreno_dev)) {
  329. /* explicitly clear all cp interrupts */
  330. kgsl_regwrite(device, REG_CP_INT_ACK, 0xFFFFFFFF);
  331. }
  332. /* setup scratch/timestamp */
  333. adreno_writereg(adreno_dev, ADRENO_REG_SCRATCH_ADDR,
  334. device->memstore.gpuaddr +
  335. KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
  336. soptimestamp));
  337. adreno_writereg(adreno_dev, ADRENO_REG_SCRATCH_UMSK,
  338. GSL_RB_MEMPTRS_SCRATCH_MASK);
  339. /* CP ROQ queue sizes (bytes) - RB:16, ST:16, IB1:32, IB2:64 */
  340. if (adreno_is_a305(adreno_dev) || adreno_is_a305c(adreno_dev) ||
  341. adreno_is_a320(adreno_dev))
  342. kgsl_regwrite(device, REG_CP_QUEUE_THRESHOLDS, 0x000E0602);
  343. else if (adreno_is_a330(adreno_dev) || adreno_is_a305b(adreno_dev))
  344. kgsl_regwrite(device, REG_CP_QUEUE_THRESHOLDS, 0x003E2008);
  345. rb->wptr = 0;
  346. }
  347. /**
  348. * _ringbuffer_start_common() - Ringbuffer start
  349. * @rb: Pointer to adreno ringbuffer
  350. *
  351. * Start ringbuffer for GPU.
  352. */
  353. int _ringbuffer_start_common(struct adreno_ringbuffer *rb)
  354. {
  355. int status;
  356. struct kgsl_device *device = rb->device;
  357. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  358. /* clear ME_HALT to start micro engine */
  359. adreno_writereg(adreno_dev, ADRENO_REG_CP_ME_CNTL, 0);
  360. /* ME init is GPU specific, so jump into the sub-function */
  361. status = adreno_dev->gpudev->rb_init(adreno_dev, rb);
  362. if (status)
  363. return status;
  364. /* idle device to validate ME INIT */
  365. status = adreno_idle(device);
  366. if (status == 0)
  367. rb->flags |= KGSL_FLAGS_STARTED;
  368. return status;
  369. }
  370. /**
  371. * adreno_ringbuffer_warm_start() - Ringbuffer warm start
  372. * @rb: Pointer to adreno ringbuffer
  373. *
  374. * Start the ringbuffer but load only jump tables part of the
  375. * microcode.
  376. */
  377. int adreno_ringbuffer_warm_start(struct adreno_ringbuffer *rb)
  378. {
  379. int status;
  380. struct kgsl_device *device = rb->device;
  381. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  382. if (rb->flags & KGSL_FLAGS_STARTED)
  383. return 0;
  384. _ringbuffer_setup_common(rb);
  385. /* If bootstrapping if supported to load jump tables */
  386. if (adreno_bootstrap_ucode(adreno_dev)) {
  387. status = _ringbuffer_bootstrap_ucode(rb, 1);
  388. if (status != 0)
  389. return status;
  390. } else {
  391. /* load the CP jump tables using AHB writes */
  392. status = adreno_ringbuffer_load_pm4_ucode(device,
  393. adreno_dev->pm4_jt_idx, adreno_dev->pm4_fw_size,
  394. adreno_dev->pm4_jt_addr);
  395. if (status != 0)
  396. return status;
  397. /* load the prefetch parser jump tables using AHB writes */
  398. status = adreno_ringbuffer_load_pfp_ucode(device,
  399. adreno_dev->pfp_jt_idx, adreno_dev->pfp_fw_size,
  400. adreno_dev->pfp_jt_addr);
  401. if (status != 0)
  402. return status;
  403. }
  404. status = _ringbuffer_start_common(rb);
  405. return status;
  406. }
  407. /**
  408. * adreno_ringbuffer_cold_start() - Ringbuffer cold start
  409. * @rb: Pointer to adreno ringbuffer
  410. *
  411. * Start the ringbuffer from power collapse.
  412. */
  413. int adreno_ringbuffer_cold_start(struct adreno_ringbuffer *rb)
  414. {
  415. int status;
  416. struct kgsl_device *device = rb->device;
  417. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  418. if (rb->flags & KGSL_FLAGS_STARTED)
  419. return 0;
  420. _ringbuffer_setup_common(rb);
  421. /* If bootstrapping if supported to load ucode */
  422. if (adreno_bootstrap_ucode(adreno_dev)) {
  423. /*
  424. * load first adreno_dev->pm4_bstrp_size +
  425. * adreno_dev->pfp_bstrp_size microcode dwords using AHB write,
  426. * this small microcode has dispatcher + booter, this initial
  427. * microcode enables CP to understand CP_BOOTSTRAP_UCODE packet
  428. * in function _ringbuffer_bootstrap_ucode. CP_BOOTSTRAP_UCODE
  429. * packet loads rest of the microcode.
  430. */
  431. status = adreno_ringbuffer_load_pm4_ucode(rb->device, 1,
  432. adreno_dev->pm4_bstrp_size+1, 0);
  433. if (status != 0)
  434. return status;
  435. status = adreno_ringbuffer_load_pfp_ucode(rb->device, 1,
  436. adreno_dev->pfp_bstrp_size+1, 0);
  437. if (status != 0)
  438. return status;
  439. /* Bootstrap rest of the ucode here */
  440. status = _ringbuffer_bootstrap_ucode(rb, 0);
  441. if (status != 0)
  442. return status;
  443. } else {
  444. /* load the CP ucode using AHB writes */
  445. status = adreno_ringbuffer_load_pm4_ucode(rb->device, 1,
  446. adreno_dev->pm4_fw_size, 0);
  447. if (status != 0)
  448. return status;
  449. /* load the prefetch parser ucode using AHB writes */
  450. status = adreno_ringbuffer_load_pfp_ucode(rb->device, 1,
  451. adreno_dev->pfp_fw_size, 0);
  452. if (status != 0)
  453. return status;
  454. }
  455. status = _ringbuffer_start_common(rb);
  456. return status;
  457. }
  458. void adreno_ringbuffer_stop(struct adreno_ringbuffer *rb)
  459. {
  460. struct kgsl_device *device = rb->device;
  461. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  462. if (rb->flags & KGSL_FLAGS_STARTED) {
  463. if (adreno_is_a200(adreno_dev))
  464. kgsl_regwrite(rb->device, REG_CP_ME_CNTL, 0x10000000);
  465. rb->flags &= ~KGSL_FLAGS_STARTED;
  466. }
  467. }
  468. int adreno_ringbuffer_init(struct kgsl_device *device)
  469. {
  470. int status;
  471. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  472. struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
  473. rb->device = device;
  474. /*
  475. * It is silly to convert this to words and then back to bytes
  476. * immediately below, but most of the rest of the code deals
  477. * in words, so we might as well only do the math once
  478. */
  479. rb->sizedwords = KGSL_RB_SIZE >> 2;
  480. rb->buffer_desc.flags = KGSL_MEMFLAGS_GPUREADONLY;
  481. /* allocate memory for ringbuffer */
  482. status = kgsl_allocate_contiguous(&rb->buffer_desc,
  483. (rb->sizedwords << 2));
  484. if (status != 0) {
  485. adreno_ringbuffer_close(rb);
  486. return status;
  487. }
  488. rb->global_ts = 0;
  489. return 0;
  490. }
  491. void adreno_ringbuffer_close(struct adreno_ringbuffer *rb)
  492. {
  493. struct adreno_device *adreno_dev = ADRENO_DEVICE(rb->device);
  494. kgsl_sharedmem_free(&rb->buffer_desc);
  495. kfree(adreno_dev->pfp_fw);
  496. kfree(adreno_dev->pm4_fw);
  497. adreno_dev->pfp_fw = NULL;
  498. adreno_dev->pm4_fw = NULL;
  499. memset(rb, 0, sizeof(struct adreno_ringbuffer));
  500. }
  501. static int
  502. adreno_ringbuffer_addcmds(struct adreno_ringbuffer *rb,
  503. struct adreno_context *drawctxt,
  504. unsigned int flags, unsigned int *cmds,
  505. int sizedwords, uint32_t timestamp)
  506. {
  507. struct adreno_device *adreno_dev = ADRENO_DEVICE(rb->device);
  508. unsigned int *ringcmds;
  509. unsigned int total_sizedwords = sizedwords;
  510. unsigned int i;
  511. unsigned int rcmd_gpu;
  512. unsigned int context_id;
  513. unsigned int gpuaddr = rb->device->memstore.gpuaddr;
  514. bool profile_ready;
  515. if (drawctxt != NULL && kgsl_context_detached(&drawctxt->base))
  516. return -EINVAL;
  517. rb->global_ts++;
  518. /* If this is a internal IB, use the global timestamp for it */
  519. if (!drawctxt || (flags & KGSL_CMD_FLAGS_INTERNAL_ISSUE)) {
  520. timestamp = rb->global_ts;
  521. context_id = KGSL_MEMSTORE_GLOBAL;
  522. } else {
  523. context_id = drawctxt->base.id;
  524. }
  525. /*
  526. * Note that we cannot safely take drawctxt->mutex here without
  527. * potential mutex inversion with device->mutex which is held
  528. * here. As a result, any other code that accesses this variable
  529. * must also use device->mutex.
  530. */
  531. if (drawctxt)
  532. drawctxt->internal_timestamp = rb->global_ts;
  533. /*
  534. * If in stream ib profiling is enabled and there are counters
  535. * assigned, then space needs to be reserved for profiling. This
  536. * space in the ringbuffer is always consumed (might be filled with
  537. * NOPs in error case. profile_ready needs to be consistent through
  538. * the _addcmds call since it is allocating additional ringbuffer
  539. * command space.
  540. */
  541. profile_ready = !adreno_is_a2xx(adreno_dev) && drawctxt &&
  542. adreno_profile_assignments_ready(&adreno_dev->profile) &&
  543. !(flags & KGSL_CMD_FLAGS_INTERNAL_ISSUE);
  544. /* reserve space to temporarily turn off protected mode
  545. * error checking if needed
  546. */
  547. total_sizedwords += flags & KGSL_CMD_FLAGS_PMODE ? 4 : 0;
  548. /* 2 dwords to store the start of command sequence */
  549. total_sizedwords += 2;
  550. /* internal ib command identifier for the ringbuffer */
  551. total_sizedwords += (flags & KGSL_CMD_FLAGS_INTERNAL_ISSUE) ? 2 : 0;
  552. /* Add two dwords for the CP_INTERRUPT */
  553. total_sizedwords +=
  554. (drawctxt || (flags & KGSL_CMD_FLAGS_INTERNAL_ISSUE)) ? 2 : 0;
  555. if (adreno_is_a3xx(adreno_dev))
  556. total_sizedwords += 7;
  557. if (adreno_is_a2xx(adreno_dev))
  558. total_sizedwords += 2; /* CP_WAIT_FOR_IDLE */
  559. total_sizedwords += 3; /* sop timestamp */
  560. total_sizedwords += 4; /* eop timestamp */
  561. if (adreno_is_a20x(adreno_dev))
  562. total_sizedwords += 2; /* CACHE_FLUSH */
  563. if (drawctxt) {
  564. total_sizedwords += 3; /* global timestamp without cache
  565. * flush for non-zero context */
  566. }
  567. if (adreno_is_a20x(adreno_dev))
  568. total_sizedwords += 2; /* CACHE_FLUSH */
  569. if (flags & KGSL_CMD_FLAGS_WFI)
  570. total_sizedwords += 2; /* WFI */
  571. if (profile_ready)
  572. total_sizedwords += 6; /* space for pre_ib and post_ib */
  573. /* Add space for the power on shader fixup if we need it */
  574. if (flags & KGSL_CMD_FLAGS_PWRON_FIXUP)
  575. total_sizedwords += 9;
  576. ringcmds = adreno_ringbuffer_allocspace(rb, drawctxt, total_sizedwords);
  577. if (IS_ERR(ringcmds))
  578. return PTR_ERR(ringcmds);
  579. if (ringcmds == NULL)
  580. return -ENOSPC;
  581. rcmd_gpu = rb->buffer_desc.gpuaddr
  582. + sizeof(uint)*(rb->wptr-total_sizedwords);
  583. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu, cp_nop_packet(1));
  584. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu, KGSL_CMD_IDENTIFIER);
  585. if (flags & KGSL_CMD_FLAGS_INTERNAL_ISSUE) {
  586. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu, cp_nop_packet(1));
  587. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu,
  588. KGSL_CMD_INTERNAL_IDENTIFIER);
  589. }
  590. if (flags & KGSL_CMD_FLAGS_PWRON_FIXUP) {
  591. /* Disable protected mode for the fixup */
  592. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu,
  593. cp_type3_packet(CP_SET_PROTECTED_MODE, 1));
  594. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu, 0);
  595. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu, cp_nop_packet(1));
  596. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu,
  597. KGSL_PWRON_FIXUP_IDENTIFIER);
  598. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu,
  599. CP_HDR_INDIRECT_BUFFER_PFD);
  600. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu,
  601. adreno_dev->pwron_fixup.gpuaddr);
  602. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu,
  603. adreno_dev->pwron_fixup_dwords);
  604. /* Re-enable protected mode */
  605. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu,
  606. cp_type3_packet(CP_SET_PROTECTED_MODE, 1));
  607. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu, 1);
  608. }
  609. /* Add any IB required for profiling if it is enabled */
  610. if (profile_ready)
  611. adreno_profile_preib_processing(rb->device, drawctxt->base.id,
  612. &flags, &ringcmds, &rcmd_gpu);
  613. /* start-of-pipeline timestamp */
  614. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu,
  615. cp_type3_packet(CP_MEM_WRITE, 2));
  616. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu, (gpuaddr +
  617. KGSL_MEMSTORE_OFFSET(context_id, soptimestamp)));
  618. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu, timestamp);
  619. if (flags & KGSL_CMD_FLAGS_PMODE) {
  620. /* disable protected mode error checking */
  621. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu,
  622. cp_type3_packet(CP_SET_PROTECTED_MODE, 1));
  623. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu, 0);
  624. }
  625. for (i = 0; i < sizedwords; i++) {
  626. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu, *cmds);
  627. cmds++;
  628. }
  629. if (flags & KGSL_CMD_FLAGS_PMODE) {
  630. /* re-enable protected mode error checking */
  631. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu,
  632. cp_type3_packet(CP_SET_PROTECTED_MODE, 1));
  633. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu, 1);
  634. }
  635. /* HW Workaround for MMU Page fault
  636. * due to memory getting free early before
  637. * GPU completes it.
  638. */
  639. if (adreno_is_a2xx(adreno_dev)) {
  640. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu,
  641. cp_type3_packet(CP_WAIT_FOR_IDLE, 1));
  642. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu, 0x00);
  643. }
  644. if (adreno_is_a3xx(adreno_dev)) {
  645. /*
  646. * Flush HLSQ lazy updates to make sure there are no
  647. * resources pending for indirect loads after the timestamp
  648. */
  649. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu,
  650. cp_type3_packet(CP_EVENT_WRITE, 1));
  651. GSL_RB_WRITE(rb->device, ringcmds,
  652. rcmd_gpu, 0x07); /* HLSQ_FLUSH */
  653. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu,
  654. cp_type3_packet(CP_WAIT_FOR_IDLE, 1));
  655. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu, 0x00);
  656. }
  657. /* Add any postIB required for profiling if it is enabled and has
  658. assigned counters */
  659. if (profile_ready)
  660. adreno_profile_postib_processing(rb->device, &flags,
  661. &ringcmds, &rcmd_gpu);
  662. /*
  663. * end-of-pipeline timestamp. If per context timestamps is not
  664. * enabled, then context_id will be KGSL_MEMSTORE_GLOBAL so all
  665. * eop timestamps will work out.
  666. */
  667. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu,
  668. cp_type3_packet(CP_EVENT_WRITE, 3));
  669. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu, CACHE_FLUSH_TS);
  670. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu, (gpuaddr +
  671. KGSL_MEMSTORE_OFFSET(context_id, eoptimestamp)));
  672. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu, timestamp);
  673. if (drawctxt) {
  674. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu,
  675. cp_type3_packet(CP_MEM_WRITE, 2));
  676. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu, (gpuaddr +
  677. KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
  678. eoptimestamp)));
  679. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu,
  680. rb->global_ts);
  681. }
  682. if (adreno_is_a20x(adreno_dev)) {
  683. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu,
  684. cp_type3_packet(CP_EVENT_WRITE, 1));
  685. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu, CACHE_FLUSH);
  686. }
  687. if (drawctxt || (flags & KGSL_CMD_FLAGS_INTERNAL_ISSUE)) {
  688. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu,
  689. cp_type3_packet(CP_INTERRUPT, 1));
  690. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu,
  691. CP_INT_CNTL__RB_INT_MASK);
  692. }
  693. if (adreno_is_a3xx(adreno_dev)) {
  694. /* Dummy set-constant to trigger context rollover */
  695. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu,
  696. cp_type3_packet(CP_SET_CONSTANT, 2));
  697. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu,
  698. (0x4<<16)|(A3XX_HLSQ_CL_KERNEL_GROUP_X_REG - 0x2000));
  699. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu, 0);
  700. }
  701. if (flags & KGSL_CMD_FLAGS_WFI) {
  702. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu,
  703. cp_type3_packet(CP_WAIT_FOR_IDLE, 1));
  704. GSL_RB_WRITE(rb->device, ringcmds, rcmd_gpu, 0x00000000);
  705. }
  706. adreno_ringbuffer_submit(rb);
  707. return 0;
  708. }
  709. unsigned int
  710. adreno_ringbuffer_issuecmds(struct kgsl_device *device,
  711. struct adreno_context *drawctxt,
  712. unsigned int flags,
  713. unsigned int *cmds,
  714. int sizedwords)
  715. {
  716. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  717. struct adreno_ringbuffer *rb = &adreno_dev->ringbuffer;
  718. flags |= KGSL_CMD_FLAGS_INTERNAL_ISSUE;
  719. return adreno_ringbuffer_addcmds(rb, drawctxt, flags, cmds,
  720. sizedwords, 0);
  721. }
  722. static bool _parse_ibs(struct kgsl_device_private *dev_priv, uint gpuaddr,
  723. int sizedwords);
  724. static bool
  725. _handle_type3(struct kgsl_device_private *dev_priv, uint *hostaddr)
  726. {
  727. unsigned int opcode = cp_type3_opcode(*hostaddr);
  728. switch (opcode) {
  729. case CP_INDIRECT_BUFFER_PFD:
  730. case CP_INDIRECT_BUFFER_PFE:
  731. case CP_COND_INDIRECT_BUFFER_PFE:
  732. case CP_COND_INDIRECT_BUFFER_PFD:
  733. return _parse_ibs(dev_priv, hostaddr[1], hostaddr[2]);
  734. case CP_NOP:
  735. case CP_WAIT_FOR_IDLE:
  736. case CP_WAIT_REG_MEM:
  737. case CP_WAIT_REG_EQ:
  738. case CP_WAT_REG_GTE:
  739. case CP_WAIT_UNTIL_READ:
  740. case CP_WAIT_IB_PFD_COMPLETE:
  741. case CP_REG_RMW:
  742. case CP_REG_TO_MEM:
  743. case CP_MEM_WRITE:
  744. case CP_MEM_WRITE_CNTR:
  745. case CP_COND_EXEC:
  746. case CP_COND_WRITE:
  747. case CP_EVENT_WRITE:
  748. case CP_EVENT_WRITE_SHD:
  749. case CP_EVENT_WRITE_CFL:
  750. case CP_EVENT_WRITE_ZPD:
  751. case CP_DRAW_INDX:
  752. case CP_DRAW_INDX_2:
  753. case CP_DRAW_INDX_BIN:
  754. case CP_DRAW_INDX_2_BIN:
  755. case CP_VIZ_QUERY:
  756. case CP_SET_STATE:
  757. case CP_SET_CONSTANT:
  758. case CP_IM_LOAD:
  759. case CP_IM_LOAD_IMMEDIATE:
  760. case CP_LOAD_CONSTANT_CONTEXT:
  761. case CP_INVALIDATE_STATE:
  762. case CP_SET_SHADER_BASES:
  763. case CP_SET_BIN_MASK:
  764. case CP_SET_BIN_SELECT:
  765. case CP_SET_BIN_BASE_OFFSET:
  766. case CP_SET_BIN_DATA:
  767. case CP_CONTEXT_UPDATE:
  768. case CP_INTERRUPT:
  769. case CP_IM_STORE:
  770. case CP_LOAD_STATE:
  771. break;
  772. /* these shouldn't come from userspace */
  773. case CP_ME_INIT:
  774. case CP_SET_PROTECTED_MODE:
  775. default:
  776. KGSL_CMD_ERR(dev_priv->device, "bad CP opcode %0x\n", opcode);
  777. return false;
  778. break;
  779. }
  780. return true;
  781. }
  782. static bool
  783. _handle_type0(struct kgsl_device_private *dev_priv, uint *hostaddr)
  784. {
  785. unsigned int reg = type0_pkt_offset(*hostaddr);
  786. unsigned int cnt = type0_pkt_size(*hostaddr);
  787. if (reg < 0x0192 || (reg + cnt) >= 0x8000) {
  788. KGSL_CMD_ERR(dev_priv->device, "bad type0 reg: 0x%0x cnt: %d\n",
  789. reg, cnt);
  790. return false;
  791. }
  792. return true;
  793. }
  794. /*
  795. * Traverse IBs and dump them to test vector. Detect swap by inspecting
  796. * register writes, keeping note of the current state, and dump
  797. * framebuffer config to test vector
  798. */
  799. static bool _parse_ibs(struct kgsl_device_private *dev_priv,
  800. uint gpuaddr, int sizedwords)
  801. {
  802. static uint level; /* recursion level */
  803. bool ret = false;
  804. uint *hostaddr, *hoststart;
  805. int dwords_left = sizedwords; /* dwords left in the current command
  806. buffer */
  807. struct kgsl_mem_entry *entry;
  808. entry = kgsl_sharedmem_find_region(dev_priv->process_priv,
  809. gpuaddr, sizedwords * sizeof(uint));
  810. if (entry == NULL) {
  811. KGSL_CMD_ERR(dev_priv->device,
  812. "no mapping for gpuaddr: 0x%08x\n", gpuaddr);
  813. return false;
  814. }
  815. hostaddr = (uint *)kgsl_gpuaddr_to_vaddr(&entry->memdesc, gpuaddr);
  816. if (hostaddr == NULL) {
  817. KGSL_CMD_ERR(dev_priv->device,
  818. "no mapping for gpuaddr: 0x%08x\n", gpuaddr);
  819. return false;
  820. }
  821. hoststart = hostaddr;
  822. level++;
  823. KGSL_CMD_INFO(dev_priv->device, "ib: gpuaddr:0x%08x, wc:%d, hptr:%pK\n",
  824. gpuaddr, sizedwords, hostaddr);
  825. mb();
  826. while (dwords_left > 0) {
  827. bool cur_ret = true;
  828. int count = 0; /* dword count including packet header */
  829. switch (*hostaddr >> 30) {
  830. case 0x0: /* type-0 */
  831. count = (*hostaddr >> 16)+2;
  832. cur_ret = _handle_type0(dev_priv, hostaddr);
  833. break;
  834. case 0x1: /* type-1 */
  835. count = 2;
  836. break;
  837. case 0x3: /* type-3 */
  838. count = ((*hostaddr >> 16) & 0x3fff) + 2;
  839. cur_ret = _handle_type3(dev_priv, hostaddr);
  840. break;
  841. default:
  842. KGSL_CMD_ERR(dev_priv->device, "unexpected type: "
  843. "type:%d, word:0x%08x @ 0x%pK, gpu:0x%08x\n",
  844. *hostaddr >> 30, *hostaddr, hostaddr,
  845. gpuaddr+4*(sizedwords-dwords_left));
  846. cur_ret = false;
  847. count = dwords_left;
  848. break;
  849. }
  850. if (!cur_ret) {
  851. KGSL_CMD_ERR(dev_priv->device,
  852. "bad sub-type: #:%d/%d, v:0x%08x"
  853. " @ 0x%pK[gb:0x%08x], level:%d\n",
  854. sizedwords-dwords_left, sizedwords, *hostaddr,
  855. hostaddr, gpuaddr+4*(sizedwords-dwords_left),
  856. level);
  857. if (ADRENO_DEVICE(dev_priv->device)->ib_check_level
  858. >= 2)
  859. print_hex_dump(KERN_ERR,
  860. level == 1 ? "IB1:" : "IB2:",
  861. DUMP_PREFIX_OFFSET, 32, 4, hoststart,
  862. sizedwords*4, 0);
  863. goto done;
  864. }
  865. /* jump to next packet */
  866. dwords_left -= count;
  867. hostaddr += count;
  868. if (dwords_left < 0) {
  869. KGSL_CMD_ERR(dev_priv->device,
  870. "bad count: c:%d, #:%d/%d, "
  871. "v:0x%08x @ 0x%pK[gb:0x%08x], level:%d\n",
  872. count, sizedwords-(dwords_left+count),
  873. sizedwords, *(hostaddr-count), hostaddr-count,
  874. gpuaddr+4*(sizedwords-(dwords_left+count)),
  875. level);
  876. if (ADRENO_DEVICE(dev_priv->device)->ib_check_level
  877. >= 2)
  878. print_hex_dump(KERN_ERR,
  879. level == 1 ? "IB1:" : "IB2:",
  880. DUMP_PREFIX_OFFSET, 32, 4, hoststart,
  881. sizedwords*4, 0);
  882. goto done;
  883. }
  884. }
  885. ret = true;
  886. done:
  887. if (!ret)
  888. KGSL_DRV_ERR(dev_priv->device,
  889. "parsing failed: gpuaddr:0x%08x, "
  890. "host:0x%pK, wc:%d\n", gpuaddr, hoststart, sizedwords);
  891. level--;
  892. return ret;
  893. }
  894. /**
  895. * _ringbuffer_verify_ib() - parse an IB and verify that it is correct
  896. * @dev_priv: Pointer to the process struct
  897. * @ibdesc: Pointer to the IB descriptor
  898. *
  899. * This function only gets called if debugging is enabled - it walks the IB and
  900. * does additional level parsing and verification above and beyond what KGSL
  901. * core does
  902. */
  903. static inline bool _ringbuffer_verify_ib(struct kgsl_device_private *dev_priv,
  904. struct kgsl_memobj_node *ib)
  905. {
  906. struct kgsl_device *device = dev_priv->device;
  907. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  908. /* Check that the size of the IBs is under the allowable limit */
  909. if (ib->sizedwords == 0 || ib->sizedwords > 0xFFFFF) {
  910. KGSL_DRV_ERR(device, "Invalid IB size 0x%X\n",
  911. ib->sizedwords);
  912. return false;
  913. }
  914. if (unlikely(adreno_dev->ib_check_level >= 1) &&
  915. !_parse_ibs(dev_priv, ib->gpuaddr, ib->sizedwords)) {
  916. KGSL_DRV_ERR(device, "Could not verify the IBs\n");
  917. return false;
  918. }
  919. return true;
  920. }
  921. int
  922. adreno_ringbuffer_issueibcmds(struct kgsl_device_private *dev_priv,
  923. struct kgsl_context *context,
  924. struct kgsl_cmdbatch *cmdbatch,
  925. uint32_t *timestamp)
  926. {
  927. struct kgsl_device *device = dev_priv->device;
  928. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  929. struct adreno_context *drawctxt = ADRENO_CONTEXT(context);
  930. struct kgsl_memobj_node *ib;
  931. int ret;
  932. if (drawctxt->state == ADRENO_CONTEXT_STATE_INVALID)
  933. return -EDEADLK;
  934. /* Verify the IBs before they get queued */
  935. list_for_each_entry(ib, &cmdbatch->cmdlist, node)
  936. if (!_ringbuffer_verify_ib(dev_priv, ib))
  937. return -EINVAL;
  938. /* wait for the suspend gate */
  939. wait_for_completion(&device->cmdbatch_gate);
  940. /*
  941. * Clear the wake on touch bit to indicate an IB has been submitted
  942. * since the last time we set it
  943. */
  944. device->flags &= ~KGSL_FLAG_WAKE_ON_TOUCH;
  945. /* Queue the command in the ringbuffer */
  946. ret = adreno_dispatcher_queue_cmd(adreno_dev, drawctxt, cmdbatch,
  947. timestamp);
  948. if (ret)
  949. KGSL_DRV_ERR(device,
  950. "adreno_dispatcher_queue_cmd returned %d\n", ret);
  951. /*
  952. * Return -EPROTO if the device has faulted since the last time we
  953. * checked - userspace uses this to perform post-fault activities
  954. */
  955. if (!ret && test_and_clear_bit(ADRENO_CONTEXT_FAULT, &drawctxt->priv))
  956. ret = -EPROTO;
  957. return ret;
  958. }
  959. unsigned int adreno_ringbuffer_get_constraint(struct kgsl_device *device,
  960. struct kgsl_context *context)
  961. {
  962. unsigned int pwrlevel = device->pwrctrl.active_pwrlevel;
  963. switch (context->pwr_constraint.type) {
  964. case KGSL_CONSTRAINT_PWRLEVEL: {
  965. switch (context->pwr_constraint.sub_type) {
  966. case KGSL_CONSTRAINT_PWR_MAX:
  967. pwrlevel = device->pwrctrl.max_pwrlevel;
  968. break;
  969. case KGSL_CONSTRAINT_PWR_MIN:
  970. pwrlevel = device->pwrctrl.min_pwrlevel;
  971. break;
  972. default:
  973. break;
  974. }
  975. }
  976. break;
  977. }
  978. return pwrlevel;
  979. }
  980. void adreno_ringbuffer_set_constraint(struct kgsl_device *device,
  981. struct kgsl_cmdbatch *cmdbatch)
  982. {
  983. unsigned int constraint;
  984. struct kgsl_context *context = cmdbatch->context;
  985. /*
  986. * Check if the context has a constraint and constraint flags are
  987. * set.
  988. */
  989. if (context->pwr_constraint.type &&
  990. ((context->flags & KGSL_CONTEXT_PWR_CONSTRAINT) ||
  991. (cmdbatch->flags & KGSL_CMDBATCH_PWR_CONSTRAINT))) {
  992. constraint = adreno_ringbuffer_get_constraint(device, context);
  993. /*
  994. * If a constraint is already set, set a new
  995. * constraint only if it is faster
  996. */
  997. if ((device->pwrctrl.constraint.type ==
  998. KGSL_CONSTRAINT_NONE) || (constraint <
  999. device->pwrctrl.constraint.hint.pwrlevel.level)) {
  1000. kgsl_pwrctrl_pwrlevel_change(device, constraint);
  1001. device->pwrctrl.constraint.type =
  1002. context->pwr_constraint.type;
  1003. device->pwrctrl.constraint.hint.
  1004. pwrlevel.level = constraint;
  1005. }
  1006. device->pwrctrl.constraint.expires = jiffies +
  1007. device->pwrctrl.interval_timeout;
  1008. }
  1009. }
  1010. /* adreno_rindbuffer_submitcmd - submit userspace IBs to the GPU */
  1011. int adreno_ringbuffer_submitcmd(struct adreno_device *adreno_dev,
  1012. struct kgsl_cmdbatch *cmdbatch)
  1013. {
  1014. struct kgsl_device *device = &adreno_dev->dev;
  1015. struct kgsl_memobj_node *ib;
  1016. unsigned int numibs = 0;
  1017. unsigned int *link;
  1018. unsigned int *cmds;
  1019. struct kgsl_context *context;
  1020. struct adreno_context *drawctxt;
  1021. bool use_preamble = true;
  1022. int flags = KGSL_CMD_FLAGS_NONE;
  1023. int ret;
  1024. context = cmdbatch->context;
  1025. drawctxt = ADRENO_CONTEXT(context);
  1026. /* Get the total IBs in the list */
  1027. list_for_each_entry(ib, &cmdbatch->cmdlist, node)
  1028. numibs++;
  1029. /* process any profiling results that are available into the log_buf */
  1030. adreno_profile_process_results(device);
  1031. /*
  1032. * If SKIP CMD flag is set for current context
  1033. * a) set SKIPCMD as fault_recovery for current commandbatch
  1034. * b) store context's commandbatch fault_policy in current
  1035. * commandbatch fault_policy and clear context's commandbatch
  1036. * fault_policy
  1037. * c) force preamble for commandbatch
  1038. */
  1039. if (test_bit(ADRENO_CONTEXT_SKIP_CMD, &drawctxt->priv) &&
  1040. (!test_bit(CMDBATCH_FLAG_SKIP, &cmdbatch->priv))) {
  1041. set_bit(KGSL_FT_SKIPCMD, &cmdbatch->fault_recovery);
  1042. cmdbatch->fault_policy = drawctxt->fault_policy;
  1043. set_bit(CMDBATCH_FLAG_FORCE_PREAMBLE, &cmdbatch->priv);
  1044. /* if context is detached print fault recovery */
  1045. adreno_fault_skipcmd_detached(device, drawctxt, cmdbatch);
  1046. /* clear the drawctxt flags */
  1047. clear_bit(ADRENO_CONTEXT_SKIP_CMD, &drawctxt->priv);
  1048. drawctxt->fault_policy = 0;
  1049. }
  1050. /*When preamble is enabled, the preamble buffer with state restoration
  1051. commands are stored in the first node of the IB chain. We can skip that
  1052. if a context switch hasn't occured */
  1053. if ((drawctxt->base.flags & KGSL_CONTEXT_PREAMBLE) &&
  1054. !test_bit(CMDBATCH_FLAG_FORCE_PREAMBLE, &cmdbatch->priv) &&
  1055. (adreno_dev->drawctxt_active == drawctxt))
  1056. use_preamble = false;
  1057. /*
  1058. * In skip mode don't issue the draw IBs but keep all the other
  1059. * accoutrements of a submision (including the interrupt) to keep
  1060. * the accounting sane. Set start_index and numibs to 0 to just
  1061. * generate the start and end markers and skip everything else
  1062. */
  1063. if (test_bit(CMDBATCH_FLAG_SKIP, &cmdbatch->priv)) {
  1064. use_preamble = false;
  1065. numibs = 0;
  1066. }
  1067. /*
  1068. * Worst case size:
  1069. * 2 - start of IB identifier
  1070. * 1 - skip preamble
  1071. * 3 * numibs - 3 per IB
  1072. * 2 - end of IB identifier
  1073. */
  1074. cmds = link = kzalloc(sizeof(unsigned int) * (numibs * 3 + 5),
  1075. GFP_KERNEL);
  1076. if (!link) {
  1077. ret = -ENOMEM;
  1078. goto done;
  1079. }
  1080. *cmds++ = cp_nop_packet(1);
  1081. *cmds++ = KGSL_START_OF_IB_IDENTIFIER;
  1082. if (numibs) {
  1083. list_for_each_entry(ib, &cmdbatch->cmdlist, node) {
  1084. /* use the preamble? */
  1085. if ((ib->priv & MEMOBJ_PREAMBLE) &&
  1086. (use_preamble == false))
  1087. *cmds++ = cp_nop_packet(3);
  1088. /*
  1089. * Skip 0 sized IBs - these are presumed to have been
  1090. * removed from consideration by the FT policy
  1091. */
  1092. if (ib->priv & MEMOBJ_SKIP)
  1093. *cmds++ = cp_nop_packet(2);
  1094. else
  1095. *cmds++ = CP_HDR_INDIRECT_BUFFER_PFD;
  1096. *cmds++ = ib->gpuaddr;
  1097. *cmds++ = ib->sizedwords;
  1098. }
  1099. }
  1100. *cmds++ = cp_nop_packet(1);
  1101. *cmds++ = KGSL_END_OF_IB_IDENTIFIER;
  1102. ret = kgsl_setstate(&device->mmu, context->id,
  1103. kgsl_mmu_pt_get_flags(device->mmu.hwpagetable,
  1104. device->id));
  1105. if (ret)
  1106. goto done;
  1107. ret = adreno_drawctxt_switch(adreno_dev, drawctxt, cmdbatch->flags);
  1108. /*
  1109. * In the unlikely event of an error in the drawctxt switch,
  1110. * treat it like a hang
  1111. */
  1112. if (ret)
  1113. goto done;
  1114. if (test_bit(CMDBATCH_FLAG_WFI, &cmdbatch->priv))
  1115. flags = KGSL_CMD_FLAGS_WFI;
  1116. /*
  1117. * For some targets, we need to execute a dummy shader operation after a
  1118. * power collapse
  1119. */
  1120. if (test_and_clear_bit(ADRENO_DEVICE_PWRON, &adreno_dev->priv) &&
  1121. test_bit(ADRENO_DEVICE_PWRON_FIXUP, &adreno_dev->priv))
  1122. flags |= KGSL_CMD_FLAGS_PWRON_FIXUP;
  1123. /* Set the constraints before adding to ringbuffer */
  1124. adreno_ringbuffer_set_constraint(device, cmdbatch);
  1125. /* CFF stuff executed only if CFF is enabled */
  1126. kgsl_cffdump_capture_ib_desc(device, context, cmdbatch);
  1127. ret = adreno_ringbuffer_addcmds(&adreno_dev->ringbuffer,
  1128. drawctxt,
  1129. flags,
  1130. &link[0], (cmds - link),
  1131. cmdbatch->timestamp);
  1132. #ifdef CONFIG_MSM_KGSL_CFF_DUMP
  1133. if (ret)
  1134. goto done;
  1135. /*
  1136. * insert wait for idle after every IB1
  1137. * this is conservative but works reliably and is ok
  1138. * even for performance simulations
  1139. */
  1140. ret = adreno_idle(device);
  1141. #endif
  1142. done:
  1143. device->pwrctrl.irq_last = 0;
  1144. kgsl_trace_issueibcmds(device, context->id, cmdbatch,
  1145. numibs, cmdbatch->timestamp, cmdbatch->flags, ret,
  1146. drawctxt->type);
  1147. kfree(link);
  1148. return ret;
  1149. }