msm_gpu.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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->core_clk && gpu->fast_rate)
  84. clk_set_rate(gpu->core_clk, gpu->fast_rate);
  85. /* Set the RBBM timer rate to 19.2Mhz */
  86. if (gpu->rbbmtimer_clk)
  87. clk_set_rate(gpu->rbbmtimer_clk, 19200000);
  88. for (i = gpu->nr_clocks - 1; i >= 0; i--)
  89. if (gpu->grp_clks[i])
  90. clk_prepare(gpu->grp_clks[i]);
  91. for (i = gpu->nr_clocks - 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 = gpu->nr_clocks - 1; i >= 0; i--)
  100. if (gpu->grp_clks[i])
  101. clk_disable(gpu->grp_clks[i]);
  102. for (i = gpu->nr_clocks - 1; i >= 0; i--)
  103. if (gpu->grp_clks[i])
  104. clk_unprepare(gpu->grp_clks[i]);
  105. /*
  106. * Set the clock to a deliberately low rate. On older targets the clock
  107. * speed had to be non zero to avoid problems. On newer targets this
  108. * will be rounded down to zero anyway so it all works out.
  109. */
  110. if (gpu->core_clk)
  111. clk_set_rate(gpu->core_clk, 27000000);
  112. if (gpu->rbbmtimer_clk)
  113. clk_set_rate(gpu->rbbmtimer_clk, 0);
  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. int ret;
  135. DBG("%s", gpu->name);
  136. ret = enable_pwrrail(gpu);
  137. if (ret)
  138. return ret;
  139. ret = enable_clk(gpu);
  140. if (ret)
  141. return ret;
  142. ret = enable_axi(gpu);
  143. if (ret)
  144. return ret;
  145. gpu->needs_hw_init = true;
  146. return 0;
  147. }
  148. int msm_gpu_pm_suspend(struct msm_gpu *gpu)
  149. {
  150. int ret;
  151. DBG("%s", gpu->name);
  152. ret = disable_axi(gpu);
  153. if (ret)
  154. return ret;
  155. ret = disable_clk(gpu);
  156. if (ret)
  157. return ret;
  158. ret = disable_pwrrail(gpu);
  159. if (ret)
  160. return ret;
  161. return 0;
  162. }
  163. int msm_gpu_hw_init(struct msm_gpu *gpu)
  164. {
  165. int ret;
  166. WARN_ON(!mutex_is_locked(&gpu->dev->struct_mutex));
  167. if (!gpu->needs_hw_init)
  168. return 0;
  169. disable_irq(gpu->irq);
  170. ret = gpu->funcs->hw_init(gpu);
  171. if (!ret)
  172. gpu->needs_hw_init = false;
  173. enable_irq(gpu->irq);
  174. return ret;
  175. }
  176. /*
  177. * Hangcheck detection for locked gpu:
  178. */
  179. static void update_fences(struct msm_gpu *gpu, struct msm_ringbuffer *ring,
  180. uint32_t fence)
  181. {
  182. struct msm_gem_submit *submit;
  183. list_for_each_entry(submit, &ring->submits, node) {
  184. if (submit->seqno > fence)
  185. break;
  186. msm_update_fence(submit->ring->fctx,
  187. submit->fence->seqno);
  188. }
  189. }
  190. static void retire_submits(struct msm_gpu *gpu);
  191. static void recover_worker(struct work_struct *work)
  192. {
  193. struct msm_gpu *gpu = container_of(work, struct msm_gpu, recover_work);
  194. struct drm_device *dev = gpu->dev;
  195. struct msm_gem_submit *submit;
  196. struct msm_ringbuffer *cur_ring = gpu->funcs->active_ring(gpu);
  197. uint64_t fence;
  198. int i;
  199. /* Update all the rings with the latest and greatest fence */
  200. for (i = 0; i < ARRAY_SIZE(gpu->rb); i++) {
  201. struct msm_ringbuffer *ring = gpu->rb[i];
  202. fence = ring->memptrs->fence;
  203. /*
  204. * For the current (faulting?) ring/submit advance the fence by
  205. * one more to clear the faulting submit
  206. */
  207. if (ring == cur_ring)
  208. fence = fence + 1;
  209. update_fences(gpu, ring, fence);
  210. }
  211. mutex_lock(&dev->struct_mutex);
  212. dev_err(dev->dev, "%s: hangcheck recover!\n", gpu->name);
  213. fence = cur_ring->memptrs->fence + 1;
  214. list_for_each_entry(submit, &cur_ring->submits, node) {
  215. if (submit->seqno == fence) {
  216. struct task_struct *task;
  217. rcu_read_lock();
  218. task = pid_task(submit->pid, PIDTYPE_PID);
  219. if (task) {
  220. dev_err(dev->dev, "%s: offending task: %s\n",
  221. gpu->name, task->comm);
  222. }
  223. rcu_read_unlock();
  224. break;
  225. }
  226. }
  227. if (msm_gpu_active(gpu)) {
  228. /* retire completed submits, plus the one that hung: */
  229. retire_submits(gpu);
  230. pm_runtime_get_sync(&gpu->pdev->dev);
  231. gpu->funcs->recover(gpu);
  232. pm_runtime_put_sync(&gpu->pdev->dev);
  233. /*
  234. * Replay all remaining submits starting with highest priority
  235. * ring
  236. */
  237. for (i = 0; i < gpu->nr_rings; i++) {
  238. struct msm_ringbuffer *ring = gpu->rb[i];
  239. list_for_each_entry(submit, &ring->submits, node)
  240. gpu->funcs->submit(gpu, submit, NULL);
  241. }
  242. }
  243. mutex_unlock(&dev->struct_mutex);
  244. msm_gpu_retire(gpu);
  245. }
  246. static void hangcheck_timer_reset(struct msm_gpu *gpu)
  247. {
  248. DBG("%s", gpu->name);
  249. mod_timer(&gpu->hangcheck_timer,
  250. round_jiffies_up(jiffies + DRM_MSM_HANGCHECK_JIFFIES));
  251. }
  252. static void hangcheck_handler(unsigned long data)
  253. {
  254. struct msm_gpu *gpu = (struct msm_gpu *)data;
  255. struct drm_device *dev = gpu->dev;
  256. struct msm_drm_private *priv = dev->dev_private;
  257. struct msm_ringbuffer *ring = gpu->funcs->active_ring(gpu);
  258. uint32_t fence = ring->memptrs->fence;
  259. if (fence != ring->hangcheck_fence) {
  260. /* some progress has been made.. ya! */
  261. ring->hangcheck_fence = fence;
  262. } else if (fence < ring->seqno) {
  263. /* no progress and not done.. hung! */
  264. ring->hangcheck_fence = fence;
  265. dev_err(dev->dev, "%s: hangcheck detected gpu lockup rb %d!\n",
  266. gpu->name, ring->id);
  267. dev_err(dev->dev, "%s: completed fence: %u\n",
  268. gpu->name, fence);
  269. dev_err(dev->dev, "%s: submitted fence: %u\n",
  270. gpu->name, ring->seqno);
  271. queue_work(priv->wq, &gpu->recover_work);
  272. }
  273. /* if still more pending work, reset the hangcheck timer: */
  274. if (ring->seqno > ring->hangcheck_fence)
  275. hangcheck_timer_reset(gpu);
  276. /* workaround for missing irq: */
  277. queue_work(priv->wq, &gpu->retire_work);
  278. }
  279. /*
  280. * Performance Counters:
  281. */
  282. /* called under perf_lock */
  283. static int update_hw_cntrs(struct msm_gpu *gpu, uint32_t ncntrs, uint32_t *cntrs)
  284. {
  285. uint32_t current_cntrs[ARRAY_SIZE(gpu->last_cntrs)];
  286. int i, n = min(ncntrs, gpu->num_perfcntrs);
  287. /* read current values: */
  288. for (i = 0; i < gpu->num_perfcntrs; i++)
  289. current_cntrs[i] = gpu_read(gpu, gpu->perfcntrs[i].sample_reg);
  290. /* update cntrs: */
  291. for (i = 0; i < n; i++)
  292. cntrs[i] = current_cntrs[i] - gpu->last_cntrs[i];
  293. /* save current values: */
  294. for (i = 0; i < gpu->num_perfcntrs; i++)
  295. gpu->last_cntrs[i] = current_cntrs[i];
  296. return n;
  297. }
  298. static void update_sw_cntrs(struct msm_gpu *gpu)
  299. {
  300. ktime_t time;
  301. uint32_t elapsed;
  302. unsigned long flags;
  303. spin_lock_irqsave(&gpu->perf_lock, flags);
  304. if (!gpu->perfcntr_active)
  305. goto out;
  306. time = ktime_get();
  307. elapsed = ktime_to_us(ktime_sub(time, gpu->last_sample.time));
  308. gpu->totaltime += elapsed;
  309. if (gpu->last_sample.active)
  310. gpu->activetime += elapsed;
  311. gpu->last_sample.active = msm_gpu_active(gpu);
  312. gpu->last_sample.time = time;
  313. out:
  314. spin_unlock_irqrestore(&gpu->perf_lock, flags);
  315. }
  316. void msm_gpu_perfcntr_start(struct msm_gpu *gpu)
  317. {
  318. unsigned long flags;
  319. pm_runtime_get_sync(&gpu->pdev->dev);
  320. spin_lock_irqsave(&gpu->perf_lock, flags);
  321. /* we could dynamically enable/disable perfcntr registers too.. */
  322. gpu->last_sample.active = msm_gpu_active(gpu);
  323. gpu->last_sample.time = ktime_get();
  324. gpu->activetime = gpu->totaltime = 0;
  325. gpu->perfcntr_active = true;
  326. update_hw_cntrs(gpu, 0, NULL);
  327. spin_unlock_irqrestore(&gpu->perf_lock, flags);
  328. }
  329. void msm_gpu_perfcntr_stop(struct msm_gpu *gpu)
  330. {
  331. gpu->perfcntr_active = false;
  332. pm_runtime_put_sync(&gpu->pdev->dev);
  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->aspace);
  364. drm_gem_object_unreference(&msm_obj->base);
  365. }
  366. pm_runtime_mark_last_busy(&gpu->pdev->dev);
  367. pm_runtime_put_autosuspend(&gpu->pdev->dev);
  368. msm_gem_submit_free(submit);
  369. }
  370. static void retire_submits(struct msm_gpu *gpu)
  371. {
  372. struct drm_device *dev = gpu->dev;
  373. struct msm_gem_submit *submit, *tmp;
  374. int i;
  375. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  376. /* Retire the commits starting with highest priority */
  377. for (i = 0; i < gpu->nr_rings; i++) {
  378. struct msm_ringbuffer *ring = gpu->rb[i];
  379. list_for_each_entry_safe(submit, tmp, &ring->submits, node) {
  380. if (dma_fence_is_signaled(submit->fence))
  381. retire_submit(gpu, submit);
  382. }
  383. }
  384. }
  385. static void retire_worker(struct work_struct *work)
  386. {
  387. struct msm_gpu *gpu = container_of(work, struct msm_gpu, retire_work);
  388. struct drm_device *dev = gpu->dev;
  389. int i;
  390. for (i = 0; i < gpu->nr_rings; i++)
  391. update_fences(gpu, gpu->rb[i], gpu->rb[i]->memptrs->fence);
  392. mutex_lock(&dev->struct_mutex);
  393. retire_submits(gpu);
  394. mutex_unlock(&dev->struct_mutex);
  395. }
  396. /* call from irq handler to schedule work to retire bo's */
  397. void msm_gpu_retire(struct msm_gpu *gpu)
  398. {
  399. struct msm_drm_private *priv = gpu->dev->dev_private;
  400. queue_work(priv->wq, &gpu->retire_work);
  401. update_sw_cntrs(gpu);
  402. }
  403. /* add bo's to gpu's ring, and kick gpu: */
  404. void msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
  405. struct msm_file_private *ctx)
  406. {
  407. struct drm_device *dev = gpu->dev;
  408. struct msm_drm_private *priv = dev->dev_private;
  409. struct msm_ringbuffer *ring = submit->ring;
  410. int i;
  411. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  412. pm_runtime_get_sync(&gpu->pdev->dev);
  413. msm_gpu_hw_init(gpu);
  414. submit->seqno = ++ring->seqno;
  415. list_add_tail(&submit->node, &ring->submits);
  416. msm_rd_dump_submit(submit);
  417. update_sw_cntrs(gpu);
  418. for (i = 0; i < submit->nr_bos; i++) {
  419. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  420. uint64_t iova;
  421. /* can't happen yet.. but when we add 2d support we'll have
  422. * to deal w/ cross-ring synchronization:
  423. */
  424. WARN_ON(is_active(msm_obj) && (msm_obj->gpu != gpu));
  425. /* submit takes a reference to the bo and iova until retired: */
  426. drm_gem_object_reference(&msm_obj->base);
  427. msm_gem_get_iova(&msm_obj->base,
  428. submit->gpu->aspace, &iova);
  429. if (submit->bos[i].flags & MSM_SUBMIT_BO_WRITE)
  430. msm_gem_move_to_active(&msm_obj->base, gpu, true, submit->fence);
  431. else if (submit->bos[i].flags & MSM_SUBMIT_BO_READ)
  432. msm_gem_move_to_active(&msm_obj->base, gpu, false, submit->fence);
  433. }
  434. gpu->funcs->submit(gpu, submit, ctx);
  435. priv->lastctx = ctx;
  436. hangcheck_timer_reset(gpu);
  437. }
  438. /*
  439. * Init/Cleanup:
  440. */
  441. static irqreturn_t irq_handler(int irq, void *data)
  442. {
  443. struct msm_gpu *gpu = data;
  444. return gpu->funcs->irq(gpu);
  445. }
  446. static struct clk *get_clock(struct device *dev, const char *name)
  447. {
  448. struct clk *clk = devm_clk_get(dev, name);
  449. return IS_ERR(clk) ? NULL : clk;
  450. }
  451. static int get_clocks(struct platform_device *pdev, struct msm_gpu *gpu)
  452. {
  453. struct device *dev = &pdev->dev;
  454. struct property *prop;
  455. const char *name;
  456. int i = 0;
  457. gpu->nr_clocks = of_property_count_strings(dev->of_node, "clock-names");
  458. if (gpu->nr_clocks < 1) {
  459. gpu->nr_clocks = 0;
  460. return 0;
  461. }
  462. gpu->grp_clks = devm_kcalloc(dev, sizeof(struct clk *), gpu->nr_clocks,
  463. GFP_KERNEL);
  464. if (!gpu->grp_clks)
  465. return -ENOMEM;
  466. of_property_for_each_string(dev->of_node, "clock-names", prop, name) {
  467. gpu->grp_clks[i] = get_clock(dev, name);
  468. /* Remember the key clocks that we need to control later */
  469. if (!strcmp(name, "core") || !strcmp(name, "core_clk"))
  470. gpu->core_clk = gpu->grp_clks[i];
  471. else if (!strcmp(name, "rbbmtimer") || !strcmp(name, "rbbmtimer_clk"))
  472. gpu->rbbmtimer_clk = gpu->grp_clks[i];
  473. ++i;
  474. }
  475. return 0;
  476. }
  477. static struct msm_gem_address_space *
  478. msm_gpu_create_address_space(struct msm_gpu *gpu, struct platform_device *pdev,
  479. uint64_t va_start, uint64_t va_end)
  480. {
  481. struct iommu_domain *iommu;
  482. struct msm_gem_address_space *aspace;
  483. int ret;
  484. /*
  485. * Setup IOMMU.. eventually we will (I think) do this once per context
  486. * and have separate page tables per context. For now, to keep things
  487. * simple and to get something working, just use a single address space:
  488. */
  489. iommu = iommu_domain_alloc(&platform_bus_type);
  490. if (!iommu)
  491. return NULL;
  492. iommu->geometry.aperture_start = va_start;
  493. iommu->geometry.aperture_end = va_end;
  494. dev_info(gpu->dev->dev, "%s: using IOMMU\n", gpu->name);
  495. aspace = msm_gem_address_space_create(&pdev->dev, iommu, "gpu");
  496. if (IS_ERR(aspace)) {
  497. dev_err(gpu->dev->dev, "failed to init iommu: %ld\n",
  498. PTR_ERR(aspace));
  499. iommu_domain_free(iommu);
  500. return ERR_CAST(aspace);
  501. }
  502. ret = aspace->mmu->funcs->attach(aspace->mmu, NULL, 0);
  503. if (ret) {
  504. msm_gem_address_space_put(aspace);
  505. return ERR_PTR(ret);
  506. }
  507. return aspace;
  508. }
  509. int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
  510. struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs,
  511. const char *name, struct msm_gpu_config *config)
  512. {
  513. int i, ret, nr_rings = config->nr_rings;
  514. void *memptrs;
  515. uint64_t memptrs_iova;
  516. if (WARN_ON(gpu->num_perfcntrs > ARRAY_SIZE(gpu->last_cntrs)))
  517. gpu->num_perfcntrs = ARRAY_SIZE(gpu->last_cntrs);
  518. gpu->dev = drm;
  519. gpu->funcs = funcs;
  520. gpu->name = name;
  521. INIT_LIST_HEAD(&gpu->active_list);
  522. INIT_WORK(&gpu->retire_work, retire_worker);
  523. INIT_WORK(&gpu->recover_work, recover_worker);
  524. setup_timer(&gpu->hangcheck_timer, hangcheck_handler,
  525. (unsigned long)gpu);
  526. spin_lock_init(&gpu->perf_lock);
  527. /* Map registers: */
  528. gpu->mmio = msm_ioremap(pdev, config->ioname, name);
  529. if (IS_ERR(gpu->mmio)) {
  530. ret = PTR_ERR(gpu->mmio);
  531. goto fail;
  532. }
  533. /* Get Interrupt: */
  534. gpu->irq = platform_get_irq_byname(pdev, config->irqname);
  535. if (gpu->irq < 0) {
  536. ret = gpu->irq;
  537. dev_err(drm->dev, "failed to get irq: %d\n", ret);
  538. goto fail;
  539. }
  540. ret = devm_request_irq(&pdev->dev, gpu->irq, irq_handler,
  541. IRQF_TRIGGER_HIGH, gpu->name, gpu);
  542. if (ret) {
  543. dev_err(drm->dev, "failed to request IRQ%u: %d\n", gpu->irq, ret);
  544. goto fail;
  545. }
  546. ret = get_clocks(pdev, gpu);
  547. if (ret)
  548. goto fail;
  549. gpu->ebi1_clk = msm_clk_get(pdev, "bus");
  550. DBG("ebi1_clk: %p", gpu->ebi1_clk);
  551. if (IS_ERR(gpu->ebi1_clk))
  552. gpu->ebi1_clk = NULL;
  553. /* Acquire regulators: */
  554. gpu->gpu_reg = devm_regulator_get(&pdev->dev, "vdd");
  555. DBG("gpu_reg: %p", gpu->gpu_reg);
  556. if (IS_ERR(gpu->gpu_reg))
  557. gpu->gpu_reg = NULL;
  558. gpu->gpu_cx = devm_regulator_get(&pdev->dev, "vddcx");
  559. DBG("gpu_cx: %p", gpu->gpu_cx);
  560. if (IS_ERR(gpu->gpu_cx))
  561. gpu->gpu_cx = NULL;
  562. gpu->pdev = pdev;
  563. platform_set_drvdata(pdev, gpu);
  564. bs_init(gpu);
  565. gpu->aspace = msm_gpu_create_address_space(gpu, pdev,
  566. config->va_start, config->va_end);
  567. if (gpu->aspace == NULL)
  568. dev_info(drm->dev, "%s: no IOMMU, fallback to VRAM carveout!\n", name);
  569. else if (IS_ERR(gpu->aspace)) {
  570. ret = PTR_ERR(gpu->aspace);
  571. goto fail;
  572. }
  573. memptrs = msm_gem_kernel_new(drm, sizeof(*gpu->memptrs_bo),
  574. MSM_BO_UNCACHED, gpu->aspace, &gpu->memptrs_bo,
  575. &memptrs_iova);
  576. if (IS_ERR(memptrs)) {
  577. ret = PTR_ERR(memptrs);
  578. dev_err(drm->dev, "could not allocate memptrs: %d\n", ret);
  579. goto fail;
  580. }
  581. if (nr_rings > ARRAY_SIZE(gpu->rb)) {
  582. DRM_DEV_INFO_ONCE(drm->dev, "Only creating %lu ringbuffers\n",
  583. ARRAY_SIZE(gpu->rb));
  584. nr_rings = ARRAY_SIZE(gpu->rb);
  585. }
  586. /* Create ringbuffer(s): */
  587. for (i = 0; i < nr_rings; i++) {
  588. gpu->rb[i] = msm_ringbuffer_new(gpu, i, memptrs, memptrs_iova);
  589. if (IS_ERR(gpu->rb[i])) {
  590. ret = PTR_ERR(gpu->rb[i]);
  591. dev_err(drm->dev,
  592. "could not create ringbuffer %d: %d\n", i, ret);
  593. goto fail;
  594. }
  595. memptrs += sizeof(struct msm_rbmemptrs);
  596. memptrs_iova += sizeof(struct msm_rbmemptrs);
  597. }
  598. gpu->nr_rings = nr_rings;
  599. return 0;
  600. fail:
  601. for (i = 0; i < ARRAY_SIZE(gpu->rb); i++) {
  602. msm_ringbuffer_destroy(gpu->rb[i]);
  603. gpu->rb[i] = NULL;
  604. }
  605. if (gpu->memptrs_bo) {
  606. msm_gem_put_vaddr(gpu->memptrs_bo);
  607. msm_gem_put_iova(gpu->memptrs_bo, gpu->aspace);
  608. drm_gem_object_unreference_unlocked(gpu->memptrs_bo);
  609. }
  610. platform_set_drvdata(pdev, NULL);
  611. return ret;
  612. }
  613. void msm_gpu_cleanup(struct msm_gpu *gpu)
  614. {
  615. int i;
  616. DBG("%s", gpu->name);
  617. WARN_ON(!list_empty(&gpu->active_list));
  618. bs_fini(gpu);
  619. for (i = 0; i < ARRAY_SIZE(gpu->rb); i++) {
  620. msm_ringbuffer_destroy(gpu->rb[i]);
  621. gpu->rb[i] = NULL;
  622. }
  623. if (gpu->memptrs_bo) {
  624. msm_gem_put_vaddr(gpu->memptrs_bo);
  625. msm_gem_put_iova(gpu->memptrs_bo, gpu->aspace);
  626. drm_gem_object_unreference_unlocked(gpu->memptrs_bo);
  627. }
  628. if (!IS_ERR_OR_NULL(gpu->aspace)) {
  629. gpu->aspace->mmu->funcs->detach(gpu->aspace->mmu,
  630. NULL, 0);
  631. msm_gem_address_space_put(gpu->aspace);
  632. }
  633. }