Linux 4.19-rc7
[linux-2.6/btrfs-unstable.git] / drivers / gpu / drm / tegra / gr2d.c
blob673059fd2fcb8283bce649a34384f302511865aa
1 /*
2 * Copyright (c) 2012-2013, NVIDIA Corporation.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
9 #include <linux/clk.h>
10 #include <linux/iommu.h>
11 #include <linux/of_device.h>
13 #include "drm.h"
14 #include "gem.h"
15 #include "gr2d.h"
17 struct gr2d_soc {
18 unsigned int version;
21 struct gr2d {
22 struct iommu_group *group;
23 struct tegra_drm_client client;
24 struct host1x_channel *channel;
25 struct clk *clk;
27 const struct gr2d_soc *soc;
29 DECLARE_BITMAP(addr_regs, GR2D_NUM_REGS);
32 static inline struct gr2d *to_gr2d(struct tegra_drm_client *client)
34 return container_of(client, struct gr2d, client);
37 static int gr2d_init(struct host1x_client *client)
39 struct tegra_drm_client *drm = host1x_to_drm_client(client);
40 struct drm_device *dev = dev_get_drvdata(client->parent);
41 unsigned long flags = HOST1X_SYNCPT_HAS_BASE;
42 struct gr2d *gr2d = to_gr2d(drm);
43 int err;
45 gr2d->channel = host1x_channel_request(client->dev);
46 if (!gr2d->channel)
47 return -ENOMEM;
49 client->syncpts[0] = host1x_syncpt_request(client, flags);
50 if (!client->syncpts[0]) {
51 err = -ENOMEM;
52 dev_err(client->dev, "failed to request syncpoint: %d\n", err);
53 goto put;
56 gr2d->group = host1x_client_iommu_attach(client, false);
57 if (IS_ERR(gr2d->group)) {
58 err = PTR_ERR(gr2d->group);
59 dev_err(client->dev, "failed to attach to domain: %d\n", err);
60 goto free;
63 err = tegra_drm_register_client(dev->dev_private, drm);
64 if (err < 0) {
65 dev_err(client->dev, "failed to register client: %d\n", err);
66 goto detach;
69 return 0;
71 detach:
72 host1x_client_iommu_detach(client, gr2d->group);
73 free:
74 host1x_syncpt_free(client->syncpts[0]);
75 put:
76 host1x_channel_put(gr2d->channel);
77 return err;
80 static int gr2d_exit(struct host1x_client *client)
82 struct tegra_drm_client *drm = host1x_to_drm_client(client);
83 struct drm_device *dev = dev_get_drvdata(client->parent);
84 struct tegra_drm *tegra = dev->dev_private;
85 struct gr2d *gr2d = to_gr2d(drm);
86 int err;
88 err = tegra_drm_unregister_client(tegra, drm);
89 if (err < 0)
90 return err;
92 host1x_client_iommu_detach(client, gr2d->group);
93 host1x_syncpt_free(client->syncpts[0]);
94 host1x_channel_put(gr2d->channel);
96 return 0;
99 static const struct host1x_client_ops gr2d_client_ops = {
100 .init = gr2d_init,
101 .exit = gr2d_exit,
104 static int gr2d_open_channel(struct tegra_drm_client *client,
105 struct tegra_drm_context *context)
107 struct gr2d *gr2d = to_gr2d(client);
109 context->channel = host1x_channel_get(gr2d->channel);
110 if (!context->channel)
111 return -ENOMEM;
113 return 0;
116 static void gr2d_close_channel(struct tegra_drm_context *context)
118 host1x_channel_put(context->channel);
121 static int gr2d_is_addr_reg(struct device *dev, u32 class, u32 offset)
123 struct gr2d *gr2d = dev_get_drvdata(dev);
125 switch (class) {
126 case HOST1X_CLASS_HOST1X:
127 if (offset == 0x2b)
128 return 1;
130 break;
132 case HOST1X_CLASS_GR2D:
133 case HOST1X_CLASS_GR2D_SB:
134 if (offset >= GR2D_NUM_REGS)
135 break;
137 if (test_bit(offset, gr2d->addr_regs))
138 return 1;
140 break;
143 return 0;
146 static int gr2d_is_valid_class(u32 class)
148 return (class == HOST1X_CLASS_GR2D ||
149 class == HOST1X_CLASS_GR2D_SB);
152 static const struct tegra_drm_client_ops gr2d_ops = {
153 .open_channel = gr2d_open_channel,
154 .close_channel = gr2d_close_channel,
155 .is_addr_reg = gr2d_is_addr_reg,
156 .is_valid_class = gr2d_is_valid_class,
157 .submit = tegra_drm_submit,
160 static const struct gr2d_soc tegra20_gr2d_soc = {
161 .version = 0x20,
164 static const struct gr2d_soc tegra30_gr2d_soc = {
165 .version = 0x30,
168 static const struct of_device_id gr2d_match[] = {
169 { .compatible = "nvidia,tegra30-gr2d", .data = &tegra20_gr2d_soc },
170 { .compatible = "nvidia,tegra20-gr2d", .data = &tegra30_gr2d_soc },
171 { },
173 MODULE_DEVICE_TABLE(of, gr2d_match);
175 static const u32 gr2d_addr_regs[] = {
176 GR2D_UA_BASE_ADDR,
177 GR2D_VA_BASE_ADDR,
178 GR2D_PAT_BASE_ADDR,
179 GR2D_DSTA_BASE_ADDR,
180 GR2D_DSTB_BASE_ADDR,
181 GR2D_DSTC_BASE_ADDR,
182 GR2D_SRCA_BASE_ADDR,
183 GR2D_SRCB_BASE_ADDR,
184 GR2D_SRC_BASE_ADDR_SB,
185 GR2D_DSTA_BASE_ADDR_SB,
186 GR2D_DSTB_BASE_ADDR_SB,
187 GR2D_UA_BASE_ADDR_SB,
188 GR2D_VA_BASE_ADDR_SB,
191 static int gr2d_probe(struct platform_device *pdev)
193 struct device *dev = &pdev->dev;
194 struct host1x_syncpt **syncpts;
195 struct gr2d *gr2d;
196 unsigned int i;
197 int err;
199 gr2d = devm_kzalloc(dev, sizeof(*gr2d), GFP_KERNEL);
200 if (!gr2d)
201 return -ENOMEM;
203 gr2d->soc = of_device_get_match_data(dev);
205 syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
206 if (!syncpts)
207 return -ENOMEM;
209 gr2d->clk = devm_clk_get(dev, NULL);
210 if (IS_ERR(gr2d->clk)) {
211 dev_err(dev, "cannot get clock\n");
212 return PTR_ERR(gr2d->clk);
215 err = clk_prepare_enable(gr2d->clk);
216 if (err) {
217 dev_err(dev, "cannot turn on clock\n");
218 return err;
221 INIT_LIST_HEAD(&gr2d->client.base.list);
222 gr2d->client.base.ops = &gr2d_client_ops;
223 gr2d->client.base.dev = dev;
224 gr2d->client.base.class = HOST1X_CLASS_GR2D;
225 gr2d->client.base.syncpts = syncpts;
226 gr2d->client.base.num_syncpts = 1;
228 INIT_LIST_HEAD(&gr2d->client.list);
229 gr2d->client.version = gr2d->soc->version;
230 gr2d->client.ops = &gr2d_ops;
232 err = host1x_client_register(&gr2d->client.base);
233 if (err < 0) {
234 dev_err(dev, "failed to register host1x client: %d\n", err);
235 clk_disable_unprepare(gr2d->clk);
236 return err;
239 /* initialize address register map */
240 for (i = 0; i < ARRAY_SIZE(gr2d_addr_regs); i++)
241 set_bit(gr2d_addr_regs[i], gr2d->addr_regs);
243 platform_set_drvdata(pdev, gr2d);
245 return 0;
248 static int gr2d_remove(struct platform_device *pdev)
250 struct gr2d *gr2d = platform_get_drvdata(pdev);
251 int err;
253 err = host1x_client_unregister(&gr2d->client.base);
254 if (err < 0) {
255 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
256 err);
257 return err;
260 clk_disable_unprepare(gr2d->clk);
262 return 0;
265 struct platform_driver tegra_gr2d_driver = {
266 .driver = {
267 .name = "tegra-gr2d",
268 .of_match_table = gr2d_match,
270 .probe = gr2d_probe,
271 .remove = gr2d_remove,