msm_gpu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. /*
  20. * Power Management:
  21. */
  22. #ifdef CONFIG_MSM_BUS_SCALING
  23. #include <mach/board.h>
  24. #include <mach/kgsl.h>
  25. static void bs_init(struct msm_gpu *gpu, struct platform_device *pdev)
  26. {
  27. struct drm_device *dev = gpu->dev;
  28. struct kgsl_device_platform_data *pdata;
  29. if (!pdev) {
  30. dev_err(dev->dev, "could not find dtv pdata\n");
  31. return;
  32. }
  33. pdata = pdev->dev.platform_data;
  34. if (pdata->bus_scale_table) {
  35. gpu->bsc = msm_bus_scale_register_client(pdata->bus_scale_table);
  36. DBG("bus scale client: %08x", gpu->bsc);
  37. }
  38. }
  39. static void bs_fini(struct msm_gpu *gpu)
  40. {
  41. if (gpu->bsc) {
  42. msm_bus_scale_unregister_client(gpu->bsc);
  43. gpu->bsc = 0;
  44. }
  45. }
  46. static void bs_set(struct msm_gpu *gpu, int idx)
  47. {
  48. if (gpu->bsc) {
  49. DBG("set bus scaling: %d", idx);
  50. msm_bus_scale_client_update_request(gpu->bsc, idx);
  51. }
  52. }
  53. #else
  54. static void bs_init(struct msm_gpu *gpu, struct platform_device *pdev) {}
  55. static void bs_fini(struct msm_gpu *gpu) {}
  56. static void bs_set(struct msm_gpu *gpu, int idx) {}
  57. #endif
  58. static int enable_pwrrail(struct msm_gpu *gpu)
  59. {
  60. struct drm_device *dev = gpu->dev;
  61. int ret = 0;
  62. if (gpu->gpu_reg) {
  63. ret = regulator_enable(gpu->gpu_reg);
  64. if (ret) {
  65. dev_err(dev->dev, "failed to enable 'gpu_reg': %d\n", ret);
  66. return ret;
  67. }
  68. }
  69. if (gpu->gpu_cx) {
  70. ret = regulator_enable(gpu->gpu_cx);
  71. if (ret) {
  72. dev_err(dev->dev, "failed to enable 'gpu_cx': %d\n", ret);
  73. return ret;
  74. }
  75. }
  76. return 0;
  77. }
  78. static int disable_pwrrail(struct msm_gpu *gpu)
  79. {
  80. if (gpu->gpu_cx)
  81. regulator_disable(gpu->gpu_cx);
  82. if (gpu->gpu_reg)
  83. regulator_disable(gpu->gpu_reg);
  84. return 0;
  85. }
  86. static int enable_clk(struct msm_gpu *gpu)
  87. {
  88. struct clk *rate_clk = NULL;
  89. int i;
  90. /* NOTE: kgsl_pwrctrl_clk() ignores grp_clks[0].. */
  91. for (i = ARRAY_SIZE(gpu->grp_clks) - 1; i > 0; i--) {
  92. if (gpu->grp_clks[i]) {
  93. clk_prepare(gpu->grp_clks[i]);
  94. rate_clk = gpu->grp_clks[i];
  95. }
  96. }
  97. if (rate_clk && gpu->fast_rate)
  98. clk_set_rate(rate_clk, gpu->fast_rate);
  99. for (i = ARRAY_SIZE(gpu->grp_clks) - 1; i > 0; i--)
  100. if (gpu->grp_clks[i])
  101. clk_enable(gpu->grp_clks[i]);
  102. return 0;
  103. }
  104. static int disable_clk(struct msm_gpu *gpu)
  105. {
  106. struct clk *rate_clk = NULL;
  107. int i;
  108. /* NOTE: kgsl_pwrctrl_clk() ignores grp_clks[0].. */
  109. for (i = ARRAY_SIZE(gpu->grp_clks) - 1; i > 0; i--) {
  110. if (gpu->grp_clks[i]) {
  111. clk_disable(gpu->grp_clks[i]);
  112. rate_clk = gpu->grp_clks[i];
  113. }
  114. }
  115. if (rate_clk && gpu->slow_rate)
  116. clk_set_rate(rate_clk, gpu->slow_rate);
  117. for (i = ARRAY_SIZE(gpu->grp_clks) - 1; i > 0; i--)
  118. if (gpu->grp_clks[i])
  119. clk_unprepare(gpu->grp_clks[i]);
  120. return 0;
  121. }
  122. static int enable_axi(struct msm_gpu *gpu)
  123. {
  124. if (gpu->ebi1_clk)
  125. clk_prepare_enable(gpu->ebi1_clk);
  126. if (gpu->bus_freq)
  127. bs_set(gpu, gpu->bus_freq);
  128. return 0;
  129. }
  130. static int disable_axi(struct msm_gpu *gpu)
  131. {
  132. if (gpu->ebi1_clk)
  133. clk_disable_unprepare(gpu->ebi1_clk);
  134. if (gpu->bus_freq)
  135. bs_set(gpu, 0);
  136. return 0;
  137. }
  138. int msm_gpu_pm_resume(struct msm_gpu *gpu)
  139. {
  140. int ret;
  141. DBG("%s", gpu->name);
  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. int ret;
  156. DBG("%s", gpu->name);
  157. ret = disable_axi(gpu);
  158. if (ret)
  159. return ret;
  160. ret = disable_clk(gpu);
  161. if (ret)
  162. return ret;
  163. ret = disable_pwrrail(gpu);
  164. if (ret)
  165. return ret;
  166. return 0;
  167. }
  168. /*
  169. * Hangcheck detection for locked gpu:
  170. */
  171. static void recover_worker(struct work_struct *work)
  172. {
  173. struct msm_gpu *gpu = container_of(work, struct msm_gpu, recover_work);
  174. struct drm_device *dev = gpu->dev;
  175. dev_err(dev->dev, "%s: hangcheck recover!\n", gpu->name);
  176. mutex_lock(&dev->struct_mutex);
  177. gpu->funcs->recover(gpu);
  178. mutex_unlock(&dev->struct_mutex);
  179. msm_gpu_retire(gpu);
  180. }
  181. static void hangcheck_timer_reset(struct msm_gpu *gpu)
  182. {
  183. DBG("%s", gpu->name);
  184. mod_timer(&gpu->hangcheck_timer,
  185. round_jiffies_up(jiffies + DRM_MSM_HANGCHECK_JIFFIES));
  186. }
  187. static void hangcheck_handler(unsigned long data)
  188. {
  189. struct msm_gpu *gpu = (struct msm_gpu *)data;
  190. struct drm_device *dev = gpu->dev;
  191. struct msm_drm_private *priv = dev->dev_private;
  192. uint32_t fence = gpu->funcs->last_fence(gpu);
  193. if (fence != gpu->hangcheck_fence) {
  194. /* some progress has been made.. ya! */
  195. gpu->hangcheck_fence = fence;
  196. } else if (fence < gpu->submitted_fence) {
  197. /* no progress and not done.. hung! */
  198. gpu->hangcheck_fence = fence;
  199. dev_err(dev->dev, "%s: hangcheck detected gpu lockup!\n",
  200. gpu->name);
  201. dev_err(dev->dev, "%s: completed fence: %u\n",
  202. gpu->name, fence);
  203. dev_err(dev->dev, "%s: submitted fence: %u\n",
  204. gpu->name, gpu->submitted_fence);
  205. queue_work(priv->wq, &gpu->recover_work);
  206. }
  207. /* if still more pending work, reset the hangcheck timer: */
  208. if (gpu->submitted_fence > gpu->hangcheck_fence)
  209. hangcheck_timer_reset(gpu);
  210. /* workaround for missing irq: */
  211. queue_work(priv->wq, &gpu->retire_work);
  212. }
  213. /*
  214. * Cmdstream submission/retirement:
  215. */
  216. static void retire_worker(struct work_struct *work)
  217. {
  218. struct msm_gpu *gpu = container_of(work, struct msm_gpu, retire_work);
  219. struct drm_device *dev = gpu->dev;
  220. uint32_t fence = gpu->funcs->last_fence(gpu);
  221. mutex_lock(&dev->struct_mutex);
  222. while (!list_empty(&gpu->active_list)) {
  223. struct msm_gem_object *obj;
  224. obj = list_first_entry(&gpu->active_list,
  225. struct msm_gem_object, mm_list);
  226. if ((obj->read_fence <= fence) &&
  227. (obj->write_fence <= fence)) {
  228. /* move to inactive: */
  229. msm_gem_move_to_inactive(&obj->base);
  230. msm_gem_put_iova(&obj->base, gpu->id);
  231. drm_gem_object_unreference(&obj->base);
  232. } else {
  233. break;
  234. }
  235. }
  236. msm_update_fence(gpu->dev, fence);
  237. mutex_unlock(&dev->struct_mutex);
  238. }
  239. /* call from irq handler to schedule work to retire bo's */
  240. void msm_gpu_retire(struct msm_gpu *gpu)
  241. {
  242. struct msm_drm_private *priv = gpu->dev->dev_private;
  243. queue_work(priv->wq, &gpu->retire_work);
  244. }
  245. /* add bo's to gpu's ring, and kick gpu: */
  246. int msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
  247. struct msm_file_private *ctx)
  248. {
  249. struct drm_device *dev = gpu->dev;
  250. struct msm_drm_private *priv = dev->dev_private;
  251. int i, ret;
  252. mutex_lock(&dev->struct_mutex);
  253. submit->fence = ++priv->next_fence;
  254. gpu->submitted_fence = submit->fence;
  255. ret = gpu->funcs->submit(gpu, submit, ctx);
  256. priv->lastctx = ctx;
  257. for (i = 0; i < submit->nr_bos; i++) {
  258. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  259. /* can't happen yet.. but when we add 2d support we'll have
  260. * to deal w/ cross-ring synchronization:
  261. */
  262. WARN_ON(is_active(msm_obj) && (msm_obj->gpu != gpu));
  263. if (!is_active(msm_obj)) {
  264. uint32_t iova;
  265. /* ring takes a reference to the bo and iova: */
  266. drm_gem_object_reference(&msm_obj->base);
  267. msm_gem_get_iova_locked(&msm_obj->base,
  268. submit->gpu->id, &iova);
  269. }
  270. if (submit->bos[i].flags & MSM_SUBMIT_BO_READ)
  271. msm_gem_move_to_active(&msm_obj->base, gpu, false, submit->fence);
  272. if (submit->bos[i].flags & MSM_SUBMIT_BO_WRITE)
  273. msm_gem_move_to_active(&msm_obj->base, gpu, true, submit->fence);
  274. }
  275. hangcheck_timer_reset(gpu);
  276. mutex_unlock(&dev->struct_mutex);
  277. return ret;
  278. }
  279. /*
  280. * Init/Cleanup:
  281. */
  282. static irqreturn_t irq_handler(int irq, void *data)
  283. {
  284. struct msm_gpu *gpu = data;
  285. return gpu->funcs->irq(gpu);
  286. }
  287. static const char *clk_names[] = {
  288. "src_clk", "core_clk", "iface_clk", "mem_clk", "mem_iface_clk",
  289. };
  290. int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
  291. struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs,
  292. const char *name, const char *ioname, const char *irqname, int ringsz)
  293. {
  294. int i, ret;
  295. gpu->dev = drm;
  296. gpu->funcs = funcs;
  297. gpu->name = name;
  298. INIT_LIST_HEAD(&gpu->active_list);
  299. INIT_WORK(&gpu->retire_work, retire_worker);
  300. INIT_WORK(&gpu->recover_work, recover_worker);
  301. setup_timer(&gpu->hangcheck_timer, hangcheck_handler,
  302. (unsigned long)gpu);
  303. BUG_ON(ARRAY_SIZE(clk_names) != ARRAY_SIZE(gpu->grp_clks));
  304. /* Map registers: */
  305. gpu->mmio = msm_ioremap(pdev, ioname, name);
  306. if (IS_ERR(gpu->mmio)) {
  307. ret = PTR_ERR(gpu->mmio);
  308. goto fail;
  309. }
  310. /* Get Interrupt: */
  311. gpu->irq = platform_get_irq_byname(pdev, irqname);
  312. if (gpu->irq < 0) {
  313. ret = gpu->irq;
  314. dev_err(drm->dev, "failed to get irq: %d\n", ret);
  315. goto fail;
  316. }
  317. ret = devm_request_irq(&pdev->dev, gpu->irq, irq_handler,
  318. IRQF_TRIGGER_HIGH, gpu->name, gpu);
  319. if (ret) {
  320. dev_err(drm->dev, "failed to request IRQ%u: %d\n", gpu->irq, ret);
  321. goto fail;
  322. }
  323. /* Acquire clocks: */
  324. for (i = 0; i < ARRAY_SIZE(clk_names); i++) {
  325. gpu->grp_clks[i] = devm_clk_get(&pdev->dev, clk_names[i]);
  326. DBG("grp_clks[%s]: %p", clk_names[i], gpu->grp_clks[i]);
  327. if (IS_ERR(gpu->grp_clks[i]))
  328. gpu->grp_clks[i] = NULL;
  329. }
  330. gpu->ebi1_clk = devm_clk_get(&pdev->dev, "bus_clk");
  331. DBG("ebi1_clk: %p", gpu->ebi1_clk);
  332. if (IS_ERR(gpu->ebi1_clk))
  333. gpu->ebi1_clk = NULL;
  334. /* Acquire regulators: */
  335. gpu->gpu_reg = devm_regulator_get(&pdev->dev, "vdd");
  336. DBG("gpu_reg: %p", gpu->gpu_reg);
  337. if (IS_ERR(gpu->gpu_reg))
  338. gpu->gpu_reg = NULL;
  339. gpu->gpu_cx = devm_regulator_get(&pdev->dev, "vddcx");
  340. DBG("gpu_cx: %p", gpu->gpu_cx);
  341. if (IS_ERR(gpu->gpu_cx))
  342. gpu->gpu_cx = NULL;
  343. /* Setup IOMMU.. eventually we will (I think) do this once per context
  344. * and have separate page tables per context. For now, to keep things
  345. * simple and to get something working, just use a single address space:
  346. */
  347. gpu->iommu = iommu_domain_alloc(&platform_bus_type);
  348. if (!gpu->iommu) {
  349. dev_err(drm->dev, "failed to allocate IOMMU\n");
  350. ret = -ENOMEM;
  351. goto fail;
  352. }
  353. gpu->id = msm_register_iommu(drm, gpu->iommu);
  354. /* Create ringbuffer: */
  355. gpu->rb = msm_ringbuffer_new(gpu, ringsz);
  356. if (IS_ERR(gpu->rb)) {
  357. ret = PTR_ERR(gpu->rb);
  358. gpu->rb = NULL;
  359. dev_err(drm->dev, "could not create ringbuffer: %d\n", ret);
  360. goto fail;
  361. }
  362. ret = msm_gem_get_iova_locked(gpu->rb->bo, gpu->id, &gpu->rb_iova);
  363. if (ret) {
  364. gpu->rb_iova = 0;
  365. dev_err(drm->dev, "could not map ringbuffer: %d\n", ret);
  366. goto fail;
  367. }
  368. bs_init(gpu, pdev);
  369. return 0;
  370. fail:
  371. return ret;
  372. }
  373. void msm_gpu_cleanup(struct msm_gpu *gpu)
  374. {
  375. DBG("%s", gpu->name);
  376. WARN_ON(!list_empty(&gpu->active_list));
  377. bs_fini(gpu);
  378. if (gpu->rb) {
  379. if (gpu->rb_iova)
  380. msm_gem_put_iova(gpu->rb->bo, gpu->id);
  381. msm_ringbuffer_destroy(gpu->rb);
  382. }
  383. if (gpu->iommu)
  384. iommu_domain_free(gpu->iommu);
  385. }