msm_gpu.c 16 KB

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