gr2d.c 7.5 KB

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