1ec3cf2ecSBecky Bruce /*
2ec3cf2ecSBecky Bruce  * Contains routines needed to support swiotlb for ppc.
3ec3cf2ecSBecky Bruce  *
4ec3cf2ecSBecky Bruce  * Copyright (C) 2009 Becky Bruce, Freescale Semiconductor
5ec3cf2ecSBecky Bruce  *
6ec3cf2ecSBecky Bruce  * This program is free software; you can redistribute  it and/or modify it
7ec3cf2ecSBecky Bruce  * under  the terms of  the GNU General  Public License as published by the
8ec3cf2ecSBecky Bruce  * Free Software Foundation;  either version 2 of the  License, or (at your
9ec3cf2ecSBecky Bruce  * option) any later version.
10ec3cf2ecSBecky Bruce  *
11ec3cf2ecSBecky Bruce  */
12ec3cf2ecSBecky Bruce 
13ec3cf2ecSBecky Bruce #include <linux/dma-mapping.h>
14ec3cf2ecSBecky Bruce #include <linux/pfn.h>
15ec3cf2ecSBecky Bruce #include <linux/of_platform.h>
16ec3cf2ecSBecky Bruce #include <linux/platform_device.h>
17ec3cf2ecSBecky Bruce #include <linux/pci.h>
18ec3cf2ecSBecky Bruce 
19ec3cf2ecSBecky Bruce #include <asm/machdep.h>
20ec3cf2ecSBecky Bruce #include <asm/swiotlb.h>
21ec3cf2ecSBecky Bruce #include <asm/dma.h>
22ec3cf2ecSBecky Bruce #include <asm/abs_addr.h>
23ec3cf2ecSBecky Bruce 
24ec3cf2ecSBecky Bruce int swiotlb __read_mostly;
25ec3cf2ecSBecky Bruce unsigned int ppc_swiotlb_enable;
26ec3cf2ecSBecky Bruce 
27ec3cf2ecSBecky Bruce /*
28ec3cf2ecSBecky Bruce  * At the moment, all platforms that use this code only require
29ec3cf2ecSBecky Bruce  * swiotlb to be used if we're operating on HIGHMEM.  Since
30ec3cf2ecSBecky Bruce  * we don't ever call anything other than map_sg, unmap_sg,
31ec3cf2ecSBecky Bruce  * map_page, and unmap_page on highmem, use normal dma_ops
32ec3cf2ecSBecky Bruce  * for everything else.
33ec3cf2ecSBecky Bruce  */
3445223c54SFUJITA Tomonori struct dma_map_ops swiotlb_dma_ops = {
35ec3cf2ecSBecky Bruce 	.alloc_coherent = dma_direct_alloc_coherent,
36ec3cf2ecSBecky Bruce 	.free_coherent = dma_direct_free_coherent,
37ec3cf2ecSBecky Bruce 	.map_sg = swiotlb_map_sg_attrs,
38ec3cf2ecSBecky Bruce 	.unmap_sg = swiotlb_unmap_sg_attrs,
39ec3cf2ecSBecky Bruce 	.dma_supported = swiotlb_dma_supported,
40ec3cf2ecSBecky Bruce 	.map_page = swiotlb_map_page,
41ec3cf2ecSBecky Bruce 	.unmap_page = swiotlb_unmap_page,
42ec3cf2ecSBecky Bruce 	.sync_single_range_for_cpu = swiotlb_sync_single_range_for_cpu,
43ec3cf2ecSBecky Bruce 	.sync_single_range_for_device = swiotlb_sync_single_range_for_device,
44ec3cf2ecSBecky Bruce 	.sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
454a9a6bfeSFUJITA Tomonori 	.sync_sg_for_device = swiotlb_sync_sg_for_device,
464a9a6bfeSFUJITA Tomonori 	.mapping_error = swiotlb_dma_mapping_error,
47ec3cf2ecSBecky Bruce };
48ec3cf2ecSBecky Bruce 
49762afb73SFUJITA Tomonori void pci_dma_dev_setup_swiotlb(struct pci_dev *pdev)
50762afb73SFUJITA Tomonori {
51762afb73SFUJITA Tomonori 	struct pci_controller *hose;
52762afb73SFUJITA Tomonori 	struct dev_archdata *sd;
53762afb73SFUJITA Tomonori 
54762afb73SFUJITA Tomonori 	hose = pci_bus_to_host(pdev->bus);
55762afb73SFUJITA Tomonori 	sd = &pdev->dev.archdata;
56762afb73SFUJITA Tomonori 	sd->max_direct_dma_addr =
57762afb73SFUJITA Tomonori 		hose->dma_window_base_cur + hose->dma_window_size;
58762afb73SFUJITA Tomonori }
59762afb73SFUJITA Tomonori 
60ec3cf2ecSBecky Bruce static int ppc_swiotlb_bus_notify(struct notifier_block *nb,
61ec3cf2ecSBecky Bruce 				  unsigned long action, void *data)
62ec3cf2ecSBecky Bruce {
63ec3cf2ecSBecky Bruce 	struct device *dev = data;
64762afb73SFUJITA Tomonori 	struct dev_archdata *sd;
65ec3cf2ecSBecky Bruce 
66ec3cf2ecSBecky Bruce 	/* We are only intereted in device addition */
67ec3cf2ecSBecky Bruce 	if (action != BUS_NOTIFY_ADD_DEVICE)
68ec3cf2ecSBecky Bruce 		return 0;
69ec3cf2ecSBecky Bruce 
70762afb73SFUJITA Tomonori 	sd = &dev->archdata;
71762afb73SFUJITA Tomonori 	sd->max_direct_dma_addr = 0;
72762afb73SFUJITA Tomonori 
73ec3cf2ecSBecky Bruce 	/* May need to bounce if the device can't address all of DRAM */
74ec3cf2ecSBecky Bruce 	if (dma_get_mask(dev) < lmb_end_of_DRAM())
75ec3cf2ecSBecky Bruce 		set_dma_ops(dev, &swiotlb_dma_ops);
76ec3cf2ecSBecky Bruce 
77ec3cf2ecSBecky Bruce 	return NOTIFY_DONE;
78ec3cf2ecSBecky Bruce }
79ec3cf2ecSBecky Bruce 
80ec3cf2ecSBecky Bruce static struct notifier_block ppc_swiotlb_plat_bus_notifier = {
81ec3cf2ecSBecky Bruce 	.notifier_call = ppc_swiotlb_bus_notify,
82ec3cf2ecSBecky Bruce 	.priority = 0,
83ec3cf2ecSBecky Bruce };
84ec3cf2ecSBecky Bruce 
85ec3cf2ecSBecky Bruce static struct notifier_block ppc_swiotlb_of_bus_notifier = {
86ec3cf2ecSBecky Bruce 	.notifier_call = ppc_swiotlb_bus_notify,
87ec3cf2ecSBecky Bruce 	.priority = 0,
88ec3cf2ecSBecky Bruce };
89ec3cf2ecSBecky Bruce 
90ec3cf2ecSBecky Bruce int __init swiotlb_setup_bus_notifier(void)
91ec3cf2ecSBecky Bruce {
92ec3cf2ecSBecky Bruce 	bus_register_notifier(&platform_bus_type,
93ec3cf2ecSBecky Bruce 			      &ppc_swiotlb_plat_bus_notifier);
94ec3cf2ecSBecky Bruce 	bus_register_notifier(&of_platform_bus_type,
95ec3cf2ecSBecky Bruce 			      &ppc_swiotlb_of_bus_notifier);
96ec3cf2ecSBecky Bruce 
97ec3cf2ecSBecky Bruce 	return 0;
98ec3cf2ecSBecky Bruce }
99