msm_gpu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 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. int ret;
  134. DBG("%s", gpu->name);
  135. ret = enable_pwrrail(gpu);
  136. if (ret)
  137. return ret;
  138. ret = enable_clk(gpu);
  139. if (ret)
  140. return ret;
  141. ret = enable_axi(gpu);
  142. if (ret)
  143. return ret;
  144. return 0;
  145. }
  146. int msm_gpu_pm_suspend(struct msm_gpu *gpu)
  147. {
  148. int ret;
  149. DBG("%s", gpu->name);
  150. ret = disable_axi(gpu);
  151. if (ret)
  152. return ret;
  153. ret = disable_clk(gpu);
  154. if (ret)
  155. return ret;
  156. ret = disable_pwrrail(gpu);
  157. if (ret)
  158. return ret;
  159. return 0;
  160. }
  161. /*
  162. * Hangcheck detection for locked gpu:
  163. */
  164. static void recover_worker(struct work_struct *work)
  165. {
  166. struct msm_gpu *gpu = container_of(work, struct msm_gpu, recover_work);
  167. struct drm_device *dev = gpu->dev;
  168. dev_err(dev->dev, "%s: hangcheck recover!\n", gpu->name);
  169. mutex_lock(&dev->struct_mutex);
  170. gpu->funcs->recover(gpu);
  171. mutex_unlock(&dev->struct_mutex);
  172. msm_gpu_retire(gpu);
  173. }
  174. static void hangcheck_timer_reset(struct msm_gpu *gpu)
  175. {
  176. DBG("%s", gpu->name);
  177. mod_timer(&gpu->hangcheck_timer,
  178. round_jiffies_up(jiffies + DRM_MSM_HANGCHECK_JIFFIES));
  179. }
  180. static void hangcheck_handler(unsigned long data)
  181. {
  182. struct msm_gpu *gpu = (struct msm_gpu *)data;
  183. struct drm_device *dev = gpu->dev;
  184. struct msm_drm_private *priv = dev->dev_private;
  185. uint32_t fence = gpu->funcs->last_fence(gpu);
  186. if (fence != gpu->hangcheck_fence) {
  187. /* some progress has been made.. ya! */
  188. gpu->hangcheck_fence = fence;
  189. } else if (fence < gpu->submitted_fence) {
  190. /* no progress and not done.. hung! */
  191. gpu->hangcheck_fence = fence;
  192. dev_err(dev->dev, "%s: hangcheck detected gpu lockup!\n",
  193. gpu->name);
  194. dev_err(dev->dev, "%s: completed fence: %u\n",
  195. gpu->name, fence);
  196. dev_err(dev->dev, "%s: submitted fence: %u\n",
  197. gpu->name, gpu->submitted_fence);
  198. queue_work(priv->wq, &gpu->recover_work);
  199. }
  200. /* if still more pending work, reset the hangcheck timer: */
  201. if (gpu->submitted_fence > gpu->hangcheck_fence)
  202. hangcheck_timer_reset(gpu);
  203. /* workaround for missing irq: */
  204. queue_work(priv->wq, &gpu->retire_work);
  205. }
  206. /*
  207. * Cmdstream submission/retirement:
  208. */
  209. static void retire_worker(struct work_struct *work)
  210. {
  211. struct msm_gpu *gpu = container_of(work, struct msm_gpu, retire_work);
  212. struct drm_device *dev = gpu->dev;
  213. uint32_t fence = gpu->funcs->last_fence(gpu);
  214. msm_update_fence(gpu->dev, fence);
  215. mutex_lock(&dev->struct_mutex);
  216. while (!list_empty(&gpu->active_list)) {
  217. struct msm_gem_object *obj;
  218. obj = list_first_entry(&gpu->active_list,
  219. struct msm_gem_object, mm_list);
  220. if ((obj->read_fence <= fence) &&
  221. (obj->write_fence <= fence)) {
  222. /* move to inactive: */
  223. msm_gem_move_to_inactive(&obj->base);
  224. msm_gem_put_iova(&obj->base, gpu->id);
  225. drm_gem_object_unreference(&obj->base);
  226. } else {
  227. break;
  228. }
  229. }
  230. mutex_unlock(&dev->struct_mutex);
  231. }
  232. /* call from irq handler to schedule work to retire bo's */
  233. void msm_gpu_retire(struct msm_gpu *gpu)
  234. {
  235. struct msm_drm_private *priv = gpu->dev->dev_private;
  236. queue_work(priv->wq, &gpu->retire_work);
  237. }
  238. /* add bo's to gpu's ring, and kick gpu: */
  239. int msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
  240. struct msm_file_private *ctx)
  241. {
  242. struct drm_device *dev = gpu->dev;
  243. struct msm_drm_private *priv = dev->dev_private;
  244. int i, ret;
  245. submit->fence = ++priv->next_fence;
  246. gpu->submitted_fence = submit->fence;
  247. ret = gpu->funcs->submit(gpu, submit, ctx);
  248. priv->lastctx = ctx;
  249. for (i = 0; i < submit->nr_bos; i++) {
  250. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  251. /* can't happen yet.. but when we add 2d support we'll have
  252. * to deal w/ cross-ring synchronization:
  253. */
  254. WARN_ON(is_active(msm_obj) && (msm_obj->gpu != gpu));
  255. if (!is_active(msm_obj)) {
  256. uint32_t iova;
  257. /* ring takes a reference to the bo and iova: */
  258. drm_gem_object_reference(&msm_obj->base);
  259. msm_gem_get_iova_locked(&msm_obj->base,
  260. submit->gpu->id, &iova);
  261. }
  262. if (submit->bos[i].flags & MSM_SUBMIT_BO_READ)
  263. msm_gem_move_to_active(&msm_obj->base, gpu, false, submit->fence);
  264. if (submit->bos[i].flags & MSM_SUBMIT_BO_WRITE)
  265. msm_gem_move_to_active(&msm_obj->base, gpu, true, submit->fence);
  266. }
  267. hangcheck_timer_reset(gpu);
  268. return ret;
  269. }
  270. /*
  271. * Init/Cleanup:
  272. */
  273. static irqreturn_t irq_handler(int irq, void *data)
  274. {
  275. struct msm_gpu *gpu = data;
  276. return gpu->funcs->irq(gpu);
  277. }
  278. static const char *clk_names[] = {
  279. "src_clk", "core_clk", "iface_clk", "mem_clk", "mem_iface_clk",
  280. };
  281. int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
  282. struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs,
  283. const char *name, const char *ioname, const char *irqname, int ringsz)
  284. {
  285. struct iommu_domain *iommu;
  286. int i, ret;
  287. gpu->dev = drm;
  288. gpu->funcs = funcs;
  289. gpu->name = name;
  290. INIT_LIST_HEAD(&gpu->active_list);
  291. INIT_WORK(&gpu->retire_work, retire_worker);
  292. INIT_WORK(&gpu->recover_work, recover_worker);
  293. setup_timer(&gpu->hangcheck_timer, hangcheck_handler,
  294. (unsigned long)gpu);
  295. BUG_ON(ARRAY_SIZE(clk_names) != ARRAY_SIZE(gpu->grp_clks));
  296. /* Map registers: */
  297. gpu->mmio = msm_ioremap(pdev, ioname, name);
  298. if (IS_ERR(gpu->mmio)) {
  299. ret = PTR_ERR(gpu->mmio);
  300. goto fail;
  301. }
  302. /* Get Interrupt: */
  303. gpu->irq = platform_get_irq_byname(pdev, irqname);
  304. if (gpu->irq < 0) {
  305. ret = gpu->irq;
  306. dev_err(drm->dev, "failed to get irq: %d\n", ret);
  307. goto fail;
  308. }
  309. ret = devm_request_irq(&pdev->dev, gpu->irq, irq_handler,
  310. IRQF_TRIGGER_HIGH, gpu->name, gpu);
  311. if (ret) {
  312. dev_err(drm->dev, "failed to request IRQ%u: %d\n", gpu->irq, ret);
  313. goto fail;
  314. }
  315. /* Acquire clocks: */
  316. for (i = 0; i < ARRAY_SIZE(clk_names); i++) {
  317. gpu->grp_clks[i] = devm_clk_get(&pdev->dev, clk_names[i]);
  318. DBG("grp_clks[%s]: %p", clk_names[i], gpu->grp_clks[i]);
  319. if (IS_ERR(gpu->grp_clks[i]))
  320. gpu->grp_clks[i] = NULL;
  321. }
  322. gpu->ebi1_clk = devm_clk_get(&pdev->dev, "bus_clk");
  323. DBG("ebi1_clk: %p", gpu->ebi1_clk);
  324. if (IS_ERR(gpu->ebi1_clk))
  325. gpu->ebi1_clk = NULL;
  326. /* Acquire regulators: */
  327. gpu->gpu_reg = devm_regulator_get(&pdev->dev, "vdd");
  328. DBG("gpu_reg: %p", gpu->gpu_reg);
  329. if (IS_ERR(gpu->gpu_reg))
  330. gpu->gpu_reg = NULL;
  331. gpu->gpu_cx = devm_regulator_get(&pdev->dev, "vddcx");
  332. DBG("gpu_cx: %p", gpu->gpu_cx);
  333. if (IS_ERR(gpu->gpu_cx))
  334. gpu->gpu_cx = NULL;
  335. /* Setup IOMMU.. eventually we will (I think) do this once per context
  336. * and have separate page tables per context. For now, to keep things
  337. * simple and to get something working, just use a single address space:
  338. */
  339. iommu = iommu_domain_alloc(&platform_bus_type);
  340. if (iommu) {
  341. dev_info(drm->dev, "%s: using IOMMU\n", name);
  342. gpu->mmu = msm_iommu_new(drm, iommu);
  343. } else {
  344. dev_info(drm->dev, "%s: no IOMMU, fallback to VRAM carveout!\n", name);
  345. }
  346. gpu->id = msm_register_mmu(drm, gpu->mmu);
  347. /* Create ringbuffer: */
  348. gpu->rb = msm_ringbuffer_new(gpu, ringsz);
  349. if (IS_ERR(gpu->rb)) {
  350. ret = PTR_ERR(gpu->rb);
  351. gpu->rb = NULL;
  352. dev_err(drm->dev, "could not create ringbuffer: %d\n", ret);
  353. goto fail;
  354. }
  355. ret = msm_gem_get_iova_locked(gpu->rb->bo, gpu->id, &gpu->rb_iova);
  356. if (ret) {
  357. gpu->rb_iova = 0;
  358. dev_err(drm->dev, "could not map ringbuffer: %d\n", ret);
  359. goto fail;
  360. }
  361. bs_init(gpu);
  362. return 0;
  363. fail:
  364. return ret;
  365. }
  366. void msm_gpu_cleanup(struct msm_gpu *gpu)
  367. {
  368. DBG("%s", gpu->name);
  369. WARN_ON(!list_empty(&gpu->active_list));
  370. bs_fini(gpu);
  371. if (gpu->rb) {
  372. if (gpu->rb_iova)
  373. msm_gem_put_iova(gpu->rb->bo, gpu->id);
  374. msm_ringbuffer_destroy(gpu->rb);
  375. }
  376. if (gpu->mmu)
  377. gpu->mmu->funcs->destroy(gpu->mmu);
  378. }