1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012-2013, NVIDIA Corporation.
7 #include <linux/delay.h>
8 #include <linux/iommu.h>
9 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/reset.h>
15 #include <soc/tegra/common.h>
32 struct tegra_drm_client client
;
33 struct host1x_channel
*channel
;
36 struct reset_control_bulk_data resets
[RST_GR2D_MAX
];
39 const struct gr2d_soc
*soc
;
41 DECLARE_BITMAP(addr_regs
, GR2D_NUM_REGS
);
44 static inline struct gr2d
*to_gr2d(struct tegra_drm_client
*client
)
46 return container_of(client
, struct gr2d
, client
);
49 static int gr2d_init(struct host1x_client
*client
)
51 struct tegra_drm_client
*drm
= host1x_to_drm_client(client
);
52 struct drm_device
*dev
= dev_get_drvdata(client
->host
);
53 unsigned long flags
= HOST1X_SYNCPT_HAS_BASE
;
54 struct gr2d
*gr2d
= to_gr2d(drm
);
57 gr2d
->channel
= host1x_channel_request(client
);
61 client
->syncpts
[0] = host1x_syncpt_request(client
, flags
);
62 if (!client
->syncpts
[0]) {
64 dev_err(client
->dev
, "failed to request syncpoint: %d\n", err
);
68 err
= host1x_client_iommu_attach(client
);
70 dev_err(client
->dev
, "failed to attach to domain: %d\n", err
);
74 err
= tegra_drm_register_client(dev
->dev_private
, drm
);
76 dev_err(client
->dev
, "failed to register client: %d\n", err
);
83 host1x_client_iommu_detach(client
);
85 host1x_syncpt_put(client
->syncpts
[0]);
87 host1x_channel_put(gr2d
->channel
);
91 static int gr2d_exit(struct host1x_client
*client
)
93 struct tegra_drm_client
*drm
= host1x_to_drm_client(client
);
94 struct drm_device
*dev
= dev_get_drvdata(client
->host
);
95 struct tegra_drm
*tegra
= dev
->dev_private
;
96 struct gr2d
*gr2d
= to_gr2d(drm
);
99 err
= tegra_drm_unregister_client(tegra
, drm
);
103 pm_runtime_dont_use_autosuspend(client
->dev
);
104 pm_runtime_force_suspend(client
->dev
);
106 host1x_client_iommu_detach(client
);
107 host1x_syncpt_put(client
->syncpts
[0]);
108 host1x_channel_put(gr2d
->channel
);
110 gr2d
->channel
= NULL
;
115 static const struct host1x_client_ops gr2d_client_ops
= {
120 static int gr2d_open_channel(struct tegra_drm_client
*client
,
121 struct tegra_drm_context
*context
)
123 struct gr2d
*gr2d
= to_gr2d(client
);
125 context
->channel
= host1x_channel_get(gr2d
->channel
);
126 if (!context
->channel
)
132 static void gr2d_close_channel(struct tegra_drm_context
*context
)
134 host1x_channel_put(context
->channel
);
137 static int gr2d_is_addr_reg(struct device
*dev
, u32
class, u32 offset
)
139 struct gr2d
*gr2d
= dev_get_drvdata(dev
);
142 case HOST1X_CLASS_HOST1X
:
148 case HOST1X_CLASS_GR2D
:
149 case HOST1X_CLASS_GR2D_SB
:
150 if (offset
>= GR2D_NUM_REGS
)
153 if (test_bit(offset
, gr2d
->addr_regs
))
162 static int gr2d_is_valid_class(u32
class)
164 return (class == HOST1X_CLASS_GR2D
||
165 class == HOST1X_CLASS_GR2D_SB
);
168 static const struct tegra_drm_client_ops gr2d_ops
= {
169 .open_channel
= gr2d_open_channel
,
170 .close_channel
= gr2d_close_channel
,
171 .is_addr_reg
= gr2d_is_addr_reg
,
172 .is_valid_class
= gr2d_is_valid_class
,
173 .submit
= tegra_drm_submit
,
176 static const struct gr2d_soc tegra20_gr2d_soc
= {
180 static const struct gr2d_soc tegra30_gr2d_soc
= {
184 static const struct gr2d_soc tegra114_gr2d_soc
= {
188 static const struct of_device_id gr2d_match
[] = {
189 { .compatible
= "nvidia,tegra114-gr2d", .data
= &tegra114_gr2d_soc
},
190 { .compatible
= "nvidia,tegra30-gr2d", .data
= &tegra30_gr2d_soc
},
191 { .compatible
= "nvidia,tegra20-gr2d", .data
= &tegra20_gr2d_soc
},
194 MODULE_DEVICE_TABLE(of
, gr2d_match
);
196 static const u32 gr2d_addr_regs
[] = {
206 GR2D_SRC_BASE_ADDR_SB
,
207 GR2D_DSTA_BASE_ADDR_SB
,
208 GR2D_DSTB_BASE_ADDR_SB
,
209 GR2D_UA_BASE_ADDR_SB
,
210 GR2D_VA_BASE_ADDR_SB
,
213 static int gr2d_get_resets(struct device
*dev
, struct gr2d
*gr2d
)
217 gr2d
->resets
[RST_MC
].id
= "mc";
218 gr2d
->resets
[RST_GR2D
].id
= "2d";
219 gr2d
->nresets
= RST_GR2D_MAX
;
221 err
= devm_reset_control_bulk_get_optional_exclusive_released(
222 dev
, gr2d
->nresets
, gr2d
->resets
);
224 dev_err(dev
, "failed to get reset: %d\n", err
);
228 if (WARN_ON(!gr2d
->resets
[RST_GR2D
].rstc
))
234 static int gr2d_probe(struct platform_device
*pdev
)
236 struct device
*dev
= &pdev
->dev
;
237 struct host1x_syncpt
**syncpts
;
242 gr2d
= devm_kzalloc(dev
, sizeof(*gr2d
), GFP_KERNEL
);
246 platform_set_drvdata(pdev
, gr2d
);
248 gr2d
->soc
= of_device_get_match_data(dev
);
250 syncpts
= devm_kzalloc(dev
, sizeof(*syncpts
), GFP_KERNEL
);
254 gr2d
->clk
= devm_clk_get(dev
, NULL
);
255 if (IS_ERR(gr2d
->clk
)) {
256 dev_err(dev
, "cannot get clock\n");
257 return PTR_ERR(gr2d
->clk
);
260 err
= gr2d_get_resets(dev
, gr2d
);
264 INIT_LIST_HEAD(&gr2d
->client
.base
.list
);
265 gr2d
->client
.base
.ops
= &gr2d_client_ops
;
266 gr2d
->client
.base
.dev
= dev
;
267 gr2d
->client
.base
.class = HOST1X_CLASS_GR2D
;
268 gr2d
->client
.base
.syncpts
= syncpts
;
269 gr2d
->client
.base
.num_syncpts
= 1;
271 INIT_LIST_HEAD(&gr2d
->client
.list
);
272 gr2d
->client
.version
= gr2d
->soc
->version
;
273 gr2d
->client
.ops
= &gr2d_ops
;
275 err
= devm_tegra_core_dev_init_opp_table_common(dev
);
279 err
= host1x_client_register(&gr2d
->client
.base
);
281 dev_err(dev
, "failed to register host1x client: %d\n", err
);
285 /* initialize address register map */
286 for (i
= 0; i
< ARRAY_SIZE(gr2d_addr_regs
); i
++)
287 set_bit(gr2d_addr_regs
[i
], gr2d
->addr_regs
);
292 static void gr2d_remove(struct platform_device
*pdev
)
294 struct gr2d
*gr2d
= platform_get_drvdata(pdev
);
296 pm_runtime_disable(&pdev
->dev
);
297 host1x_client_unregister(&gr2d
->client
.base
);
300 static int __maybe_unused
gr2d_runtime_suspend(struct device
*dev
)
302 struct gr2d
*gr2d
= dev_get_drvdata(dev
);
305 host1x_channel_stop(gr2d
->channel
);
306 reset_control_bulk_release(gr2d
->nresets
, gr2d
->resets
);
309 * GR2D module shouldn't be reset while hardware is idling, otherwise
310 * host1x's cmdproc will stuck on trying to access any G2 register
311 * after reset. GR2D module could be either hot-reset or reset after
312 * power-gating of the HEG partition. Hence we will put in reset only
313 * the memory client part of the module, the HEG GENPD will take care
314 * of resetting GR2D module across power-gating.
316 * On Tegra20 there is no HEG partition, but it's okay to have
317 * undetermined h/w state since userspace is expected to reprogram
318 * the state on each job submission anyways.
320 err
= reset_control_acquire(gr2d
->resets
[RST_MC
].rstc
);
322 dev_err(dev
, "failed to acquire MC reset: %d\n", err
);
326 err
= reset_control_assert(gr2d
->resets
[RST_MC
].rstc
);
327 reset_control_release(gr2d
->resets
[RST_MC
].rstc
);
329 dev_err(dev
, "failed to assert MC reset: %d\n", err
);
333 clk_disable_unprepare(gr2d
->clk
);
338 reset_control_bulk_acquire(gr2d
->nresets
, gr2d
->resets
);
339 reset_control_bulk_deassert(gr2d
->nresets
, gr2d
->resets
);
344 static int __maybe_unused
gr2d_runtime_resume(struct device
*dev
)
346 struct gr2d
*gr2d
= dev_get_drvdata(dev
);
349 err
= reset_control_bulk_acquire(gr2d
->nresets
, gr2d
->resets
);
351 dev_err(dev
, "failed to acquire reset: %d\n", err
);
355 err
= clk_prepare_enable(gr2d
->clk
);
357 dev_err(dev
, "failed to enable clock: %d\n", err
);
361 usleep_range(2000, 4000);
363 /* this is a reset array which deasserts both 2D MC and 2D itself */
364 err
= reset_control_bulk_deassert(gr2d
->nresets
, gr2d
->resets
);
366 dev_err(dev
, "failed to deassert reset: %d\n", err
);
370 pm_runtime_enable(dev
);
371 pm_runtime_use_autosuspend(dev
);
372 pm_runtime_set_autosuspend_delay(dev
, 500);
377 clk_disable_unprepare(gr2d
->clk
);
379 reset_control_bulk_release(gr2d
->nresets
, gr2d
->resets
);
384 static const struct dev_pm_ops tegra_gr2d_pm
= {
385 SET_RUNTIME_PM_OPS(gr2d_runtime_suspend
, gr2d_runtime_resume
, NULL
)
386 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
387 pm_runtime_force_resume
)
390 struct platform_driver tegra_gr2d_driver
= {
392 .name
= "tegra-gr2d",
393 .of_match_table
= gr2d_match
,
394 .pm
= &tegra_gr2d_pm
,
397 .remove
= gr2d_remove
,