blob: 1ebc9189aada9ff21d9268b71e486589010a20a8 [file] [log] [blame]
Becky Bruceec3cf2e2009-05-14 12:42:28 +00001/*
2 * Contains routines needed to support swiotlb for ppc.
3 *
Kumar Galab8b14c62010-05-03 07:36:22 -05004 * Copyright (C) 2009-2010 Freescale Semiconductor, Inc.
5 * Author: Becky Bruce
Becky Bruceec3cf2e2009-05-14 12:42:28 +00006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 */
13
14#include <linux/dma-mapping.h>
15#include <linux/pfn.h>
16#include <linux/of_platform.h>
17#include <linux/platform_device.h>
18#include <linux/pci.h>
19
20#include <asm/machdep.h>
21#include <asm/swiotlb.h>
22#include <asm/dma.h>
23#include <asm/abs_addr.h>
24
Becky Bruceec3cf2e2009-05-14 12:42:28 +000025unsigned int ppc_swiotlb_enable;
26
Milton Millerd24f9c62011-06-24 09:05:24 +000027static u64 swiotlb_powerpc_get_required(struct device *dev)
28{
29 u64 end, mask, max_direct_dma_addr = dev->archdata.max_direct_dma_addr;
30
31 end = memblock_end_of_DRAM();
32 if (max_direct_dma_addr && end > max_direct_dma_addr)
33 end = max_direct_dma_addr;
34 end += get_dma_offset(dev);
35
36 mask = 1ULL << (fls64(end) - 1);
37 mask += mask - 1;
38
39 return mask;
40}
41
Becky Bruceec3cf2e2009-05-14 12:42:28 +000042/*
Becky Bruceec3cf2e2009-05-14 12:42:28 +000043 * At the moment, all platforms that use this code only require
44 * swiotlb to be used if we're operating on HIGHMEM. Since
45 * we don't ever call anything other than map_sg, unmap_sg,
46 * map_page, and unmap_page on highmem, use normal dma_ops
47 * for everything else.
48 */
FUJITA Tomonori45223c52009-08-04 19:08:25 +000049struct dma_map_ops swiotlb_dma_ops = {
Becky Bruceec3cf2e2009-05-14 12:42:28 +000050 .alloc_coherent = dma_direct_alloc_coherent,
51 .free_coherent = dma_direct_free_coherent,
52 .map_sg = swiotlb_map_sg_attrs,
53 .unmap_sg = swiotlb_unmap_sg_attrs,
54 .dma_supported = swiotlb_dma_supported,
55 .map_page = swiotlb_map_page,
56 .unmap_page = swiotlb_unmap_page,
FUJITA Tomonori712d3e22010-05-26 14:44:17 -070057 .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
58 .sync_single_for_device = swiotlb_sync_single_for_device,
Becky Bruceec3cf2e2009-05-14 12:42:28 +000059 .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
FUJITA Tomonori4a9a6bf2009-08-04 19:08:27 +000060 .sync_sg_for_device = swiotlb_sync_sg_for_device,
61 .mapping_error = swiotlb_dma_mapping_error,
Milton Millerd24f9c62011-06-24 09:05:24 +000062 .get_required_mask = swiotlb_powerpc_get_required,
Becky Bruceec3cf2e2009-05-14 12:42:28 +000063};
64
FUJITA Tomonori762afb72009-08-04 19:08:22 +000065void pci_dma_dev_setup_swiotlb(struct pci_dev *pdev)
66{
67 struct pci_controller *hose;
68 struct dev_archdata *sd;
69
70 hose = pci_bus_to_host(pdev->bus);
71 sd = &pdev->dev.archdata;
72 sd->max_direct_dma_addr =
73 hose->dma_window_base_cur + hose->dma_window_size;
74}
75
Becky Bruceec3cf2e2009-05-14 12:42:28 +000076static int ppc_swiotlb_bus_notify(struct notifier_block *nb,
77 unsigned long action, void *data)
78{
79 struct device *dev = data;
FUJITA Tomonori762afb72009-08-04 19:08:22 +000080 struct dev_archdata *sd;
Becky Bruceec3cf2e2009-05-14 12:42:28 +000081
82 /* We are only intereted in device addition */
83 if (action != BUS_NOTIFY_ADD_DEVICE)
84 return 0;
85
FUJITA Tomonori762afb72009-08-04 19:08:22 +000086 sd = &dev->archdata;
87 sd->max_direct_dma_addr = 0;
88
Becky Bruceec3cf2e2009-05-14 12:42:28 +000089 /* May need to bounce if the device can't address all of DRAM */
Yinghai Lu95f72d12010-07-12 14:36:09 +100090 if ((dma_get_mask(dev) + 1) < memblock_end_of_DRAM())
Becky Bruceec3cf2e2009-05-14 12:42:28 +000091 set_dma_ops(dev, &swiotlb_dma_ops);
92
93 return NOTIFY_DONE;
94}
95
96static struct notifier_block ppc_swiotlb_plat_bus_notifier = {
97 .notifier_call = ppc_swiotlb_bus_notify,
98 .priority = 0,
99};
100
Becky Bruceec3cf2e2009-05-14 12:42:28 +0000101int __init swiotlb_setup_bus_notifier(void)
102{
103 bus_register_notifier(&platform_bus_type,
104 &ppc_swiotlb_plat_bus_notifier);
Becky Bruceec3cf2e2009-05-14 12:42:28 +0000105 return 0;
106}