blob: c0a528be03692ce155a73153985645ba4ce95e06 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Thierry Reding5f60ed02013-02-28 08:08:01 +01002/*
3 * Copyright (C) 2013 Avionic Design GmbH
4 * Copyright (C) 2013 NVIDIA Corporation
Thierry Reding5f60ed02013-02-28 08:08:01 +01005 */
6
7#include <linux/clk.h>
8#include <linux/host1x.h>
Dmitry Osipenkoc9ac5212018-05-04 02:47:21 +03009#include <linux/iommu.h>
Thierry Reding5f60ed02013-02-28 08:08:01 +010010#include <linux/module.h>
Thierry Reding33f150e2018-05-16 17:07:38 +020011#include <linux/of_device.h>
Thierry Reding5f60ed02013-02-28 08:08:01 +010012#include <linux/platform_device.h>
Stephen Warrenca480802013-11-06 16:20:54 -070013#include <linux/reset.h>
Thierry Reding306a7f92014-07-17 13:17:24 +020014
Thierry Reding72323982014-07-11 13:19:06 +020015#include <soc/tegra/pmc.h>
Thierry Reding5f60ed02013-02-28 08:08:01 +010016
17#include "drm.h"
18#include "gem.h"
19#include "gr3d.h"
20
Thierry Reding33f150e2018-05-16 17:07:38 +020021struct gr3d_soc {
22 unsigned int version;
23};
24
Thierry Reding5f60ed02013-02-28 08:08:01 +010025struct gr3d {
26 struct tegra_drm_client client;
27 struct host1x_channel *channel;
28 struct clk *clk_secondary;
29 struct clk *clk;
Stephen Warrenca480802013-11-06 16:20:54 -070030 struct reset_control *rst_secondary;
31 struct reset_control *rst;
Thierry Reding5f60ed02013-02-28 08:08:01 +010032
Thierry Reding33f150e2018-05-16 17:07:38 +020033 const struct gr3d_soc *soc;
34
Thierry Reding5f60ed02013-02-28 08:08:01 +010035 DECLARE_BITMAP(addr_regs, GR3D_NUM_REGS);
36};
37
38static inline struct gr3d *to_gr3d(struct tegra_drm_client *client)
39{
40 return container_of(client, struct gr3d, client);
41}
42
43static int gr3d_init(struct host1x_client *client)
44{
45 struct tegra_drm_client *drm = host1x_to_drm_client(client);
Thierry Reding608f43a2019-12-02 10:51:58 +010046 struct drm_device *dev = dev_get_drvdata(client->host);
Thierry Reding977386a2013-10-28 10:23:11 +010047 unsigned long flags = HOST1X_SYNCPT_HAS_BASE;
Thierry Reding5f60ed02013-02-28 08:08:01 +010048 struct gr3d *gr3d = to_gr3d(drm);
Dmitry Osipenkoc9ac5212018-05-04 02:47:21 +030049 int err;
Thierry Reding5f60ed02013-02-28 08:08:01 +010050
Thierry Redingcaccddc2018-06-18 14:01:51 +020051 gr3d->channel = host1x_channel_request(client);
Thierry Reding5f60ed02013-02-28 08:08:01 +010052 if (!gr3d->channel)
53 return -ENOMEM;
54
Thierry Reding617dd7c2017-08-30 12:48:31 +020055 client->syncpts[0] = host1x_syncpt_request(client, flags);
Thierry Reding5f60ed02013-02-28 08:08:01 +010056 if (!client->syncpts[0]) {
Thierry Reding230630b2018-05-04 15:08:49 +020057 err = -ENOMEM;
58 dev_err(client->dev, "failed to request syncpoint: %d\n", err);
59 goto put;
Thierry Reding5f60ed02013-02-28 08:08:01 +010060 }
61
Thierry Reding7edd7962019-10-28 13:37:08 +010062 err = host1x_client_iommu_attach(client);
Thierry Redingaacdf192019-02-08 14:35:13 +010063 if (err < 0) {
Thierry Reding0c407de2018-05-04 15:02:24 +020064 dev_err(client->dev, "failed to attach to domain: %d\n", err);
65 goto free;
Dmitry Osipenkoc9ac5212018-05-04 02:47:21 +030066 }
67
Thierry Reding230630b2018-05-04 15:08:49 +020068 err = tegra_drm_register_client(dev->dev_private, drm);
69 if (err < 0) {
70 dev_err(client->dev, "failed to register client: %d\n", err);
71 goto detach;
72 }
73
74 return 0;
75
76detach:
Thierry Redingaacdf192019-02-08 14:35:13 +010077 host1x_client_iommu_detach(client);
Thierry Reding230630b2018-05-04 15:08:49 +020078free:
79 host1x_syncpt_free(client->syncpts[0]);
80put:
81 host1x_channel_put(gr3d->channel);
82 return err;
Thierry Reding5f60ed02013-02-28 08:08:01 +010083}
84
85static int gr3d_exit(struct host1x_client *client)
86{
87 struct tegra_drm_client *drm = host1x_to_drm_client(client);
Thierry Reding608f43a2019-12-02 10:51:58 +010088 struct drm_device *dev = dev_get_drvdata(client->host);
Thierry Reding5f60ed02013-02-28 08:08:01 +010089 struct gr3d *gr3d = to_gr3d(drm);
90 int err;
91
Thierry Reding9910f5c2014-05-22 09:57:15 +020092 err = tegra_drm_unregister_client(dev->dev_private, drm);
Thierry Reding5f60ed02013-02-28 08:08:01 +010093 if (err < 0)
94 return err;
95
Thierry Redingaacdf192019-02-08 14:35:13 +010096 host1x_client_iommu_detach(client);
Thierry Reding5f60ed02013-02-28 08:08:01 +010097 host1x_syncpt_free(client->syncpts[0]);
Mikko Perttunen8474b022017-06-15 02:18:42 +030098 host1x_channel_put(gr3d->channel);
Thierry Reding5f60ed02013-02-28 08:08:01 +010099
100 return 0;
101}
102
103static const struct host1x_client_ops gr3d_client_ops = {
104 .init = gr3d_init,
105 .exit = gr3d_exit,
106};
107
108static int gr3d_open_channel(struct tegra_drm_client *client,
109 struct tegra_drm_context *context)
110{
111 struct gr3d *gr3d = to_gr3d(client);
112
113 context->channel = host1x_channel_get(gr3d->channel);
114 if (!context->channel)
115 return -ENOMEM;
116
117 return 0;
118}
119
120static void gr3d_close_channel(struct tegra_drm_context *context)
121{
122 host1x_channel_put(context->channel);
123}
124
125static int gr3d_is_addr_reg(struct device *dev, u32 class, u32 offset)
126{
127 struct gr3d *gr3d = dev_get_drvdata(dev);
128
129 switch (class) {
130 case HOST1X_CLASS_HOST1X:
131 if (offset == 0x2b)
132 return 1;
133
134 break;
135
136 case HOST1X_CLASS_GR3D:
137 if (offset >= GR3D_NUM_REGS)
138 break;
139
140 if (test_bit(offset, gr3d->addr_regs))
141 return 1;
142
143 break;
144 }
145
146 return 0;
147}
148
149static const struct tegra_drm_client_ops gr3d_ops = {
150 .open_channel = gr3d_open_channel,
151 .close_channel = gr3d_close_channel,
152 .is_addr_reg = gr3d_is_addr_reg,
153 .submit = tegra_drm_submit,
154};
155
Thierry Reding33f150e2018-05-16 17:07:38 +0200156static const struct gr3d_soc tegra20_gr3d_soc = {
157 .version = 0x20,
158};
159
160static const struct gr3d_soc tegra30_gr3d_soc = {
161 .version = 0x30,
162};
163
164static const struct gr3d_soc tegra114_gr3d_soc = {
165 .version = 0x35,
166};
167
Thierry Reding5f60ed02013-02-28 08:08:01 +0100168static const struct of_device_id tegra_gr3d_match[] = {
Thierry Reding33f150e2018-05-16 17:07:38 +0200169 { .compatible = "nvidia,tegra114-gr3d", .data = &tegra114_gr3d_soc },
170 { .compatible = "nvidia,tegra30-gr3d", .data = &tegra30_gr3d_soc },
171 { .compatible = "nvidia,tegra20-gr3d", .data = &tegra20_gr3d_soc },
Thierry Reding5f60ed02013-02-28 08:08:01 +0100172 { }
173};
Stephen Warrenef707282014-06-18 16:21:55 -0600174MODULE_DEVICE_TABLE(of, tegra_gr3d_match);
Thierry Reding5f60ed02013-02-28 08:08:01 +0100175
176static const u32 gr3d_addr_regs[] = {
177 GR3D_IDX_ATTRIBUTE( 0),
178 GR3D_IDX_ATTRIBUTE( 1),
179 GR3D_IDX_ATTRIBUTE( 2),
180 GR3D_IDX_ATTRIBUTE( 3),
181 GR3D_IDX_ATTRIBUTE( 4),
182 GR3D_IDX_ATTRIBUTE( 5),
183 GR3D_IDX_ATTRIBUTE( 6),
184 GR3D_IDX_ATTRIBUTE( 7),
185 GR3D_IDX_ATTRIBUTE( 8),
186 GR3D_IDX_ATTRIBUTE( 9),
187 GR3D_IDX_ATTRIBUTE(10),
188 GR3D_IDX_ATTRIBUTE(11),
189 GR3D_IDX_ATTRIBUTE(12),
190 GR3D_IDX_ATTRIBUTE(13),
191 GR3D_IDX_ATTRIBUTE(14),
192 GR3D_IDX_ATTRIBUTE(15),
193 GR3D_IDX_INDEX_BASE,
194 GR3D_QR_ZTAG_ADDR,
195 GR3D_QR_CTAG_ADDR,
196 GR3D_QR_CZ_ADDR,
197 GR3D_TEX_TEX_ADDR( 0),
198 GR3D_TEX_TEX_ADDR( 1),
199 GR3D_TEX_TEX_ADDR( 2),
200 GR3D_TEX_TEX_ADDR( 3),
201 GR3D_TEX_TEX_ADDR( 4),
202 GR3D_TEX_TEX_ADDR( 5),
203 GR3D_TEX_TEX_ADDR( 6),
204 GR3D_TEX_TEX_ADDR( 7),
205 GR3D_TEX_TEX_ADDR( 8),
206 GR3D_TEX_TEX_ADDR( 9),
207 GR3D_TEX_TEX_ADDR(10),
208 GR3D_TEX_TEX_ADDR(11),
209 GR3D_TEX_TEX_ADDR(12),
210 GR3D_TEX_TEX_ADDR(13),
211 GR3D_TEX_TEX_ADDR(14),
212 GR3D_TEX_TEX_ADDR(15),
213 GR3D_DW_MEMORY_OUTPUT_ADDRESS,
214 GR3D_GLOBAL_SURFADDR( 0),
215 GR3D_GLOBAL_SURFADDR( 1),
216 GR3D_GLOBAL_SURFADDR( 2),
217 GR3D_GLOBAL_SURFADDR( 3),
218 GR3D_GLOBAL_SURFADDR( 4),
219 GR3D_GLOBAL_SURFADDR( 5),
220 GR3D_GLOBAL_SURFADDR( 6),
221 GR3D_GLOBAL_SURFADDR( 7),
222 GR3D_GLOBAL_SURFADDR( 8),
223 GR3D_GLOBAL_SURFADDR( 9),
224 GR3D_GLOBAL_SURFADDR(10),
225 GR3D_GLOBAL_SURFADDR(11),
226 GR3D_GLOBAL_SURFADDR(12),
227 GR3D_GLOBAL_SURFADDR(13),
228 GR3D_GLOBAL_SURFADDR(14),
229 GR3D_GLOBAL_SURFADDR(15),
230 GR3D_GLOBAL_SPILLSURFADDR,
231 GR3D_GLOBAL_SURFOVERADDR( 0),
232 GR3D_GLOBAL_SURFOVERADDR( 1),
233 GR3D_GLOBAL_SURFOVERADDR( 2),
234 GR3D_GLOBAL_SURFOVERADDR( 3),
235 GR3D_GLOBAL_SURFOVERADDR( 4),
236 GR3D_GLOBAL_SURFOVERADDR( 5),
237 GR3D_GLOBAL_SURFOVERADDR( 6),
238 GR3D_GLOBAL_SURFOVERADDR( 7),
239 GR3D_GLOBAL_SURFOVERADDR( 8),
240 GR3D_GLOBAL_SURFOVERADDR( 9),
241 GR3D_GLOBAL_SURFOVERADDR(10),
242 GR3D_GLOBAL_SURFOVERADDR(11),
243 GR3D_GLOBAL_SURFOVERADDR(12),
244 GR3D_GLOBAL_SURFOVERADDR(13),
245 GR3D_GLOBAL_SURFOVERADDR(14),
246 GR3D_GLOBAL_SURFOVERADDR(15),
247 GR3D_GLOBAL_SAMP01SURFADDR( 0),
248 GR3D_GLOBAL_SAMP01SURFADDR( 1),
249 GR3D_GLOBAL_SAMP01SURFADDR( 2),
250 GR3D_GLOBAL_SAMP01SURFADDR( 3),
251 GR3D_GLOBAL_SAMP01SURFADDR( 4),
252 GR3D_GLOBAL_SAMP01SURFADDR( 5),
253 GR3D_GLOBAL_SAMP01SURFADDR( 6),
254 GR3D_GLOBAL_SAMP01SURFADDR( 7),
255 GR3D_GLOBAL_SAMP01SURFADDR( 8),
256 GR3D_GLOBAL_SAMP01SURFADDR( 9),
257 GR3D_GLOBAL_SAMP01SURFADDR(10),
258 GR3D_GLOBAL_SAMP01SURFADDR(11),
259 GR3D_GLOBAL_SAMP01SURFADDR(12),
260 GR3D_GLOBAL_SAMP01SURFADDR(13),
261 GR3D_GLOBAL_SAMP01SURFADDR(14),
262 GR3D_GLOBAL_SAMP01SURFADDR(15),
263 GR3D_GLOBAL_SAMP23SURFADDR( 0),
264 GR3D_GLOBAL_SAMP23SURFADDR( 1),
265 GR3D_GLOBAL_SAMP23SURFADDR( 2),
266 GR3D_GLOBAL_SAMP23SURFADDR( 3),
267 GR3D_GLOBAL_SAMP23SURFADDR( 4),
268 GR3D_GLOBAL_SAMP23SURFADDR( 5),
269 GR3D_GLOBAL_SAMP23SURFADDR( 6),
270 GR3D_GLOBAL_SAMP23SURFADDR( 7),
271 GR3D_GLOBAL_SAMP23SURFADDR( 8),
272 GR3D_GLOBAL_SAMP23SURFADDR( 9),
273 GR3D_GLOBAL_SAMP23SURFADDR(10),
274 GR3D_GLOBAL_SAMP23SURFADDR(11),
275 GR3D_GLOBAL_SAMP23SURFADDR(12),
276 GR3D_GLOBAL_SAMP23SURFADDR(13),
277 GR3D_GLOBAL_SAMP23SURFADDR(14),
278 GR3D_GLOBAL_SAMP23SURFADDR(15),
279};
280
281static int gr3d_probe(struct platform_device *pdev)
282{
283 struct device_node *np = pdev->dev.of_node;
284 struct host1x_syncpt **syncpts;
285 struct gr3d *gr3d;
286 unsigned int i;
287 int err;
288
289 gr3d = devm_kzalloc(&pdev->dev, sizeof(*gr3d), GFP_KERNEL);
290 if (!gr3d)
291 return -ENOMEM;
292
Thierry Reding33f150e2018-05-16 17:07:38 +0200293 gr3d->soc = of_device_get_match_data(&pdev->dev);
294
Thierry Reding5f60ed02013-02-28 08:08:01 +0100295 syncpts = devm_kzalloc(&pdev->dev, sizeof(*syncpts), GFP_KERNEL);
296 if (!syncpts)
297 return -ENOMEM;
298
299 gr3d->clk = devm_clk_get(&pdev->dev, NULL);
300 if (IS_ERR(gr3d->clk)) {
301 dev_err(&pdev->dev, "cannot get clock\n");
302 return PTR_ERR(gr3d->clk);
303 }
304
Stephen Warrenca480802013-11-06 16:20:54 -0700305 gr3d->rst = devm_reset_control_get(&pdev->dev, "3d");
306 if (IS_ERR(gr3d->rst)) {
307 dev_err(&pdev->dev, "cannot get reset\n");
308 return PTR_ERR(gr3d->rst);
309 }
310
Thierry Reding5f60ed02013-02-28 08:08:01 +0100311 if (of_device_is_compatible(np, "nvidia,tegra30-gr3d")) {
312 gr3d->clk_secondary = devm_clk_get(&pdev->dev, "3d2");
Christophe Jaillet87ba3e12016-07-03 08:18:57 +0200313 if (IS_ERR(gr3d->clk_secondary)) {
Thierry Reding5f60ed02013-02-28 08:08:01 +0100314 dev_err(&pdev->dev, "cannot get secondary clock\n");
Christophe Jaillet87ba3e12016-07-03 08:18:57 +0200315 return PTR_ERR(gr3d->clk_secondary);
Thierry Reding5f60ed02013-02-28 08:08:01 +0100316 }
Stephen Warrenca480802013-11-06 16:20:54 -0700317
318 gr3d->rst_secondary = devm_reset_control_get(&pdev->dev,
319 "3d2");
320 if (IS_ERR(gr3d->rst_secondary)) {
321 dev_err(&pdev->dev, "cannot get secondary reset\n");
322 return PTR_ERR(gr3d->rst_secondary);
323 }
Thierry Reding5f60ed02013-02-28 08:08:01 +0100324 }
325
Stephen Warren80b28792013-11-06 15:45:46 -0700326 err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_3D, gr3d->clk,
327 gr3d->rst);
Thierry Reding5f60ed02013-02-28 08:08:01 +0100328 if (err < 0) {
329 dev_err(&pdev->dev, "failed to power up 3D unit\n");
330 return err;
331 }
332
333 if (gr3d->clk_secondary) {
334 err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_3D1,
Stephen Warren80b28792013-11-06 15:45:46 -0700335 gr3d->clk_secondary,
336 gr3d->rst_secondary);
Thierry Reding5f60ed02013-02-28 08:08:01 +0100337 if (err < 0) {
338 dev_err(&pdev->dev,
339 "failed to power up secondary 3D unit\n");
340 return err;
341 }
342 }
343
344 INIT_LIST_HEAD(&gr3d->client.base.list);
345 gr3d->client.base.ops = &gr3d_client_ops;
346 gr3d->client.base.dev = &pdev->dev;
347 gr3d->client.base.class = HOST1X_CLASS_GR3D;
348 gr3d->client.base.syncpts = syncpts;
349 gr3d->client.base.num_syncpts = 1;
350
351 INIT_LIST_HEAD(&gr3d->client.list);
Thierry Reding33f150e2018-05-16 17:07:38 +0200352 gr3d->client.version = gr3d->soc->version;
Thierry Reding5f60ed02013-02-28 08:08:01 +0100353 gr3d->client.ops = &gr3d_ops;
354
355 err = host1x_client_register(&gr3d->client.base);
356 if (err < 0) {
357 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
358 err);
359 return err;
360 }
361
362 /* initialize address register map */
363 for (i = 0; i < ARRAY_SIZE(gr3d_addr_regs); i++)
364 set_bit(gr3d_addr_regs[i], gr3d->addr_regs);
365
366 platform_set_drvdata(pdev, gr3d);
367
368 return 0;
369}
370
371static int gr3d_remove(struct platform_device *pdev)
372{
373 struct gr3d *gr3d = platform_get_drvdata(pdev);
374 int err;
375
376 err = host1x_client_unregister(&gr3d->client.base);
377 if (err < 0) {
378 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
379 err);
380 return err;
381 }
382
383 if (gr3d->clk_secondary) {
384 tegra_powergate_power_off(TEGRA_POWERGATE_3D1);
385 clk_disable_unprepare(gr3d->clk_secondary);
386 }
387
388 tegra_powergate_power_off(TEGRA_POWERGATE_3D);
389 clk_disable_unprepare(gr3d->clk);
390
391 return 0;
392}
393
394struct platform_driver tegra_gr3d_driver = {
395 .driver = {
396 .name = "tegra-gr3d",
397 .of_match_table = tegra_gr3d_match,
398 },
399 .probe = gr3d_probe,
400 .remove = gr3d_remove,
401};