gr2d.c 7.7 KB

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