Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007-2008 Advanced Micro Devices, Inc. |
| 3 | * Author: Joerg Roedel <joerg.roedel@amd.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published |
| 7 | * by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | #include <linux/bug.h> |
| 20 | #include <linux/types.h> |
Andrew Morton | 60db402 | 2009-05-06 16:03:07 -0700 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/slab.h> |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 23 | #include <linux/errno.h> |
| 24 | #include <linux/iommu.h> |
| 25 | |
| 26 | static struct iommu_ops *iommu_ops; |
| 27 | |
| 28 | void register_iommu(struct iommu_ops *ops) |
| 29 | { |
| 30 | if (iommu_ops) |
| 31 | BUG(); |
| 32 | |
| 33 | iommu_ops = ops; |
| 34 | } |
| 35 | |
Hannes Eder | ff2c8a4 | 2009-03-05 12:12:44 +0100 | [diff] [blame] | 36 | bool iommu_found(void) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 37 | { |
| 38 | return iommu_ops != NULL; |
| 39 | } |
| 40 | EXPORT_SYMBOL_GPL(iommu_found); |
| 41 | |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 42 | /** |
| 43 | * iommu_set_fault_handler() - set a fault handler for an iommu domain |
| 44 | * @domain: iommu domain |
| 45 | * @handler: fault handler |
Ohad Ben-Cohen | 0ed6d2d | 2011-09-27 07:36:40 -0400 | [diff] [blame^] | 46 | * |
| 47 | * This function should be used by IOMMU users which want to be notified |
| 48 | * whenever an IOMMU fault happens. |
| 49 | * |
| 50 | * The fault handler itself should return 0 on success, and an appropriate |
| 51 | * error code otherwise. |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 52 | */ |
| 53 | void iommu_set_fault_handler(struct iommu_domain *domain, |
| 54 | iommu_fault_handler_t handler) |
| 55 | { |
| 56 | BUG_ON(!domain); |
| 57 | |
| 58 | domain->handler = handler; |
| 59 | } |
Ohad Ben-Cohen | 30bd918 | 2011-09-26 09:11:46 -0400 | [diff] [blame] | 60 | EXPORT_SYMBOL_GPL(iommu_set_fault_handler); |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 61 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 62 | struct iommu_domain *iommu_domain_alloc(void) |
| 63 | { |
| 64 | struct iommu_domain *domain; |
| 65 | int ret; |
| 66 | |
| 67 | domain = kmalloc(sizeof(*domain), GFP_KERNEL); |
| 68 | if (!domain) |
| 69 | return NULL; |
| 70 | |
| 71 | ret = iommu_ops->domain_init(domain); |
| 72 | if (ret) |
| 73 | goto out_free; |
| 74 | |
| 75 | return domain; |
| 76 | |
| 77 | out_free: |
| 78 | kfree(domain); |
| 79 | |
| 80 | return NULL; |
| 81 | } |
| 82 | EXPORT_SYMBOL_GPL(iommu_domain_alloc); |
| 83 | |
| 84 | void iommu_domain_free(struct iommu_domain *domain) |
| 85 | { |
| 86 | iommu_ops->domain_destroy(domain); |
| 87 | kfree(domain); |
| 88 | } |
| 89 | EXPORT_SYMBOL_GPL(iommu_domain_free); |
| 90 | |
| 91 | int iommu_attach_device(struct iommu_domain *domain, struct device *dev) |
| 92 | { |
| 93 | return iommu_ops->attach_dev(domain, dev); |
| 94 | } |
| 95 | EXPORT_SYMBOL_GPL(iommu_attach_device); |
| 96 | |
| 97 | void iommu_detach_device(struct iommu_domain *domain, struct device *dev) |
| 98 | { |
| 99 | iommu_ops->detach_dev(domain, dev); |
| 100 | } |
| 101 | EXPORT_SYMBOL_GPL(iommu_detach_device); |
| 102 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 103 | phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, |
| 104 | unsigned long iova) |
| 105 | { |
| 106 | return iommu_ops->iova_to_phys(domain, iova); |
| 107 | } |
| 108 | EXPORT_SYMBOL_GPL(iommu_iova_to_phys); |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 109 | |
| 110 | int iommu_domain_has_cap(struct iommu_domain *domain, |
| 111 | unsigned long cap) |
| 112 | { |
| 113 | return iommu_ops->domain_has_cap(domain, cap); |
| 114 | } |
| 115 | EXPORT_SYMBOL_GPL(iommu_domain_has_cap); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 116 | |
| 117 | int iommu_map(struct iommu_domain *domain, unsigned long iova, |
| 118 | phys_addr_t paddr, int gfp_order, int prot) |
| 119 | { |
| 120 | unsigned long invalid_mask; |
| 121 | size_t size; |
| 122 | |
| 123 | size = 0x1000UL << gfp_order; |
| 124 | invalid_mask = size - 1; |
| 125 | |
| 126 | BUG_ON((iova | paddr) & invalid_mask); |
| 127 | |
Joerg Roedel | 12c7389 | 2010-01-21 11:50:28 +0100 | [diff] [blame] | 128 | return iommu_ops->map(domain, iova, paddr, gfp_order, prot); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 129 | } |
| 130 | EXPORT_SYMBOL_GPL(iommu_map); |
| 131 | |
| 132 | int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order) |
| 133 | { |
| 134 | unsigned long invalid_mask; |
| 135 | size_t size; |
| 136 | |
| 137 | size = 0x1000UL << gfp_order; |
| 138 | invalid_mask = size - 1; |
| 139 | |
| 140 | BUG_ON(iova & invalid_mask); |
| 141 | |
Joerg Roedel | 12c7389 | 2010-01-21 11:50:28 +0100 | [diff] [blame] | 142 | return iommu_ops->unmap(domain, iova, gfp_order); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 143 | } |
| 144 | EXPORT_SYMBOL_GPL(iommu_unmap); |