1ec3cf2ecSBecky Bruce /*
2ec3cf2ecSBecky Bruce  * Contains routines needed to support swiotlb for ppc.
3ec3cf2ecSBecky Bruce  *
4b8b14c66SKumar Gala  * Copyright (C) 2009-2010 Freescale Semiconductor, Inc.
5b8b14c66SKumar Gala  * Author: Becky Bruce
6ec3cf2ecSBecky Bruce  *
7ec3cf2ecSBecky Bruce  * This program is free software; you can redistribute  it and/or modify it
8ec3cf2ecSBecky Bruce  * under  the terms of  the GNU General  Public License as published by the
9ec3cf2ecSBecky Bruce  * Free Software Foundation;  either version 2 of the  License, or (at your
10ec3cf2ecSBecky Bruce  * option) any later version.
11ec3cf2ecSBecky Bruce  *
12ec3cf2ecSBecky Bruce  */
13ec3cf2ecSBecky Bruce 
14ec3cf2ecSBecky Bruce #include <linux/dma-mapping.h>
15beacc6daSMichael Ellerman #include <linux/memblock.h>
16ec3cf2ecSBecky Bruce #include <linux/pfn.h>
17ec3cf2ecSBecky Bruce #include <linux/of_platform.h>
18ec3cf2ecSBecky Bruce #include <linux/platform_device.h>
19ec3cf2ecSBecky Bruce #include <linux/pci.h>
20ec3cf2ecSBecky Bruce 
21ec3cf2ecSBecky Bruce #include <asm/machdep.h>
22ec3cf2ecSBecky Bruce #include <asm/swiotlb.h>
23ec3cf2ecSBecky Bruce #include <asm/dma.h>
24ec3cf2ecSBecky Bruce 
25ec3cf2ecSBecky Bruce unsigned int ppc_swiotlb_enable;
26ec3cf2ecSBecky Bruce 
27d24f9c69SMilton Miller static u64 swiotlb_powerpc_get_required(struct device *dev)
28d24f9c69SMilton Miller {
29d24f9c69SMilton Miller 	u64 end, mask, max_direct_dma_addr = dev->archdata.max_direct_dma_addr;
30d24f9c69SMilton Miller 
31d24f9c69SMilton Miller 	end = memblock_end_of_DRAM();
32d24f9c69SMilton Miller 	if (max_direct_dma_addr && end > max_direct_dma_addr)
33d24f9c69SMilton Miller 		end = max_direct_dma_addr;
34d24f9c69SMilton Miller 	end += get_dma_offset(dev);
35d24f9c69SMilton Miller 
36d24f9c69SMilton Miller 	mask = 1ULL << (fls64(end) - 1);
37d24f9c69SMilton Miller 	mask += mask - 1;
38d24f9c69SMilton Miller 
39d24f9c69SMilton Miller 	return mask;
40d24f9c69SMilton Miller }
41d24f9c69SMilton Miller 
42ec3cf2ecSBecky Bruce /*
43ec3cf2ecSBecky Bruce  * At the moment, all platforms that use this code only require
44ec3cf2ecSBecky Bruce  * swiotlb to be used if we're operating on HIGHMEM.  Since
45ec3cf2ecSBecky Bruce  * we don't ever call anything other than map_sg, unmap_sg,
46ec3cf2ecSBecky Bruce  * map_page, and unmap_page on highmem, use normal dma_ops
47ec3cf2ecSBecky Bruce  * for everything else.
48ec3cf2ecSBecky Bruce  */
495299709dSBart Van Assche const struct dma_map_ops swiotlb_dma_ops = {
50817820b0SBenjamin Herrenschmidt 	.alloc = __dma_direct_alloc_coherent,
51817820b0SBenjamin Herrenschmidt 	.free = __dma_direct_free_coherent,
5264ccc9c0SMarek Szyprowski 	.mmap = dma_direct_mmap_coherent,
53ec3cf2ecSBecky Bruce 	.map_sg = swiotlb_map_sg_attrs,
54ec3cf2ecSBecky Bruce 	.unmap_sg = swiotlb_unmap_sg_attrs,
55ec3cf2ecSBecky Bruce 	.dma_supported = swiotlb_dma_supported,
56ec3cf2ecSBecky Bruce 	.map_page = swiotlb_map_page,
57ec3cf2ecSBecky Bruce 	.unmap_page = swiotlb_unmap_page,
58712d3e22SFUJITA Tomonori 	.sync_single_for_cpu = swiotlb_sync_single_for_cpu,
59712d3e22SFUJITA Tomonori 	.sync_single_for_device = swiotlb_sync_single_for_device,
60ec3cf2ecSBecky Bruce 	.sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
614a9a6bfeSFUJITA Tomonori 	.sync_sg_for_device = swiotlb_sync_sg_for_device,
624a9a6bfeSFUJITA Tomonori 	.mapping_error = swiotlb_dma_mapping_error,
63d24f9c69SMilton Miller 	.get_required_mask = swiotlb_powerpc_get_required,
64ec3cf2ecSBecky Bruce };
65ec3cf2ecSBecky Bruce 
66762afb73SFUJITA Tomonori void pci_dma_dev_setup_swiotlb(struct pci_dev *pdev)
67762afb73SFUJITA Tomonori {
68762afb73SFUJITA Tomonori 	struct pci_controller *hose;
69762afb73SFUJITA Tomonori 	struct dev_archdata *sd;
70762afb73SFUJITA Tomonori 
71762afb73SFUJITA Tomonori 	hose = pci_bus_to_host(pdev->bus);
72762afb73SFUJITA Tomonori 	sd = &pdev->dev.archdata;
73762afb73SFUJITA Tomonori 	sd->max_direct_dma_addr =
74762afb73SFUJITA Tomonori 		hose->dma_window_base_cur + hose->dma_window_size;
75762afb73SFUJITA Tomonori }
76762afb73SFUJITA Tomonori 
77ec3cf2ecSBecky Bruce static int ppc_swiotlb_bus_notify(struct notifier_block *nb,
78ec3cf2ecSBecky Bruce 				  unsigned long action, void *data)
79ec3cf2ecSBecky Bruce {
80ec3cf2ecSBecky Bruce 	struct device *dev = data;
81762afb73SFUJITA Tomonori 	struct dev_archdata *sd;
82ec3cf2ecSBecky Bruce 
83ec3cf2ecSBecky Bruce 	/* We are only intereted in device addition */
84ec3cf2ecSBecky Bruce 	if (action != BUS_NOTIFY_ADD_DEVICE)
85ec3cf2ecSBecky Bruce 		return 0;
86ec3cf2ecSBecky Bruce 
87762afb73SFUJITA Tomonori 	sd = &dev->archdata;
88762afb73SFUJITA Tomonori 	sd->max_direct_dma_addr = 0;
89762afb73SFUJITA Tomonori 
90ec3cf2ecSBecky Bruce 	/* May need to bounce if the device can't address all of DRAM */
9195f72d1eSYinghai Lu 	if ((dma_get_mask(dev) + 1) < memblock_end_of_DRAM())
92ec3cf2ecSBecky Bruce 		set_dma_ops(dev, &swiotlb_dma_ops);
93ec3cf2ecSBecky Bruce 
94ec3cf2ecSBecky Bruce 	return NOTIFY_DONE;
95ec3cf2ecSBecky Bruce }
96ec3cf2ecSBecky Bruce 
97ec3cf2ecSBecky Bruce static struct notifier_block ppc_swiotlb_plat_bus_notifier = {
98ec3cf2ecSBecky Bruce 	.notifier_call = ppc_swiotlb_bus_notify,
99ec3cf2ecSBecky Bruce 	.priority = 0,
100ec3cf2ecSBecky Bruce };
101ec3cf2ecSBecky Bruce 
102ec3cf2ecSBecky Bruce int __init swiotlb_setup_bus_notifier(void)
103ec3cf2ecSBecky Bruce {
104ec3cf2ecSBecky Bruce 	bus_register_notifier(&platform_bus_type,
105ec3cf2ecSBecky Bruce 			      &ppc_swiotlb_plat_bus_notifier);
106ec3cf2ecSBecky Bruce 	return 0;
107ec3cf2ecSBecky Bruce }
108688ba1dbSJia Hongtao 
109cf562103SScott Wood void __init swiotlb_detect_4g(void)
110688ba1dbSJia Hongtao {
111cf562103SScott Wood 	if ((memblock_end_of_DRAM() - 1) > 0xffffffff) {
112688ba1dbSJia Hongtao 		ppc_swiotlb_enable = 1;
113cf562103SScott Wood #ifdef CONFIG_ZONE_DMA32
114cf562103SScott Wood 		limit_zone_pfn(ZONE_DMA32, (1ULL << 32) >> PAGE_SHIFT);
115cf562103SScott Wood #endif
116cf562103SScott Wood 	}
117688ba1dbSJia Hongtao }
118688ba1dbSJia Hongtao 
11997884e00SDaniel Axtens static int __init check_swiotlb_enabled(void)
120688ba1dbSJia Hongtao {
12197884e00SDaniel Axtens 	if (ppc_swiotlb_enable)
122688ba1dbSJia Hongtao 		swiotlb_print_info();
12397884e00SDaniel Axtens 	else
124688ba1dbSJia Hongtao 		swiotlb_free();
125688ba1dbSJia Hongtao 
126688ba1dbSJia Hongtao 	return 0;
127688ba1dbSJia Hongtao }
12897884e00SDaniel Axtens subsys_initcall(check_swiotlb_enabled);
129