msm_gpu.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. /*
  22. * Power Management:
  23. */
  24. #ifdef DOWNSTREAM_CONFIG_MSM_BUS_SCALING
  25. #include <mach/board.h>
  26. static void bs_init(struct msm_gpu *gpu)
  27. {
  28. if (gpu->bus_scale_table) {
  29. gpu->bsc = msm_bus_scale_register_client(gpu->bus_scale_table);
  30. DBG("bus scale client: %08x", gpu->bsc);
  31. }
  32. }
  33. static void bs_fini(struct msm_gpu *gpu)
  34. {
  35. if (gpu->bsc) {
  36. msm_bus_scale_unregister_client(gpu->bsc);
  37. gpu->bsc = 0;
  38. }
  39. }
  40. static void bs_set(struct msm_gpu *gpu, int idx)
  41. {
  42. if (gpu->bsc) {
  43. DBG("set bus scaling: %d", idx);
  44. msm_bus_scale_client_update_request(gpu->bsc, idx);
  45. }
  46. }
  47. #else
  48. static void bs_init(struct msm_gpu *gpu) {}
  49. static void bs_fini(struct msm_gpu *gpu) {}
  50. static void bs_set(struct msm_gpu *gpu, int idx) {}
  51. #endif
  52. static int enable_pwrrail(struct msm_gpu *gpu)
  53. {
  54. struct drm_device *dev = gpu->dev;
  55. int ret = 0;
  56. if (gpu->gpu_reg) {
  57. ret = regulator_enable(gpu->gpu_reg);
  58. if (ret) {
  59. dev_err(dev->dev, "failed to enable 'gpu_reg': %d\n", ret);
  60. return ret;
  61. }
  62. }
  63. if (gpu->gpu_cx) {
  64. ret = regulator_enable(gpu->gpu_cx);
  65. if (ret) {
  66. dev_err(dev->dev, "failed to enable 'gpu_cx': %d\n", ret);
  67. return ret;
  68. }
  69. }
  70. return 0;
  71. }
  72. static int disable_pwrrail(struct msm_gpu *gpu)
  73. {
  74. if (gpu->gpu_cx)
  75. regulator_disable(gpu->gpu_cx);
  76. if (gpu->gpu_reg)
  77. regulator_disable(gpu->gpu_reg);
  78. return 0;
  79. }
  80. static int enable_clk(struct msm_gpu *gpu)
  81. {
  82. struct clk *rate_clk = NULL;
  83. int i;
  84. /* NOTE: kgsl_pwrctrl_clk() ignores grp_clks[0].. */
  85. for (i = ARRAY_SIZE(gpu->grp_clks) - 1; i > 0; i--) {
  86. if (gpu->grp_clks[i]) {
  87. clk_prepare(gpu->grp_clks[i]);
  88. rate_clk = gpu->grp_clks[i];
  89. }
  90. }
  91. if (rate_clk && gpu->fast_rate)
  92. clk_set_rate(rate_clk, gpu->fast_rate);
  93. for (i = ARRAY_SIZE(gpu->grp_clks) - 1; i > 0; i--)
  94. if (gpu->grp_clks[i])
  95. clk_enable(gpu->grp_clks[i]);
  96. return 0;
  97. }
  98. static int disable_clk(struct msm_gpu *gpu)
  99. {
  100. struct clk *rate_clk = NULL;
  101. int i;
  102. /* NOTE: kgsl_pwrctrl_clk() ignores grp_clks[0].. */
  103. for (i = ARRAY_SIZE(gpu->grp_clks) - 1; i > 0; i--) {
  104. if (gpu->grp_clks[i]) {
  105. clk_disable(gpu->grp_clks[i]);
  106. rate_clk = gpu->grp_clks[i];
  107. }
  108. }
  109. if (rate_clk && gpu->slow_rate)
  110. clk_set_rate(rate_clk, gpu->slow_rate);
  111. for (i = ARRAY_SIZE(gpu->grp_clks) - 1; i > 0; i--)
  112. if (gpu->grp_clks[i])
  113. clk_unprepare(gpu->grp_clks[i]);
  114. return 0;
  115. }
  116. static int enable_axi(struct msm_gpu *gpu)
  117. {
  118. if (gpu->ebi1_clk)
  119. clk_prepare_enable(gpu->ebi1_clk);
  120. if (gpu->bus_freq)
  121. bs_set(gpu, gpu->bus_freq);
  122. return 0;
  123. }
  124. static int disable_axi(struct msm_gpu *gpu)
  125. {
  126. if (gpu->ebi1_clk)
  127. clk_disable_unprepare(gpu->ebi1_clk);
  128. if (gpu->bus_freq)
  129. bs_set(gpu, 0);
  130. return 0;
  131. }
  132. int msm_gpu_pm_resume(struct msm_gpu *gpu)
  133. {
  134. struct drm_device *dev = gpu->dev;
  135. int ret;
  136. DBG("%s: active_cnt=%d", gpu->name, gpu->active_cnt);
  137. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  138. if (gpu->active_cnt++ > 0)
  139. return 0;
  140. if (WARN_ON(gpu->active_cnt <= 0))
  141. return -EINVAL;
  142. ret = enable_pwrrail(gpu);
  143. if (ret)
  144. return ret;
  145. ret = enable_clk(gpu);
  146. if (ret)
  147. return ret;
  148. ret = enable_axi(gpu);
  149. if (ret)
  150. return ret;
  151. return 0;
  152. }
  153. int msm_gpu_pm_suspend(struct msm_gpu *gpu)
  154. {
  155. struct drm_device *dev = gpu->dev;
  156. int ret;
  157. DBG("%s: active_cnt=%d", gpu->name, gpu->active_cnt);
  158. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  159. if (--gpu->active_cnt > 0)
  160. return 0;
  161. if (WARN_ON(gpu->active_cnt < 0))
  162. return -EINVAL;
  163. ret = disable_axi(gpu);
  164. if (ret)
  165. return ret;
  166. ret = disable_clk(gpu);
  167. if (ret)
  168. return ret;
  169. ret = disable_pwrrail(gpu);
  170. if (ret)
  171. return ret;
  172. return 0;
  173. }
  174. /*
  175. * Inactivity detection (for suspend):
  176. */
  177. static void inactive_worker(struct work_struct *work)
  178. {
  179. struct msm_gpu *gpu = container_of(work, struct msm_gpu, inactive_work);
  180. struct drm_device *dev = gpu->dev;
  181. if (gpu->inactive)
  182. return;
  183. DBG("%s: inactive!\n", gpu->name);
  184. mutex_lock(&dev->struct_mutex);
  185. if (!(msm_gpu_active(gpu) || gpu->inactive)) {
  186. disable_axi(gpu);
  187. disable_clk(gpu);
  188. gpu->inactive = true;
  189. }
  190. mutex_unlock(&dev->struct_mutex);
  191. }
  192. static void inactive_handler(unsigned long data)
  193. {
  194. struct msm_gpu *gpu = (struct msm_gpu *)data;
  195. struct msm_drm_private *priv = gpu->dev->dev_private;
  196. queue_work(priv->wq, &gpu->inactive_work);
  197. }
  198. /* cancel inactive timer and make sure we are awake: */
  199. static void inactive_cancel(struct msm_gpu *gpu)
  200. {
  201. DBG("%s", gpu->name);
  202. del_timer(&gpu->inactive_timer);
  203. if (gpu->inactive) {
  204. enable_clk(gpu);
  205. enable_axi(gpu);
  206. gpu->inactive = false;
  207. }
  208. }
  209. static void inactive_start(struct msm_gpu *gpu)
  210. {
  211. DBG("%s", gpu->name);
  212. mod_timer(&gpu->inactive_timer,
  213. round_jiffies_up(jiffies + DRM_MSM_INACTIVE_JIFFIES));
  214. }
  215. /*
  216. * Hangcheck detection for locked gpu:
  217. */
  218. static void retire_submits(struct msm_gpu *gpu);
  219. static void recover_worker(struct work_struct *work)
  220. {
  221. struct msm_gpu *gpu = container_of(work, struct msm_gpu, recover_work);
  222. struct drm_device *dev = gpu->dev;
  223. struct msm_gem_submit *submit;
  224. uint32_t fence = gpu->funcs->last_fence(gpu);
  225. msm_update_fence(gpu->fctx, fence + 1);
  226. mutex_lock(&dev->struct_mutex);
  227. dev_err(dev->dev, "%s: hangcheck recover!\n", gpu->name);
  228. list_for_each_entry(submit, &gpu->submit_list, node) {
  229. if (submit->fence->seqno == (fence + 1)) {
  230. struct task_struct *task;
  231. rcu_read_lock();
  232. task = pid_task(submit->pid, PIDTYPE_PID);
  233. if (task) {
  234. dev_err(dev->dev, "%s: offending task: %s\n",
  235. gpu->name, task->comm);
  236. }
  237. rcu_read_unlock();
  238. break;
  239. }
  240. }
  241. if (msm_gpu_active(gpu)) {
  242. /* retire completed submits, plus the one that hung: */
  243. retire_submits(gpu);
  244. inactive_cancel(gpu);
  245. gpu->funcs->recover(gpu);
  246. /* replay the remaining submits after the one that hung: */
  247. list_for_each_entry(submit, &gpu->submit_list, node) {
  248. gpu->funcs->submit(gpu, submit, NULL);
  249. }
  250. }
  251. mutex_unlock(&dev->struct_mutex);
  252. msm_gpu_retire(gpu);
  253. }
  254. static void hangcheck_timer_reset(struct msm_gpu *gpu)
  255. {
  256. DBG("%s", gpu->name);
  257. mod_timer(&gpu->hangcheck_timer,
  258. round_jiffies_up(jiffies + DRM_MSM_HANGCHECK_JIFFIES));
  259. }
  260. static void hangcheck_handler(unsigned long data)
  261. {
  262. struct msm_gpu *gpu = (struct msm_gpu *)data;
  263. struct drm_device *dev = gpu->dev;
  264. struct msm_drm_private *priv = dev->dev_private;
  265. uint32_t fence = gpu->funcs->last_fence(gpu);
  266. if (fence != gpu->hangcheck_fence) {
  267. /* some progress has been made.. ya! */
  268. gpu->hangcheck_fence = fence;
  269. } else if (fence < gpu->fctx->last_fence) {
  270. /* no progress and not done.. hung! */
  271. gpu->hangcheck_fence = fence;
  272. dev_err(dev->dev, "%s: hangcheck detected gpu lockup!\n",
  273. gpu->name);
  274. dev_err(dev->dev, "%s: completed fence: %u\n",
  275. gpu->name, fence);
  276. dev_err(dev->dev, "%s: submitted fence: %u\n",
  277. gpu->name, gpu->fctx->last_fence);
  278. queue_work(priv->wq, &gpu->recover_work);
  279. }
  280. /* if still more pending work, reset the hangcheck timer: */
  281. if (gpu->fctx->last_fence > gpu->hangcheck_fence)
  282. hangcheck_timer_reset(gpu);
  283. /* workaround for missing irq: */
  284. queue_work(priv->wq, &gpu->retire_work);
  285. }
  286. /*
  287. * Performance Counters:
  288. */
  289. /* called under perf_lock */
  290. static int update_hw_cntrs(struct msm_gpu *gpu, uint32_t ncntrs, uint32_t *cntrs)
  291. {
  292. uint32_t current_cntrs[ARRAY_SIZE(gpu->last_cntrs)];
  293. int i, n = min(ncntrs, gpu->num_perfcntrs);
  294. /* read current values: */
  295. for (i = 0; i < gpu->num_perfcntrs; i++)
  296. current_cntrs[i] = gpu_read(gpu, gpu->perfcntrs[i].sample_reg);
  297. /* update cntrs: */
  298. for (i = 0; i < n; i++)
  299. cntrs[i] = current_cntrs[i] - gpu->last_cntrs[i];
  300. /* save current values: */
  301. for (i = 0; i < gpu->num_perfcntrs; i++)
  302. gpu->last_cntrs[i] = current_cntrs[i];
  303. return n;
  304. }
  305. static void update_sw_cntrs(struct msm_gpu *gpu)
  306. {
  307. ktime_t time;
  308. uint32_t elapsed;
  309. unsigned long flags;
  310. spin_lock_irqsave(&gpu->perf_lock, flags);
  311. if (!gpu->perfcntr_active)
  312. goto out;
  313. time = ktime_get();
  314. elapsed = ktime_to_us(ktime_sub(time, gpu->last_sample.time));
  315. gpu->totaltime += elapsed;
  316. if (gpu->last_sample.active)
  317. gpu->activetime += elapsed;
  318. gpu->last_sample.active = msm_gpu_active(gpu);
  319. gpu->last_sample.time = time;
  320. out:
  321. spin_unlock_irqrestore(&gpu->perf_lock, flags);
  322. }
  323. void msm_gpu_perfcntr_start(struct msm_gpu *gpu)
  324. {
  325. unsigned long flags;
  326. spin_lock_irqsave(&gpu->perf_lock, flags);
  327. /* we could dynamically enable/disable perfcntr registers too.. */
  328. gpu->last_sample.active = msm_gpu_active(gpu);
  329. gpu->last_sample.time = ktime_get();
  330. gpu->activetime = gpu->totaltime = 0;
  331. gpu->perfcntr_active = true;
  332. update_hw_cntrs(gpu, 0, NULL);
  333. spin_unlock_irqrestore(&gpu->perf_lock, flags);
  334. }
  335. void msm_gpu_perfcntr_stop(struct msm_gpu *gpu)
  336. {
  337. gpu->perfcntr_active = false;
  338. }
  339. /* returns -errno or # of cntrs sampled */
  340. int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime,
  341. uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs)
  342. {
  343. unsigned long flags;
  344. int ret;
  345. spin_lock_irqsave(&gpu->perf_lock, flags);
  346. if (!gpu->perfcntr_active) {
  347. ret = -EINVAL;
  348. goto out;
  349. }
  350. *activetime = gpu->activetime;
  351. *totaltime = gpu->totaltime;
  352. gpu->activetime = gpu->totaltime = 0;
  353. ret = update_hw_cntrs(gpu, ncntrs, cntrs);
  354. out:
  355. spin_unlock_irqrestore(&gpu->perf_lock, flags);
  356. return ret;
  357. }
  358. /*
  359. * Cmdstream submission/retirement:
  360. */
  361. static void retire_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
  362. {
  363. int i;
  364. for (i = 0; i < submit->nr_bos; i++) {
  365. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  366. /* move to inactive: */
  367. msm_gem_move_to_inactive(&msm_obj->base);
  368. msm_gem_put_iova(&msm_obj->base, gpu->id);
  369. drm_gem_object_unreference(&msm_obj->base);
  370. }
  371. msm_gem_submit_free(submit);
  372. }
  373. static void retire_submits(struct msm_gpu *gpu)
  374. {
  375. struct drm_device *dev = gpu->dev;
  376. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  377. while (!list_empty(&gpu->submit_list)) {
  378. struct msm_gem_submit *submit;
  379. submit = list_first_entry(&gpu->submit_list,
  380. struct msm_gem_submit, node);
  381. if (fence_is_signaled(submit->fence)) {
  382. retire_submit(gpu, submit);
  383. } else {
  384. break;
  385. }
  386. }
  387. }
  388. static void retire_worker(struct work_struct *work)
  389. {
  390. struct msm_gpu *gpu = container_of(work, struct msm_gpu, retire_work);
  391. struct drm_device *dev = gpu->dev;
  392. uint32_t fence = gpu->funcs->last_fence(gpu);
  393. msm_update_fence(gpu->fctx, fence);
  394. mutex_lock(&dev->struct_mutex);
  395. retire_submits(gpu);
  396. mutex_unlock(&dev->struct_mutex);
  397. if (!msm_gpu_active(gpu))
  398. inactive_start(gpu);
  399. }
  400. /* call from irq handler to schedule work to retire bo's */
  401. void msm_gpu_retire(struct msm_gpu *gpu)
  402. {
  403. struct msm_drm_private *priv = gpu->dev->dev_private;
  404. queue_work(priv->wq, &gpu->retire_work);
  405. update_sw_cntrs(gpu);
  406. }
  407. /* add bo's to gpu's ring, and kick gpu: */
  408. void msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
  409. struct msm_file_private *ctx)
  410. {
  411. struct drm_device *dev = gpu->dev;
  412. struct msm_drm_private *priv = dev->dev_private;
  413. int i;
  414. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  415. inactive_cancel(gpu);
  416. list_add_tail(&submit->node, &gpu->submit_list);
  417. msm_rd_dump_submit(submit);
  418. update_sw_cntrs(gpu);
  419. for (i = 0; i < submit->nr_bos; i++) {
  420. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  421. uint32_t iova;
  422. /* can't happen yet.. but when we add 2d support we'll have
  423. * to deal w/ cross-ring synchronization:
  424. */
  425. WARN_ON(is_active(msm_obj) && (msm_obj->gpu != gpu));
  426. /* submit takes a reference to the bo and iova until retired: */
  427. drm_gem_object_reference(&msm_obj->base);
  428. msm_gem_get_iova_locked(&msm_obj->base,
  429. submit->gpu->id, &iova);
  430. if (submit->bos[i].flags & MSM_SUBMIT_BO_WRITE)
  431. msm_gem_move_to_active(&msm_obj->base, gpu, true, submit->fence);
  432. else if (submit->bos[i].flags & MSM_SUBMIT_BO_READ)
  433. msm_gem_move_to_active(&msm_obj->base, gpu, false, submit->fence);
  434. }
  435. gpu->funcs->submit(gpu, submit, ctx);
  436. priv->lastctx = ctx;
  437. hangcheck_timer_reset(gpu);
  438. }
  439. /*
  440. * Init/Cleanup:
  441. */
  442. static irqreturn_t irq_handler(int irq, void *data)
  443. {
  444. struct msm_gpu *gpu = data;
  445. return gpu->funcs->irq(gpu);
  446. }
  447. static const char *clk_names[] = {
  448. "src_clk", "core_clk", "iface_clk", "mem_clk", "mem_iface_clk",
  449. "alt_mem_iface_clk",
  450. };
  451. int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
  452. struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs,
  453. const char *name, const char *ioname, const char *irqname, int ringsz)
  454. {
  455. struct iommu_domain *iommu;
  456. int i, ret;
  457. if (WARN_ON(gpu->num_perfcntrs > ARRAY_SIZE(gpu->last_cntrs)))
  458. gpu->num_perfcntrs = ARRAY_SIZE(gpu->last_cntrs);
  459. gpu->dev = drm;
  460. gpu->funcs = funcs;
  461. gpu->name = name;
  462. gpu->inactive = true;
  463. gpu->fctx = msm_fence_context_alloc(drm, name);
  464. if (IS_ERR(gpu->fctx)) {
  465. ret = PTR_ERR(gpu->fctx);
  466. gpu->fctx = NULL;
  467. goto fail;
  468. }
  469. INIT_LIST_HEAD(&gpu->active_list);
  470. INIT_WORK(&gpu->retire_work, retire_worker);
  471. INIT_WORK(&gpu->inactive_work, inactive_worker);
  472. INIT_WORK(&gpu->recover_work, recover_worker);
  473. INIT_LIST_HEAD(&gpu->submit_list);
  474. setup_timer(&gpu->inactive_timer, inactive_handler,
  475. (unsigned long)gpu);
  476. setup_timer(&gpu->hangcheck_timer, hangcheck_handler,
  477. (unsigned long)gpu);
  478. spin_lock_init(&gpu->perf_lock);
  479. BUG_ON(ARRAY_SIZE(clk_names) != ARRAY_SIZE(gpu->grp_clks));
  480. /* Map registers: */
  481. gpu->mmio = msm_ioremap(pdev, ioname, name);
  482. if (IS_ERR(gpu->mmio)) {
  483. ret = PTR_ERR(gpu->mmio);
  484. goto fail;
  485. }
  486. /* Get Interrupt: */
  487. gpu->irq = platform_get_irq_byname(pdev, irqname);
  488. if (gpu->irq < 0) {
  489. ret = gpu->irq;
  490. dev_err(drm->dev, "failed to get irq: %d\n", ret);
  491. goto fail;
  492. }
  493. ret = devm_request_irq(&pdev->dev, gpu->irq, irq_handler,
  494. IRQF_TRIGGER_HIGH, gpu->name, gpu);
  495. if (ret) {
  496. dev_err(drm->dev, "failed to request IRQ%u: %d\n", gpu->irq, ret);
  497. goto fail;
  498. }
  499. /* Acquire clocks: */
  500. for (i = 0; i < ARRAY_SIZE(clk_names); i++) {
  501. gpu->grp_clks[i] = devm_clk_get(&pdev->dev, clk_names[i]);
  502. DBG("grp_clks[%s]: %p", clk_names[i], gpu->grp_clks[i]);
  503. if (IS_ERR(gpu->grp_clks[i]))
  504. gpu->grp_clks[i] = NULL;
  505. }
  506. gpu->ebi1_clk = devm_clk_get(&pdev->dev, "bus_clk");
  507. DBG("ebi1_clk: %p", gpu->ebi1_clk);
  508. if (IS_ERR(gpu->ebi1_clk))
  509. gpu->ebi1_clk = NULL;
  510. /* Acquire regulators: */
  511. gpu->gpu_reg = devm_regulator_get(&pdev->dev, "vdd");
  512. DBG("gpu_reg: %p", gpu->gpu_reg);
  513. if (IS_ERR(gpu->gpu_reg))
  514. gpu->gpu_reg = NULL;
  515. gpu->gpu_cx = devm_regulator_get(&pdev->dev, "vddcx");
  516. DBG("gpu_cx: %p", gpu->gpu_cx);
  517. if (IS_ERR(gpu->gpu_cx))
  518. gpu->gpu_cx = NULL;
  519. /* Setup IOMMU.. eventually we will (I think) do this once per context
  520. * and have separate page tables per context. For now, to keep things
  521. * simple and to get something working, just use a single address space:
  522. */
  523. iommu = iommu_domain_alloc(&platform_bus_type);
  524. if (iommu) {
  525. dev_info(drm->dev, "%s: using IOMMU\n", name);
  526. gpu->mmu = msm_iommu_new(&pdev->dev, iommu);
  527. if (IS_ERR(gpu->mmu)) {
  528. ret = PTR_ERR(gpu->mmu);
  529. dev_err(drm->dev, "failed to init iommu: %d\n", ret);
  530. gpu->mmu = NULL;
  531. iommu_domain_free(iommu);
  532. goto fail;
  533. }
  534. } else {
  535. dev_info(drm->dev, "%s: no IOMMU, fallback to VRAM carveout!\n", name);
  536. }
  537. gpu->id = msm_register_mmu(drm, gpu->mmu);
  538. /* Create ringbuffer: */
  539. mutex_lock(&drm->struct_mutex);
  540. gpu->rb = msm_ringbuffer_new(gpu, ringsz);
  541. mutex_unlock(&drm->struct_mutex);
  542. if (IS_ERR(gpu->rb)) {
  543. ret = PTR_ERR(gpu->rb);
  544. gpu->rb = NULL;
  545. dev_err(drm->dev, "could not create ringbuffer: %d\n", ret);
  546. goto fail;
  547. }
  548. bs_init(gpu);
  549. return 0;
  550. fail:
  551. return ret;
  552. }
  553. void msm_gpu_cleanup(struct msm_gpu *gpu)
  554. {
  555. DBG("%s", gpu->name);
  556. WARN_ON(!list_empty(&gpu->active_list));
  557. bs_fini(gpu);
  558. if (gpu->rb) {
  559. if (gpu->rb_iova)
  560. msm_gem_put_iova(gpu->rb->bo, gpu->id);
  561. msm_ringbuffer_destroy(gpu->rb);
  562. }
  563. if (gpu->mmu)
  564. gpu->mmu->funcs->destroy(gpu->mmu);
  565. if (gpu->fctx)
  566. msm_fence_context_free(gpu->fctx);
  567. }