gr2d.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 NULL;
  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. err = -ENOENT;
  117. goto fail;
  118. }
  119. host1x_job_add_gather(job, bo, cmdbuf.words, cmdbuf.offset);
  120. num_cmdbufs--;
  121. cmdbufs++;
  122. }
  123. err = copy_from_user(job->relocarray, relocs,
  124. sizeof(*relocs) * num_relocs);
  125. if (err)
  126. goto fail;
  127. while (num_relocs--) {
  128. struct host1x_reloc *reloc = &job->relocarray[num_relocs];
  129. struct host1x_bo *cmdbuf, *target;
  130. cmdbuf = host1x_bo_lookup(drm, file, (u32)reloc->cmdbuf);
  131. target = host1x_bo_lookup(drm, file, (u32)reloc->target);
  132. reloc->cmdbuf = cmdbuf;
  133. reloc->target = target;
  134. if (!reloc->target || !reloc->cmdbuf) {
  135. err = -ENOENT;
  136. goto fail;
  137. }
  138. }
  139. err = copy_from_user(job->waitchk, waitchks,
  140. sizeof(*waitchks) * num_waitchks);
  141. if (err)
  142. goto fail;
  143. err = copy_from_user(&syncpt, (void * __user)(uintptr_t)args->syncpts,
  144. sizeof(syncpt));
  145. if (err)
  146. goto fail;
  147. job->syncpt_id = syncpt.id;
  148. job->syncpt_incrs = syncpt.incrs;
  149. job->timeout = 10000;
  150. job->is_addr_reg = gr2d_is_addr_reg;
  151. if (args->timeout && args->timeout < 10000)
  152. job->timeout = args->timeout;
  153. err = host1x_job_pin(job, context->client->dev);
  154. if (err)
  155. goto fail;
  156. err = host1x_job_submit(job);
  157. if (err)
  158. goto fail_submit;
  159. args->fence = job->syncpt_end;
  160. host1x_job_put(job);
  161. return 0;
  162. fail_submit:
  163. host1x_job_unpin(job);
  164. fail:
  165. host1x_job_put(job);
  166. return err;
  167. }
  168. static struct host1x_client_ops gr2d_client_ops = {
  169. .drm_init = gr2d_client_init,
  170. .drm_exit = gr2d_client_exit,
  171. .open_channel = gr2d_open_channel,
  172. .close_channel = gr2d_close_channel,
  173. .submit = gr2d_submit,
  174. };
  175. static void gr2d_init_addr_reg_map(struct device *dev, struct gr2d *gr2d)
  176. {
  177. const u32 gr2d_addr_regs[] = {0x1a, 0x1b, 0x26, 0x2b, 0x2c, 0x2d, 0x31,
  178. 0x32, 0x48, 0x49, 0x4a, 0x4b, 0x4c};
  179. unsigned long *bitmap;
  180. int i;
  181. bitmap = devm_kzalloc(dev, DIV_ROUND_UP(256, BITS_PER_BYTE),
  182. GFP_KERNEL);
  183. for (i = 0; i < ARRAY_SIZE(gr2d_addr_regs); ++i) {
  184. u32 reg = gr2d_addr_regs[i];
  185. bitmap[BIT_WORD(reg)] |= BIT_MASK(reg);
  186. }
  187. gr2d->addr_regs = bitmap;
  188. }
  189. static int gr2d_is_addr_reg(struct device *dev, u32 class, u32 reg)
  190. {
  191. struct gr2d *gr2d = dev_get_drvdata(dev);
  192. switch (class) {
  193. case HOST1X_CLASS_HOST1X:
  194. return reg == 0x2b;
  195. case HOST1X_CLASS_GR2D:
  196. case HOST1X_CLASS_GR2D_SB:
  197. reg &= 0xff;
  198. if (gr2d->addr_regs[BIT_WORD(reg)] & BIT_MASK(reg))
  199. return 1;
  200. default:
  201. return 0;
  202. }
  203. }
  204. static const struct of_device_id gr2d_match[] = {
  205. { .compatible = "nvidia,tegra30-gr2d" },
  206. { .compatible = "nvidia,tegra20-gr2d" },
  207. { },
  208. };
  209. static int gr2d_probe(struct platform_device *pdev)
  210. {
  211. struct device *dev = &pdev->dev;
  212. struct host1x_drm *host1x = host1x_get_drm_data(dev->parent);
  213. int err;
  214. struct gr2d *gr2d = NULL;
  215. struct host1x_syncpt **syncpts;
  216. gr2d = devm_kzalloc(dev, sizeof(*gr2d), GFP_KERNEL);
  217. if (!gr2d)
  218. return -ENOMEM;
  219. syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
  220. if (!syncpts)
  221. return -ENOMEM;
  222. gr2d->clk = devm_clk_get(dev, NULL);
  223. if (IS_ERR(gr2d->clk)) {
  224. dev_err(dev, "cannot get clock\n");
  225. return PTR_ERR(gr2d->clk);
  226. }
  227. err = clk_prepare_enable(gr2d->clk);
  228. if (err) {
  229. dev_err(dev, "cannot turn on clock\n");
  230. return err;
  231. }
  232. gr2d->channel = host1x_channel_request(dev);
  233. if (!gr2d->channel)
  234. return -ENOMEM;
  235. *syncpts = host1x_syncpt_request(dev, false);
  236. if (!(*syncpts)) {
  237. host1x_channel_free(gr2d->channel);
  238. return -ENOMEM;
  239. }
  240. gr2d->client.ops = &gr2d_client_ops;
  241. gr2d->client.dev = dev;
  242. gr2d->client.class = HOST1X_CLASS_GR2D;
  243. gr2d->client.syncpts = syncpts;
  244. gr2d->client.num_syncpts = 1;
  245. err = host1x_register_client(host1x, &gr2d->client);
  246. if (err < 0) {
  247. dev_err(dev, "failed to register host1x client: %d\n", err);
  248. return err;
  249. }
  250. gr2d_init_addr_reg_map(dev, gr2d);
  251. platform_set_drvdata(pdev, gr2d);
  252. return 0;
  253. }
  254. static int __exit gr2d_remove(struct platform_device *pdev)
  255. {
  256. struct host1x_drm *host1x = host1x_get_drm_data(pdev->dev.parent);
  257. struct gr2d *gr2d = platform_get_drvdata(pdev);
  258. unsigned int i;
  259. int err;
  260. err = host1x_unregister_client(host1x, &gr2d->client);
  261. if (err < 0) {
  262. dev_err(&pdev->dev, "failed to unregister client: %d\n", err);
  263. return err;
  264. }
  265. for (i = 0; i < gr2d->client.num_syncpts; i++)
  266. host1x_syncpt_free(gr2d->client.syncpts[i]);
  267. host1x_channel_free(gr2d->channel);
  268. clk_disable_unprepare(gr2d->clk);
  269. return 0;
  270. }
  271. struct platform_driver tegra_gr2d_driver = {
  272. .probe = gr2d_probe,
  273. .remove = __exit_p(gr2d_remove),
  274. .driver = {
  275. .owner = THIS_MODULE,
  276. .name = "gr2d",
  277. .of_match_table = gr2d_match,
  278. }
  279. };