msm_gpu.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. 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. for (i = ARRAY_SIZE(gpu->grp_clks) - 1; i >= 0; i--)
  89. if (gpu->grp_clks[i])
  90. clk_enable(gpu->grp_clks[i]);
  91. return 0;
  92. }
  93. static int disable_clk(struct msm_gpu *gpu)
  94. {
  95. int i;
  96. for (i = ARRAY_SIZE(gpu->grp_clks) - 1; i >= 0; i--)
  97. if (gpu->grp_clks[i])
  98. clk_disable(gpu->grp_clks[i]);
  99. for (i = ARRAY_SIZE(gpu->grp_clks) - 1; i >= 0; i--)
  100. if (gpu->grp_clks[i])
  101. clk_unprepare(gpu->grp_clks[i]);
  102. if (gpu->grp_clks[0] && gpu->slow_rate)
  103. clk_set_rate(gpu->grp_clks[0], gpu->slow_rate);
  104. return 0;
  105. }
  106. static int enable_axi(struct msm_gpu *gpu)
  107. {
  108. if (gpu->ebi1_clk)
  109. clk_prepare_enable(gpu->ebi1_clk);
  110. if (gpu->bus_freq)
  111. bs_set(gpu, gpu->bus_freq);
  112. return 0;
  113. }
  114. static int disable_axi(struct msm_gpu *gpu)
  115. {
  116. if (gpu->ebi1_clk)
  117. clk_disable_unprepare(gpu->ebi1_clk);
  118. if (gpu->bus_freq)
  119. bs_set(gpu, 0);
  120. return 0;
  121. }
  122. int msm_gpu_pm_resume(struct msm_gpu *gpu)
  123. {
  124. struct drm_device *dev = gpu->dev;
  125. int ret;
  126. DBG("%s: active_cnt=%d", gpu->name, gpu->active_cnt);
  127. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  128. if (gpu->active_cnt++ > 0)
  129. return 0;
  130. if (WARN_ON(gpu->active_cnt <= 0))
  131. return -EINVAL;
  132. ret = enable_pwrrail(gpu);
  133. if (ret)
  134. return ret;
  135. ret = enable_clk(gpu);
  136. if (ret)
  137. return ret;
  138. ret = enable_axi(gpu);
  139. if (ret)
  140. return ret;
  141. return 0;
  142. }
  143. int msm_gpu_pm_suspend(struct msm_gpu *gpu)
  144. {
  145. struct drm_device *dev = gpu->dev;
  146. int ret;
  147. DBG("%s: active_cnt=%d", gpu->name, gpu->active_cnt);
  148. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  149. if (--gpu->active_cnt > 0)
  150. return 0;
  151. if (WARN_ON(gpu->active_cnt < 0))
  152. return -EINVAL;
  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. /*
  165. * Inactivity detection (for suspend):
  166. */
  167. static void inactive_worker(struct work_struct *work)
  168. {
  169. struct msm_gpu *gpu = container_of(work, struct msm_gpu, inactive_work);
  170. struct drm_device *dev = gpu->dev;
  171. if (gpu->inactive)
  172. return;
  173. DBG("%s: inactive!\n", gpu->name);
  174. mutex_lock(&dev->struct_mutex);
  175. if (!(msm_gpu_active(gpu) || gpu->inactive)) {
  176. disable_axi(gpu);
  177. disable_clk(gpu);
  178. gpu->inactive = true;
  179. }
  180. mutex_unlock(&dev->struct_mutex);
  181. }
  182. static void inactive_handler(unsigned long data)
  183. {
  184. struct msm_gpu *gpu = (struct msm_gpu *)data;
  185. struct msm_drm_private *priv = gpu->dev->dev_private;
  186. queue_work(priv->wq, &gpu->inactive_work);
  187. }
  188. /* cancel inactive timer and make sure we are awake: */
  189. static void inactive_cancel(struct msm_gpu *gpu)
  190. {
  191. DBG("%s", gpu->name);
  192. del_timer(&gpu->inactive_timer);
  193. if (gpu->inactive) {
  194. enable_clk(gpu);
  195. enable_axi(gpu);
  196. gpu->inactive = false;
  197. }
  198. }
  199. static void inactive_start(struct msm_gpu *gpu)
  200. {
  201. DBG("%s", gpu->name);
  202. mod_timer(&gpu->inactive_timer,
  203. round_jiffies_up(jiffies + DRM_MSM_INACTIVE_JIFFIES));
  204. }
  205. /*
  206. * Hangcheck detection for locked gpu:
  207. */
  208. static void retire_submits(struct msm_gpu *gpu);
  209. static void recover_worker(struct work_struct *work)
  210. {
  211. struct msm_gpu *gpu = container_of(work, struct msm_gpu, recover_work);
  212. struct drm_device *dev = gpu->dev;
  213. struct msm_gem_submit *submit;
  214. uint32_t fence = gpu->funcs->last_fence(gpu);
  215. msm_update_fence(gpu->fctx, fence + 1);
  216. mutex_lock(&dev->struct_mutex);
  217. dev_err(dev->dev, "%s: hangcheck recover!\n", gpu->name);
  218. list_for_each_entry(submit, &gpu->submit_list, node) {
  219. if (submit->fence->seqno == (fence + 1)) {
  220. struct task_struct *task;
  221. rcu_read_lock();
  222. task = pid_task(submit->pid, PIDTYPE_PID);
  223. if (task) {
  224. dev_err(dev->dev, "%s: offending task: %s\n",
  225. gpu->name, task->comm);
  226. }
  227. rcu_read_unlock();
  228. break;
  229. }
  230. }
  231. if (msm_gpu_active(gpu)) {
  232. /* retire completed submits, plus the one that hung: */
  233. retire_submits(gpu);
  234. inactive_cancel(gpu);
  235. gpu->funcs->recover(gpu);
  236. /* replay the remaining submits after the one that hung: */
  237. list_for_each_entry(submit, &gpu->submit_list, node) {
  238. gpu->funcs->submit(gpu, submit, NULL);
  239. }
  240. }
  241. mutex_unlock(&dev->struct_mutex);
  242. msm_gpu_retire(gpu);
  243. }
  244. static void hangcheck_timer_reset(struct msm_gpu *gpu)
  245. {
  246. DBG("%s", gpu->name);
  247. mod_timer(&gpu->hangcheck_timer,
  248. round_jiffies_up(jiffies + DRM_MSM_HANGCHECK_JIFFIES));
  249. }
  250. static void hangcheck_handler(unsigned long data)
  251. {
  252. struct msm_gpu *gpu = (struct msm_gpu *)data;
  253. struct drm_device *dev = gpu->dev;
  254. struct msm_drm_private *priv = dev->dev_private;
  255. uint32_t fence = gpu->funcs->last_fence(gpu);
  256. if (fence != gpu->hangcheck_fence) {
  257. /* some progress has been made.. ya! */
  258. gpu->hangcheck_fence = fence;
  259. } else if (fence < gpu->fctx->last_fence) {
  260. /* no progress and not done.. hung! */
  261. gpu->hangcheck_fence = fence;
  262. dev_err(dev->dev, "%s: hangcheck detected gpu lockup!\n",
  263. gpu->name);
  264. dev_err(dev->dev, "%s: completed fence: %u\n",
  265. gpu->name, fence);
  266. dev_err(dev->dev, "%s: submitted fence: %u\n",
  267. gpu->name, gpu->fctx->last_fence);
  268. queue_work(priv->wq, &gpu->recover_work);
  269. }
  270. /* if still more pending work, reset the hangcheck timer: */
  271. if (gpu->fctx->last_fence > gpu->hangcheck_fence)
  272. hangcheck_timer_reset(gpu);
  273. /* workaround for missing irq: */
  274. queue_work(priv->wq, &gpu->retire_work);
  275. }
  276. /*
  277. * Performance Counters:
  278. */
  279. /* called under perf_lock */
  280. static int update_hw_cntrs(struct msm_gpu *gpu, uint32_t ncntrs, uint32_t *cntrs)
  281. {
  282. uint32_t current_cntrs[ARRAY_SIZE(gpu->last_cntrs)];
  283. int i, n = min(ncntrs, gpu->num_perfcntrs);
  284. /* read current values: */
  285. for (i = 0; i < gpu->num_perfcntrs; i++)
  286. current_cntrs[i] = gpu_read(gpu, gpu->perfcntrs[i].sample_reg);
  287. /* update cntrs: */
  288. for (i = 0; i < n; i++)
  289. cntrs[i] = current_cntrs[i] - gpu->last_cntrs[i];
  290. /* save current values: */
  291. for (i = 0; i < gpu->num_perfcntrs; i++)
  292. gpu->last_cntrs[i] = current_cntrs[i];
  293. return n;
  294. }
  295. static void update_sw_cntrs(struct msm_gpu *gpu)
  296. {
  297. ktime_t time;
  298. uint32_t elapsed;
  299. unsigned long flags;
  300. spin_lock_irqsave(&gpu->perf_lock, flags);
  301. if (!gpu->perfcntr_active)
  302. goto out;
  303. time = ktime_get();
  304. elapsed = ktime_to_us(ktime_sub(time, gpu->last_sample.time));
  305. gpu->totaltime += elapsed;
  306. if (gpu->last_sample.active)
  307. gpu->activetime += elapsed;
  308. gpu->last_sample.active = msm_gpu_active(gpu);
  309. gpu->last_sample.time = time;
  310. out:
  311. spin_unlock_irqrestore(&gpu->perf_lock, flags);
  312. }
  313. void msm_gpu_perfcntr_start(struct msm_gpu *gpu)
  314. {
  315. unsigned long flags;
  316. spin_lock_irqsave(&gpu->perf_lock, flags);
  317. /* we could dynamically enable/disable perfcntr registers too.. */
  318. gpu->last_sample.active = msm_gpu_active(gpu);
  319. gpu->last_sample.time = ktime_get();
  320. gpu->activetime = gpu->totaltime = 0;
  321. gpu->perfcntr_active = true;
  322. update_hw_cntrs(gpu, 0, NULL);
  323. spin_unlock_irqrestore(&gpu->perf_lock, flags);
  324. }
  325. void msm_gpu_perfcntr_stop(struct msm_gpu *gpu)
  326. {
  327. gpu->perfcntr_active = false;
  328. }
  329. /* returns -errno or # of cntrs sampled */
  330. int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime,
  331. uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs)
  332. {
  333. unsigned long flags;
  334. int ret;
  335. spin_lock_irqsave(&gpu->perf_lock, flags);
  336. if (!gpu->perfcntr_active) {
  337. ret = -EINVAL;
  338. goto out;
  339. }
  340. *activetime = gpu->activetime;
  341. *totaltime = gpu->totaltime;
  342. gpu->activetime = gpu->totaltime = 0;
  343. ret = update_hw_cntrs(gpu, ncntrs, cntrs);
  344. out:
  345. spin_unlock_irqrestore(&gpu->perf_lock, flags);
  346. return ret;
  347. }
  348. /*
  349. * Cmdstream submission/retirement:
  350. */
  351. static void retire_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
  352. {
  353. int i;
  354. for (i = 0; i < submit->nr_bos; i++) {
  355. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  356. /* move to inactive: */
  357. msm_gem_move_to_inactive(&msm_obj->base);
  358. msm_gem_put_iova(&msm_obj->base, gpu->id);
  359. drm_gem_object_unreference(&msm_obj->base);
  360. }
  361. msm_gem_submit_free(submit);
  362. }
  363. static void retire_submits(struct msm_gpu *gpu)
  364. {
  365. struct drm_device *dev = gpu->dev;
  366. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  367. while (!list_empty(&gpu->submit_list)) {
  368. struct msm_gem_submit *submit;
  369. submit = list_first_entry(&gpu->submit_list,
  370. struct msm_gem_submit, node);
  371. if (dma_fence_is_signaled(submit->fence)) {
  372. retire_submit(gpu, submit);
  373. } else {
  374. break;
  375. }
  376. }
  377. }
  378. static void retire_worker(struct work_struct *work)
  379. {
  380. struct msm_gpu *gpu = container_of(work, struct msm_gpu, retire_work);
  381. struct drm_device *dev = gpu->dev;
  382. uint32_t fence = gpu->funcs->last_fence(gpu);
  383. msm_update_fence(gpu->fctx, fence);
  384. mutex_lock(&dev->struct_mutex);
  385. retire_submits(gpu);
  386. mutex_unlock(&dev->struct_mutex);
  387. if (!msm_gpu_active(gpu))
  388. inactive_start(gpu);
  389. }
  390. /* call from irq handler to schedule work to retire bo's */
  391. void msm_gpu_retire(struct msm_gpu *gpu)
  392. {
  393. struct msm_drm_private *priv = gpu->dev->dev_private;
  394. queue_work(priv->wq, &gpu->retire_work);
  395. update_sw_cntrs(gpu);
  396. }
  397. /* add bo's to gpu's ring, and kick gpu: */
  398. void msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
  399. struct msm_file_private *ctx)
  400. {
  401. struct drm_device *dev = gpu->dev;
  402. struct msm_drm_private *priv = dev->dev_private;
  403. int i;
  404. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  405. inactive_cancel(gpu);
  406. list_add_tail(&submit->node, &gpu->submit_list);
  407. msm_rd_dump_submit(submit);
  408. update_sw_cntrs(gpu);
  409. for (i = 0; i < submit->nr_bos; i++) {
  410. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  411. uint64_t iova;
  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. /* submit takes a reference to the bo and iova until retired: */
  417. drm_gem_object_reference(&msm_obj->base);
  418. msm_gem_get_iova_locked(&msm_obj->base,
  419. submit->gpu->id, &iova);
  420. if (submit->bos[i].flags & MSM_SUBMIT_BO_WRITE)
  421. msm_gem_move_to_active(&msm_obj->base, gpu, true, submit->fence);
  422. else if (submit->bos[i].flags & MSM_SUBMIT_BO_READ)
  423. msm_gem_move_to_active(&msm_obj->base, gpu, false, submit->fence);
  424. }
  425. gpu->funcs->submit(gpu, submit, ctx);
  426. priv->lastctx = ctx;
  427. hangcheck_timer_reset(gpu);
  428. }
  429. /*
  430. * Init/Cleanup:
  431. */
  432. static irqreturn_t irq_handler(int irq, void *data)
  433. {
  434. struct msm_gpu *gpu = data;
  435. return gpu->funcs->irq(gpu);
  436. }
  437. static const char *clk_names[] = {
  438. "core_clk", "iface_clk", "mem_clk", "mem_iface_clk",
  439. "alt_mem_iface_clk",
  440. };
  441. int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
  442. struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs,
  443. const char *name, const char *ioname, const char *irqname, int ringsz)
  444. {
  445. struct iommu_domain *iommu;
  446. int i, ret;
  447. if (WARN_ON(gpu->num_perfcntrs > ARRAY_SIZE(gpu->last_cntrs)))
  448. gpu->num_perfcntrs = ARRAY_SIZE(gpu->last_cntrs);
  449. gpu->dev = drm;
  450. gpu->funcs = funcs;
  451. gpu->name = name;
  452. gpu->inactive = true;
  453. gpu->fctx = msm_fence_context_alloc(drm, name);
  454. if (IS_ERR(gpu->fctx)) {
  455. ret = PTR_ERR(gpu->fctx);
  456. gpu->fctx = NULL;
  457. goto fail;
  458. }
  459. INIT_LIST_HEAD(&gpu->active_list);
  460. INIT_WORK(&gpu->retire_work, retire_worker);
  461. INIT_WORK(&gpu->inactive_work, inactive_worker);
  462. INIT_WORK(&gpu->recover_work, recover_worker);
  463. INIT_LIST_HEAD(&gpu->submit_list);
  464. setup_timer(&gpu->inactive_timer, inactive_handler,
  465. (unsigned long)gpu);
  466. setup_timer(&gpu->hangcheck_timer, hangcheck_handler,
  467. (unsigned long)gpu);
  468. spin_lock_init(&gpu->perf_lock);
  469. BUG_ON(ARRAY_SIZE(clk_names) != ARRAY_SIZE(gpu->grp_clks));
  470. /* Map registers: */
  471. gpu->mmio = msm_ioremap(pdev, ioname, name);
  472. if (IS_ERR(gpu->mmio)) {
  473. ret = PTR_ERR(gpu->mmio);
  474. goto fail;
  475. }
  476. /* Get Interrupt: */
  477. gpu->irq = platform_get_irq_byname(pdev, irqname);
  478. if (gpu->irq < 0) {
  479. ret = gpu->irq;
  480. dev_err(drm->dev, "failed to get irq: %d\n", ret);
  481. goto fail;
  482. }
  483. ret = devm_request_irq(&pdev->dev, gpu->irq, irq_handler,
  484. IRQF_TRIGGER_HIGH, gpu->name, gpu);
  485. if (ret) {
  486. dev_err(drm->dev, "failed to request IRQ%u: %d\n", gpu->irq, ret);
  487. goto fail;
  488. }
  489. /* Acquire clocks: */
  490. for (i = 0; i < ARRAY_SIZE(clk_names); i++) {
  491. gpu->grp_clks[i] = devm_clk_get(&pdev->dev, clk_names[i]);
  492. DBG("grp_clks[%s]: %p", clk_names[i], gpu->grp_clks[i]);
  493. if (IS_ERR(gpu->grp_clks[i]))
  494. gpu->grp_clks[i] = NULL;
  495. }
  496. gpu->ebi1_clk = devm_clk_get(&pdev->dev, "bus_clk");
  497. DBG("ebi1_clk: %p", gpu->ebi1_clk);
  498. if (IS_ERR(gpu->ebi1_clk))
  499. gpu->ebi1_clk = NULL;
  500. /* Acquire regulators: */
  501. gpu->gpu_reg = devm_regulator_get(&pdev->dev, "vdd");
  502. DBG("gpu_reg: %p", gpu->gpu_reg);
  503. if (IS_ERR(gpu->gpu_reg))
  504. gpu->gpu_reg = NULL;
  505. gpu->gpu_cx = devm_regulator_get(&pdev->dev, "vddcx");
  506. DBG("gpu_cx: %p", gpu->gpu_cx);
  507. if (IS_ERR(gpu->gpu_cx))
  508. gpu->gpu_cx = NULL;
  509. /* Setup IOMMU.. eventually we will (I think) do this once per context
  510. * and have separate page tables per context. For now, to keep things
  511. * simple and to get something working, just use a single address space:
  512. */
  513. iommu = iommu_domain_alloc(&platform_bus_type);
  514. if (iommu) {
  515. /* TODO 32b vs 64b address space.. */
  516. iommu->geometry.aperture_start = 0x1000;
  517. iommu->geometry.aperture_end = 0xffffffff;
  518. dev_info(drm->dev, "%s: using IOMMU\n", name);
  519. gpu->aspace = msm_gem_address_space_create(&pdev->dev,
  520. iommu, "gpu");
  521. if (IS_ERR(gpu->aspace)) {
  522. ret = PTR_ERR(gpu->aspace);
  523. dev_err(drm->dev, "failed to init iommu: %d\n", ret);
  524. gpu->aspace = 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_address_space(drm, gpu->aspace);
  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->aspace)
  558. msm_gem_address_space_destroy(gpu->aspace);
  559. if (gpu->fctx)
  560. msm_fence_context_free(gpu->fctx);
  561. }