xref: /openbmc/linux/drivers/gpu/drm/tegra/gr2d.c (revision b593bce5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2012-2013, NVIDIA Corporation.
4  */
5 
6 #include <linux/clk.h>
7 #include <linux/iommu.h>
8 #include <linux/of_device.h>
9 
10 #include "drm.h"
11 #include "gem.h"
12 #include "gr2d.h"
13 
14 struct gr2d_soc {
15 	unsigned int version;
16 };
17 
18 struct gr2d {
19 	struct iommu_group *group;
20 	struct tegra_drm_client client;
21 	struct host1x_channel *channel;
22 	struct clk *clk;
23 
24 	const struct gr2d_soc *soc;
25 
26 	DECLARE_BITMAP(addr_regs, GR2D_NUM_REGS);
27 };
28 
29 static inline struct gr2d *to_gr2d(struct tegra_drm_client *client)
30 {
31 	return container_of(client, struct gr2d, client);
32 }
33 
34 static int gr2d_init(struct host1x_client *client)
35 {
36 	struct tegra_drm_client *drm = host1x_to_drm_client(client);
37 	struct drm_device *dev = dev_get_drvdata(client->parent);
38 	unsigned long flags = HOST1X_SYNCPT_HAS_BASE;
39 	struct gr2d *gr2d = to_gr2d(drm);
40 	int err;
41 
42 	gr2d->channel = host1x_channel_request(client->dev);
43 	if (!gr2d->channel)
44 		return -ENOMEM;
45 
46 	client->syncpts[0] = host1x_syncpt_request(client, flags);
47 	if (!client->syncpts[0]) {
48 		err = -ENOMEM;
49 		dev_err(client->dev, "failed to request syncpoint: %d\n", err);
50 		goto put;
51 	}
52 
53 	gr2d->group = host1x_client_iommu_attach(client, false);
54 	if (IS_ERR(gr2d->group)) {
55 		err = PTR_ERR(gr2d->group);
56 		dev_err(client->dev, "failed to attach to domain: %d\n", err);
57 		goto free;
58 	}
59 
60 	err = tegra_drm_register_client(dev->dev_private, drm);
61 	if (err < 0) {
62 		dev_err(client->dev, "failed to register client: %d\n", err);
63 		goto detach;
64 	}
65 
66 	return 0;
67 
68 detach:
69 	host1x_client_iommu_detach(client, gr2d->group);
70 free:
71 	host1x_syncpt_free(client->syncpts[0]);
72 put:
73 	host1x_channel_put(gr2d->channel);
74 	return err;
75 }
76 
77 static int gr2d_exit(struct host1x_client *client)
78 {
79 	struct tegra_drm_client *drm = host1x_to_drm_client(client);
80 	struct drm_device *dev = dev_get_drvdata(client->parent);
81 	struct tegra_drm *tegra = dev->dev_private;
82 	struct gr2d *gr2d = to_gr2d(drm);
83 	int err;
84 
85 	err = tegra_drm_unregister_client(tegra, drm);
86 	if (err < 0)
87 		return err;
88 
89 	host1x_client_iommu_detach(client, gr2d->group);
90 	host1x_syncpt_free(client->syncpts[0]);
91 	host1x_channel_put(gr2d->channel);
92 
93 	return 0;
94 }
95 
96 static const struct host1x_client_ops gr2d_client_ops = {
97 	.init = gr2d_init,
98 	.exit = gr2d_exit,
99 };
100 
101 static int gr2d_open_channel(struct tegra_drm_client *client,
102 			     struct tegra_drm_context *context)
103 {
104 	struct gr2d *gr2d = to_gr2d(client);
105 
106 	context->channel = host1x_channel_get(gr2d->channel);
107 	if (!context->channel)
108 		return -ENOMEM;
109 
110 	return 0;
111 }
112 
113 static void gr2d_close_channel(struct tegra_drm_context *context)
114 {
115 	host1x_channel_put(context->channel);
116 }
117 
118 static int gr2d_is_addr_reg(struct device *dev, u32 class, u32 offset)
119 {
120 	struct gr2d *gr2d = dev_get_drvdata(dev);
121 
122 	switch (class) {
123 	case HOST1X_CLASS_HOST1X:
124 		if (offset == 0x2b)
125 			return 1;
126 
127 		break;
128 
129 	case HOST1X_CLASS_GR2D:
130 	case HOST1X_CLASS_GR2D_SB:
131 		if (offset >= GR2D_NUM_REGS)
132 			break;
133 
134 		if (test_bit(offset, gr2d->addr_regs))
135 			return 1;
136 
137 		break;
138 	}
139 
140 	return 0;
141 }
142 
143 static int gr2d_is_valid_class(u32 class)
144 {
145 	return (class == HOST1X_CLASS_GR2D ||
146 		class == HOST1X_CLASS_GR2D_SB);
147 }
148 
149 static const struct tegra_drm_client_ops gr2d_ops = {
150 	.open_channel = gr2d_open_channel,
151 	.close_channel = gr2d_close_channel,
152 	.is_addr_reg = gr2d_is_addr_reg,
153 	.is_valid_class = gr2d_is_valid_class,
154 	.submit = tegra_drm_submit,
155 };
156 
157 static const struct gr2d_soc tegra20_gr2d_soc = {
158 	.version = 0x20,
159 };
160 
161 static const struct gr2d_soc tegra30_gr2d_soc = {
162 	.version = 0x30,
163 };
164 
165 static const struct of_device_id gr2d_match[] = {
166 	{ .compatible = "nvidia,tegra30-gr2d", .data = &tegra20_gr2d_soc },
167 	{ .compatible = "nvidia,tegra20-gr2d", .data = &tegra30_gr2d_soc },
168 	{ },
169 };
170 MODULE_DEVICE_TABLE(of, gr2d_match);
171 
172 static const u32 gr2d_addr_regs[] = {
173 	GR2D_UA_BASE_ADDR,
174 	GR2D_VA_BASE_ADDR,
175 	GR2D_PAT_BASE_ADDR,
176 	GR2D_DSTA_BASE_ADDR,
177 	GR2D_DSTB_BASE_ADDR,
178 	GR2D_DSTC_BASE_ADDR,
179 	GR2D_SRCA_BASE_ADDR,
180 	GR2D_SRCB_BASE_ADDR,
181 	GR2D_SRC_BASE_ADDR_SB,
182 	GR2D_DSTA_BASE_ADDR_SB,
183 	GR2D_DSTB_BASE_ADDR_SB,
184 	GR2D_UA_BASE_ADDR_SB,
185 	GR2D_VA_BASE_ADDR_SB,
186 };
187 
188 static int gr2d_probe(struct platform_device *pdev)
189 {
190 	struct device *dev = &pdev->dev;
191 	struct host1x_syncpt **syncpts;
192 	struct gr2d *gr2d;
193 	unsigned int i;
194 	int err;
195 
196 	gr2d = devm_kzalloc(dev, sizeof(*gr2d), GFP_KERNEL);
197 	if (!gr2d)
198 		return -ENOMEM;
199 
200 	gr2d->soc = of_device_get_match_data(dev);
201 
202 	syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
203 	if (!syncpts)
204 		return -ENOMEM;
205 
206 	gr2d->clk = devm_clk_get(dev, NULL);
207 	if (IS_ERR(gr2d->clk)) {
208 		dev_err(dev, "cannot get clock\n");
209 		return PTR_ERR(gr2d->clk);
210 	}
211 
212 	err = clk_prepare_enable(gr2d->clk);
213 	if (err) {
214 		dev_err(dev, "cannot turn on clock\n");
215 		return err;
216 	}
217 
218 	INIT_LIST_HEAD(&gr2d->client.base.list);
219 	gr2d->client.base.ops = &gr2d_client_ops;
220 	gr2d->client.base.dev = dev;
221 	gr2d->client.base.class = HOST1X_CLASS_GR2D;
222 	gr2d->client.base.syncpts = syncpts;
223 	gr2d->client.base.num_syncpts = 1;
224 
225 	INIT_LIST_HEAD(&gr2d->client.list);
226 	gr2d->client.version = gr2d->soc->version;
227 	gr2d->client.ops = &gr2d_ops;
228 
229 	err = host1x_client_register(&gr2d->client.base);
230 	if (err < 0) {
231 		dev_err(dev, "failed to register host1x client: %d\n", err);
232 		clk_disable_unprepare(gr2d->clk);
233 		return err;
234 	}
235 
236 	/* initialize address register map */
237 	for (i = 0; i < ARRAY_SIZE(gr2d_addr_regs); i++)
238 		set_bit(gr2d_addr_regs[i], gr2d->addr_regs);
239 
240 	platform_set_drvdata(pdev, gr2d);
241 
242 	return 0;
243 }
244 
245 static int gr2d_remove(struct platform_device *pdev)
246 {
247 	struct gr2d *gr2d = platform_get_drvdata(pdev);
248 	int err;
249 
250 	err = host1x_client_unregister(&gr2d->client.base);
251 	if (err < 0) {
252 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
253 			err);
254 		return err;
255 	}
256 
257 	clk_disable_unprepare(gr2d->clk);
258 
259 	return 0;
260 }
261 
262 struct platform_driver tegra_gr2d_driver = {
263 	.driver = {
264 		.name = "tegra-gr2d",
265 		.of_match_table = gr2d_match,
266 	},
267 	.probe = gr2d_probe,
268 	.remove = gr2d_remove,
269 };
270