gr2d.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright (c) 2012-2013, NVIDIA Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <linux/clk.h>
  17. #include "drm.h"
  18. #include "gem.h"
  19. #define GR2D_NUM_REGS 0x4d
  20. struct gr2d {
  21. struct tegra_drm_client client;
  22. struct host1x_channel *channel;
  23. struct clk *clk;
  24. DECLARE_BITMAP(addr_regs, GR2D_NUM_REGS);
  25. };
  26. static inline struct gr2d *to_gr2d(struct tegra_drm_client *client)
  27. {
  28. return container_of(client, struct gr2d, client);
  29. }
  30. static int gr2d_init(struct host1x_client *client)
  31. {
  32. struct tegra_drm_client *drm = host1x_to_drm_client(client);
  33. struct tegra_drm *tegra = dev_get_drvdata(client->parent);
  34. struct gr2d *gr2d = to_gr2d(drm);
  35. gr2d->channel = host1x_channel_request(client->dev);
  36. if (!gr2d->channel)
  37. return -ENOMEM;
  38. client->syncpts[0] = host1x_syncpt_request(client->dev, false);
  39. if (!client->syncpts[0]) {
  40. host1x_channel_free(gr2d->channel);
  41. return -ENOMEM;
  42. }
  43. return tegra_drm_register_client(tegra, drm);
  44. }
  45. static int gr2d_exit(struct host1x_client *client)
  46. {
  47. struct tegra_drm_client *drm = host1x_to_drm_client(client);
  48. struct tegra_drm *tegra = dev_get_drvdata(client->parent);
  49. struct gr2d *gr2d = to_gr2d(drm);
  50. int err;
  51. err = tegra_drm_unregister_client(tegra, drm);
  52. if (err < 0)
  53. return err;
  54. host1x_syncpt_free(client->syncpts[0]);
  55. host1x_channel_free(gr2d->channel);
  56. return 0;
  57. }
  58. static const struct host1x_client_ops gr2d_client_ops = {
  59. .init = gr2d_init,
  60. .exit = gr2d_exit,
  61. };
  62. static int gr2d_open_channel(struct tegra_drm_client *client,
  63. struct tegra_drm_context *context)
  64. {
  65. struct gr2d *gr2d = to_gr2d(client);
  66. context->channel = host1x_channel_get(gr2d->channel);
  67. if (!context->channel)
  68. return -ENOMEM;
  69. return 0;
  70. }
  71. static void gr2d_close_channel(struct tegra_drm_context *context)
  72. {
  73. host1x_channel_put(context->channel);
  74. }
  75. static struct host1x_bo *host1x_bo_lookup(struct drm_device *drm,
  76. struct drm_file *file,
  77. u32 handle)
  78. {
  79. struct drm_gem_object *gem;
  80. struct tegra_bo *bo;
  81. gem = drm_gem_object_lookup(drm, file, handle);
  82. if (!gem)
  83. return NULL;
  84. mutex_lock(&drm->struct_mutex);
  85. drm_gem_object_unreference(gem);
  86. mutex_unlock(&drm->struct_mutex);
  87. bo = to_tegra_bo(gem);
  88. return &bo->base;
  89. }
  90. static int gr2d_is_addr_reg(struct device *dev, u32 class, u32 offset)
  91. {
  92. struct gr2d *gr2d = dev_get_drvdata(dev);
  93. switch (class) {
  94. case HOST1X_CLASS_HOST1X:
  95. if (offset == 0x2b)
  96. return 1;
  97. break;
  98. case HOST1X_CLASS_GR2D:
  99. case HOST1X_CLASS_GR2D_SB:
  100. if (offset >= GR2D_NUM_REGS)
  101. break;
  102. if (test_bit(offset, gr2d->addr_regs))
  103. return 1;
  104. break;
  105. }
  106. return 0;
  107. }
  108. static int gr2d_submit(struct tegra_drm_context *context,
  109. struct drm_tegra_submit *args, struct drm_device *drm,
  110. struct drm_file *file)
  111. {
  112. unsigned int num_cmdbufs = args->num_cmdbufs;
  113. unsigned int num_relocs = args->num_relocs;
  114. unsigned int num_waitchks = args->num_waitchks;
  115. struct drm_tegra_cmdbuf __user *cmdbufs =
  116. (void * __user)(uintptr_t)args->cmdbufs;
  117. struct drm_tegra_reloc __user *relocs =
  118. (void * __user)(uintptr_t)args->relocs;
  119. struct drm_tegra_waitchk __user *waitchks =
  120. (void * __user)(uintptr_t)args->waitchks;
  121. struct drm_tegra_syncpt syncpt;
  122. struct host1x_job *job;
  123. int err;
  124. /* We don't yet support other than one syncpt_incr struct per submit */
  125. if (args->num_syncpts != 1)
  126. return -EINVAL;
  127. job = host1x_job_alloc(context->channel, args->num_cmdbufs,
  128. args->num_relocs, args->num_waitchks);
  129. if (!job)
  130. return -ENOMEM;
  131. job->num_relocs = args->num_relocs;
  132. job->num_waitchk = args->num_waitchks;
  133. job->client = (u32)args->context;
  134. job->class = context->client->base.class;
  135. job->serialize = true;
  136. while (num_cmdbufs) {
  137. struct drm_tegra_cmdbuf cmdbuf;
  138. struct host1x_bo *bo;
  139. err = copy_from_user(&cmdbuf, cmdbufs, sizeof(cmdbuf));
  140. if (err)
  141. goto fail;
  142. bo = host1x_bo_lookup(drm, file, cmdbuf.handle);
  143. if (!bo) {
  144. err = -ENOENT;
  145. goto fail;
  146. }
  147. host1x_job_add_gather(job, bo, cmdbuf.words, cmdbuf.offset);
  148. num_cmdbufs--;
  149. cmdbufs++;
  150. }
  151. err = copy_from_user(job->relocarray, relocs,
  152. sizeof(*relocs) * num_relocs);
  153. if (err)
  154. goto fail;
  155. while (num_relocs--) {
  156. struct host1x_reloc *reloc = &job->relocarray[num_relocs];
  157. struct host1x_bo *cmdbuf, *target;
  158. cmdbuf = host1x_bo_lookup(drm, file, (u32)reloc->cmdbuf);
  159. target = host1x_bo_lookup(drm, file, (u32)reloc->target);
  160. reloc->cmdbuf = cmdbuf;
  161. reloc->target = target;
  162. if (!reloc->target || !reloc->cmdbuf) {
  163. err = -ENOENT;
  164. goto fail;
  165. }
  166. }
  167. err = copy_from_user(job->waitchk, waitchks,
  168. sizeof(*waitchks) * num_waitchks);
  169. if (err)
  170. goto fail;
  171. err = copy_from_user(&syncpt, (void * __user)(uintptr_t)args->syncpts,
  172. sizeof(syncpt));
  173. if (err)
  174. goto fail;
  175. job->syncpt_id = syncpt.id;
  176. job->syncpt_incrs = syncpt.incrs;
  177. job->timeout = 10000;
  178. job->is_addr_reg = gr2d_is_addr_reg;
  179. if (args->timeout && args->timeout < 10000)
  180. job->timeout = args->timeout;
  181. err = host1x_job_pin(job, context->client->base.dev);
  182. if (err)
  183. goto fail;
  184. err = host1x_job_submit(job);
  185. if (err)
  186. goto fail_submit;
  187. args->fence = job->syncpt_end;
  188. host1x_job_put(job);
  189. return 0;
  190. fail_submit:
  191. host1x_job_unpin(job);
  192. fail:
  193. host1x_job_put(job);
  194. return err;
  195. }
  196. static const struct tegra_drm_client_ops gr2d_ops = {
  197. .open_channel = gr2d_open_channel,
  198. .close_channel = gr2d_close_channel,
  199. .submit = gr2d_submit,
  200. };
  201. static const struct of_device_id gr2d_match[] = {
  202. { .compatible = "nvidia,tegra30-gr2d" },
  203. { .compatible = "nvidia,tegra20-gr2d" },
  204. { },
  205. };
  206. static const u32 gr2d_addr_regs[] = {
  207. 0x1a, 0x1b, 0x26, 0x2b, 0x2c, 0x2d, 0x31, 0x32,
  208. 0x48, 0x49, 0x4a, 0x4b, 0x4c
  209. };
  210. static int gr2d_probe(struct platform_device *pdev)
  211. {
  212. struct device *dev = &pdev->dev;
  213. struct host1x_syncpt **syncpts;
  214. struct gr2d *gr2d;
  215. unsigned int i;
  216. int err;
  217. gr2d = devm_kzalloc(dev, sizeof(*gr2d), GFP_KERNEL);
  218. if (!gr2d)
  219. return -ENOMEM;
  220. syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
  221. if (!syncpts)
  222. return -ENOMEM;
  223. gr2d->clk = devm_clk_get(dev, NULL);
  224. if (IS_ERR(gr2d->clk)) {
  225. dev_err(dev, "cannot get clock\n");
  226. return PTR_ERR(gr2d->clk);
  227. }
  228. err = clk_prepare_enable(gr2d->clk);
  229. if (err) {
  230. dev_err(dev, "cannot turn on clock\n");
  231. return err;
  232. }
  233. INIT_LIST_HEAD(&gr2d->client.base.list);
  234. gr2d->client.base.ops = &gr2d_client_ops;
  235. gr2d->client.base.dev = dev;
  236. gr2d->client.base.class = HOST1X_CLASS_GR2D;
  237. gr2d->client.base.syncpts = syncpts;
  238. gr2d->client.base.num_syncpts = 1;
  239. INIT_LIST_HEAD(&gr2d->client.list);
  240. gr2d->client.ops = &gr2d_ops;
  241. err = host1x_client_register(&gr2d->client.base);
  242. if (err < 0) {
  243. dev_err(dev, "failed to register host1x client: %d\n", err);
  244. return err;
  245. }
  246. /* initialize address register map */
  247. for (i = 0; i < ARRAY_SIZE(gr2d_addr_regs); i++)
  248. set_bit(gr2d_addr_regs[i], gr2d->addr_regs);
  249. platform_set_drvdata(pdev, gr2d);
  250. return 0;
  251. }
  252. static int gr2d_remove(struct platform_device *pdev)
  253. {
  254. struct gr2d *gr2d = platform_get_drvdata(pdev);
  255. int err;
  256. err = host1x_client_unregister(&gr2d->client.base);
  257. if (err < 0) {
  258. dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
  259. err);
  260. return err;
  261. }
  262. clk_disable_unprepare(gr2d->clk);
  263. return 0;
  264. }
  265. struct platform_driver tegra_gr2d_driver = {
  266. .driver = {
  267. .name = "tegra-gr2d",
  268. .of_match_table = gr2d_match,
  269. },
  270. .probe = gr2d_probe,
  271. .remove = gr2d_remove,
  272. };