msm_gpu.c 21 KB

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