msm_gpu.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "msm_gpu.h"
  18. #include "msm_gem.h"
  19. #include "msm_mmu.h"
  20. #include "msm_fence.h"
  21. #include <generated/utsrelease.h>
  22. #include <linux/string_helpers.h>
  23. #include <linux/pm_opp.h>
  24. #include <linux/devfreq.h>
  25. #include <linux/devcoredump.h>
  26. /*
  27. * Power Management:
  28. */
  29. static int msm_devfreq_target(struct device *dev, unsigned long *freq,
  30. u32 flags)
  31. {
  32. struct msm_gpu *gpu = platform_get_drvdata(to_platform_device(dev));
  33. struct dev_pm_opp *opp;
  34. opp = devfreq_recommended_opp(dev, freq, flags);
  35. if (IS_ERR(opp))
  36. return PTR_ERR(opp);
  37. if (gpu->funcs->gpu_set_freq)
  38. gpu->funcs->gpu_set_freq(gpu, (u64)*freq);
  39. else
  40. clk_set_rate(gpu->core_clk, *freq);
  41. dev_pm_opp_put(opp);
  42. return 0;
  43. }
  44. static int msm_devfreq_get_dev_status(struct device *dev,
  45. struct devfreq_dev_status *status)
  46. {
  47. struct msm_gpu *gpu = platform_get_drvdata(to_platform_device(dev));
  48. ktime_t time;
  49. if (gpu->funcs->gpu_get_freq)
  50. status->current_frequency = gpu->funcs->gpu_get_freq(gpu);
  51. else
  52. status->current_frequency = clk_get_rate(gpu->core_clk);
  53. status->busy_time = gpu->funcs->gpu_busy(gpu);
  54. time = ktime_get();
  55. status->total_time = ktime_us_delta(time, gpu->devfreq.time);
  56. gpu->devfreq.time = time;
  57. return 0;
  58. }
  59. static int msm_devfreq_get_cur_freq(struct device *dev, unsigned long *freq)
  60. {
  61. struct msm_gpu *gpu = platform_get_drvdata(to_platform_device(dev));
  62. if (gpu->funcs->gpu_get_freq)
  63. *freq = gpu->funcs->gpu_get_freq(gpu);
  64. else
  65. *freq = clk_get_rate(gpu->core_clk);
  66. return 0;
  67. }
  68. static struct devfreq_dev_profile msm_devfreq_profile = {
  69. .polling_ms = 10,
  70. .target = msm_devfreq_target,
  71. .get_dev_status = msm_devfreq_get_dev_status,
  72. .get_cur_freq = msm_devfreq_get_cur_freq,
  73. };
  74. static void msm_devfreq_init(struct msm_gpu *gpu)
  75. {
  76. /* We need target support to do devfreq */
  77. if (!gpu->funcs->gpu_busy)
  78. return;
  79. msm_devfreq_profile.initial_freq = gpu->fast_rate;
  80. /*
  81. * Don't set the freq_table or max_state and let devfreq build the table
  82. * from OPP
  83. */
  84. gpu->devfreq.devfreq = devm_devfreq_add_device(&gpu->pdev->dev,
  85. &msm_devfreq_profile, "simple_ondemand", NULL);
  86. if (IS_ERR(gpu->devfreq.devfreq)) {
  87. dev_err(&gpu->pdev->dev, "Couldn't initialize GPU devfreq\n");
  88. gpu->devfreq.devfreq = NULL;
  89. }
  90. devfreq_suspend_device(gpu->devfreq.devfreq);
  91. }
  92. static int enable_pwrrail(struct msm_gpu *gpu)
  93. {
  94. struct drm_device *dev = gpu->dev;
  95. int ret = 0;
  96. if (gpu->gpu_reg) {
  97. ret = regulator_enable(gpu->gpu_reg);
  98. if (ret) {
  99. dev_err(dev->dev, "failed to enable 'gpu_reg': %d\n", ret);
  100. return ret;
  101. }
  102. }
  103. if (gpu->gpu_cx) {
  104. ret = regulator_enable(gpu->gpu_cx);
  105. if (ret) {
  106. dev_err(dev->dev, "failed to enable 'gpu_cx': %d\n", ret);
  107. return ret;
  108. }
  109. }
  110. return 0;
  111. }
  112. static int disable_pwrrail(struct msm_gpu *gpu)
  113. {
  114. if (gpu->gpu_cx)
  115. regulator_disable(gpu->gpu_cx);
  116. if (gpu->gpu_reg)
  117. regulator_disable(gpu->gpu_reg);
  118. return 0;
  119. }
  120. static int enable_clk(struct msm_gpu *gpu)
  121. {
  122. if (gpu->core_clk && gpu->fast_rate)
  123. clk_set_rate(gpu->core_clk, gpu->fast_rate);
  124. /* Set the RBBM timer rate to 19.2Mhz */
  125. if (gpu->rbbmtimer_clk)
  126. clk_set_rate(gpu->rbbmtimer_clk, 19200000);
  127. return clk_bulk_prepare_enable(gpu->nr_clocks, gpu->grp_clks);
  128. }
  129. static int disable_clk(struct msm_gpu *gpu)
  130. {
  131. clk_bulk_disable_unprepare(gpu->nr_clocks, gpu->grp_clks);
  132. /*
  133. * Set the clock to a deliberately low rate. On older targets the clock
  134. * speed had to be non zero to avoid problems. On newer targets this
  135. * will be rounded down to zero anyway so it all works out.
  136. */
  137. if (gpu->core_clk)
  138. clk_set_rate(gpu->core_clk, 27000000);
  139. if (gpu->rbbmtimer_clk)
  140. clk_set_rate(gpu->rbbmtimer_clk, 0);
  141. return 0;
  142. }
  143. static int enable_axi(struct msm_gpu *gpu)
  144. {
  145. if (gpu->ebi1_clk)
  146. clk_prepare_enable(gpu->ebi1_clk);
  147. return 0;
  148. }
  149. static int disable_axi(struct msm_gpu *gpu)
  150. {
  151. if (gpu->ebi1_clk)
  152. clk_disable_unprepare(gpu->ebi1_clk);
  153. return 0;
  154. }
  155. void msm_gpu_resume_devfreq(struct msm_gpu *gpu)
  156. {
  157. gpu->devfreq.busy_cycles = 0;
  158. gpu->devfreq.time = ktime_get();
  159. devfreq_resume_device(gpu->devfreq.devfreq);
  160. }
  161. int msm_gpu_pm_resume(struct msm_gpu *gpu)
  162. {
  163. int ret;
  164. DBG("%s", gpu->name);
  165. ret = enable_pwrrail(gpu);
  166. if (ret)
  167. return ret;
  168. ret = enable_clk(gpu);
  169. if (ret)
  170. return ret;
  171. ret = enable_axi(gpu);
  172. if (ret)
  173. return ret;
  174. msm_gpu_resume_devfreq(gpu);
  175. gpu->needs_hw_init = true;
  176. return 0;
  177. }
  178. int msm_gpu_pm_suspend(struct msm_gpu *gpu)
  179. {
  180. int ret;
  181. DBG("%s", gpu->name);
  182. devfreq_suspend_device(gpu->devfreq.devfreq);
  183. ret = disable_axi(gpu);
  184. if (ret)
  185. return ret;
  186. ret = disable_clk(gpu);
  187. if (ret)
  188. return ret;
  189. ret = disable_pwrrail(gpu);
  190. if (ret)
  191. return ret;
  192. return 0;
  193. }
  194. int msm_gpu_hw_init(struct msm_gpu *gpu)
  195. {
  196. int ret;
  197. WARN_ON(!mutex_is_locked(&gpu->dev->struct_mutex));
  198. if (!gpu->needs_hw_init)
  199. return 0;
  200. disable_irq(gpu->irq);
  201. ret = gpu->funcs->hw_init(gpu);
  202. if (!ret)
  203. gpu->needs_hw_init = false;
  204. enable_irq(gpu->irq);
  205. return ret;
  206. }
  207. #ifdef CONFIG_DEV_COREDUMP
  208. static ssize_t msm_gpu_devcoredump_read(char *buffer, loff_t offset,
  209. size_t count, void *data, size_t datalen)
  210. {
  211. struct msm_gpu *gpu = data;
  212. struct drm_print_iterator iter;
  213. struct drm_printer p;
  214. struct msm_gpu_state *state;
  215. state = msm_gpu_crashstate_get(gpu);
  216. if (!state)
  217. return 0;
  218. iter.data = buffer;
  219. iter.offset = 0;
  220. iter.start = offset;
  221. iter.remain = count;
  222. p = drm_coredump_printer(&iter);
  223. drm_printf(&p, "---\n");
  224. drm_printf(&p, "kernel: " UTS_RELEASE "\n");
  225. drm_printf(&p, "module: " KBUILD_MODNAME "\n");
  226. drm_printf(&p, "time: %lld.%09ld\n",
  227. state->time.tv_sec, state->time.tv_nsec);
  228. if (state->comm)
  229. drm_printf(&p, "comm: %s\n", state->comm);
  230. if (state->cmd)
  231. drm_printf(&p, "cmdline: %s\n", state->cmd);
  232. gpu->funcs->show(gpu, state, &p);
  233. msm_gpu_crashstate_put(gpu);
  234. return count - iter.remain;
  235. }
  236. static void msm_gpu_devcoredump_free(void *data)
  237. {
  238. struct msm_gpu *gpu = data;
  239. msm_gpu_crashstate_put(gpu);
  240. }
  241. static void msm_gpu_crashstate_get_bo(struct msm_gpu_state *state,
  242. struct msm_gem_object *obj, u64 iova, u32 flags)
  243. {
  244. struct msm_gpu_state_bo *state_bo = &state->bos[state->nr_bos];
  245. /* Don't record write only objects */
  246. state_bo->size = obj->base.size;
  247. state_bo->iova = iova;
  248. /* Only store the data for buffer objects marked for read */
  249. if ((flags & MSM_SUBMIT_BO_READ)) {
  250. void *ptr;
  251. state_bo->data = kvmalloc(obj->base.size, GFP_KERNEL);
  252. if (!state_bo->data)
  253. return;
  254. ptr = msm_gem_get_vaddr_active(&obj->base);
  255. if (IS_ERR(ptr)) {
  256. kvfree(state_bo->data);
  257. return;
  258. }
  259. memcpy(state_bo->data, ptr, obj->base.size);
  260. msm_gem_put_vaddr(&obj->base);
  261. }
  262. state->nr_bos++;
  263. }
  264. static void msm_gpu_crashstate_capture(struct msm_gpu *gpu,
  265. struct msm_gem_submit *submit, char *comm, char *cmd)
  266. {
  267. struct msm_gpu_state *state;
  268. /* Check if the target supports capturing crash state */
  269. if (!gpu->funcs->gpu_state_get)
  270. return;
  271. /* Only save one crash state at a time */
  272. if (gpu->crashstate)
  273. return;
  274. state = gpu->funcs->gpu_state_get(gpu);
  275. if (IS_ERR_OR_NULL(state))
  276. return;
  277. /* Fill in the additional crash state information */
  278. state->comm = kstrdup(comm, GFP_KERNEL);
  279. state->cmd = kstrdup(cmd, GFP_KERNEL);
  280. if (submit) {
  281. int i;
  282. state->bos = kcalloc(submit->nr_bos,
  283. sizeof(struct msm_gpu_state_bo), GFP_KERNEL);
  284. for (i = 0; state->bos && i < submit->nr_bos; i++)
  285. msm_gpu_crashstate_get_bo(state, submit->bos[i].obj,
  286. submit->bos[i].iova, submit->bos[i].flags);
  287. }
  288. /* Set the active crash state to be dumped on failure */
  289. gpu->crashstate = state;
  290. /* FIXME: Release the crashstate if this errors out? */
  291. dev_coredumpm(gpu->dev->dev, THIS_MODULE, gpu, 0, GFP_KERNEL,
  292. msm_gpu_devcoredump_read, msm_gpu_devcoredump_free);
  293. }
  294. #else
  295. static void msm_gpu_crashstate_capture(struct msm_gpu *gpu,
  296. struct msm_gem_submit *submit, char *comm, char *cmd)
  297. {
  298. }
  299. #endif
  300. /*
  301. * Hangcheck detection for locked gpu:
  302. */
  303. static void update_fences(struct msm_gpu *gpu, struct msm_ringbuffer *ring,
  304. uint32_t fence)
  305. {
  306. struct msm_gem_submit *submit;
  307. list_for_each_entry(submit, &ring->submits, node) {
  308. if (submit->seqno > fence)
  309. break;
  310. msm_update_fence(submit->ring->fctx,
  311. submit->fence->seqno);
  312. }
  313. }
  314. static struct msm_gem_submit *
  315. find_submit(struct msm_ringbuffer *ring, uint32_t fence)
  316. {
  317. struct msm_gem_submit *submit;
  318. WARN_ON(!mutex_is_locked(&ring->gpu->dev->struct_mutex));
  319. list_for_each_entry(submit, &ring->submits, node)
  320. if (submit->seqno == fence)
  321. return submit;
  322. return NULL;
  323. }
  324. static void retire_submits(struct msm_gpu *gpu);
  325. static void recover_worker(struct work_struct *work)
  326. {
  327. struct msm_gpu *gpu = container_of(work, struct msm_gpu, recover_work);
  328. struct drm_device *dev = gpu->dev;
  329. struct msm_drm_private *priv = dev->dev_private;
  330. struct msm_gem_submit *submit;
  331. struct msm_ringbuffer *cur_ring = gpu->funcs->active_ring(gpu);
  332. char *comm = NULL, *cmd = NULL;
  333. int i;
  334. mutex_lock(&dev->struct_mutex);
  335. dev_err(dev->dev, "%s: hangcheck recover!\n", gpu->name);
  336. submit = find_submit(cur_ring, cur_ring->memptrs->fence + 1);
  337. if (submit) {
  338. struct task_struct *task;
  339. task = get_pid_task(submit->pid, PIDTYPE_PID);
  340. if (task) {
  341. comm = kstrdup(task->comm, GFP_KERNEL);
  342. /*
  343. * So slightly annoying, in other paths like
  344. * mmap'ing gem buffers, mmap_sem is acquired
  345. * before struct_mutex, which means we can't
  346. * hold struct_mutex across the call to
  347. * get_cmdline(). But submits are retired
  348. * from the same in-order workqueue, so we can
  349. * safely drop the lock here without worrying
  350. * about the submit going away.
  351. */
  352. mutex_unlock(&dev->struct_mutex);
  353. cmd = kstrdup_quotable_cmdline(task, GFP_KERNEL);
  354. put_task_struct(task);
  355. mutex_lock(&dev->struct_mutex);
  356. }
  357. if (comm && cmd) {
  358. dev_err(dev->dev, "%s: offending task: %s (%s)\n",
  359. gpu->name, comm, cmd);
  360. msm_rd_dump_submit(priv->hangrd, submit,
  361. "offending task: %s (%s)", comm, cmd);
  362. } else
  363. msm_rd_dump_submit(priv->hangrd, submit, NULL);
  364. }
  365. /* Record the crash state */
  366. pm_runtime_get_sync(&gpu->pdev->dev);
  367. msm_gpu_crashstate_capture(gpu, submit, comm, cmd);
  368. pm_runtime_put_sync(&gpu->pdev->dev);
  369. kfree(cmd);
  370. kfree(comm);
  371. /*
  372. * Update all the rings with the latest and greatest fence.. this
  373. * needs to happen after msm_rd_dump_submit() to ensure that the
  374. * bo's referenced by the offending submit are still around.
  375. */
  376. for (i = 0; i < gpu->nr_rings; i++) {
  377. struct msm_ringbuffer *ring = gpu->rb[i];
  378. uint32_t fence = ring->memptrs->fence;
  379. /*
  380. * For the current (faulting?) ring/submit advance the fence by
  381. * one more to clear the faulting submit
  382. */
  383. if (ring == cur_ring)
  384. fence++;
  385. update_fences(gpu, ring, fence);
  386. }
  387. if (msm_gpu_active(gpu)) {
  388. /* retire completed submits, plus the one that hung: */
  389. retire_submits(gpu);
  390. pm_runtime_get_sync(&gpu->pdev->dev);
  391. gpu->funcs->recover(gpu);
  392. pm_runtime_put_sync(&gpu->pdev->dev);
  393. /*
  394. * Replay all remaining submits starting with highest priority
  395. * ring
  396. */
  397. for (i = 0; i < gpu->nr_rings; i++) {
  398. struct msm_ringbuffer *ring = gpu->rb[i];
  399. list_for_each_entry(submit, &ring->submits, node)
  400. gpu->funcs->submit(gpu, submit, NULL);
  401. }
  402. }
  403. mutex_unlock(&dev->struct_mutex);
  404. msm_gpu_retire(gpu);
  405. }
  406. static void hangcheck_timer_reset(struct msm_gpu *gpu)
  407. {
  408. DBG("%s", gpu->name);
  409. mod_timer(&gpu->hangcheck_timer,
  410. round_jiffies_up(jiffies + DRM_MSM_HANGCHECK_JIFFIES));
  411. }
  412. static void hangcheck_handler(struct timer_list *t)
  413. {
  414. struct msm_gpu *gpu = from_timer(gpu, t, hangcheck_timer);
  415. struct drm_device *dev = gpu->dev;
  416. struct msm_drm_private *priv = dev->dev_private;
  417. struct msm_ringbuffer *ring = gpu->funcs->active_ring(gpu);
  418. uint32_t fence = ring->memptrs->fence;
  419. if (fence != ring->hangcheck_fence) {
  420. /* some progress has been made.. ya! */
  421. ring->hangcheck_fence = fence;
  422. } else if (fence < ring->seqno) {
  423. /* no progress and not done.. hung! */
  424. ring->hangcheck_fence = fence;
  425. dev_err(dev->dev, "%s: hangcheck detected gpu lockup rb %d!\n",
  426. gpu->name, ring->id);
  427. dev_err(dev->dev, "%s: completed fence: %u\n",
  428. gpu->name, fence);
  429. dev_err(dev->dev, "%s: submitted fence: %u\n",
  430. gpu->name, ring->seqno);
  431. queue_work(priv->wq, &gpu->recover_work);
  432. }
  433. /* if still more pending work, reset the hangcheck timer: */
  434. if (ring->seqno > ring->hangcheck_fence)
  435. hangcheck_timer_reset(gpu);
  436. /* workaround for missing irq: */
  437. queue_work(priv->wq, &gpu->retire_work);
  438. }
  439. /*
  440. * Performance Counters:
  441. */
  442. /* called under perf_lock */
  443. static int update_hw_cntrs(struct msm_gpu *gpu, uint32_t ncntrs, uint32_t *cntrs)
  444. {
  445. uint32_t current_cntrs[ARRAY_SIZE(gpu->last_cntrs)];
  446. int i, n = min(ncntrs, gpu->num_perfcntrs);
  447. /* read current values: */
  448. for (i = 0; i < gpu->num_perfcntrs; i++)
  449. current_cntrs[i] = gpu_read(gpu, gpu->perfcntrs[i].sample_reg);
  450. /* update cntrs: */
  451. for (i = 0; i < n; i++)
  452. cntrs[i] = current_cntrs[i] - gpu->last_cntrs[i];
  453. /* save current values: */
  454. for (i = 0; i < gpu->num_perfcntrs; i++)
  455. gpu->last_cntrs[i] = current_cntrs[i];
  456. return n;
  457. }
  458. static void update_sw_cntrs(struct msm_gpu *gpu)
  459. {
  460. ktime_t time;
  461. uint32_t elapsed;
  462. unsigned long flags;
  463. spin_lock_irqsave(&gpu->perf_lock, flags);
  464. if (!gpu->perfcntr_active)
  465. goto out;
  466. time = ktime_get();
  467. elapsed = ktime_to_us(ktime_sub(time, gpu->last_sample.time));
  468. gpu->totaltime += elapsed;
  469. if (gpu->last_sample.active)
  470. gpu->activetime += elapsed;
  471. gpu->last_sample.active = msm_gpu_active(gpu);
  472. gpu->last_sample.time = time;
  473. out:
  474. spin_unlock_irqrestore(&gpu->perf_lock, flags);
  475. }
  476. void msm_gpu_perfcntr_start(struct msm_gpu *gpu)
  477. {
  478. unsigned long flags;
  479. pm_runtime_get_sync(&gpu->pdev->dev);
  480. spin_lock_irqsave(&gpu->perf_lock, flags);
  481. /* we could dynamically enable/disable perfcntr registers too.. */
  482. gpu->last_sample.active = msm_gpu_active(gpu);
  483. gpu->last_sample.time = ktime_get();
  484. gpu->activetime = gpu->totaltime = 0;
  485. gpu->perfcntr_active = true;
  486. update_hw_cntrs(gpu, 0, NULL);
  487. spin_unlock_irqrestore(&gpu->perf_lock, flags);
  488. }
  489. void msm_gpu_perfcntr_stop(struct msm_gpu *gpu)
  490. {
  491. gpu->perfcntr_active = false;
  492. pm_runtime_put_sync(&gpu->pdev->dev);
  493. }
  494. /* returns -errno or # of cntrs sampled */
  495. int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime,
  496. uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs)
  497. {
  498. unsigned long flags;
  499. int ret;
  500. spin_lock_irqsave(&gpu->perf_lock, flags);
  501. if (!gpu->perfcntr_active) {
  502. ret = -EINVAL;
  503. goto out;
  504. }
  505. *activetime = gpu->activetime;
  506. *totaltime = gpu->totaltime;
  507. gpu->activetime = gpu->totaltime = 0;
  508. ret = update_hw_cntrs(gpu, ncntrs, cntrs);
  509. out:
  510. spin_unlock_irqrestore(&gpu->perf_lock, flags);
  511. return ret;
  512. }
  513. /*
  514. * Cmdstream submission/retirement:
  515. */
  516. static void retire_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
  517. {
  518. int i;
  519. for (i = 0; i < submit->nr_bos; i++) {
  520. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  521. /* move to inactive: */
  522. msm_gem_move_to_inactive(&msm_obj->base);
  523. msm_gem_put_iova(&msm_obj->base, gpu->aspace);
  524. drm_gem_object_put(&msm_obj->base);
  525. }
  526. pm_runtime_mark_last_busy(&gpu->pdev->dev);
  527. pm_runtime_put_autosuspend(&gpu->pdev->dev);
  528. msm_gem_submit_free(submit);
  529. }
  530. static void retire_submits(struct msm_gpu *gpu)
  531. {
  532. struct drm_device *dev = gpu->dev;
  533. struct msm_gem_submit *submit, *tmp;
  534. int i;
  535. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  536. /* Retire the commits starting with highest priority */
  537. for (i = 0; i < gpu->nr_rings; i++) {
  538. struct msm_ringbuffer *ring = gpu->rb[i];
  539. list_for_each_entry_safe(submit, tmp, &ring->submits, node) {
  540. if (dma_fence_is_signaled(submit->fence))
  541. retire_submit(gpu, submit);
  542. }
  543. }
  544. }
  545. static void retire_worker(struct work_struct *work)
  546. {
  547. struct msm_gpu *gpu = container_of(work, struct msm_gpu, retire_work);
  548. struct drm_device *dev = gpu->dev;
  549. int i;
  550. for (i = 0; i < gpu->nr_rings; i++)
  551. update_fences(gpu, gpu->rb[i], gpu->rb[i]->memptrs->fence);
  552. mutex_lock(&dev->struct_mutex);
  553. retire_submits(gpu);
  554. mutex_unlock(&dev->struct_mutex);
  555. }
  556. /* call from irq handler to schedule work to retire bo's */
  557. void msm_gpu_retire(struct msm_gpu *gpu)
  558. {
  559. struct msm_drm_private *priv = gpu->dev->dev_private;
  560. queue_work(priv->wq, &gpu->retire_work);
  561. update_sw_cntrs(gpu);
  562. }
  563. /* add bo's to gpu's ring, and kick gpu: */
  564. void msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
  565. struct msm_file_private *ctx)
  566. {
  567. struct drm_device *dev = gpu->dev;
  568. struct msm_drm_private *priv = dev->dev_private;
  569. struct msm_ringbuffer *ring = submit->ring;
  570. int i;
  571. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  572. pm_runtime_get_sync(&gpu->pdev->dev);
  573. msm_gpu_hw_init(gpu);
  574. submit->seqno = ++ring->seqno;
  575. list_add_tail(&submit->node, &ring->submits);
  576. msm_rd_dump_submit(priv->rd, submit, NULL);
  577. update_sw_cntrs(gpu);
  578. for (i = 0; i < submit->nr_bos; i++) {
  579. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  580. uint64_t iova;
  581. /* can't happen yet.. but when we add 2d support we'll have
  582. * to deal w/ cross-ring synchronization:
  583. */
  584. WARN_ON(is_active(msm_obj) && (msm_obj->gpu != gpu));
  585. /* submit takes a reference to the bo and iova until retired: */
  586. drm_gem_object_get(&msm_obj->base);
  587. msm_gem_get_iova(&msm_obj->base,
  588. submit->gpu->aspace, &iova);
  589. if (submit->bos[i].flags & MSM_SUBMIT_BO_WRITE)
  590. msm_gem_move_to_active(&msm_obj->base, gpu, true, submit->fence);
  591. else if (submit->bos[i].flags & MSM_SUBMIT_BO_READ)
  592. msm_gem_move_to_active(&msm_obj->base, gpu, false, submit->fence);
  593. }
  594. gpu->funcs->submit(gpu, submit, ctx);
  595. priv->lastctx = ctx;
  596. hangcheck_timer_reset(gpu);
  597. }
  598. /*
  599. * Init/Cleanup:
  600. */
  601. static irqreturn_t irq_handler(int irq, void *data)
  602. {
  603. struct msm_gpu *gpu = data;
  604. return gpu->funcs->irq(gpu);
  605. }
  606. static int get_clocks(struct platform_device *pdev, struct msm_gpu *gpu)
  607. {
  608. int ret = msm_clk_bulk_get(&pdev->dev, &gpu->grp_clks);
  609. if (ret < 1) {
  610. gpu->nr_clocks = 0;
  611. return ret;
  612. }
  613. gpu->nr_clocks = ret;
  614. gpu->core_clk = msm_clk_bulk_get_clock(gpu->grp_clks,
  615. gpu->nr_clocks, "core");
  616. gpu->rbbmtimer_clk = msm_clk_bulk_get_clock(gpu->grp_clks,
  617. gpu->nr_clocks, "rbbmtimer");
  618. return 0;
  619. }
  620. static struct msm_gem_address_space *
  621. msm_gpu_create_address_space(struct msm_gpu *gpu, struct platform_device *pdev,
  622. uint64_t va_start, uint64_t va_end)
  623. {
  624. struct iommu_domain *iommu;
  625. struct msm_gem_address_space *aspace;
  626. int ret;
  627. /*
  628. * Setup IOMMU.. eventually we will (I think) do this once per context
  629. * and have separate page tables per context. For now, to keep things
  630. * simple and to get something working, just use a single address space:
  631. */
  632. iommu = iommu_domain_alloc(&platform_bus_type);
  633. if (!iommu)
  634. return NULL;
  635. iommu->geometry.aperture_start = va_start;
  636. iommu->geometry.aperture_end = va_end;
  637. dev_info(gpu->dev->dev, "%s: using IOMMU\n", gpu->name);
  638. aspace = msm_gem_address_space_create(&pdev->dev, iommu, "gpu");
  639. if (IS_ERR(aspace)) {
  640. dev_err(gpu->dev->dev, "failed to init iommu: %ld\n",
  641. PTR_ERR(aspace));
  642. iommu_domain_free(iommu);
  643. return ERR_CAST(aspace);
  644. }
  645. ret = aspace->mmu->funcs->attach(aspace->mmu, NULL, 0);
  646. if (ret) {
  647. msm_gem_address_space_put(aspace);
  648. return ERR_PTR(ret);
  649. }
  650. return aspace;
  651. }
  652. int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
  653. struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs,
  654. const char *name, struct msm_gpu_config *config)
  655. {
  656. int i, ret, nr_rings = config->nr_rings;
  657. void *memptrs;
  658. uint64_t memptrs_iova;
  659. if (WARN_ON(gpu->num_perfcntrs > ARRAY_SIZE(gpu->last_cntrs)))
  660. gpu->num_perfcntrs = ARRAY_SIZE(gpu->last_cntrs);
  661. gpu->dev = drm;
  662. gpu->funcs = funcs;
  663. gpu->name = name;
  664. INIT_LIST_HEAD(&gpu->active_list);
  665. INIT_WORK(&gpu->retire_work, retire_worker);
  666. INIT_WORK(&gpu->recover_work, recover_worker);
  667. timer_setup(&gpu->hangcheck_timer, hangcheck_handler, 0);
  668. spin_lock_init(&gpu->perf_lock);
  669. /* Map registers: */
  670. gpu->mmio = msm_ioremap(pdev, config->ioname, name);
  671. if (IS_ERR(gpu->mmio)) {
  672. ret = PTR_ERR(gpu->mmio);
  673. goto fail;
  674. }
  675. /* Get Interrupt: */
  676. gpu->irq = platform_get_irq_byname(pdev, config->irqname);
  677. if (gpu->irq < 0) {
  678. ret = gpu->irq;
  679. dev_err(drm->dev, "failed to get irq: %d\n", ret);
  680. goto fail;
  681. }
  682. ret = devm_request_irq(&pdev->dev, gpu->irq, irq_handler,
  683. IRQF_TRIGGER_HIGH, gpu->name, gpu);
  684. if (ret) {
  685. dev_err(drm->dev, "failed to request IRQ%u: %d\n", gpu->irq, ret);
  686. goto fail;
  687. }
  688. ret = get_clocks(pdev, gpu);
  689. if (ret)
  690. goto fail;
  691. gpu->ebi1_clk = msm_clk_get(pdev, "bus");
  692. DBG("ebi1_clk: %p", gpu->ebi1_clk);
  693. if (IS_ERR(gpu->ebi1_clk))
  694. gpu->ebi1_clk = NULL;
  695. /* Acquire regulators: */
  696. gpu->gpu_reg = devm_regulator_get(&pdev->dev, "vdd");
  697. DBG("gpu_reg: %p", gpu->gpu_reg);
  698. if (IS_ERR(gpu->gpu_reg))
  699. gpu->gpu_reg = NULL;
  700. gpu->gpu_cx = devm_regulator_get(&pdev->dev, "vddcx");
  701. DBG("gpu_cx: %p", gpu->gpu_cx);
  702. if (IS_ERR(gpu->gpu_cx))
  703. gpu->gpu_cx = NULL;
  704. gpu->pdev = pdev;
  705. platform_set_drvdata(pdev, gpu);
  706. msm_devfreq_init(gpu);
  707. gpu->aspace = msm_gpu_create_address_space(gpu, pdev,
  708. config->va_start, config->va_end);
  709. if (gpu->aspace == NULL)
  710. dev_info(drm->dev, "%s: no IOMMU, fallback to VRAM carveout!\n", name);
  711. else if (IS_ERR(gpu->aspace)) {
  712. ret = PTR_ERR(gpu->aspace);
  713. goto fail;
  714. }
  715. memptrs = msm_gem_kernel_new(drm, sizeof(*gpu->memptrs_bo),
  716. MSM_BO_UNCACHED, gpu->aspace, &gpu->memptrs_bo,
  717. &memptrs_iova);
  718. if (IS_ERR(memptrs)) {
  719. ret = PTR_ERR(memptrs);
  720. dev_err(drm->dev, "could not allocate memptrs: %d\n", ret);
  721. goto fail;
  722. }
  723. if (nr_rings > ARRAY_SIZE(gpu->rb)) {
  724. DRM_DEV_INFO_ONCE(drm->dev, "Only creating %zu ringbuffers\n",
  725. ARRAY_SIZE(gpu->rb));
  726. nr_rings = ARRAY_SIZE(gpu->rb);
  727. }
  728. /* Create ringbuffer(s): */
  729. for (i = 0; i < nr_rings; i++) {
  730. gpu->rb[i] = msm_ringbuffer_new(gpu, i, memptrs, memptrs_iova);
  731. if (IS_ERR(gpu->rb[i])) {
  732. ret = PTR_ERR(gpu->rb[i]);
  733. dev_err(drm->dev,
  734. "could not create ringbuffer %d: %d\n", i, ret);
  735. goto fail;
  736. }
  737. memptrs += sizeof(struct msm_rbmemptrs);
  738. memptrs_iova += sizeof(struct msm_rbmemptrs);
  739. }
  740. gpu->nr_rings = nr_rings;
  741. return 0;
  742. fail:
  743. for (i = 0; i < ARRAY_SIZE(gpu->rb); i++) {
  744. msm_ringbuffer_destroy(gpu->rb[i]);
  745. gpu->rb[i] = NULL;
  746. }
  747. if (gpu->memptrs_bo) {
  748. msm_gem_put_vaddr(gpu->memptrs_bo);
  749. msm_gem_put_iova(gpu->memptrs_bo, gpu->aspace);
  750. drm_gem_object_put_unlocked(gpu->memptrs_bo);
  751. }
  752. platform_set_drvdata(pdev, NULL);
  753. return ret;
  754. }
  755. void msm_gpu_cleanup(struct msm_gpu *gpu)
  756. {
  757. int i;
  758. DBG("%s", gpu->name);
  759. WARN_ON(!list_empty(&gpu->active_list));
  760. for (i = 0; i < ARRAY_SIZE(gpu->rb); i++) {
  761. msm_ringbuffer_destroy(gpu->rb[i]);
  762. gpu->rb[i] = NULL;
  763. }
  764. if (gpu->memptrs_bo) {
  765. msm_gem_put_vaddr(gpu->memptrs_bo);
  766. msm_gem_put_iova(gpu->memptrs_bo, gpu->aspace);
  767. drm_gem_object_put_unlocked(gpu->memptrs_bo);
  768. }
  769. if (!IS_ERR_OR_NULL(gpu->aspace)) {
  770. gpu->aspace->mmu->funcs->detach(gpu->aspace->mmu,
  771. NULL, 0);
  772. msm_gem_address_space_put(gpu->aspace);
  773. }
  774. }