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_sid_entry tegra186_sid_table[] = {
132 static const struct host1x_info host1x06_info = {
137 .init = host1x06_init,
139 .dma_mask = DMA_BIT_MASK(40),
140 .has_hypervisor = true,
141 .num_sid_entries = ARRAY_SIZE(tegra186_sid_table),
142 .sid_table = tegra186_sid_table,
145 static const struct host1x_sid_entry tegra194_sid_table[] = {
154 static const struct host1x_info host1x07_info = {
159 .init = host1x07_init,
161 .dma_mask = DMA_BIT_MASK(40),
162 .has_hypervisor = true,
163 .num_sid_entries = ARRAY_SIZE(tegra194_sid_table),
164 .sid_table = tegra194_sid_table,
167 static const struct of_device_id host1x_of_match[] = {
168 { .compatible = "nvidia,tegra194-host1x", .data = &host1x07_info, },
169 { .compatible = "nvidia,tegra186-host1x", .data = &host1x06_info, },
170 { .compatible = "nvidia,tegra210-host1x", .data = &host1x05_info, },
171 { .compatible = "nvidia,tegra124-host1x", .data = &host1x04_info, },
172 { .compatible = "nvidia,tegra114-host1x", .data = &host1x02_info, },
173 { .compatible = "nvidia,tegra30-host1x", .data = &host1x01_info, },
174 { .compatible = "nvidia,tegra20-host1x", .data = &host1x01_info, },
177 MODULE_DEVICE_TABLE(of, host1x_of_match);
179 static void host1x_setup_sid_table(struct host1x *host)
181 const struct host1x_info *info = host->info;
184 for (i = 0; i < info->num_sid_entries; i++) {
185 const struct host1x_sid_entry *entry = &info->sid_table[i];
187 host1x_hypervisor_writel(host, entry->offset, entry->base);
188 host1x_hypervisor_writel(host, entry->limit, entry->base + 4);
192 static int host1x_probe(struct platform_device *pdev)
195 struct resource *regs, *hv_regs = NULL;
199 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
203 host->info = of_device_get_match_data(&pdev->dev);
205 if (host->info->has_hypervisor) {
206 regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vm");
208 dev_err(&pdev->dev, "failed to get vm registers\n");
212 hv_regs = platform_get_resource_byname(pdev, IORESOURCE_MEM,
216 "failed to get hypervisor registers\n");
220 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
222 dev_err(&pdev->dev, "failed to get registers\n");
227 syncpt_irq = platform_get_irq(pdev, 0);
228 if (syncpt_irq < 0) {
229 dev_err(&pdev->dev, "failed to get IRQ: %d\n", syncpt_irq);
233 mutex_init(&host->devices_lock);
234 INIT_LIST_HEAD(&host->devices);
235 INIT_LIST_HEAD(&host->list);
236 host->dev = &pdev->dev;
238 /* set common host1x device data */
239 platform_set_drvdata(pdev, host);
241 host->regs = devm_ioremap_resource(&pdev->dev, regs);
242 if (IS_ERR(host->regs))
243 return PTR_ERR(host->regs);
245 if (host->info->has_hypervisor) {
246 host->hv_regs = devm_ioremap_resource(&pdev->dev, hv_regs);
247 if (IS_ERR(host->hv_regs))
248 return PTR_ERR(host->hv_regs);
251 dma_set_mask_and_coherent(host->dev, host->info->dma_mask);
253 if (host->info->init) {
254 err = host->info->init(host);
259 host->clk = devm_clk_get(&pdev->dev, NULL);
260 if (IS_ERR(host->clk)) {
261 dev_err(&pdev->dev, "failed to get clock\n");
262 err = PTR_ERR(host->clk);
266 host->rst = devm_reset_control_get(&pdev->dev, "host1x");
267 if (IS_ERR(host->rst)) {
268 err = PTR_ERR(host->rst);
269 dev_err(&pdev->dev, "failed to get reset: %d\n", err);
272 #if IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)
273 if (host->dev->archdata.mapping) {
274 struct dma_iommu_mapping *mapping =
275 to_dma_iommu_mapping(host->dev);
276 arm_iommu_detach_device(host->dev);
277 arm_iommu_release_mapping(mapping);
280 if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
283 host->group = iommu_group_get(&pdev->dev);
285 struct iommu_domain_geometry *geometry;
286 u64 mask = dma_get_mask(host->dev);
287 dma_addr_t start, end;
290 err = iova_cache_get();
294 host->domain = iommu_domain_alloc(&platform_bus_type);
300 err = iommu_attach_group(host->domain, host->group);
302 if (err == -ENODEV) {
303 iommu_domain_free(host->domain);
306 iommu_group_put(host->group);
311 goto fail_free_domain;
314 geometry = &host->domain->geometry;
315 start = geometry->aperture_start & mask;
316 end = geometry->aperture_end & mask;
318 order = __ffs(host->domain->pgsize_bitmap);
319 init_iova_domain(&host->iova, 1UL << order, start >> order);
320 host->iova_end = end;
324 err = host1x_channel_list_init(&host->channel_list,
325 host->info->nb_channels);
327 dev_err(&pdev->dev, "failed to initialize channel list\n");
328 goto fail_detach_device;
331 err = clk_prepare_enable(host->clk);
333 dev_err(&pdev->dev, "failed to enable clock\n");
334 goto fail_free_channels;
337 err = reset_control_deassert(host->rst);
339 dev_err(&pdev->dev, "failed to deassert reset: %d\n", err);
340 goto fail_unprepare_disable;
343 err = host1x_syncpt_init(host);
345 dev_err(&pdev->dev, "failed to initialize syncpts\n");
346 goto fail_reset_assert;
349 err = host1x_intr_init(host, syncpt_irq);
351 dev_err(&pdev->dev, "failed to initialize interrupts\n");
352 goto fail_deinit_syncpt;
355 host1x_debug_init(host);
357 if (host->info->has_hypervisor)
358 host1x_setup_sid_table(host);
360 err = host1x_register(host);
362 goto fail_deinit_intr;
367 host1x_intr_deinit(host);
369 host1x_syncpt_deinit(host);
371 reset_control_assert(host->rst);
372 fail_unprepare_disable:
373 clk_disable_unprepare(host->clk);
375 host1x_channel_list_free(&host->channel_list);
377 if (host->group && host->domain) {
378 put_iova_domain(&host->iova);
379 iommu_detach_group(host->domain, host->group);
383 iommu_domain_free(host->domain);
388 iommu_group_put(host->group);
393 static int host1x_remove(struct platform_device *pdev)
395 struct host1x *host = platform_get_drvdata(pdev);
397 host1x_unregister(host);
398 host1x_intr_deinit(host);
399 host1x_syncpt_deinit(host);
400 reset_control_assert(host->rst);
401 clk_disable_unprepare(host->clk);
404 put_iova_domain(&host->iova);
405 iommu_detach_group(host->domain, host->group);
406 iommu_domain_free(host->domain);
408 iommu_group_put(host->group);
414 static struct platform_driver tegra_host1x_driver = {
416 .name = "tegra-host1x",
417 .of_match_table = host1x_of_match,
419 .probe = host1x_probe,
420 .remove = host1x_remove,
423 static struct platform_driver * const drivers[] = {
424 &tegra_host1x_driver,
428 static int __init tegra_host1x_init(void)
432 err = bus_register(&host1x_bus_type);
436 err = platform_register_drivers(drivers, ARRAY_SIZE(drivers));
438 bus_unregister(&host1x_bus_type);
442 module_init(tegra_host1x_init);
444 static void __exit tegra_host1x_exit(void)
446 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
447 bus_unregister(&host1x_bus_type);
449 module_exit(tegra_host1x_exit);
453 MODULE_DESCRIPTION("Host1x driver for Tegra products");
454 MODULE_LICENSE("GPL");