msm_gpu.c 19 KB

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