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"
48 void host1x_hypervisor_writel(struct host1x *host1x, u32 v, u32 r)
50 writel(v, host1x->hv_regs + r);
53 u32 host1x_hypervisor_readl(struct host1x *host1x, u32 r)
55 return readl(host1x->hv_regs + r);
58 void host1x_sync_writel(struct host1x *host1x, u32 v, u32 r)
60 void __iomem *sync_regs = host1x->regs + host1x->info->sync_offset;
62 writel(v, sync_regs + r);
65 u32 host1x_sync_readl(struct host1x *host1x, u32 r)
67 void __iomem *sync_regs = host1x->regs + host1x->info->sync_offset;
69 return readl(sync_regs + r);
72 void host1x_ch_writel(struct host1x_channel *ch, u32 v, u32 r)
74 writel(v, ch->regs + r);
77 u32 host1x_ch_readl(struct host1x_channel *ch, u32 r)
79 return readl(ch->regs + r);
82 static const struct host1x_info host1x01_info = {
87 .init = host1x01_init,
88 .sync_offset = 0x3000,
89 .dma_mask = DMA_BIT_MASK(32),
92 static const struct host1x_info host1x02_info = {
97 .init = host1x02_init,
98 .sync_offset = 0x3000,
99 .dma_mask = DMA_BIT_MASK(32),
102 static const struct host1x_info host1x04_info = {
107 .init = host1x04_init,
108 .sync_offset = 0x2100,
109 .dma_mask = DMA_BIT_MASK(34),
112 static const struct host1x_info host1x05_info = {
117 .init = host1x05_init,
118 .sync_offset = 0x2100,
119 .dma_mask = DMA_BIT_MASK(34),
122 static const struct host1x_info host1x06_info = {
127 .init = host1x06_init,
129 .dma_mask = DMA_BIT_MASK(34),
130 .has_hypervisor = true,
133 static const struct of_device_id host1x_of_match[] = {
134 { .compatible = "nvidia,tegra186-host1x", .data = &host1x06_info, },
135 { .compatible = "nvidia,tegra210-host1x", .data = &host1x05_info, },
136 { .compatible = "nvidia,tegra124-host1x", .data = &host1x04_info, },
137 { .compatible = "nvidia,tegra114-host1x", .data = &host1x02_info, },
138 { .compatible = "nvidia,tegra30-host1x", .data = &host1x01_info, },
139 { .compatible = "nvidia,tegra20-host1x", .data = &host1x01_info, },
142 MODULE_DEVICE_TABLE(of, host1x_of_match);
144 static int host1x_probe(struct platform_device *pdev)
147 struct resource *regs, *hv_regs = NULL;
151 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
155 host->info = of_device_get_match_data(&pdev->dev);
157 if (host->info->has_hypervisor) {
158 regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vm");
160 dev_err(&pdev->dev, "failed to get vm registers\n");
164 hv_regs = platform_get_resource_byname(pdev, IORESOURCE_MEM,
168 "failed to get hypervisor registers\n");
172 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
174 dev_err(&pdev->dev, "failed to get registers\n");
179 syncpt_irq = platform_get_irq(pdev, 0);
180 if (syncpt_irq < 0) {
181 dev_err(&pdev->dev, "failed to get IRQ: %d\n", syncpt_irq);
185 mutex_init(&host->devices_lock);
186 INIT_LIST_HEAD(&host->devices);
187 INIT_LIST_HEAD(&host->list);
188 host->dev = &pdev->dev;
190 /* set common host1x device data */
191 platform_set_drvdata(pdev, host);
193 host->regs = devm_ioremap_resource(&pdev->dev, regs);
194 if (IS_ERR(host->regs))
195 return PTR_ERR(host->regs);
197 if (host->info->has_hypervisor) {
198 host->hv_regs = devm_ioremap_resource(&pdev->dev, hv_regs);
199 if (IS_ERR(host->hv_regs))
200 return PTR_ERR(host->hv_regs);
203 dma_set_mask_and_coherent(host->dev, host->info->dma_mask);
205 if (host->info->init) {
206 err = host->info->init(host);
211 host->clk = devm_clk_get(&pdev->dev, NULL);
212 if (IS_ERR(host->clk)) {
213 dev_err(&pdev->dev, "failed to get clock\n");
214 err = PTR_ERR(host->clk);
218 host->rst = devm_reset_control_get(&pdev->dev, "host1x");
219 if (IS_ERR(host->rst)) {
220 err = PTR_ERR(host->rst);
221 dev_err(&pdev->dev, "failed to get reset: %d\n", err);
224 #if IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)
225 if (host->dev->archdata.mapping) {
226 struct dma_iommu_mapping *mapping =
227 to_dma_iommu_mapping(host->dev);
228 arm_iommu_detach_device(host->dev);
229 arm_iommu_release_mapping(mapping);
232 if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
235 host->group = iommu_group_get(&pdev->dev);
237 struct iommu_domain_geometry *geometry;
240 err = iova_cache_get();
244 host->domain = iommu_domain_alloc(&platform_bus_type);
250 err = iommu_attach_group(host->domain, host->group);
252 if (err == -ENODEV) {
253 iommu_domain_free(host->domain);
256 iommu_group_put(host->group);
261 goto fail_free_domain;
264 geometry = &host->domain->geometry;
266 order = __ffs(host->domain->pgsize_bitmap);
267 init_iova_domain(&host->iova, 1UL << order,
268 geometry->aperture_start >> order);
269 host->iova_end = geometry->aperture_end;
273 err = host1x_channel_list_init(&host->channel_list,
274 host->info->nb_channels);
276 dev_err(&pdev->dev, "failed to initialize channel list\n");
277 goto fail_detach_device;
280 err = clk_prepare_enable(host->clk);
282 dev_err(&pdev->dev, "failed to enable clock\n");
283 goto fail_free_channels;
286 err = reset_control_deassert(host->rst);
288 dev_err(&pdev->dev, "failed to deassert reset: %d\n", err);
289 goto fail_unprepare_disable;
292 err = host1x_syncpt_init(host);
294 dev_err(&pdev->dev, "failed to initialize syncpts\n");
295 goto fail_reset_assert;
298 err = host1x_intr_init(host, syncpt_irq);
300 dev_err(&pdev->dev, "failed to initialize interrupts\n");
301 goto fail_deinit_syncpt;
304 host1x_debug_init(host);
306 err = host1x_register(host);
308 goto fail_deinit_intr;
313 host1x_intr_deinit(host);
315 host1x_syncpt_deinit(host);
317 reset_control_assert(host->rst);
318 fail_unprepare_disable:
319 clk_disable_unprepare(host->clk);
321 host1x_channel_list_free(&host->channel_list);
323 if (host->group && host->domain) {
324 put_iova_domain(&host->iova);
325 iommu_detach_group(host->domain, host->group);
329 iommu_domain_free(host->domain);
334 iommu_group_put(host->group);
339 static int host1x_remove(struct platform_device *pdev)
341 struct host1x *host = platform_get_drvdata(pdev);
343 host1x_unregister(host);
344 host1x_intr_deinit(host);
345 host1x_syncpt_deinit(host);
346 reset_control_assert(host->rst);
347 clk_disable_unprepare(host->clk);
350 put_iova_domain(&host->iova);
351 iommu_detach_group(host->domain, host->group);
352 iommu_domain_free(host->domain);
354 iommu_group_put(host->group);
360 static struct platform_driver tegra_host1x_driver = {
362 .name = "tegra-host1x",
363 .of_match_table = host1x_of_match,
365 .probe = host1x_probe,
366 .remove = host1x_remove,
369 static struct platform_driver * const drivers[] = {
370 &tegra_host1x_driver,
374 static int __init tegra_host1x_init(void)
378 err = bus_register(&host1x_bus_type);
382 err = platform_register_drivers(drivers, ARRAY_SIZE(drivers));
384 bus_unregister(&host1x_bus_type);
388 module_init(tegra_host1x_init);
390 static void __exit tegra_host1x_exit(void)
392 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
393 bus_unregister(&host1x_bus_type);
395 module_exit(tegra_host1x_exit);
399 MODULE_DESCRIPTION("Host1x driver for Tegra products");
400 MODULE_LICENSE("GPL");