gr3d.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright (C) 2013 Avionic Design GmbH
  3. * Copyright (C) 2013 NVIDIA Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/host1x.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/reset.h>
  14. #include <soc/tegra/pmc.h>
  15. #include "drm.h"
  16. #include "gem.h"
  17. #include "gr3d.h"
  18. struct gr3d {
  19. struct tegra_drm_client client;
  20. struct host1x_channel *channel;
  21. struct clk *clk_secondary;
  22. struct clk *clk;
  23. struct reset_control *rst_secondary;
  24. struct reset_control *rst;
  25. DECLARE_BITMAP(addr_regs, GR3D_NUM_REGS);
  26. };
  27. static inline struct gr3d *to_gr3d(struct tegra_drm_client *client)
  28. {
  29. return container_of(client, struct gr3d, client);
  30. }
  31. static int gr3d_init(struct host1x_client *client)
  32. {
  33. struct tegra_drm_client *drm = host1x_to_drm_client(client);
  34. struct drm_device *dev = dev_get_drvdata(client->parent);
  35. unsigned long flags = HOST1X_SYNCPT_HAS_BASE;
  36. struct gr3d *gr3d = to_gr3d(drm);
  37. gr3d->channel = host1x_channel_request(client->dev);
  38. if (!gr3d->channel)
  39. return -ENOMEM;
  40. client->syncpts[0] = host1x_syncpt_request(client->dev, flags);
  41. if (!client->syncpts[0]) {
  42. host1x_channel_free(gr3d->channel);
  43. return -ENOMEM;
  44. }
  45. return tegra_drm_register_client(dev->dev_private, drm);
  46. }
  47. static int gr3d_exit(struct host1x_client *client)
  48. {
  49. struct tegra_drm_client *drm = host1x_to_drm_client(client);
  50. struct drm_device *dev = dev_get_drvdata(client->parent);
  51. struct gr3d *gr3d = to_gr3d(drm);
  52. int err;
  53. err = tegra_drm_unregister_client(dev->dev_private, drm);
  54. if (err < 0)
  55. return err;
  56. host1x_syncpt_free(client->syncpts[0]);
  57. host1x_channel_free(gr3d->channel);
  58. return 0;
  59. }
  60. static const struct host1x_client_ops gr3d_client_ops = {
  61. .init = gr3d_init,
  62. .exit = gr3d_exit,
  63. };
  64. static int gr3d_open_channel(struct tegra_drm_client *client,
  65. struct tegra_drm_context *context)
  66. {
  67. struct gr3d *gr3d = to_gr3d(client);
  68. context->channel = host1x_channel_get(gr3d->channel);
  69. if (!context->channel)
  70. return -ENOMEM;
  71. return 0;
  72. }
  73. static void gr3d_close_channel(struct tegra_drm_context *context)
  74. {
  75. host1x_channel_put(context->channel);
  76. }
  77. static int gr3d_is_addr_reg(struct device *dev, u32 class, u32 offset)
  78. {
  79. struct gr3d *gr3d = dev_get_drvdata(dev);
  80. switch (class) {
  81. case HOST1X_CLASS_HOST1X:
  82. if (offset == 0x2b)
  83. return 1;
  84. break;
  85. case HOST1X_CLASS_GR3D:
  86. if (offset >= GR3D_NUM_REGS)
  87. break;
  88. if (test_bit(offset, gr3d->addr_regs))
  89. return 1;
  90. break;
  91. }
  92. return 0;
  93. }
  94. static const struct tegra_drm_client_ops gr3d_ops = {
  95. .open_channel = gr3d_open_channel,
  96. .close_channel = gr3d_close_channel,
  97. .is_addr_reg = gr3d_is_addr_reg,
  98. .submit = tegra_drm_submit,
  99. };
  100. static const struct of_device_id tegra_gr3d_match[] = {
  101. { .compatible = "nvidia,tegra114-gr3d" },
  102. { .compatible = "nvidia,tegra30-gr3d" },
  103. { .compatible = "nvidia,tegra20-gr3d" },
  104. { }
  105. };
  106. MODULE_DEVICE_TABLE(of, tegra_gr3d_match);
  107. static const u32 gr3d_addr_regs[] = {
  108. GR3D_IDX_ATTRIBUTE( 0),
  109. GR3D_IDX_ATTRIBUTE( 1),
  110. GR3D_IDX_ATTRIBUTE( 2),
  111. GR3D_IDX_ATTRIBUTE( 3),
  112. GR3D_IDX_ATTRIBUTE( 4),
  113. GR3D_IDX_ATTRIBUTE( 5),
  114. GR3D_IDX_ATTRIBUTE( 6),
  115. GR3D_IDX_ATTRIBUTE( 7),
  116. GR3D_IDX_ATTRIBUTE( 8),
  117. GR3D_IDX_ATTRIBUTE( 9),
  118. GR3D_IDX_ATTRIBUTE(10),
  119. GR3D_IDX_ATTRIBUTE(11),
  120. GR3D_IDX_ATTRIBUTE(12),
  121. GR3D_IDX_ATTRIBUTE(13),
  122. GR3D_IDX_ATTRIBUTE(14),
  123. GR3D_IDX_ATTRIBUTE(15),
  124. GR3D_IDX_INDEX_BASE,
  125. GR3D_QR_ZTAG_ADDR,
  126. GR3D_QR_CTAG_ADDR,
  127. GR3D_QR_CZ_ADDR,
  128. GR3D_TEX_TEX_ADDR( 0),
  129. GR3D_TEX_TEX_ADDR( 1),
  130. GR3D_TEX_TEX_ADDR( 2),
  131. GR3D_TEX_TEX_ADDR( 3),
  132. GR3D_TEX_TEX_ADDR( 4),
  133. GR3D_TEX_TEX_ADDR( 5),
  134. GR3D_TEX_TEX_ADDR( 6),
  135. GR3D_TEX_TEX_ADDR( 7),
  136. GR3D_TEX_TEX_ADDR( 8),
  137. GR3D_TEX_TEX_ADDR( 9),
  138. GR3D_TEX_TEX_ADDR(10),
  139. GR3D_TEX_TEX_ADDR(11),
  140. GR3D_TEX_TEX_ADDR(12),
  141. GR3D_TEX_TEX_ADDR(13),
  142. GR3D_TEX_TEX_ADDR(14),
  143. GR3D_TEX_TEX_ADDR(15),
  144. GR3D_DW_MEMORY_OUTPUT_ADDRESS,
  145. GR3D_GLOBAL_SURFADDR( 0),
  146. GR3D_GLOBAL_SURFADDR( 1),
  147. GR3D_GLOBAL_SURFADDR( 2),
  148. GR3D_GLOBAL_SURFADDR( 3),
  149. GR3D_GLOBAL_SURFADDR( 4),
  150. GR3D_GLOBAL_SURFADDR( 5),
  151. GR3D_GLOBAL_SURFADDR( 6),
  152. GR3D_GLOBAL_SURFADDR( 7),
  153. GR3D_GLOBAL_SURFADDR( 8),
  154. GR3D_GLOBAL_SURFADDR( 9),
  155. GR3D_GLOBAL_SURFADDR(10),
  156. GR3D_GLOBAL_SURFADDR(11),
  157. GR3D_GLOBAL_SURFADDR(12),
  158. GR3D_GLOBAL_SURFADDR(13),
  159. GR3D_GLOBAL_SURFADDR(14),
  160. GR3D_GLOBAL_SURFADDR(15),
  161. GR3D_GLOBAL_SPILLSURFADDR,
  162. GR3D_GLOBAL_SURFOVERADDR( 0),
  163. GR3D_GLOBAL_SURFOVERADDR( 1),
  164. GR3D_GLOBAL_SURFOVERADDR( 2),
  165. GR3D_GLOBAL_SURFOVERADDR( 3),
  166. GR3D_GLOBAL_SURFOVERADDR( 4),
  167. GR3D_GLOBAL_SURFOVERADDR( 5),
  168. GR3D_GLOBAL_SURFOVERADDR( 6),
  169. GR3D_GLOBAL_SURFOVERADDR( 7),
  170. GR3D_GLOBAL_SURFOVERADDR( 8),
  171. GR3D_GLOBAL_SURFOVERADDR( 9),
  172. GR3D_GLOBAL_SURFOVERADDR(10),
  173. GR3D_GLOBAL_SURFOVERADDR(11),
  174. GR3D_GLOBAL_SURFOVERADDR(12),
  175. GR3D_GLOBAL_SURFOVERADDR(13),
  176. GR3D_GLOBAL_SURFOVERADDR(14),
  177. GR3D_GLOBAL_SURFOVERADDR(15),
  178. GR3D_GLOBAL_SAMP01SURFADDR( 0),
  179. GR3D_GLOBAL_SAMP01SURFADDR( 1),
  180. GR3D_GLOBAL_SAMP01SURFADDR( 2),
  181. GR3D_GLOBAL_SAMP01SURFADDR( 3),
  182. GR3D_GLOBAL_SAMP01SURFADDR( 4),
  183. GR3D_GLOBAL_SAMP01SURFADDR( 5),
  184. GR3D_GLOBAL_SAMP01SURFADDR( 6),
  185. GR3D_GLOBAL_SAMP01SURFADDR( 7),
  186. GR3D_GLOBAL_SAMP01SURFADDR( 8),
  187. GR3D_GLOBAL_SAMP01SURFADDR( 9),
  188. GR3D_GLOBAL_SAMP01SURFADDR(10),
  189. GR3D_GLOBAL_SAMP01SURFADDR(11),
  190. GR3D_GLOBAL_SAMP01SURFADDR(12),
  191. GR3D_GLOBAL_SAMP01SURFADDR(13),
  192. GR3D_GLOBAL_SAMP01SURFADDR(14),
  193. GR3D_GLOBAL_SAMP01SURFADDR(15),
  194. GR3D_GLOBAL_SAMP23SURFADDR( 0),
  195. GR3D_GLOBAL_SAMP23SURFADDR( 1),
  196. GR3D_GLOBAL_SAMP23SURFADDR( 2),
  197. GR3D_GLOBAL_SAMP23SURFADDR( 3),
  198. GR3D_GLOBAL_SAMP23SURFADDR( 4),
  199. GR3D_GLOBAL_SAMP23SURFADDR( 5),
  200. GR3D_GLOBAL_SAMP23SURFADDR( 6),
  201. GR3D_GLOBAL_SAMP23SURFADDR( 7),
  202. GR3D_GLOBAL_SAMP23SURFADDR( 8),
  203. GR3D_GLOBAL_SAMP23SURFADDR( 9),
  204. GR3D_GLOBAL_SAMP23SURFADDR(10),
  205. GR3D_GLOBAL_SAMP23SURFADDR(11),
  206. GR3D_GLOBAL_SAMP23SURFADDR(12),
  207. GR3D_GLOBAL_SAMP23SURFADDR(13),
  208. GR3D_GLOBAL_SAMP23SURFADDR(14),
  209. GR3D_GLOBAL_SAMP23SURFADDR(15),
  210. };
  211. static int gr3d_probe(struct platform_device *pdev)
  212. {
  213. struct device_node *np = pdev->dev.of_node;
  214. struct host1x_syncpt **syncpts;
  215. struct gr3d *gr3d;
  216. unsigned int i;
  217. int err;
  218. gr3d = devm_kzalloc(&pdev->dev, sizeof(*gr3d), GFP_KERNEL);
  219. if (!gr3d)
  220. return -ENOMEM;
  221. syncpts = devm_kzalloc(&pdev->dev, sizeof(*syncpts), GFP_KERNEL);
  222. if (!syncpts)
  223. return -ENOMEM;
  224. gr3d->clk = devm_clk_get(&pdev->dev, NULL);
  225. if (IS_ERR(gr3d->clk)) {
  226. dev_err(&pdev->dev, "cannot get clock\n");
  227. return PTR_ERR(gr3d->clk);
  228. }
  229. gr3d->rst = devm_reset_control_get(&pdev->dev, "3d");
  230. if (IS_ERR(gr3d->rst)) {
  231. dev_err(&pdev->dev, "cannot get reset\n");
  232. return PTR_ERR(gr3d->rst);
  233. }
  234. if (of_device_is_compatible(np, "nvidia,tegra30-gr3d")) {
  235. gr3d->clk_secondary = devm_clk_get(&pdev->dev, "3d2");
  236. if (IS_ERR(gr3d->clk)) {
  237. dev_err(&pdev->dev, "cannot get secondary clock\n");
  238. return PTR_ERR(gr3d->clk);
  239. }
  240. gr3d->rst_secondary = devm_reset_control_get(&pdev->dev,
  241. "3d2");
  242. if (IS_ERR(gr3d->rst_secondary)) {
  243. dev_err(&pdev->dev, "cannot get secondary reset\n");
  244. return PTR_ERR(gr3d->rst_secondary);
  245. }
  246. }
  247. err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_3D, gr3d->clk,
  248. gr3d->rst);
  249. if (err < 0) {
  250. dev_err(&pdev->dev, "failed to power up 3D unit\n");
  251. return err;
  252. }
  253. if (gr3d->clk_secondary) {
  254. err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_3D1,
  255. gr3d->clk_secondary,
  256. gr3d->rst_secondary);
  257. if (err < 0) {
  258. dev_err(&pdev->dev,
  259. "failed to power up secondary 3D unit\n");
  260. return err;
  261. }
  262. }
  263. INIT_LIST_HEAD(&gr3d->client.base.list);
  264. gr3d->client.base.ops = &gr3d_client_ops;
  265. gr3d->client.base.dev = &pdev->dev;
  266. gr3d->client.base.class = HOST1X_CLASS_GR3D;
  267. gr3d->client.base.syncpts = syncpts;
  268. gr3d->client.base.num_syncpts = 1;
  269. INIT_LIST_HEAD(&gr3d->client.list);
  270. gr3d->client.ops = &gr3d_ops;
  271. err = host1x_client_register(&gr3d->client.base);
  272. if (err < 0) {
  273. dev_err(&pdev->dev, "failed to register host1x client: %d\n",
  274. err);
  275. return err;
  276. }
  277. /* initialize address register map */
  278. for (i = 0; i < ARRAY_SIZE(gr3d_addr_regs); i++)
  279. set_bit(gr3d_addr_regs[i], gr3d->addr_regs);
  280. platform_set_drvdata(pdev, gr3d);
  281. return 0;
  282. }
  283. static int gr3d_remove(struct platform_device *pdev)
  284. {
  285. struct gr3d *gr3d = platform_get_drvdata(pdev);
  286. int err;
  287. err = host1x_client_unregister(&gr3d->client.base);
  288. if (err < 0) {
  289. dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
  290. err);
  291. return err;
  292. }
  293. if (gr3d->clk_secondary) {
  294. tegra_powergate_power_off(TEGRA_POWERGATE_3D1);
  295. clk_disable_unprepare(gr3d->clk_secondary);
  296. }
  297. tegra_powergate_power_off(TEGRA_POWERGATE_3D);
  298. clk_disable_unprepare(gr3d->clk);
  299. return 0;
  300. }
  301. struct platform_driver tegra_gr3d_driver = {
  302. .driver = {
  303. .name = "tegra-gr3d",
  304. .of_match_table = tegra_gr3d_match,
  305. },
  306. .probe = gr3d_probe,
  307. .remove = gr3d_remove,
  308. };