4 * Copyright (c) 2010-2013, NVIDIA Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <linux/clk.h>
20 #include <linux/dma-mapping.h>
22 #include <linux/list.h>
23 #include <linux/module.h>
24 #include <linux/of_device.h>
26 #include <linux/slab.h>
28 #define CREATE_TRACE_POINTS
29 #include <trace/events/host1x.h>
30 #undef CREATE_TRACE_POINTS
32 #if IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)
33 #include <asm/dma-iommu.h>
42 #include "hw/host1x01.h"
43 #include "hw/host1x02.h"
44 #include "hw/host1x04.h"
45 #include "hw/host1x05.h"
46 #include "hw/host1x06.h"
47 #include "hw/host1x07.h"
49 void host1x_hypervisor_writel(struct host1x *host1x, u32 v, u32 r)
51 writel(v, host1x->hv_regs + r);
54 u32 host1x_hypervisor_readl(struct host1x *host1x, u32 r)
56 return readl(host1x->hv_regs + r);
59 void host1x_sync_writel(struct host1x *host1x, u32 v, u32 r)
61 void __iomem *sync_regs = host1x->regs + host1x->info->sync_offset;
63 writel(v, sync_regs + r);
66 u32 host1x_sync_readl(struct host1x *host1x, u32 r)
68 void __iomem *sync_regs = host1x->regs + host1x->info->sync_offset;
70 return readl(sync_regs + r);
73 void host1x_ch_writel(struct host1x_channel *ch, u32 v, u32 r)
75 writel(v, ch->regs + r);
78 u32 host1x_ch_readl(struct host1x_channel *ch, u32 r)
80 return readl(ch->regs + r);
83 static const struct host1x_info host1x01_info = {
88 .init = host1x01_init,
89 .sync_offset = 0x3000,
90 .dma_mask = DMA_BIT_MASK(32),
93 static const struct host1x_info host1x02_info = {
98 .init = host1x02_init,
99 .sync_offset = 0x3000,
100 .dma_mask = DMA_BIT_MASK(32),
103 static const struct host1x_info host1x04_info = {
108 .init = host1x04_init,
109 .sync_offset = 0x2100,
110 .dma_mask = DMA_BIT_MASK(34),
113 static const struct host1x_info host1x05_info = {
118 .init = host1x05_init,
119 .sync_offset = 0x2100,
120 .dma_mask = DMA_BIT_MASK(34),
123 static const struct host1x_info host1x06_info = {
128 .init = host1x06_init,
130 .dma_mask = DMA_BIT_MASK(34),
131 .has_hypervisor = true,
134 static const struct host1x_info host1x07_info = {
139 .init = host1x07_init,
141 .dma_mask = DMA_BIT_MASK(40),
142 .has_hypervisor = true,
145 static const struct of_device_id host1x_of_match[] = {
146 { .compatible = "nvidia,tegra194-host1x", .data = &host1x07_info, },
147 { .compatible = "nvidia,tegra186-host1x", .data = &host1x06_info, },
148 { .compatible = "nvidia,tegra210-host1x", .data = &host1x05_info, },
149 { .compatible = "nvidia,tegra124-host1x", .data = &host1x04_info, },
150 { .compatible = "nvidia,tegra114-host1x", .data = &host1x02_info, },
151 { .compatible = "nvidia,tegra30-host1x", .data = &host1x01_info, },
152 { .compatible = "nvidia,tegra20-host1x", .data = &host1x01_info, },
155 MODULE_DEVICE_TABLE(of, host1x_of_match);
157 static int host1x_probe(struct platform_device *pdev)
160 struct resource *regs, *hv_regs = NULL;
164 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
168 host->info = of_device_get_match_data(&pdev->dev);
170 if (host->info->has_hypervisor) {
171 regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vm");
173 dev_err(&pdev->dev, "failed to get vm registers\n");
177 hv_regs = platform_get_resource_byname(pdev, IORESOURCE_MEM,
181 "failed to get hypervisor registers\n");
185 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
187 dev_err(&pdev->dev, "failed to get registers\n");
192 syncpt_irq = platform_get_irq(pdev, 0);
193 if (syncpt_irq < 0) {
194 dev_err(&pdev->dev, "failed to get IRQ: %d\n", syncpt_irq);
198 mutex_init(&host->devices_lock);
199 INIT_LIST_HEAD(&host->devices);
200 INIT_LIST_HEAD(&host->list);
201 host->dev = &pdev->dev;
203 /* set common host1x device data */
204 platform_set_drvdata(pdev, host);
206 host->regs = devm_ioremap_resource(&pdev->dev, regs);
207 if (IS_ERR(host->regs))
208 return PTR_ERR(host->regs);
210 if (host->info->has_hypervisor) {
211 host->hv_regs = devm_ioremap_resource(&pdev->dev, hv_regs);
212 if (IS_ERR(host->hv_regs))
213 return PTR_ERR(host->hv_regs);
216 dma_set_mask_and_coherent(host->dev, host->info->dma_mask);
218 if (host->info->init) {
219 err = host->info->init(host);
224 host->clk = devm_clk_get(&pdev->dev, NULL);
225 if (IS_ERR(host->clk)) {
226 dev_err(&pdev->dev, "failed to get clock\n");
227 err = PTR_ERR(host->clk);
231 host->rst = devm_reset_control_get(&pdev->dev, "host1x");
232 if (IS_ERR(host->rst)) {
233 err = PTR_ERR(host->rst);
234 dev_err(&pdev->dev, "failed to get reset: %d\n", err);
237 #if IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)
238 if (host->dev->archdata.mapping) {
239 struct dma_iommu_mapping *mapping =
240 to_dma_iommu_mapping(host->dev);
241 arm_iommu_detach_device(host->dev);
242 arm_iommu_release_mapping(mapping);
245 if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
248 host->group = iommu_group_get(&pdev->dev);
250 struct iommu_domain_geometry *geometry;
253 err = iova_cache_get();
257 host->domain = iommu_domain_alloc(&platform_bus_type);
263 err = iommu_attach_group(host->domain, host->group);
265 if (err == -ENODEV) {
266 iommu_domain_free(host->domain);
269 iommu_group_put(host->group);
274 goto fail_free_domain;
277 geometry = &host->domain->geometry;
279 order = __ffs(host->domain->pgsize_bitmap);
280 init_iova_domain(&host->iova, 1UL << order,
281 geometry->aperture_start >> order);
282 host->iova_end = geometry->aperture_end;
286 err = host1x_channel_list_init(&host->channel_list,
287 host->info->nb_channels);
289 dev_err(&pdev->dev, "failed to initialize channel list\n");
290 goto fail_detach_device;
293 err = clk_prepare_enable(host->clk);
295 dev_err(&pdev->dev, "failed to enable clock\n");
296 goto fail_free_channels;
299 err = reset_control_deassert(host->rst);
301 dev_err(&pdev->dev, "failed to deassert reset: %d\n", err);
302 goto fail_unprepare_disable;
305 err = host1x_syncpt_init(host);
307 dev_err(&pdev->dev, "failed to initialize syncpts\n");
308 goto fail_reset_assert;
311 err = host1x_intr_init(host, syncpt_irq);
313 dev_err(&pdev->dev, "failed to initialize interrupts\n");
314 goto fail_deinit_syncpt;
317 host1x_debug_init(host);
319 err = host1x_register(host);
321 goto fail_deinit_intr;
326 host1x_intr_deinit(host);
328 host1x_syncpt_deinit(host);
330 reset_control_assert(host->rst);
331 fail_unprepare_disable:
332 clk_disable_unprepare(host->clk);
334 host1x_channel_list_free(&host->channel_list);
336 if (host->group && host->domain) {
337 put_iova_domain(&host->iova);
338 iommu_detach_group(host->domain, host->group);
342 iommu_domain_free(host->domain);
347 iommu_group_put(host->group);
352 static int host1x_remove(struct platform_device *pdev)
354 struct host1x *host = platform_get_drvdata(pdev);
356 host1x_unregister(host);
357 host1x_intr_deinit(host);
358 host1x_syncpt_deinit(host);
359 reset_control_assert(host->rst);
360 clk_disable_unprepare(host->clk);
363 put_iova_domain(&host->iova);
364 iommu_detach_group(host->domain, host->group);
365 iommu_domain_free(host->domain);
367 iommu_group_put(host->group);
373 static struct platform_driver tegra_host1x_driver = {
375 .name = "tegra-host1x",
376 .of_match_table = host1x_of_match,
378 .probe = host1x_probe,
379 .remove = host1x_remove,
382 static struct platform_driver * const drivers[] = {
383 &tegra_host1x_driver,
387 static int __init tegra_host1x_init(void)
391 err = bus_register(&host1x_bus_type);
395 err = platform_register_drivers(drivers, ARRAY_SIZE(drivers));
397 bus_unregister(&host1x_bus_type);
401 module_init(tegra_host1x_init);
403 static void __exit tegra_host1x_exit(void)
405 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
406 bus_unregister(&host1x_bus_type);
408 module_exit(tegra_host1x_exit);
412 MODULE_DESCRIPTION("Host1x driver for Tegra products");
413 MODULE_LICENSE("GPL");