Becky Bruce | ec3cf2e | 2009-05-14 12:42:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Contains routines needed to support swiotlb for ppc. |
| 3 | * |
Kumar Gala | b8b14c6 | 2010-05-03 07:36:22 -0500 | [diff] [blame] | 4 | * Copyright (C) 2009-2010 Freescale Semiconductor, Inc. |
| 5 | * Author: Becky Bruce |
Becky Bruce | ec3cf2e | 2009-05-14 12:42:28 +0000 | [diff] [blame] | 6 | * |
| 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> |
Michael Ellerman | beacc6d | 2012-07-25 21:20:03 +0000 | [diff] [blame] | 15 | #include <linux/memblock.h> |
Becky Bruce | ec3cf2e | 2009-05-14 12:42:28 +0000 | [diff] [blame] | 16 | #include <linux/pfn.h> |
| 17 | #include <linux/of_platform.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/pci.h> |
| 20 | |
| 21 | #include <asm/machdep.h> |
| 22 | #include <asm/swiotlb.h> |
| 23 | #include <asm/dma.h> |
Becky Bruce | ec3cf2e | 2009-05-14 12:42:28 +0000 | [diff] [blame] | 24 | |
Becky Bruce | ec3cf2e | 2009-05-14 12:42:28 +0000 | [diff] [blame] | 25 | unsigned int ppc_swiotlb_enable; |
| 26 | |
Milton Miller | d24f9c6 | 2011-06-24 09:05:24 +0000 | [diff] [blame] | 27 | static 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 Bruce | ec3cf2e | 2009-05-14 12:42:28 +0000 | [diff] [blame] | 42 | /* |
Becky Bruce | ec3cf2e | 2009-05-14 12:42:28 +0000 | [diff] [blame] | 43 | * 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 Tomonori | 45223c5 | 2009-08-04 19:08:25 +0000 | [diff] [blame] | 49 | struct dma_map_ops swiotlb_dma_ops = { |
Andrzej Pietrasiewicz | bfbf7d6 | 2011-12-06 14:14:46 +0100 | [diff] [blame] | 50 | .alloc = dma_direct_alloc_coherent, |
| 51 | .free = dma_direct_free_coherent, |
Marek Szyprowski | 64ccc9c | 2012-06-14 13:03:04 +0200 | [diff] [blame] | 52 | .mmap = dma_direct_mmap_coherent, |
Becky Bruce | ec3cf2e | 2009-05-14 12:42:28 +0000 | [diff] [blame] | 53 | .map_sg = swiotlb_map_sg_attrs, |
| 54 | .unmap_sg = swiotlb_unmap_sg_attrs, |
| 55 | .dma_supported = swiotlb_dma_supported, |
| 56 | .map_page = swiotlb_map_page, |
| 57 | .unmap_page = swiotlb_unmap_page, |
FUJITA Tomonori | 712d3e2 | 2010-05-26 14:44:17 -0700 | [diff] [blame] | 58 | .sync_single_for_cpu = swiotlb_sync_single_for_cpu, |
| 59 | .sync_single_for_device = swiotlb_sync_single_for_device, |
Becky Bruce | ec3cf2e | 2009-05-14 12:42:28 +0000 | [diff] [blame] | 60 | .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu, |
FUJITA Tomonori | 4a9a6bf | 2009-08-04 19:08:27 +0000 | [diff] [blame] | 61 | .sync_sg_for_device = swiotlb_sync_sg_for_device, |
| 62 | .mapping_error = swiotlb_dma_mapping_error, |
Milton Miller | d24f9c6 | 2011-06-24 09:05:24 +0000 | [diff] [blame] | 63 | .get_required_mask = swiotlb_powerpc_get_required, |
Becky Bruce | ec3cf2e | 2009-05-14 12:42:28 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
FUJITA Tomonori | 762afb7 | 2009-08-04 19:08:22 +0000 | [diff] [blame] | 66 | void pci_dma_dev_setup_swiotlb(struct pci_dev *pdev) |
| 67 | { |
| 68 | struct pci_controller *hose; |
| 69 | struct dev_archdata *sd; |
| 70 | |
| 71 | hose = pci_bus_to_host(pdev->bus); |
| 72 | sd = &pdev->dev.archdata; |
| 73 | sd->max_direct_dma_addr = |
| 74 | hose->dma_window_base_cur + hose->dma_window_size; |
| 75 | } |
| 76 | |
Becky Bruce | ec3cf2e | 2009-05-14 12:42:28 +0000 | [diff] [blame] | 77 | static int ppc_swiotlb_bus_notify(struct notifier_block *nb, |
| 78 | unsigned long action, void *data) |
| 79 | { |
| 80 | struct device *dev = data; |
FUJITA Tomonori | 762afb7 | 2009-08-04 19:08:22 +0000 | [diff] [blame] | 81 | struct dev_archdata *sd; |
Becky Bruce | ec3cf2e | 2009-05-14 12:42:28 +0000 | [diff] [blame] | 82 | |
| 83 | /* We are only intereted in device addition */ |
| 84 | if (action != BUS_NOTIFY_ADD_DEVICE) |
| 85 | return 0; |
| 86 | |
FUJITA Tomonori | 762afb7 | 2009-08-04 19:08:22 +0000 | [diff] [blame] | 87 | sd = &dev->archdata; |
| 88 | sd->max_direct_dma_addr = 0; |
| 89 | |
Becky Bruce | ec3cf2e | 2009-05-14 12:42:28 +0000 | [diff] [blame] | 90 | /* May need to bounce if the device can't address all of DRAM */ |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 91 | if ((dma_get_mask(dev) + 1) < memblock_end_of_DRAM()) |
Becky Bruce | ec3cf2e | 2009-05-14 12:42:28 +0000 | [diff] [blame] | 92 | set_dma_ops(dev, &swiotlb_dma_ops); |
| 93 | |
| 94 | return NOTIFY_DONE; |
| 95 | } |
| 96 | |
| 97 | static struct notifier_block ppc_swiotlb_plat_bus_notifier = { |
| 98 | .notifier_call = ppc_swiotlb_bus_notify, |
| 99 | .priority = 0, |
| 100 | }; |
| 101 | |
Becky Bruce | ec3cf2e | 2009-05-14 12:42:28 +0000 | [diff] [blame] | 102 | int __init swiotlb_setup_bus_notifier(void) |
| 103 | { |
| 104 | bus_register_notifier(&platform_bus_type, |
| 105 | &ppc_swiotlb_plat_bus_notifier); |
Becky Bruce | ec3cf2e | 2009-05-14 12:42:28 +0000 | [diff] [blame] | 106 | return 0; |
| 107 | } |
Jia Hongtao | 688ba1d | 2012-08-03 18:14:10 +0800 | [diff] [blame] | 108 | |
| 109 | void swiotlb_detect_4g(void) |
| 110 | { |
| 111 | if ((memblock_end_of_DRAM() - 1) > 0xffffffff) |
| 112 | ppc_swiotlb_enable = 1; |
| 113 | } |
| 114 | |
| 115 | static int __init swiotlb_late_init(void) |
| 116 | { |
| 117 | if (ppc_swiotlb_enable) { |
| 118 | swiotlb_print_info(); |
| 119 | set_pci_dma_ops(&swiotlb_dma_ops); |
| 120 | ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_swiotlb; |
| 121 | } else { |
| 122 | swiotlb_free(); |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | subsys_initcall(swiotlb_late_init); |