Ishizaki Kou | 97a9b58 | 2007-02-02 16:39:34 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Support for IOMMU on Celleb platform. |
| 3 | * |
| 4 | * (C) Copyright 2006-2007 TOSHIBA CORPORATION |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/dma-mapping.h> |
| 24 | #include <linux/pci.h> |
Jon Loeliger | d8caf74 | 2007-11-13 11:10:58 -0600 | [diff] [blame] | 25 | #include <linux/of_platform.h> |
Ishizaki Kou | 97a9b58 | 2007-02-02 16:39:34 +0900 | [diff] [blame] | 26 | |
Ishizaki Kou | 7e1961f | 2007-12-13 21:13:14 +1100 | [diff] [blame] | 27 | #include <asm/machdep.h> |
Ishizaki Kou | 97a9b58 | 2007-02-02 16:39:34 +0900 | [diff] [blame] | 28 | |
Ishizaki Kou | 8ae6e30 | 2008-04-24 19:28:48 +1000 | [diff] [blame] | 29 | #include "beat_wrapper.h" |
Ishizaki Kou | 97a9b58 | 2007-02-02 16:39:34 +0900 | [diff] [blame] | 30 | |
| 31 | #define DMA_FLAGS 0xf800000000000000UL /* r/w permitted, coherency required, |
| 32 | strongest order */ |
| 33 | |
| 34 | static int __init find_dma_window(u64 *io_space_id, u64 *ioid, |
| 35 | u64 *base, u64 *size, u64 *io_page_size) |
| 36 | { |
| 37 | struct device_node *dn; |
| 38 | const unsigned long *dma_window; |
| 39 | |
| 40 | for_each_node_by_type(dn, "ioif") { |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 41 | dma_window = of_get_property(dn, "toshiba,dma-window", NULL); |
Ishizaki Kou | 97a9b58 | 2007-02-02 16:39:34 +0900 | [diff] [blame] | 42 | if (dma_window) { |
| 43 | *io_space_id = (dma_window[0] >> 32) & 0xffffffffUL; |
| 44 | *ioid = dma_window[0] & 0x7ffUL; |
| 45 | *base = dma_window[1]; |
| 46 | *size = dma_window[2]; |
| 47 | *io_page_size = 1 << dma_window[3]; |
| 48 | of_node_put(dn); |
| 49 | return 1; |
| 50 | } |
| 51 | } |
| 52 | return 0; |
| 53 | } |
| 54 | |
Michael Ellerman | 15c60cc | 2008-01-21 16:42:46 +1100 | [diff] [blame] | 55 | static unsigned long celleb_dma_direct_offset; |
| 56 | |
Ishizaki Kou | 97a9b58 | 2007-02-02 16:39:34 +0900 | [diff] [blame] | 57 | static void __init celleb_init_direct_mapping(void) |
| 58 | { |
| 59 | u64 lpar_addr, io_addr; |
| 60 | u64 io_space_id, ioid, dma_base, dma_size, io_page_size; |
| 61 | |
| 62 | if (!find_dma_window(&io_space_id, &ioid, &dma_base, &dma_size, |
| 63 | &io_page_size)) { |
| 64 | pr_info("No dma window found !\n"); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | for (lpar_addr = 0; lpar_addr < dma_size; lpar_addr += io_page_size) { |
| 69 | io_addr = lpar_addr + dma_base; |
| 70 | (void)beat_put_iopte(io_space_id, io_addr, lpar_addr, |
| 71 | ioid, DMA_FLAGS); |
| 72 | } |
| 73 | |
Michael Ellerman | 15c60cc | 2008-01-21 16:42:46 +1100 | [diff] [blame] | 74 | celleb_dma_direct_offset = dma_base; |
Ishizaki Kou | 97a9b58 | 2007-02-02 16:39:34 +0900 | [diff] [blame] | 75 | } |
| 76 | |
Michael Ellerman | 450d87e | 2008-01-21 16:42:42 +1100 | [diff] [blame] | 77 | static void celleb_dma_dev_setup(struct device *dev) |
| 78 | { |
Nishanth Aravamudan | 741d204 | 2010-10-18 07:27:01 +0000 | [diff] [blame] | 79 | set_dma_ops(dev, &dma_direct_ops); |
Becky Bruce | 738ef42 | 2009-09-21 08:26:35 +0000 | [diff] [blame] | 80 | set_dma_offset(dev, celleb_dma_direct_offset); |
Michael Ellerman | 450d87e | 2008-01-21 16:42:42 +1100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static void celleb_pci_dma_dev_setup(struct pci_dev *pdev) |
| 84 | { |
| 85 | celleb_dma_dev_setup(&pdev->dev); |
| 86 | } |
| 87 | |
Ishizaki Kou | 97a9b58 | 2007-02-02 16:39:34 +0900 | [diff] [blame] | 88 | static int celleb_of_bus_notify(struct notifier_block *nb, |
| 89 | unsigned long action, void *data) |
| 90 | { |
| 91 | struct device *dev = data; |
| 92 | |
| 93 | /* We are only intereted in device addition */ |
| 94 | if (action != BUS_NOTIFY_ADD_DEVICE) |
| 95 | return 0; |
| 96 | |
Michael Ellerman | 450d87e | 2008-01-21 16:42:42 +1100 | [diff] [blame] | 97 | celleb_dma_dev_setup(dev); |
Ishizaki Kou | 97a9b58 | 2007-02-02 16:39:34 +0900 | [diff] [blame] | 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static struct notifier_block celleb_of_bus_notifier = { |
| 103 | .notifier_call = celleb_of_bus_notify |
| 104 | }; |
| 105 | |
| 106 | static int __init celleb_init_iommu(void) |
| 107 | { |
Ishizaki Kou | 97a9b58 | 2007-02-02 16:39:34 +0900 | [diff] [blame] | 108 | celleb_init_direct_mapping(); |
Michael Ellerman | 450d87e | 2008-01-21 16:42:42 +1100 | [diff] [blame] | 109 | ppc_md.pci_dma_dev_setup = celleb_pci_dma_dev_setup; |
Grant Likely | eca3930 | 2010-06-08 07:48:21 -0600 | [diff] [blame] | 110 | bus_register_notifier(&platform_bus_type, &celleb_of_bus_notifier); |
Ishizaki Kou | 97a9b58 | 2007-02-02 16:39:34 +0900 | [diff] [blame] | 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
Grant Likely | e25c47f | 2008-01-03 06:14:36 +1100 | [diff] [blame] | 115 | machine_arch_initcall(celleb_beat, celleb_init_iommu); |