xref: /linux/drivers/gpu/drm/tegra/vic.c (revision 47b15779b03bf70ca5a315775d2b171c7913ebc3)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015, NVIDIA Corporation.
4  */
5 
6 #include <linux/clk.h>
7 #include <linux/delay.h>
8 #include <linux/host1x.h>
9 #include <linux/iommu.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/of_device.h>
13 #include <linux/of_platform.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/reset.h>
17 
18 #include <soc/tegra/pmc.h>
19 
20 #include "drm.h"
21 #include "falcon.h"
22 #include "vic.h"
23 
24 struct vic_config {
25 	const char *firmware;
26 	unsigned int version;
27 	bool supports_sid;
28 };
29 
30 struct vic {
31 	struct falcon falcon;
32 	bool booted;
33 
34 	void __iomem *regs;
35 	struct tegra_drm_client client;
36 	struct host1x_channel *channel;
37 	struct iommu_domain *domain;
38 	struct device *dev;
39 	struct clk *clk;
40 	struct reset_control *rst;
41 
42 	/* Platform configuration */
43 	const struct vic_config *config;
44 };
45 
46 static inline struct vic *to_vic(struct tegra_drm_client *client)
47 {
48 	return container_of(client, struct vic, client);
49 }
50 
51 static void vic_writel(struct vic *vic, u32 value, unsigned int offset)
52 {
53 	writel(value, vic->regs + offset);
54 }
55 
56 static int vic_runtime_resume(struct device *dev)
57 {
58 	struct vic *vic = dev_get_drvdata(dev);
59 	int err;
60 
61 	err = clk_prepare_enable(vic->clk);
62 	if (err < 0)
63 		return err;
64 
65 	usleep_range(10, 20);
66 
67 	err = reset_control_deassert(vic->rst);
68 	if (err < 0)
69 		goto disable;
70 
71 	usleep_range(10, 20);
72 
73 	return 0;
74 
75 disable:
76 	clk_disable_unprepare(vic->clk);
77 	return err;
78 }
79 
80 static int vic_runtime_suspend(struct device *dev)
81 {
82 	struct vic *vic = dev_get_drvdata(dev);
83 	int err;
84 
85 	err = reset_control_assert(vic->rst);
86 	if (err < 0)
87 		return err;
88 
89 	usleep_range(2000, 4000);
90 
91 	clk_disable_unprepare(vic->clk);
92 
93 	vic->booted = false;
94 
95 	return 0;
96 }
97 
98 static int vic_boot(struct vic *vic)
99 {
100 	u32 fce_ucode_size, fce_bin_data_offset;
101 	void *hdr;
102 	int err = 0;
103 
104 	if (vic->booted)
105 		return 0;
106 
107 #ifdef CONFIG_IOMMU_API
108 	if (vic->config->supports_sid) {
109 		struct iommu_fwspec *spec = dev_iommu_fwspec_get(vic->dev);
110 		u32 value;
111 
112 		value = TRANSCFG_ATT(1, TRANSCFG_SID_FALCON) |
113 			TRANSCFG_ATT(0, TRANSCFG_SID_HW);
114 		vic_writel(vic, value, VIC_TFBIF_TRANSCFG);
115 
116 		if (spec && spec->num_ids > 0) {
117 			value = spec->ids[0] & 0xffff;
118 
119 			vic_writel(vic, value, VIC_THI_STREAMID0);
120 			vic_writel(vic, value, VIC_THI_STREAMID1);
121 		}
122 	}
123 #endif
124 
125 	/* setup clockgating registers */
126 	vic_writel(vic, CG_IDLE_CG_DLY_CNT(4) |
127 			CG_IDLE_CG_EN |
128 			CG_WAKEUP_DLY_CNT(4),
129 		   NV_PVIC_MISC_PRI_VIC_CG);
130 
131 	err = falcon_boot(&vic->falcon);
132 	if (err < 0)
133 		return err;
134 
135 	hdr = vic->falcon.firmware.vaddr;
136 	fce_bin_data_offset = *(u32 *)(hdr + VIC_UCODE_FCE_DATA_OFFSET);
137 	hdr = vic->falcon.firmware.vaddr +
138 		*(u32 *)(hdr + VIC_UCODE_FCE_HEADER_OFFSET);
139 	fce_ucode_size = *(u32 *)(hdr + FCE_UCODE_SIZE_OFFSET);
140 
141 	falcon_execute_method(&vic->falcon, VIC_SET_APPLICATION_ID, 1);
142 	falcon_execute_method(&vic->falcon, VIC_SET_FCE_UCODE_SIZE,
143 			      fce_ucode_size);
144 	falcon_execute_method(&vic->falcon, VIC_SET_FCE_UCODE_OFFSET,
145 			      (vic->falcon.firmware.paddr + fce_bin_data_offset)
146 				>> 8);
147 
148 	err = falcon_wait_idle(&vic->falcon);
149 	if (err < 0) {
150 		dev_err(vic->dev,
151 			"failed to set application ID and FCE base\n");
152 		return err;
153 	}
154 
155 	vic->booted = true;
156 
157 	return 0;
158 }
159 
160 static void *vic_falcon_alloc(struct falcon *falcon, size_t size,
161 			      dma_addr_t *iova)
162 {
163 	struct tegra_drm *tegra = falcon->data;
164 
165 	return tegra_drm_alloc(tegra, size, iova);
166 }
167 
168 static void vic_falcon_free(struct falcon *falcon, size_t size,
169 			    dma_addr_t iova, void *va)
170 {
171 	struct tegra_drm *tegra = falcon->data;
172 
173 	return tegra_drm_free(tegra, size, va, iova);
174 }
175 
176 static const struct falcon_ops vic_falcon_ops = {
177 	.alloc = vic_falcon_alloc,
178 	.free = vic_falcon_free
179 };
180 
181 static int vic_init(struct host1x_client *client)
182 {
183 	struct tegra_drm_client *drm = host1x_to_drm_client(client);
184 	struct iommu_group *group = iommu_group_get(client->dev);
185 	struct drm_device *dev = dev_get_drvdata(client->parent);
186 	struct tegra_drm *tegra = dev->dev_private;
187 	struct vic *vic = to_vic(drm);
188 	int err;
189 
190 	if (group && tegra->domain) {
191 		err = iommu_attach_group(tegra->domain, group);
192 		if (err < 0) {
193 			dev_err(vic->dev, "failed to attach to domain: %d\n",
194 				err);
195 			return err;
196 		}
197 
198 		vic->domain = tegra->domain;
199 	}
200 
201 	vic->channel = host1x_channel_request(client);
202 	if (!vic->channel) {
203 		err = -ENOMEM;
204 		goto detach;
205 	}
206 
207 	client->syncpts[0] = host1x_syncpt_request(client, 0);
208 	if (!client->syncpts[0]) {
209 		err = -ENOMEM;
210 		goto free_channel;
211 	}
212 
213 	err = tegra_drm_register_client(tegra, drm);
214 	if (err < 0)
215 		goto free_syncpt;
216 
217 	/*
218 	 * Inherit the DMA parameters (such as maximum segment size) from the
219 	 * parent device.
220 	 */
221 	client->dev->dma_parms = client->parent->dma_parms;
222 
223 	return 0;
224 
225 free_syncpt:
226 	host1x_syncpt_free(client->syncpts[0]);
227 free_channel:
228 	host1x_channel_put(vic->channel);
229 detach:
230 	if (group && tegra->domain)
231 		iommu_detach_group(tegra->domain, group);
232 
233 	return err;
234 }
235 
236 static int vic_exit(struct host1x_client *client)
237 {
238 	struct tegra_drm_client *drm = host1x_to_drm_client(client);
239 	struct iommu_group *group = iommu_group_get(client->dev);
240 	struct drm_device *dev = dev_get_drvdata(client->parent);
241 	struct tegra_drm *tegra = dev->dev_private;
242 	struct vic *vic = to_vic(drm);
243 	int err;
244 
245 	/* avoid a dangling pointer just in case this disappears */
246 	client->dev->dma_parms = NULL;
247 
248 	err = tegra_drm_unregister_client(tegra, drm);
249 	if (err < 0)
250 		return err;
251 
252 	host1x_syncpt_free(client->syncpts[0]);
253 	host1x_channel_put(vic->channel);
254 
255 	if (vic->domain) {
256 		iommu_detach_group(vic->domain, group);
257 		vic->domain = NULL;
258 	}
259 
260 	return 0;
261 }
262 
263 static const struct host1x_client_ops vic_client_ops = {
264 	.init = vic_init,
265 	.exit = vic_exit,
266 };
267 
268 static int vic_load_firmware(struct vic *vic)
269 {
270 	int err;
271 
272 	if (vic->falcon.data)
273 		return 0;
274 
275 	vic->falcon.data = vic->client.drm;
276 
277 	err = falcon_read_firmware(&vic->falcon, vic->config->firmware);
278 	if (err < 0)
279 		goto cleanup;
280 
281 	err = falcon_load_firmware(&vic->falcon);
282 	if (err < 0)
283 		goto cleanup;
284 
285 	return 0;
286 
287 cleanup:
288 	vic->falcon.data = NULL;
289 	return err;
290 }
291 
292 static int vic_open_channel(struct tegra_drm_client *client,
293 			    struct tegra_drm_context *context)
294 {
295 	struct vic *vic = to_vic(client);
296 	int err;
297 
298 	err = pm_runtime_get_sync(vic->dev);
299 	if (err < 0)
300 		return err;
301 
302 	err = vic_load_firmware(vic);
303 	if (err < 0)
304 		goto rpm_put;
305 
306 	err = vic_boot(vic);
307 	if (err < 0)
308 		goto rpm_put;
309 
310 	context->channel = host1x_channel_get(vic->channel);
311 	if (!context->channel) {
312 		err = -ENOMEM;
313 		goto rpm_put;
314 	}
315 
316 	return 0;
317 
318 rpm_put:
319 	pm_runtime_put(vic->dev);
320 	return err;
321 }
322 
323 static void vic_close_channel(struct tegra_drm_context *context)
324 {
325 	struct vic *vic = to_vic(context->client);
326 
327 	host1x_channel_put(context->channel);
328 
329 	pm_runtime_put(vic->dev);
330 }
331 
332 static const struct tegra_drm_client_ops vic_ops = {
333 	.open_channel = vic_open_channel,
334 	.close_channel = vic_close_channel,
335 	.submit = tegra_drm_submit,
336 };
337 
338 #define NVIDIA_TEGRA_124_VIC_FIRMWARE "nvidia/tegra124/vic03_ucode.bin"
339 
340 static const struct vic_config vic_t124_config = {
341 	.firmware = NVIDIA_TEGRA_124_VIC_FIRMWARE,
342 	.version = 0x40,
343 	.supports_sid = false,
344 };
345 
346 #define NVIDIA_TEGRA_210_VIC_FIRMWARE "nvidia/tegra210/vic04_ucode.bin"
347 
348 static const struct vic_config vic_t210_config = {
349 	.firmware = NVIDIA_TEGRA_210_VIC_FIRMWARE,
350 	.version = 0x21,
351 	.supports_sid = false,
352 };
353 
354 #define NVIDIA_TEGRA_186_VIC_FIRMWARE "nvidia/tegra186/vic04_ucode.bin"
355 
356 static const struct vic_config vic_t186_config = {
357 	.firmware = NVIDIA_TEGRA_186_VIC_FIRMWARE,
358 	.version = 0x18,
359 	.supports_sid = true,
360 };
361 
362 #define NVIDIA_TEGRA_194_VIC_FIRMWARE "nvidia/tegra194/vic.bin"
363 
364 static const struct vic_config vic_t194_config = {
365 	.firmware = NVIDIA_TEGRA_194_VIC_FIRMWARE,
366 	.version = 0x19,
367 	.supports_sid = true,
368 };
369 
370 static const struct of_device_id vic_match[] = {
371 	{ .compatible = "nvidia,tegra124-vic", .data = &vic_t124_config },
372 	{ .compatible = "nvidia,tegra210-vic", .data = &vic_t210_config },
373 	{ .compatible = "nvidia,tegra186-vic", .data = &vic_t186_config },
374 	{ .compatible = "nvidia,tegra194-vic", .data = &vic_t194_config },
375 	{ },
376 };
377 
378 static int vic_probe(struct platform_device *pdev)
379 {
380 	struct device *dev = &pdev->dev;
381 	struct host1x_syncpt **syncpts;
382 	struct resource *regs;
383 	struct vic *vic;
384 	int err;
385 
386 	vic = devm_kzalloc(dev, sizeof(*vic), GFP_KERNEL);
387 	if (!vic)
388 		return -ENOMEM;
389 
390 	vic->config = of_device_get_match_data(dev);
391 
392 	syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
393 	if (!syncpts)
394 		return -ENOMEM;
395 
396 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
397 	if (!regs) {
398 		dev_err(&pdev->dev, "failed to get registers\n");
399 		return -ENXIO;
400 	}
401 
402 	vic->regs = devm_ioremap_resource(dev, regs);
403 	if (IS_ERR(vic->regs))
404 		return PTR_ERR(vic->regs);
405 
406 	vic->clk = devm_clk_get(dev, NULL);
407 	if (IS_ERR(vic->clk)) {
408 		dev_err(&pdev->dev, "failed to get clock\n");
409 		return PTR_ERR(vic->clk);
410 	}
411 
412 	if (!dev->pm_domain) {
413 		vic->rst = devm_reset_control_get(dev, "vic");
414 		if (IS_ERR(vic->rst)) {
415 			dev_err(&pdev->dev, "failed to get reset\n");
416 			return PTR_ERR(vic->rst);
417 		}
418 	}
419 
420 	vic->falcon.dev = dev;
421 	vic->falcon.regs = vic->regs;
422 	vic->falcon.ops = &vic_falcon_ops;
423 
424 	err = falcon_init(&vic->falcon);
425 	if (err < 0)
426 		return err;
427 
428 	platform_set_drvdata(pdev, vic);
429 
430 	INIT_LIST_HEAD(&vic->client.base.list);
431 	vic->client.base.ops = &vic_client_ops;
432 	vic->client.base.dev = dev;
433 	vic->client.base.class = HOST1X_CLASS_VIC;
434 	vic->client.base.syncpts = syncpts;
435 	vic->client.base.num_syncpts = 1;
436 	vic->dev = dev;
437 
438 	INIT_LIST_HEAD(&vic->client.list);
439 	vic->client.version = vic->config->version;
440 	vic->client.ops = &vic_ops;
441 
442 	err = host1x_client_register(&vic->client.base);
443 	if (err < 0) {
444 		dev_err(dev, "failed to register host1x client: %d\n", err);
445 		goto exit_falcon;
446 	}
447 
448 	pm_runtime_enable(&pdev->dev);
449 	if (!pm_runtime_enabled(&pdev->dev)) {
450 		err = vic_runtime_resume(&pdev->dev);
451 		if (err < 0)
452 			goto unregister_client;
453 	}
454 
455 	return 0;
456 
457 unregister_client:
458 	host1x_client_unregister(&vic->client.base);
459 exit_falcon:
460 	falcon_exit(&vic->falcon);
461 
462 	return err;
463 }
464 
465 static int vic_remove(struct platform_device *pdev)
466 {
467 	struct vic *vic = platform_get_drvdata(pdev);
468 	int err;
469 
470 	err = host1x_client_unregister(&vic->client.base);
471 	if (err < 0) {
472 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
473 			err);
474 		return err;
475 	}
476 
477 	if (pm_runtime_enabled(&pdev->dev))
478 		pm_runtime_disable(&pdev->dev);
479 	else
480 		vic_runtime_suspend(&pdev->dev);
481 
482 	falcon_exit(&vic->falcon);
483 
484 	return 0;
485 }
486 
487 static const struct dev_pm_ops vic_pm_ops = {
488 	SET_RUNTIME_PM_OPS(vic_runtime_suspend, vic_runtime_resume, NULL)
489 };
490 
491 struct platform_driver tegra_vic_driver = {
492 	.driver = {
493 		.name = "tegra-vic",
494 		.of_match_table = vic_match,
495 		.pm = &vic_pm_ops
496 	},
497 	.probe = vic_probe,
498 	.remove = vic_remove,
499 };
500 
501 #if IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC)
502 MODULE_FIRMWARE(NVIDIA_TEGRA_124_VIC_FIRMWARE);
503 #endif
504 #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC)
505 MODULE_FIRMWARE(NVIDIA_TEGRA_210_VIC_FIRMWARE);
506 #endif
507 #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC)
508 MODULE_FIRMWARE(NVIDIA_TEGRA_186_VIC_FIRMWARE);
509 #endif
510 #if IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC)
511 MODULE_FIRMWARE(NVIDIA_TEGRA_194_VIC_FIRMWARE);
512 #endif
513