1 /* 2 * Contains routines needed to support swiotlb for ppc. 3 * 4 * Copyright (C) 2009-2010 Freescale Semiconductor, Inc. 5 * Author: Becky Bruce 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-direct.h> 15 #include <linux/memblock.h> 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> 24 25 unsigned int ppc_swiotlb_enable; 26 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 42 /* 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 */ 49 const struct dma_map_ops powerpc_swiotlb_dma_ops = { 50 .alloc = __dma_nommu_alloc_coherent, 51 .free = __dma_nommu_free_coherent, 52 .mmap = dma_nommu_mmap_coherent, 53 .map_sg = dma_direct_map_sg, 54 .unmap_sg = dma_direct_unmap_sg, 55 .dma_supported = swiotlb_dma_supported, 56 .map_page = dma_direct_map_page, 57 .unmap_page = dma_direct_unmap_page, 58 .sync_single_for_cpu = dma_direct_sync_single_for_cpu, 59 .sync_single_for_device = dma_direct_sync_single_for_device, 60 .sync_sg_for_cpu = dma_direct_sync_sg_for_cpu, 61 .sync_sg_for_device = dma_direct_sync_sg_for_device, 62 .get_required_mask = swiotlb_powerpc_get_required, 63 }; 64 65 void 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 76 static int ppc_swiotlb_bus_notify(struct notifier_block *nb, 77 unsigned long action, void *data) 78 { 79 struct device *dev = data; 80 struct dev_archdata *sd; 81 82 /* We are only intereted in device addition */ 83 if (action != BUS_NOTIFY_ADD_DEVICE) 84 return 0; 85 86 sd = &dev->archdata; 87 sd->max_direct_dma_addr = 0; 88 89 /* May need to bounce if the device can't address all of DRAM */ 90 if ((dma_get_mask(dev) + 1) < memblock_end_of_DRAM()) 91 set_dma_ops(dev, &powerpc_swiotlb_dma_ops); 92 93 return NOTIFY_DONE; 94 } 95 96 static struct notifier_block ppc_swiotlb_plat_bus_notifier = { 97 .notifier_call = ppc_swiotlb_bus_notify, 98 .priority = 0, 99 }; 100 101 int __init swiotlb_setup_bus_notifier(void) 102 { 103 bus_register_notifier(&platform_bus_type, 104 &ppc_swiotlb_plat_bus_notifier); 105 return 0; 106 } 107 108 void __init swiotlb_detect_4g(void) 109 { 110 if ((memblock_end_of_DRAM() - 1) > 0xffffffff) 111 ppc_swiotlb_enable = 1; 112 } 113 114 static int __init check_swiotlb_enabled(void) 115 { 116 if (ppc_swiotlb_enable) 117 swiotlb_print_info(); 118 else 119 swiotlb_exit(); 120 121 return 0; 122 } 123 subsys_initcall(check_swiotlb_enabled); 124