msm_gpu.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. * Cmdstream submission/retirement:
  169. */
  170. static void retire_worker(struct work_struct *work)
  171. {
  172. struct msm_gpu *gpu = container_of(work, struct msm_gpu, retire_work);
  173. struct drm_device *dev = gpu->dev;
  174. uint32_t fence = gpu->funcs->last_fence(gpu);
  175. mutex_lock(&dev->struct_mutex);
  176. while (!list_empty(&gpu->active_list)) {
  177. struct msm_gem_object *obj;
  178. obj = list_first_entry(&gpu->active_list,
  179. struct msm_gem_object, mm_list);
  180. if (obj->fence <= fence) {
  181. /* move to inactive: */
  182. msm_gem_move_to_inactive(&obj->base);
  183. msm_gem_put_iova(&obj->base, gpu->id);
  184. drm_gem_object_unreference(&obj->base);
  185. } else {
  186. break;
  187. }
  188. }
  189. msm_update_fence(gpu->dev, fence);
  190. mutex_unlock(&dev->struct_mutex);
  191. }
  192. /* call from irq handler to schedule work to retire bo's */
  193. void msm_gpu_retire(struct msm_gpu *gpu)
  194. {
  195. struct msm_drm_private *priv = gpu->dev->dev_private;
  196. queue_work(priv->wq, &gpu->retire_work);
  197. }
  198. /* add bo's to gpu's ring, and kick gpu: */
  199. int msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
  200. struct msm_file_private *ctx)
  201. {
  202. struct drm_device *dev = gpu->dev;
  203. struct msm_drm_private *priv = dev->dev_private;
  204. int i, ret;
  205. mutex_lock(&dev->struct_mutex);
  206. submit->fence = ++priv->next_fence;
  207. ret = gpu->funcs->submit(gpu, submit, ctx);
  208. priv->lastctx = ctx;
  209. for (i = 0; i < submit->nr_bos; i++) {
  210. struct msm_gem_object *msm_obj = submit->bos[i].obj;
  211. /* can't happen yet.. but when we add 2d support we'll have
  212. * to deal w/ cross-ring synchronization:
  213. */
  214. WARN_ON(is_active(msm_obj) && (msm_obj->gpu != gpu));
  215. if (!is_active(msm_obj)) {
  216. uint32_t iova;
  217. /* ring takes a reference to the bo and iova: */
  218. drm_gem_object_reference(&msm_obj->base);
  219. msm_gem_get_iova_locked(&msm_obj->base,
  220. submit->gpu->id, &iova);
  221. }
  222. msm_gem_move_to_active(&msm_obj->base, gpu, submit->fence);
  223. }
  224. mutex_unlock(&dev->struct_mutex);
  225. return ret;
  226. }
  227. /*
  228. * Init/Cleanup:
  229. */
  230. static irqreturn_t irq_handler(int irq, void *data)
  231. {
  232. struct msm_gpu *gpu = data;
  233. return gpu->funcs->irq(gpu);
  234. }
  235. static const char *clk_names[] = {
  236. "src_clk", "core_clk", "iface_clk", "mem_clk", "mem_iface_clk",
  237. };
  238. int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
  239. struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs,
  240. const char *name, const char *ioname, const char *irqname, int ringsz)
  241. {
  242. int i, ret;
  243. gpu->dev = drm;
  244. gpu->funcs = funcs;
  245. gpu->name = name;
  246. INIT_LIST_HEAD(&gpu->active_list);
  247. INIT_WORK(&gpu->retire_work, retire_worker);
  248. BUG_ON(ARRAY_SIZE(clk_names) != ARRAY_SIZE(gpu->grp_clks));
  249. /* Map registers: */
  250. gpu->mmio = msm_ioremap(pdev, ioname, name);
  251. if (IS_ERR(gpu->mmio)) {
  252. ret = PTR_ERR(gpu->mmio);
  253. goto fail;
  254. }
  255. /* Get Interrupt: */
  256. gpu->irq = platform_get_irq_byname(pdev, irqname);
  257. if (gpu->irq < 0) {
  258. ret = gpu->irq;
  259. dev_err(drm->dev, "failed to get irq: %d\n", ret);
  260. goto fail;
  261. }
  262. ret = devm_request_irq(&pdev->dev, gpu->irq, irq_handler,
  263. IRQF_TRIGGER_HIGH, gpu->name, gpu);
  264. if (ret) {
  265. dev_err(drm->dev, "failed to request IRQ%u: %d\n", gpu->irq, ret);
  266. goto fail;
  267. }
  268. /* Acquire clocks: */
  269. for (i = 0; i < ARRAY_SIZE(clk_names); i++) {
  270. gpu->grp_clks[i] = devm_clk_get(&pdev->dev, clk_names[i]);
  271. DBG("grp_clks[%s]: %p", clk_names[i], gpu->grp_clks[i]);
  272. if (IS_ERR(gpu->grp_clks[i]))
  273. gpu->grp_clks[i] = NULL;
  274. }
  275. gpu->ebi1_clk = devm_clk_get(&pdev->dev, "bus_clk");
  276. DBG("ebi1_clk: %p", gpu->ebi1_clk);
  277. if (IS_ERR(gpu->ebi1_clk))
  278. gpu->ebi1_clk = NULL;
  279. /* Acquire regulators: */
  280. gpu->gpu_reg = devm_regulator_get(&pdev->dev, "vdd");
  281. DBG("gpu_reg: %p", gpu->gpu_reg);
  282. if (IS_ERR(gpu->gpu_reg))
  283. gpu->gpu_reg = NULL;
  284. gpu->gpu_cx = devm_regulator_get(&pdev->dev, "vddcx");
  285. DBG("gpu_cx: %p", gpu->gpu_cx);
  286. if (IS_ERR(gpu->gpu_cx))
  287. gpu->gpu_cx = NULL;
  288. /* Setup IOMMU.. eventually we will (I think) do this once per context
  289. * and have separate page tables per context. For now, to keep things
  290. * simple and to get something working, just use a single address space:
  291. */
  292. gpu->iommu = iommu_domain_alloc(&platform_bus_type);
  293. if (!gpu->iommu) {
  294. dev_err(drm->dev, "failed to allocate IOMMU\n");
  295. ret = -ENOMEM;
  296. goto fail;
  297. }
  298. gpu->id = msm_register_iommu(drm, gpu->iommu);
  299. /* Create ringbuffer: */
  300. gpu->rb = msm_ringbuffer_new(gpu, ringsz);
  301. if (IS_ERR(gpu->rb)) {
  302. ret = PTR_ERR(gpu->rb);
  303. gpu->rb = NULL;
  304. dev_err(drm->dev, "could not create ringbuffer: %d\n", ret);
  305. goto fail;
  306. }
  307. ret = msm_gem_get_iova_locked(gpu->rb->bo, gpu->id, &gpu->rb_iova);
  308. if (ret) {
  309. gpu->rb_iova = 0;
  310. dev_err(drm->dev, "could not map ringbuffer: %d\n", ret);
  311. goto fail;
  312. }
  313. bs_init(gpu, pdev);
  314. return 0;
  315. fail:
  316. return ret;
  317. }
  318. void msm_gpu_cleanup(struct msm_gpu *gpu)
  319. {
  320. DBG("%s", gpu->name);
  321. WARN_ON(!list_empty(&gpu->active_list));
  322. bs_fini(gpu);
  323. if (gpu->rb) {
  324. if (gpu->rb_iova)
  325. msm_gem_put_iova(gpu->rb->bo, gpu->id);
  326. msm_ringbuffer_destroy(gpu->rb);
  327. }
  328. if (gpu->iommu)
  329. iommu_domain_free(gpu->iommu);
  330. }