msm_gpu.c 10 KB

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