msm_gpu.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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. int i;
  83. if (gpu->grp_clks[0] && gpu->fast_rate)
  84. clk_set_rate(gpu->grp_clks[0], gpu->fast_rate);
  85. /* Set the RBBM timer rate to 19.2Mhz */
  86. if (gpu->grp_clks[2])
  87. clk_set_rate(gpu->grp_clks[2], 19200000);
  88. for (i = ARRAY_SIZE(gpu->grp_clks) - 1; i >= 0; i--)
  89. if (gpu->grp_clks[i])
  90. clk_prepare(gpu->grp_clks[i]);
  91. for (i = ARRAY_SIZE(gpu->grp_clks) - 1; i >= 0; i--)
  92. if (gpu->grp_clks[i])
  93. clk_enable(gpu->grp_clks[i]);
  94. return 0;
  95. }
  96. static int disable_clk(struct msm_gpu *gpu)
  97. {
  98. int i;
  99. for (i = ARRAY_SIZE(gpu->grp_clks) - 1; i >= 0; i--)
  100. if (gpu->grp_clks[i])
  101. clk_disable(gpu->grp_clks[i]);
  102. for (i = ARRAY_SIZE(gpu->grp_clks) - 1; i >= 0; i--)
  103. if (gpu->grp_clks[i])
  104. clk_unprepare(gpu->grp_clks[i]);
  105. if (gpu->grp_clks[0] && gpu->slow_rate)
  106. clk_set_rate(gpu->grp_clks[0], gpu->slow_rate);
  107. if (gpu->grp_clks[2])
  108. clk_set_rate(gpu->grp_clks[2], 0);
  109. return 0;
  110. }
  111. static int enable_axi(struct msm_gpu *gpu)
  112. {
  113. if (gpu->ebi1_clk)
  114. clk_prepare_enable(gpu->ebi1_clk);
  115. if (gpu->bus_freq)
  116. bs_set(gpu, gpu->bus_freq);
  117. return 0;
  118. }
  119. static int disable_axi(struct msm_gpu *gpu)
  120. {
  121. if (gpu->ebi1_clk)
  122. clk_disable_unprepare(gpu->ebi1_clk);
  123. if (gpu->bus_freq)
  124. bs_set(gpu, 0);
  125. return 0;
  126. }
  127. int msm_gpu_pm_resume(struct msm_gpu *gpu)
  128. {
  129. struct drm_device *dev = gpu->dev;
  130. int ret;
  131. DBG("%s: active_cnt=%d", gpu->name, gpu->active_cnt);
  132. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  133. if (gpu->active_cnt++ > 0)
  134. return 0;
  135. if (WARN_ON(gpu->active_cnt <= 0))
  136. return -EINVAL;
  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. return 0;
  147. }
  148. int msm_gpu_pm_suspend(struct msm_gpu *gpu)
  149. {
  150. struct drm_device *dev = gpu->dev;
  151. int ret;
  152. DBG("%s: active_cnt=%d", gpu->name, gpu->active_cnt);
  153. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  154. if (--gpu->active_cnt > 0)
  155. return 0;
  156. if (WARN_ON(gpu->active_cnt < 0))
  157. return -EINVAL;
  158. ret = disable_axi(gpu);
  159. if (ret)
  160. return ret;
  161. ret = disable_clk(gpu);
  162. if (ret)
  163. return ret;
  164. ret = disable_pwrrail(gpu);
  165. if (ret)
  166. return ret;
  167. return 0;
  168. }
  169. /*
  170. * Inactivity detection (for suspend):
  171. */
  172. static void inactive_worker(struct work_struct *work)
  173. {
  174. struct msm_gpu *gpu = container_of(work, struct msm_gpu, inactive_work);
  175. struct drm_device *dev = gpu->dev;
  176. if (gpu->inactive)
  177. return;
  178. DBG("%s: inactive!\n", gpu->name);
  179. mutex_lock(&dev->struct_mutex);
  180. if (!(msm_gpu_active(gpu) || gpu->inactive)) {
  181. disable_axi(gpu);
  182. disable_clk(gpu);
  183. gpu->inactive = true;
  184. }
  185. mutex_unlock(&dev->struct_mutex);
  186. }
  187. static void inactive_handler(unsigned long data)
  188. {
  189. struct msm_gpu *gpu = (struct msm_gpu *)data;
  190. struct msm_drm_private *priv = gpu->dev->dev_private;
  191. queue_work(priv->wq, &gpu->inactive_work);
  192. }
  193. /* cancel inactive timer and make sure we are awake: */
  194. static void inactive_cancel(struct msm_gpu *gpu)
  195. {
  196. DBG("%s", gpu->name);
  197. del_timer(&gpu->inactive_timer);
  198. if (gpu->inactive) {
  199. enable_clk(gpu);
  200. enable_axi(gpu);
  201. gpu->inactive = false;
  202. }
  203. }
  204. static void inactive_start(struct msm_gpu *gpu)
  205. {
  206. DBG("%s", gpu->name);
  207. mod_timer(&gpu->inactive_timer,
  208. round_jiffies_up(jiffies + DRM_MSM_INACTIVE_JIFFIES));
  209. }
  210. /*
  211. * Hangcheck detection for locked gpu:
  212. */
  213. static void retire_submits(struct msm_gpu *gpu);
  214. static void recover_worker(struct work_struct *work)
  215. {
  216. struct msm_gpu *gpu = container_of(work, struct msm_gpu, recover_work);
  217. struct drm_device *dev = gpu->dev;
  218. struct msm_gem_submit *submit;
  219. uint32_t fence = gpu->funcs->last_fence(gpu);
  220. msm_update_fence(gpu->fctx, fence + 1);
  221. mutex_lock(&dev->struct_mutex);
  222. dev_err(dev->dev, "%s: hangcheck recover!\n", gpu->name);
  223. list_for_each_entry(submit, &gpu->submit_list, node) {
  224. if (submit->fence->seqno == (fence + 1)) {
  225. struct task_struct *task;
  226. rcu_read_lock();
  227. task = pid_task(submit->pid, PIDTYPE_PID);
  228. if (task) {
  229. dev_err(dev->dev, "%s: offending task: %s\n",
  230. gpu->name, task->comm);
  231. }
  232. rcu_read_unlock();
  233. break;
  234. }
  235. }
  236. if (msm_gpu_active(gpu)) {
  237. /* retire completed submits, plus the one that hung: */
  238. retire_submits(gpu);
  239. inactive_cancel(gpu);
  240. gpu->funcs->recover(gpu);
  241. /* replay the remaining submits after the one that hung: */
  242. list_for_each_entry(submit, &gpu->submit_list, node) {
  243. gpu->funcs->submit(gpu, submit, NULL);
  244. }
  245. }
  246. mutex_unlock(&dev->struct_mutex);
  247. msm_gpu_retire(gpu);
  248. }
  249. static void hangcheck_timer_reset(struct msm_gpu *gpu)
  250. {
  251. DBG("%s", gpu->name);
  252. mod_timer(&gpu->hangcheck_timer,
  253. round_jiffies_up(jiffies + DRM_MSM_HANGCHECK_JIFFIES));
  254. }
  255. static void hangcheck_handler(unsigned long data)
  256. {
  257. struct msm_gpu *gpu = (struct msm_gpu *)data;
  258. struct drm_device *dev = gpu->dev;
  259. struct msm_drm_private *priv = dev->dev_private;
  260. uint32_t fence = gpu->funcs->last_fence(gpu);
  261. if (fence != gpu->hangcheck_fence) {
  262. /* some progress has been made.. ya! */
  263. gpu->hangcheck_fence = fence;
  264. } else if (fence < gpu->fctx->last_fence) {
  265. /* no progress and not done.. hung! */
  266. gpu->hangcheck_fence = fence;
  267. dev_err(dev->dev, "%s: hangcheck detected gpu lockup!\n",
  268. gpu->name);
  269. dev_err(dev->dev, "%s: completed fence: %u\n",
  270. gpu->name, fence);
  271. dev_err(dev->dev, "%s: submitted fence: %u\n",
  272. gpu->name, gpu->fctx->last_fence);
  273. queue_work(priv->wq, &gpu->recover_work);
  274. }
  275. /* if still more pending work, reset the hangcheck timer: */
  276. if (gpu->fctx->last_fence > gpu->hangcheck_fence)
  277. hangcheck_timer_reset(gpu);
  278. /* workaround for missing irq: */
  279. queue_work(priv->wq, &gpu->retire_work);
  280. }
  281. /*
  282. * Performance Counters:
  283. */
  284. /* called under perf_lock */
  285. static int update_hw_cntrs(struct msm_gpu *gpu, uint32_t ncntrs, uint32_t *cntrs)
  286. {
  287. uint32_t current_cntrs[ARRAY_SIZE(gpu->last_cntrs)];
  288. int i, n = min(ncntrs, gpu->num_perfcntrs);
  289. /* read current values: */
  290. for (i = 0; i < gpu->num_perfcntrs; i++)
  291. current_cntrs[i] = gpu_read(gpu, gpu->perfcntrs[i].sample_reg);
  292. /* update cntrs: */
  293. for (i = 0; i < n; i++)
  294. cntrs[i] = current_cntrs[i] - gpu->last_cntrs[i];
  295. /* save current values: */
  296. for (i = 0; i < gpu->num_perfcntrs; i++)
  297. gpu->last_cntrs[i] = current_cntrs[i];
  298. return n;
  299. }
  300. static void update_sw_cntrs(struct msm_gpu *gpu)
  301. {
  302. ktime_t time;
  303. uint32_t elapsed;
  304. unsigned long flags;
  305. spin_lock_irqsave(&gpu->perf_lock, flags);
  306. if (!gpu->perfcntr_active)
  307. goto out;
  308. time = ktime_get();
  309. elapsed = ktime_to_us(ktime_sub(time, gpu->last_sample.time));
  310. gpu->totaltime += elapsed;
  311. if (gpu->last_sample.active)
  312. gpu->activetime += elapsed;
  313. gpu->last_sample.active = msm_gpu_active(gpu);
  314. gpu->last_sample.time = time;
  315. out:
  316. spin_unlock_irqrestore(&gpu->perf_lock, flags);
  317. }
  318. void msm_gpu_perfcntr_start(struct msm_gpu *gpu)
  319. {
  320. unsigned long flags;
  321. spin_lock_irqsave(&gpu->perf_lock, flags);
  322. /* we could dynamically enable/disable perfcntr registers too.. */
  323. gpu->last_sample.active = msm_gpu_active(gpu);
  324. gpu->last_sample.time = ktime_get();
  325. gpu->activetime = gpu->totaltime = 0;
  326. gpu->perfcntr_active = true;
  327. update_hw_cntrs(gpu, 0, NULL);
  328. spin_unlock_irqrestore(&gpu->perf_lock, flags);
  329. }
  330. void msm_gpu_perfcntr_stop(struct msm_gpu *gpu)
  331. {
  332. gpu->perfcntr_active = false;
  333. }
  334. /* returns -errno or # of cntrs sampled */
  335. int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime,
  336. uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs)
  337. {
  338. unsigned long flags;
  339. int ret;
  340. spin_lock_irqsave(&gpu->perf_lock, flags);
  341. if (!gpu->perfcntr_active) {
  342. ret = -EINVAL;
  343. goto out;
  344. }
  345. *activetime = gpu->activetime;
  346. *totaltime = gpu->totaltime;
  347. gpu->activetime = gpu->totaltime = 0;
  348. ret = update_hw_cntrs(gpu, ncntrs, cntrs);
  349. out:
  350. spin_unlock_irqrestore(&gpu->perf_lock, flags);
  351. return ret;
  352. }
  353. /*
  354. * Cmdstream submission/retirement:
  355. */
  356. static void retire_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
  357. {
  358. int i;
  359. for (i = 0; i < submit->nr_bos; i++) {
  360. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  361. /* move to inactive: */
  362. msm_gem_move_to_inactive(&msm_obj->base);
  363. msm_gem_put_iova(&msm_obj->base, gpu->id);
  364. drm_gem_object_unreference(&msm_obj->base);
  365. }
  366. msm_gem_submit_free(submit);
  367. }
  368. static void retire_submits(struct msm_gpu *gpu)
  369. {
  370. struct drm_device *dev = gpu->dev;
  371. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  372. while (!list_empty(&gpu->submit_list)) {
  373. struct msm_gem_submit *submit;
  374. submit = list_first_entry(&gpu->submit_list,
  375. struct msm_gem_submit, node);
  376. if (dma_fence_is_signaled(submit->fence)) {
  377. retire_submit(gpu, submit);
  378. } else {
  379. break;
  380. }
  381. }
  382. }
  383. static void retire_worker(struct work_struct *work)
  384. {
  385. struct msm_gpu *gpu = container_of(work, struct msm_gpu, retire_work);
  386. struct drm_device *dev = gpu->dev;
  387. uint32_t fence = gpu->funcs->last_fence(gpu);
  388. msm_update_fence(gpu->fctx, fence);
  389. mutex_lock(&dev->struct_mutex);
  390. retire_submits(gpu);
  391. mutex_unlock(&dev->struct_mutex);
  392. if (!msm_gpu_active(gpu))
  393. inactive_start(gpu);
  394. }
  395. /* call from irq handler to schedule work to retire bo's */
  396. void msm_gpu_retire(struct msm_gpu *gpu)
  397. {
  398. struct msm_drm_private *priv = gpu->dev->dev_private;
  399. queue_work(priv->wq, &gpu->retire_work);
  400. update_sw_cntrs(gpu);
  401. }
  402. /* add bo's to gpu's ring, and kick gpu: */
  403. void msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
  404. struct msm_file_private *ctx)
  405. {
  406. struct drm_device *dev = gpu->dev;
  407. struct msm_drm_private *priv = dev->dev_private;
  408. int i;
  409. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  410. inactive_cancel(gpu);
  411. list_add_tail(&submit->node, &gpu->submit_list);
  412. msm_rd_dump_submit(submit);
  413. update_sw_cntrs(gpu);
  414. for (i = 0; i < submit->nr_bos; i++) {
  415. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  416. uint64_t iova;
  417. /* can't happen yet.. but when we add 2d support we'll have
  418. * to deal w/ cross-ring synchronization:
  419. */
  420. WARN_ON(is_active(msm_obj) && (msm_obj->gpu != gpu));
  421. /* submit takes a reference to the bo and iova until retired: */
  422. drm_gem_object_reference(&msm_obj->base);
  423. msm_gem_get_iova_locked(&msm_obj->base,
  424. submit->gpu->id, &iova);
  425. if (submit->bos[i].flags & MSM_SUBMIT_BO_WRITE)
  426. msm_gem_move_to_active(&msm_obj->base, gpu, true, submit->fence);
  427. else if (submit->bos[i].flags & MSM_SUBMIT_BO_READ)
  428. msm_gem_move_to_active(&msm_obj->base, gpu, false, submit->fence);
  429. }
  430. gpu->funcs->submit(gpu, submit, ctx);
  431. priv->lastctx = ctx;
  432. hangcheck_timer_reset(gpu);
  433. }
  434. /*
  435. * Init/Cleanup:
  436. */
  437. static irqreturn_t irq_handler(int irq, void *data)
  438. {
  439. struct msm_gpu *gpu = data;
  440. return gpu->funcs->irq(gpu);
  441. }
  442. static const char *clk_names[] = {
  443. "core", "iface", "rbbmtimer", "mem", "mem_iface", "alt_mem_iface",
  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] = msm_clk_get(pdev, 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 = msm_clk_get(pdev, "bus");
  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. /* TODO 32b vs 64b address space.. */
  520. iommu->geometry.aperture_start = SZ_16M;
  521. iommu->geometry.aperture_end = 0xffffffff;
  522. dev_info(drm->dev, "%s: using IOMMU\n", name);
  523. gpu->aspace = msm_gem_address_space_create(&pdev->dev,
  524. iommu, "gpu");
  525. if (IS_ERR(gpu->aspace)) {
  526. ret = PTR_ERR(gpu->aspace);
  527. dev_err(drm->dev, "failed to init iommu: %d\n", ret);
  528. gpu->aspace = NULL;
  529. iommu_domain_free(iommu);
  530. goto fail;
  531. }
  532. } else {
  533. dev_info(drm->dev, "%s: no IOMMU, fallback to VRAM carveout!\n", name);
  534. }
  535. gpu->id = msm_register_address_space(drm, gpu->aspace);
  536. /* Create ringbuffer: */
  537. mutex_lock(&drm->struct_mutex);
  538. gpu->rb = msm_ringbuffer_new(gpu, ringsz);
  539. mutex_unlock(&drm->struct_mutex);
  540. if (IS_ERR(gpu->rb)) {
  541. ret = PTR_ERR(gpu->rb);
  542. gpu->rb = NULL;
  543. dev_err(drm->dev, "could not create ringbuffer: %d\n", ret);
  544. goto fail;
  545. }
  546. bs_init(gpu);
  547. return 0;
  548. fail:
  549. return ret;
  550. }
  551. void msm_gpu_cleanup(struct msm_gpu *gpu)
  552. {
  553. DBG("%s", gpu->name);
  554. WARN_ON(!list_empty(&gpu->active_list));
  555. bs_fini(gpu);
  556. if (gpu->rb) {
  557. if (gpu->rb_iova)
  558. msm_gem_put_iova(gpu->rb->bo, gpu->id);
  559. msm_ringbuffer_destroy(gpu->rb);
  560. }
  561. if (gpu->fctx)
  562. msm_fence_context_free(gpu->fctx);
  563. }