gr2d.c 7.8 KB

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