msm_gpu.c 23 KB

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