msm_gpu.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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. uint32_t fence = gpu->funcs->last_fence(gpu);
  224. dev_err(dev->dev, "%s: hangcheck recover!\n", gpu->name);
  225. msm_update_fence(gpu->fctx, fence + 1);
  226. mutex_lock(&dev->struct_mutex);
  227. if (msm_gpu_active(gpu)) {
  228. struct msm_gem_submit *submit;
  229. /* retire completed submits, plus the one that hung: */
  230. retire_submits(gpu);
  231. inactive_cancel(gpu);
  232. gpu->funcs->recover(gpu);
  233. /* replay the remaining submits after the one that hung: */
  234. list_for_each_entry(submit, &gpu->submit_list, node) {
  235. gpu->funcs->submit(gpu, submit, NULL);
  236. }
  237. }
  238. mutex_unlock(&dev->struct_mutex);
  239. msm_gpu_retire(gpu);
  240. }
  241. static void hangcheck_timer_reset(struct msm_gpu *gpu)
  242. {
  243. DBG("%s", gpu->name);
  244. mod_timer(&gpu->hangcheck_timer,
  245. round_jiffies_up(jiffies + DRM_MSM_HANGCHECK_JIFFIES));
  246. }
  247. static void hangcheck_handler(unsigned long data)
  248. {
  249. struct msm_gpu *gpu = (struct msm_gpu *)data;
  250. struct drm_device *dev = gpu->dev;
  251. struct msm_drm_private *priv = dev->dev_private;
  252. uint32_t fence = gpu->funcs->last_fence(gpu);
  253. if (fence != gpu->hangcheck_fence) {
  254. /* some progress has been made.. ya! */
  255. gpu->hangcheck_fence = fence;
  256. } else if (fence < gpu->fctx->last_fence) {
  257. /* no progress and not done.. hung! */
  258. gpu->hangcheck_fence = fence;
  259. dev_err(dev->dev, "%s: hangcheck detected gpu lockup!\n",
  260. gpu->name);
  261. dev_err(dev->dev, "%s: completed fence: %u\n",
  262. gpu->name, fence);
  263. dev_err(dev->dev, "%s: submitted fence: %u\n",
  264. gpu->name, gpu->fctx->last_fence);
  265. queue_work(priv->wq, &gpu->recover_work);
  266. }
  267. /* if still more pending work, reset the hangcheck timer: */
  268. if (gpu->fctx->last_fence > gpu->hangcheck_fence)
  269. hangcheck_timer_reset(gpu);
  270. /* workaround for missing irq: */
  271. queue_work(priv->wq, &gpu->retire_work);
  272. }
  273. /*
  274. * Performance Counters:
  275. */
  276. /* called under perf_lock */
  277. static int update_hw_cntrs(struct msm_gpu *gpu, uint32_t ncntrs, uint32_t *cntrs)
  278. {
  279. uint32_t current_cntrs[ARRAY_SIZE(gpu->last_cntrs)];
  280. int i, n = min(ncntrs, gpu->num_perfcntrs);
  281. /* read current values: */
  282. for (i = 0; i < gpu->num_perfcntrs; i++)
  283. current_cntrs[i] = gpu_read(gpu, gpu->perfcntrs[i].sample_reg);
  284. /* update cntrs: */
  285. for (i = 0; i < n; i++)
  286. cntrs[i] = current_cntrs[i] - gpu->last_cntrs[i];
  287. /* save current values: */
  288. for (i = 0; i < gpu->num_perfcntrs; i++)
  289. gpu->last_cntrs[i] = current_cntrs[i];
  290. return n;
  291. }
  292. static void update_sw_cntrs(struct msm_gpu *gpu)
  293. {
  294. ktime_t time;
  295. uint32_t elapsed;
  296. unsigned long flags;
  297. spin_lock_irqsave(&gpu->perf_lock, flags);
  298. if (!gpu->perfcntr_active)
  299. goto out;
  300. time = ktime_get();
  301. elapsed = ktime_to_us(ktime_sub(time, gpu->last_sample.time));
  302. gpu->totaltime += elapsed;
  303. if (gpu->last_sample.active)
  304. gpu->activetime += elapsed;
  305. gpu->last_sample.active = msm_gpu_active(gpu);
  306. gpu->last_sample.time = time;
  307. out:
  308. spin_unlock_irqrestore(&gpu->perf_lock, flags);
  309. }
  310. void msm_gpu_perfcntr_start(struct msm_gpu *gpu)
  311. {
  312. unsigned long flags;
  313. spin_lock_irqsave(&gpu->perf_lock, flags);
  314. /* we could dynamically enable/disable perfcntr registers too.. */
  315. gpu->last_sample.active = msm_gpu_active(gpu);
  316. gpu->last_sample.time = ktime_get();
  317. gpu->activetime = gpu->totaltime = 0;
  318. gpu->perfcntr_active = true;
  319. update_hw_cntrs(gpu, 0, NULL);
  320. spin_unlock_irqrestore(&gpu->perf_lock, flags);
  321. }
  322. void msm_gpu_perfcntr_stop(struct msm_gpu *gpu)
  323. {
  324. gpu->perfcntr_active = false;
  325. }
  326. /* returns -errno or # of cntrs sampled */
  327. int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime,
  328. uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs)
  329. {
  330. unsigned long flags;
  331. int ret;
  332. spin_lock_irqsave(&gpu->perf_lock, flags);
  333. if (!gpu->perfcntr_active) {
  334. ret = -EINVAL;
  335. goto out;
  336. }
  337. *activetime = gpu->activetime;
  338. *totaltime = gpu->totaltime;
  339. gpu->activetime = gpu->totaltime = 0;
  340. ret = update_hw_cntrs(gpu, ncntrs, cntrs);
  341. out:
  342. spin_unlock_irqrestore(&gpu->perf_lock, flags);
  343. return ret;
  344. }
  345. /*
  346. * Cmdstream submission/retirement:
  347. */
  348. static void retire_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
  349. {
  350. int i;
  351. for (i = 0; i < submit->nr_bos; i++) {
  352. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  353. /* move to inactive: */
  354. msm_gem_move_to_inactive(&msm_obj->base);
  355. msm_gem_put_iova(&msm_obj->base, gpu->id);
  356. drm_gem_object_unreference(&msm_obj->base);
  357. }
  358. msm_gem_submit_free(submit);
  359. }
  360. static void retire_submits(struct msm_gpu *gpu)
  361. {
  362. struct drm_device *dev = gpu->dev;
  363. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  364. while (!list_empty(&gpu->submit_list)) {
  365. struct msm_gem_submit *submit;
  366. submit = list_first_entry(&gpu->submit_list,
  367. struct msm_gem_submit, node);
  368. if (fence_is_signaled(submit->fence)) {
  369. retire_submit(gpu, submit);
  370. } else {
  371. break;
  372. }
  373. }
  374. }
  375. static void retire_worker(struct work_struct *work)
  376. {
  377. struct msm_gpu *gpu = container_of(work, struct msm_gpu, retire_work);
  378. struct drm_device *dev = gpu->dev;
  379. uint32_t fence = gpu->funcs->last_fence(gpu);
  380. msm_update_fence(gpu->fctx, fence);
  381. mutex_lock(&dev->struct_mutex);
  382. retire_submits(gpu);
  383. mutex_unlock(&dev->struct_mutex);
  384. if (!msm_gpu_active(gpu))
  385. inactive_start(gpu);
  386. }
  387. /* call from irq handler to schedule work to retire bo's */
  388. void msm_gpu_retire(struct msm_gpu *gpu)
  389. {
  390. struct msm_drm_private *priv = gpu->dev->dev_private;
  391. queue_work(priv->wq, &gpu->retire_work);
  392. update_sw_cntrs(gpu);
  393. }
  394. /* add bo's to gpu's ring, and kick gpu: */
  395. int msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
  396. struct msm_file_private *ctx)
  397. {
  398. struct drm_device *dev = gpu->dev;
  399. struct msm_drm_private *priv = dev->dev_private;
  400. int i, ret;
  401. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  402. submit->fence = msm_fence_alloc(gpu->fctx);
  403. if (IS_ERR(submit->fence)) {
  404. ret = PTR_ERR(submit->fence);
  405. submit->fence = NULL;
  406. return ret;
  407. }
  408. inactive_cancel(gpu);
  409. list_add_tail(&submit->node, &gpu->submit_list);
  410. msm_rd_dump_submit(submit);
  411. update_sw_cntrs(gpu);
  412. for (i = 0; i < submit->nr_bos; i++) {
  413. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  414. uint32_t iova;
  415. /* can't happen yet.. but when we add 2d support we'll have
  416. * to deal w/ cross-ring synchronization:
  417. */
  418. WARN_ON(is_active(msm_obj) && (msm_obj->gpu != gpu));
  419. /* submit takes a reference to the bo and iova until retired: */
  420. drm_gem_object_reference(&msm_obj->base);
  421. msm_gem_get_iova_locked(&msm_obj->base,
  422. submit->gpu->id, &iova);
  423. if (submit->bos[i].flags & MSM_SUBMIT_BO_WRITE)
  424. msm_gem_move_to_active(&msm_obj->base, gpu, true, submit->fence);
  425. else if (submit->bos[i].flags & MSM_SUBMIT_BO_READ)
  426. msm_gem_move_to_active(&msm_obj->base, gpu, false, submit->fence);
  427. }
  428. gpu->funcs->submit(gpu, submit, ctx);
  429. priv->lastctx = ctx;
  430. hangcheck_timer_reset(gpu);
  431. return 0;
  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. gpu->fctx = msm_fence_context_alloc(drm, name);
  458. if (IS_ERR(gpu->fctx)) {
  459. ret = PTR_ERR(gpu->fctx);
  460. gpu->fctx = NULL;
  461. goto fail;
  462. }
  463. INIT_LIST_HEAD(&gpu->active_list);
  464. INIT_WORK(&gpu->retire_work, retire_worker);
  465. INIT_WORK(&gpu->inactive_work, inactive_worker);
  466. INIT_WORK(&gpu->recover_work, recover_worker);
  467. INIT_LIST_HEAD(&gpu->submit_list);
  468. setup_timer(&gpu->inactive_timer, inactive_handler,
  469. (unsigned long)gpu);
  470. setup_timer(&gpu->hangcheck_timer, hangcheck_handler,
  471. (unsigned long)gpu);
  472. spin_lock_init(&gpu->perf_lock);
  473. BUG_ON(ARRAY_SIZE(clk_names) != ARRAY_SIZE(gpu->grp_clks));
  474. /* Map registers: */
  475. gpu->mmio = msm_ioremap(pdev, ioname, name);
  476. if (IS_ERR(gpu->mmio)) {
  477. ret = PTR_ERR(gpu->mmio);
  478. goto fail;
  479. }
  480. /* Get Interrupt: */
  481. gpu->irq = platform_get_irq_byname(pdev, irqname);
  482. if (gpu->irq < 0) {
  483. ret = gpu->irq;
  484. dev_err(drm->dev, "failed to get irq: %d\n", ret);
  485. goto fail;
  486. }
  487. ret = devm_request_irq(&pdev->dev, gpu->irq, irq_handler,
  488. IRQF_TRIGGER_HIGH, gpu->name, gpu);
  489. if (ret) {
  490. dev_err(drm->dev, "failed to request IRQ%u: %d\n", gpu->irq, ret);
  491. goto fail;
  492. }
  493. /* Acquire clocks: */
  494. for (i = 0; i < ARRAY_SIZE(clk_names); i++) {
  495. gpu->grp_clks[i] = devm_clk_get(&pdev->dev, clk_names[i]);
  496. DBG("grp_clks[%s]: %p", clk_names[i], gpu->grp_clks[i]);
  497. if (IS_ERR(gpu->grp_clks[i]))
  498. gpu->grp_clks[i] = NULL;
  499. }
  500. gpu->ebi1_clk = devm_clk_get(&pdev->dev, "bus_clk");
  501. DBG("ebi1_clk: %p", gpu->ebi1_clk);
  502. if (IS_ERR(gpu->ebi1_clk))
  503. gpu->ebi1_clk = NULL;
  504. /* Acquire regulators: */
  505. gpu->gpu_reg = devm_regulator_get(&pdev->dev, "vdd");
  506. DBG("gpu_reg: %p", gpu->gpu_reg);
  507. if (IS_ERR(gpu->gpu_reg))
  508. gpu->gpu_reg = NULL;
  509. gpu->gpu_cx = devm_regulator_get(&pdev->dev, "vddcx");
  510. DBG("gpu_cx: %p", gpu->gpu_cx);
  511. if (IS_ERR(gpu->gpu_cx))
  512. gpu->gpu_cx = NULL;
  513. /* Setup IOMMU.. eventually we will (I think) do this once per context
  514. * and have separate page tables per context. For now, to keep things
  515. * simple and to get something working, just use a single address space:
  516. */
  517. iommu = iommu_domain_alloc(&platform_bus_type);
  518. if (iommu) {
  519. dev_info(drm->dev, "%s: using IOMMU\n", name);
  520. gpu->mmu = msm_iommu_new(&pdev->dev, iommu);
  521. if (IS_ERR(gpu->mmu)) {
  522. ret = PTR_ERR(gpu->mmu);
  523. dev_err(drm->dev, "failed to init iommu: %d\n", ret);
  524. gpu->mmu = NULL;
  525. iommu_domain_free(iommu);
  526. goto fail;
  527. }
  528. } else {
  529. dev_info(drm->dev, "%s: no IOMMU, fallback to VRAM carveout!\n", name);
  530. }
  531. gpu->id = msm_register_mmu(drm, gpu->mmu);
  532. /* Create ringbuffer: */
  533. mutex_lock(&drm->struct_mutex);
  534. gpu->rb = msm_ringbuffer_new(gpu, ringsz);
  535. mutex_unlock(&drm->struct_mutex);
  536. if (IS_ERR(gpu->rb)) {
  537. ret = PTR_ERR(gpu->rb);
  538. gpu->rb = NULL;
  539. dev_err(drm->dev, "could not create ringbuffer: %d\n", ret);
  540. goto fail;
  541. }
  542. bs_init(gpu);
  543. return 0;
  544. fail:
  545. return ret;
  546. }
  547. void msm_gpu_cleanup(struct msm_gpu *gpu)
  548. {
  549. DBG("%s", gpu->name);
  550. WARN_ON(!list_empty(&gpu->active_list));
  551. bs_fini(gpu);
  552. if (gpu->rb) {
  553. if (gpu->rb_iova)
  554. msm_gem_put_iova(gpu->rb->bo, gpu->id);
  555. msm_ringbuffer_destroy(gpu->rb);
  556. }
  557. if (gpu->mmu)
  558. gpu->mmu->funcs->destroy(gpu->mmu);
  559. if (gpu->fctx)
  560. msm_fence_context_free(gpu->fctx);
  561. }