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  */
34ec3cf2ecSBecky Bruce struct dma_mapping_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,
45ec3cf2ecSBecky Bruce 	.sync_sg_for_device = swiotlb_sync_sg_for_device
46ec3cf2ecSBecky Bruce };
47ec3cf2ecSBecky Bruce 
48ec3cf2ecSBecky Bruce struct dma_mapping_ops swiotlb_pci_dma_ops = {
49ec3cf2ecSBecky Bruce 	.alloc_coherent = dma_direct_alloc_coherent,
50ec3cf2ecSBecky Bruce 	.free_coherent = dma_direct_free_coherent,
51ec3cf2ecSBecky Bruce 	.map_sg = swiotlb_map_sg_attrs,
52ec3cf2ecSBecky Bruce 	.unmap_sg = swiotlb_unmap_sg_attrs,
53ec3cf2ecSBecky Bruce 	.dma_supported = swiotlb_dma_supported,
54ec3cf2ecSBecky Bruce 	.map_page = swiotlb_map_page,
55ec3cf2ecSBecky Bruce 	.unmap_page = swiotlb_unmap_page,
56ec3cf2ecSBecky Bruce 	.sync_single_range_for_cpu = swiotlb_sync_single_range_for_cpu,
57ec3cf2ecSBecky Bruce 	.sync_single_range_for_device = swiotlb_sync_single_range_for_device,
58ec3cf2ecSBecky Bruce 	.sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
59ec3cf2ecSBecky Bruce 	.sync_sg_for_device = swiotlb_sync_sg_for_device
60ec3cf2ecSBecky Bruce };
61ec3cf2ecSBecky Bruce 
62762afb73SFUJITA Tomonori void pci_dma_dev_setup_swiotlb(struct pci_dev *pdev)
63762afb73SFUJITA Tomonori {
64762afb73SFUJITA Tomonori 	struct pci_controller *hose;
65762afb73SFUJITA Tomonori 	struct dev_archdata *sd;
66762afb73SFUJITA Tomonori 
67762afb73SFUJITA Tomonori 	hose = pci_bus_to_host(pdev->bus);
68762afb73SFUJITA Tomonori 	sd = &pdev->dev.archdata;
69762afb73SFUJITA Tomonori 	sd->max_direct_dma_addr =
70762afb73SFUJITA Tomonori 		hose->dma_window_base_cur + hose->dma_window_size;
71762afb73SFUJITA Tomonori }
72762afb73SFUJITA Tomonori 
73ec3cf2ecSBecky Bruce static int ppc_swiotlb_bus_notify(struct notifier_block *nb,
74ec3cf2ecSBecky Bruce 				  unsigned long action, void *data)
75ec3cf2ecSBecky Bruce {
76ec3cf2ecSBecky Bruce 	struct device *dev = data;
77762afb73SFUJITA Tomonori 	struct dev_archdata *sd;
78ec3cf2ecSBecky Bruce 
79ec3cf2ecSBecky Bruce 	/* We are only intereted in device addition */
80ec3cf2ecSBecky Bruce 	if (action != BUS_NOTIFY_ADD_DEVICE)
81ec3cf2ecSBecky Bruce 		return 0;
82ec3cf2ecSBecky Bruce 
83762afb73SFUJITA Tomonori 	sd = &dev->archdata;
84762afb73SFUJITA Tomonori 	sd->max_direct_dma_addr = 0;
85762afb73SFUJITA Tomonori 
86ec3cf2ecSBecky Bruce 	/* May need to bounce if the device can't address all of DRAM */
87ec3cf2ecSBecky Bruce 	if (dma_get_mask(dev) < lmb_end_of_DRAM())
88ec3cf2ecSBecky Bruce 		set_dma_ops(dev, &swiotlb_dma_ops);
89ec3cf2ecSBecky Bruce 
90ec3cf2ecSBecky Bruce 	return NOTIFY_DONE;
91ec3cf2ecSBecky Bruce }
92ec3cf2ecSBecky Bruce 
93ec3cf2ecSBecky Bruce static struct notifier_block ppc_swiotlb_plat_bus_notifier = {
94ec3cf2ecSBecky Bruce 	.notifier_call = ppc_swiotlb_bus_notify,
95ec3cf2ecSBecky Bruce 	.priority = 0,
96ec3cf2ecSBecky Bruce };
97ec3cf2ecSBecky Bruce 
98ec3cf2ecSBecky Bruce static struct notifier_block ppc_swiotlb_of_bus_notifier = {
99ec3cf2ecSBecky Bruce 	.notifier_call = ppc_swiotlb_bus_notify,
100ec3cf2ecSBecky Bruce 	.priority = 0,
101ec3cf2ecSBecky Bruce };
102ec3cf2ecSBecky Bruce 
103ec3cf2ecSBecky Bruce int __init swiotlb_setup_bus_notifier(void)
104ec3cf2ecSBecky Bruce {
105ec3cf2ecSBecky Bruce 	bus_register_notifier(&platform_bus_type,
106ec3cf2ecSBecky Bruce 			      &ppc_swiotlb_plat_bus_notifier);
107ec3cf2ecSBecky Bruce 	bus_register_notifier(&of_platform_bus_type,
108ec3cf2ecSBecky Bruce 			      &ppc_swiotlb_of_bus_notifier);
109ec3cf2ecSBecky Bruce 
110ec3cf2ecSBecky Bruce 	return 0;
111ec3cf2ecSBecky Bruce }
112