blob: 3e62d7836522ae3fe544b82bc068ebbceb4b045c [file] [log] [blame]
Joerg Roedelb6c02712008-06-26 21:27:53 +02001/*
2 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
3 * Author: Joerg Roedel <joerg.roedel@amd.com>
4 * Leo Duran <leo.duran@amd.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/pci.h>
21#include <linux/gfp.h>
22#include <linux/bitops.h>
Joerg Roedel7f265082008-12-12 13:50:21 +010023#include <linux/debugfs.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020024#include <linux/scatterlist.h>
FUJITA Tomonori51491362009-01-05 23:47:25 +090025#include <linux/dma-mapping.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020026#include <linux/iommu-helper.h>
Joerg Roedelc156e342008-12-02 18:13:27 +010027#include <linux/iommu.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020028#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090029#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010030#include <asm/gart.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020031#include <asm/amd_iommu_types.h>
Joerg Roedelc6da9922008-06-26 21:28:06 +020032#include <asm/amd_iommu.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020033
34#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
35
Joerg Roedel136f78a2008-07-11 17:14:27 +020036#define EXIT_LOOP_COUNT 10000000
37
Joerg Roedelb6c02712008-06-26 21:27:53 +020038static DEFINE_RWLOCK(amd_iommu_devtable_lock);
39
Joerg Roedelbd60b732008-09-11 10:24:48 +020040/* A list of preallocated protection domains */
41static LIST_HEAD(iommu_pd_list);
42static DEFINE_SPINLOCK(iommu_pd_list_lock);
43
Joerg Roedel26961ef2008-12-03 17:00:17 +010044static struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010045
Joerg Roedel431b2a22008-07-11 17:14:22 +020046/*
47 * general struct to manage commands send to an IOMMU
48 */
Joerg Roedeld6449532008-07-11 17:14:28 +020049struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +020050 u32 data[4];
51};
52
Joerg Roedelbd0e5212008-06-26 21:27:56 +020053static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
54 struct unity_map_entry *e);
Joerg Roedele275a2a2008-12-10 18:27:25 +010055static struct dma_ops_domain *find_protection_domain(u16 devid);
Joerg Roedel8bda3092009-05-12 12:02:46 +020056static u64* alloc_pte(struct protection_domain *dom,
57 unsigned long address, u64
58 **pte_page, gfp_t gfp);
Joerg Roedel00cd1222009-05-19 09:52:40 +020059static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
60 unsigned long start_page,
61 unsigned int pages);
Joerg Roedelbd0e5212008-06-26 21:27:56 +020062
Joerg Roedel7f265082008-12-12 13:50:21 +010063#ifdef CONFIG_AMD_IOMMU_STATS
64
65/*
66 * Initialization code for statistics collection
67 */
68
Joerg Roedelda49f6d2008-12-12 14:59:58 +010069DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +010070DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +010071DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +010072DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +010073DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +010074DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +010075DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +010076DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +010077DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +010078DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +010079DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +010080DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedelda49f6d2008-12-12 14:59:58 +010081
Joerg Roedel7f265082008-12-12 13:50:21 +010082static struct dentry *stats_dir;
83static struct dentry *de_isolate;
84static struct dentry *de_fflush;
85
86static void amd_iommu_stats_add(struct __iommu_counter *cnt)
87{
88 if (stats_dir == NULL)
89 return;
90
91 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
92 &cnt->value);
93}
94
95static void amd_iommu_stats_init(void)
96{
97 stats_dir = debugfs_create_dir("amd-iommu", NULL);
98 if (stats_dir == NULL)
99 return;
100
101 de_isolate = debugfs_create_bool("isolation", 0444, stats_dir,
102 (u32 *)&amd_iommu_isolate);
103
104 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
105 (u32 *)&amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100106
107 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100108 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100109 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100110 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100111 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100112 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100113 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100114 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100115 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100116 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100117 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100118 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100119}
120
121#endif
122
Joerg Roedel431b2a22008-07-11 17:14:22 +0200123/* returns !0 if the IOMMU is caching non-present entries in its TLB */
Joerg Roedel4da70b92008-06-26 21:28:01 +0200124static int iommu_has_npcache(struct amd_iommu *iommu)
125{
Joerg Roedelae9b9402008-10-30 17:43:57 +0100126 return iommu->cap & (1UL << IOMMU_CAP_NPCACHE);
Joerg Roedel4da70b92008-06-26 21:28:01 +0200127}
128
Joerg Roedel431b2a22008-07-11 17:14:22 +0200129/****************************************************************************
130 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200131 * Interrupt handling functions
132 *
133 ****************************************************************************/
134
Joerg Roedel90008ee2008-09-09 16:41:05 +0200135static void iommu_print_event(void *__evt)
136{
137 u32 *event = __evt;
138 int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
139 int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
140 int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
141 int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
142 u64 address = (u64)(((u64)event[3]) << 32) | event[2];
143
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200144 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200145
146 switch (type) {
147 case EVENT_TYPE_ILL_DEV:
148 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
149 "address=0x%016llx flags=0x%04x]\n",
150 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
151 address, flags);
152 break;
153 case EVENT_TYPE_IO_FAULT:
154 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
155 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
156 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
157 domid, address, flags);
158 break;
159 case EVENT_TYPE_DEV_TAB_ERR:
160 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
161 "address=0x%016llx flags=0x%04x]\n",
162 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
163 address, flags);
164 break;
165 case EVENT_TYPE_PAGE_TAB_ERR:
166 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
167 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
168 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
169 domid, address, flags);
170 break;
171 case EVENT_TYPE_ILL_CMD:
172 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
173 break;
174 case EVENT_TYPE_CMD_HARD_ERR:
175 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
176 "flags=0x%04x]\n", address, flags);
177 break;
178 case EVENT_TYPE_IOTLB_INV_TO:
179 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
180 "address=0x%016llx]\n",
181 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
182 address);
183 break;
184 case EVENT_TYPE_INV_DEV_REQ:
185 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
186 "address=0x%016llx flags=0x%04x]\n",
187 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
188 address, flags);
189 break;
190 default:
191 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
192 }
193}
194
195static void iommu_poll_events(struct amd_iommu *iommu)
196{
197 u32 head, tail;
198 unsigned long flags;
199
200 spin_lock_irqsave(&iommu->lock, flags);
201
202 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
203 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
204
205 while (head != tail) {
206 iommu_print_event(iommu->evt_buf + head);
207 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
208 }
209
210 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
211
212 spin_unlock_irqrestore(&iommu->lock, flags);
213}
214
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200215irqreturn_t amd_iommu_int_handler(int irq, void *data)
216{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200217 struct amd_iommu *iommu;
218
Joerg Roedel3bd22172009-05-04 15:06:20 +0200219 for_each_iommu(iommu)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200220 iommu_poll_events(iommu);
221
222 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200223}
224
225/****************************************************************************
226 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200227 * IOMMU command queuing functions
228 *
229 ****************************************************************************/
230
231/*
232 * Writes the command to the IOMMUs command buffer and informs the
233 * hardware about the new command. Must be called with iommu->lock held.
234 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200235static int __iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200236{
237 u32 tail, head;
238 u8 *target;
239
240 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Jiri Kosina8a7c5ef2008-08-19 02:13:55 +0200241 target = iommu->cmd_buf + tail;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200242 memcpy_toio(target, cmd, sizeof(*cmd));
243 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
244 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
245 if (tail == head)
246 return -ENOMEM;
247 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
248
249 return 0;
250}
251
Joerg Roedel431b2a22008-07-11 17:14:22 +0200252/*
253 * General queuing function for commands. Takes iommu->lock and calls
254 * __iommu_queue_command().
255 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200256static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200257{
258 unsigned long flags;
259 int ret;
260
261 spin_lock_irqsave(&iommu->lock, flags);
262 ret = __iommu_queue_command(iommu, cmd);
Joerg Roedel09ee17e2008-12-03 12:19:27 +0100263 if (!ret)
Joerg Roedel0cfd7aa2008-12-10 19:58:00 +0100264 iommu->need_sync = true;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200265 spin_unlock_irqrestore(&iommu->lock, flags);
266
267 return ret;
268}
269
Joerg Roedel431b2a22008-07-11 17:14:22 +0200270/*
Joerg Roedel8d201962008-12-02 20:34:41 +0100271 * This function waits until an IOMMU has completed a completion
272 * wait command
Joerg Roedel431b2a22008-07-11 17:14:22 +0200273 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100274static void __iommu_wait_for_completion(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200275{
Joerg Roedel8d201962008-12-02 20:34:41 +0100276 int ready = 0;
Joerg Roedel519c31b2008-08-14 19:55:15 +0200277 unsigned status = 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100278 unsigned long i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200279
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100280 INC_STATS_COUNTER(compl_wait);
281
Joerg Roedel136f78a2008-07-11 17:14:27 +0200282 while (!ready && (i < EXIT_LOOP_COUNT)) {
283 ++i;
Joerg Roedel519c31b2008-08-14 19:55:15 +0200284 /* wait for the bit to become one */
285 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
286 ready = status & MMIO_STATUS_COM_WAIT_INT_MASK;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200287 }
288
Joerg Roedel519c31b2008-08-14 19:55:15 +0200289 /* set bit back to zero */
290 status &= ~MMIO_STATUS_COM_WAIT_INT_MASK;
291 writel(status, iommu->mmio_base + MMIO_STATUS_OFFSET);
292
Joerg Roedel84df8172008-12-17 16:36:44 +0100293 if (unlikely(i == EXIT_LOOP_COUNT))
294 panic("AMD IOMMU: Completion wait loop failed\n");
Joerg Roedel8d201962008-12-02 20:34:41 +0100295}
296
297/*
298 * This function queues a completion wait command into the command
299 * buffer of an IOMMU
300 */
301static int __iommu_completion_wait(struct amd_iommu *iommu)
302{
303 struct iommu_cmd cmd;
304
305 memset(&cmd, 0, sizeof(cmd));
306 cmd.data[0] = CMD_COMPL_WAIT_INT_MASK;
307 CMD_SET_TYPE(&cmd, CMD_COMPL_WAIT);
308
309 return __iommu_queue_command(iommu, &cmd);
310}
311
312/*
313 * This function is called whenever we need to ensure that the IOMMU has
314 * completed execution of all commands we sent. It sends a
315 * COMPLETION_WAIT command and waits for it to finish. The IOMMU informs
316 * us about that by writing a value to a physical address we pass with
317 * the command.
318 */
319static int iommu_completion_wait(struct amd_iommu *iommu)
320{
321 int ret = 0;
322 unsigned long flags;
323
324 spin_lock_irqsave(&iommu->lock, flags);
325
326 if (!iommu->need_sync)
327 goto out;
328
329 ret = __iommu_completion_wait(iommu);
330
Joerg Roedel0cfd7aa2008-12-10 19:58:00 +0100331 iommu->need_sync = false;
Joerg Roedel8d201962008-12-02 20:34:41 +0100332
333 if (ret)
334 goto out;
335
336 __iommu_wait_for_completion(iommu);
Joerg Roedel84df8172008-12-17 16:36:44 +0100337
Joerg Roedel7e4f88d2008-09-17 14:19:15 +0200338out:
339 spin_unlock_irqrestore(&iommu->lock, flags);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200340
341 return 0;
342}
343
Joerg Roedel431b2a22008-07-11 17:14:22 +0200344/*
345 * Command send function for invalidating a device table entry
346 */
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200347static int iommu_queue_inv_dev_entry(struct amd_iommu *iommu, u16 devid)
348{
Joerg Roedeld6449532008-07-11 17:14:28 +0200349 struct iommu_cmd cmd;
Joerg Roedelee2fa742008-09-17 13:47:25 +0200350 int ret;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200351
352 BUG_ON(iommu == NULL);
353
354 memset(&cmd, 0, sizeof(cmd));
355 CMD_SET_TYPE(&cmd, CMD_INV_DEV_ENTRY);
356 cmd.data[0] = devid;
357
Joerg Roedelee2fa742008-09-17 13:47:25 +0200358 ret = iommu_queue_command(iommu, &cmd);
359
Joerg Roedelee2fa742008-09-17 13:47:25 +0200360 return ret;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200361}
362
Joerg Roedel237b6f32008-12-02 20:54:37 +0100363static void __iommu_build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
364 u16 domid, int pde, int s)
365{
366 memset(cmd, 0, sizeof(*cmd));
367 address &= PAGE_MASK;
368 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
369 cmd->data[1] |= domid;
370 cmd->data[2] = lower_32_bits(address);
371 cmd->data[3] = upper_32_bits(address);
372 if (s) /* size bit - we flush more than one 4kb page */
373 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
374 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
375 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
376}
377
Joerg Roedel431b2a22008-07-11 17:14:22 +0200378/*
379 * Generic command send function for invalidaing TLB entries
380 */
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200381static int iommu_queue_inv_iommu_pages(struct amd_iommu *iommu,
382 u64 address, u16 domid, int pde, int s)
383{
Joerg Roedeld6449532008-07-11 17:14:28 +0200384 struct iommu_cmd cmd;
Joerg Roedelee2fa742008-09-17 13:47:25 +0200385 int ret;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200386
Joerg Roedel237b6f32008-12-02 20:54:37 +0100387 __iommu_build_inv_iommu_pages(&cmd, address, domid, pde, s);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200388
Joerg Roedelee2fa742008-09-17 13:47:25 +0200389 ret = iommu_queue_command(iommu, &cmd);
390
Joerg Roedelee2fa742008-09-17 13:47:25 +0200391 return ret;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200392}
393
Joerg Roedel431b2a22008-07-11 17:14:22 +0200394/*
395 * TLB invalidation function which is called from the mapping functions.
396 * It invalidates a single PTE if the range to flush is within a single
397 * page. Otherwise it flushes the whole TLB of the IOMMU.
398 */
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200399static int iommu_flush_pages(struct amd_iommu *iommu, u16 domid,
400 u64 address, size_t size)
401{
Joerg Roedel999ba412008-07-03 19:35:08 +0200402 int s = 0;
Joerg Roedele3c449f2008-10-15 22:02:11 -0700403 unsigned pages = iommu_num_pages(address, size, PAGE_SIZE);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200404
405 address &= PAGE_MASK;
406
Joerg Roedel999ba412008-07-03 19:35:08 +0200407 if (pages > 1) {
408 /*
409 * If we have to flush more than one page, flush all
410 * TLB entries for this domain
411 */
412 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
413 s = 1;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200414 }
415
Joerg Roedel999ba412008-07-03 19:35:08 +0200416 iommu_queue_inv_iommu_pages(iommu, address, domid, 0, s);
417
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200418 return 0;
419}
Joerg Roedelb6c02712008-06-26 21:27:53 +0200420
Joerg Roedel1c655772008-09-04 18:40:05 +0200421/* Flush the whole IO/TLB for a given protection domain */
422static void iommu_flush_tlb(struct amd_iommu *iommu, u16 domid)
423{
424 u64 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
425
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100426 INC_STATS_COUNTER(domain_flush_single);
427
Joerg Roedel1c655772008-09-04 18:40:05 +0200428 iommu_queue_inv_iommu_pages(iommu, address, domid, 0, 1);
429}
430
Chris Wright42a49f92009-06-15 15:42:00 +0200431/* Flush the whole IO/TLB for a given protection domain - including PDE */
432static void iommu_flush_tlb_pde(struct amd_iommu *iommu, u16 domid)
433{
434 u64 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
435
436 INC_STATS_COUNTER(domain_flush_single);
437
438 iommu_queue_inv_iommu_pages(iommu, address, domid, 1, 1);
439}
440
Joerg Roedel43f49602008-12-02 21:01:12 +0100441/*
442 * This function is used to flush the IO/TLB for a given protection domain
443 * on every IOMMU in the system
444 */
445static void iommu_flush_domain(u16 domid)
446{
447 unsigned long flags;
448 struct amd_iommu *iommu;
449 struct iommu_cmd cmd;
450
Joerg Roedel18811f52008-12-12 15:48:28 +0100451 INC_STATS_COUNTER(domain_flush_all);
452
Joerg Roedel43f49602008-12-02 21:01:12 +0100453 __iommu_build_inv_iommu_pages(&cmd, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
454 domid, 1, 1);
455
Joerg Roedel3bd22172009-05-04 15:06:20 +0200456 for_each_iommu(iommu) {
Joerg Roedel43f49602008-12-02 21:01:12 +0100457 spin_lock_irqsave(&iommu->lock, flags);
458 __iommu_queue_command(iommu, &cmd);
459 __iommu_completion_wait(iommu);
460 __iommu_wait_for_completion(iommu);
461 spin_unlock_irqrestore(&iommu->lock, flags);
462 }
463}
Joerg Roedel43f49602008-12-02 21:01:12 +0100464
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200465void amd_iommu_flush_all_domains(void)
466{
467 int i;
468
469 for (i = 1; i < MAX_DOMAIN_ID; ++i) {
470 if (!test_bit(i, amd_iommu_pd_alloc_bitmap))
471 continue;
472 iommu_flush_domain(i);
473 }
474}
475
Joerg Roedel7d7a1102009-05-05 15:48:10 +0200476void amd_iommu_flush_all_devices(void)
477{
478 struct amd_iommu *iommu;
479 int i;
480
481 for (i = 0; i <= amd_iommu_last_bdf; ++i) {
482 if (amd_iommu_pd_table[i] == NULL)
483 continue;
484
485 iommu = amd_iommu_rlookup_table[i];
486 if (!iommu)
487 continue;
488
489 iommu_queue_inv_dev_entry(iommu, i);
490 iommu_completion_wait(iommu);
491 }
492}
493
Joerg Roedel431b2a22008-07-11 17:14:22 +0200494/****************************************************************************
495 *
496 * The functions below are used the create the page table mappings for
497 * unity mapped regions.
498 *
499 ****************************************************************************/
500
501/*
502 * Generic mapping functions. It maps a physical address into a DMA
503 * address space. It allocates the page table pages if necessary.
504 * In the future it can be extended to a generic mapping function
505 * supporting all features of AMD IOMMU page tables like level skipping
506 * and full 64 bit address spaces.
507 */
Joerg Roedel38e817f2008-12-02 17:27:52 +0100508static int iommu_map_page(struct protection_domain *dom,
509 unsigned long bus_addr,
510 unsigned long phys_addr,
511 int prot)
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200512{
Joerg Roedel8bda3092009-05-12 12:02:46 +0200513 u64 __pte, *pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200514
515 bus_addr = PAGE_ALIGN(bus_addr);
Joerg Roedelbb9d4ff2008-12-04 15:59:48 +0100516 phys_addr = PAGE_ALIGN(phys_addr);
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200517
518 /* only support 512GB address spaces for now */
519 if (bus_addr > IOMMU_MAP_SIZE_L3 || !(prot & IOMMU_PROT_MASK))
520 return -EINVAL;
521
Joerg Roedel8bda3092009-05-12 12:02:46 +0200522 pte = alloc_pte(dom, bus_addr, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200523
524 if (IOMMU_PTE_PRESENT(*pte))
525 return -EBUSY;
526
527 __pte = phys_addr | IOMMU_PTE_P;
528 if (prot & IOMMU_PROT_IR)
529 __pte |= IOMMU_PTE_IR;
530 if (prot & IOMMU_PROT_IW)
531 __pte |= IOMMU_PTE_IW;
532
533 *pte = __pte;
534
535 return 0;
536}
537
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100538static void iommu_unmap_page(struct protection_domain *dom,
539 unsigned long bus_addr)
540{
541 u64 *pte;
542
543 pte = &dom->pt_root[IOMMU_PTE_L2_INDEX(bus_addr)];
544
545 if (!IOMMU_PTE_PRESENT(*pte))
546 return;
547
548 pte = IOMMU_PTE_PAGE(*pte);
549 pte = &pte[IOMMU_PTE_L1_INDEX(bus_addr)];
550
551 if (!IOMMU_PTE_PRESENT(*pte))
552 return;
553
554 pte = IOMMU_PTE_PAGE(*pte);
555 pte = &pte[IOMMU_PTE_L1_INDEX(bus_addr)];
556
557 *pte = 0;
558}
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100559
Joerg Roedel431b2a22008-07-11 17:14:22 +0200560/*
561 * This function checks if a specific unity mapping entry is needed for
562 * this specific IOMMU.
563 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200564static int iommu_for_unity_map(struct amd_iommu *iommu,
565 struct unity_map_entry *entry)
566{
567 u16 bdf, i;
568
569 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
570 bdf = amd_iommu_alias_table[i];
571 if (amd_iommu_rlookup_table[bdf] == iommu)
572 return 1;
573 }
574
575 return 0;
576}
577
Joerg Roedel431b2a22008-07-11 17:14:22 +0200578/*
579 * Init the unity mappings for a specific IOMMU in the system
580 *
581 * Basically iterates over all unity mapping entries and applies them to
582 * the default domain DMA of that IOMMU if necessary.
583 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200584static int iommu_init_unity_mappings(struct amd_iommu *iommu)
585{
586 struct unity_map_entry *entry;
587 int ret;
588
589 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
590 if (!iommu_for_unity_map(iommu, entry))
591 continue;
592 ret = dma_ops_unity_map(iommu->default_dom, entry);
593 if (ret)
594 return ret;
595 }
596
597 return 0;
598}
599
Joerg Roedel431b2a22008-07-11 17:14:22 +0200600/*
601 * This function actually applies the mapping to the page table of the
602 * dma_ops domain.
603 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200604static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
605 struct unity_map_entry *e)
606{
607 u64 addr;
608 int ret;
609
610 for (addr = e->address_start; addr < e->address_end;
611 addr += PAGE_SIZE) {
Joerg Roedel38e817f2008-12-02 17:27:52 +0100612 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot);
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200613 if (ret)
614 return ret;
615 /*
616 * if unity mapping is in aperture range mark the page
617 * as allocated in the aperture
618 */
619 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +0200620 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +0200621 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200622 }
623
624 return 0;
625}
626
Joerg Roedel431b2a22008-07-11 17:14:22 +0200627/*
628 * Inits the unity mappings required for a specific device
629 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200630static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
631 u16 devid)
632{
633 struct unity_map_entry *e;
634 int ret;
635
636 list_for_each_entry(e, &amd_iommu_unity_map, list) {
637 if (!(devid >= e->devid_start && devid <= e->devid_end))
638 continue;
639 ret = dma_ops_unity_map(dma_dom, e);
640 if (ret)
641 return ret;
642 }
643
644 return 0;
645}
646
Joerg Roedel431b2a22008-07-11 17:14:22 +0200647/****************************************************************************
648 *
649 * The next functions belong to the address allocator for the dma_ops
650 * interface functions. They work like the allocators in the other IOMMU
651 * drivers. Its basically a bitmap which marks the allocated pages in
652 * the aperture. Maybe it could be enhanced in the future to a more
653 * efficient allocator.
654 *
655 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +0200656
Joerg Roedel431b2a22008-07-11 17:14:22 +0200657/*
Joerg Roedel384de722009-05-15 12:30:05 +0200658 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200659 *
660 * called with domain->lock held
661 */
Joerg Roedel384de722009-05-15 12:30:05 +0200662
Joerg Roedel9cabe892009-05-18 16:38:55 +0200663/*
Joerg Roedel00cd1222009-05-19 09:52:40 +0200664 * This function checks if there is a PTE for a given dma address. If
665 * there is one, it returns the pointer to it.
666 */
667static u64* fetch_pte(struct protection_domain *domain,
668 unsigned long address)
669{
670 u64 *pte;
671
672 pte = &domain->pt_root[IOMMU_PTE_L2_INDEX(address)];
673
674 if (!IOMMU_PTE_PRESENT(*pte))
675 return NULL;
676
677 pte = IOMMU_PTE_PAGE(*pte);
678 pte = &pte[IOMMU_PTE_L1_INDEX(address)];
679
680 if (!IOMMU_PTE_PRESENT(*pte))
681 return NULL;
682
683 pte = IOMMU_PTE_PAGE(*pte);
684 pte = &pte[IOMMU_PTE_L0_INDEX(address)];
685
686 return pte;
687}
688
689/*
Joerg Roedel9cabe892009-05-18 16:38:55 +0200690 * This function is used to add a new aperture range to an existing
691 * aperture in case of dma_ops domain allocation or address allocation
692 * failure.
693 */
Joerg Roedel00cd1222009-05-19 09:52:40 +0200694static int alloc_new_range(struct amd_iommu *iommu,
695 struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +0200696 bool populate, gfp_t gfp)
697{
698 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel00cd1222009-05-19 09:52:40 +0200699 int i;
Joerg Roedel9cabe892009-05-18 16:38:55 +0200700
Joerg Roedelf5e97052009-05-22 12:31:53 +0200701#ifdef CONFIG_IOMMU_STRESS
702 populate = false;
703#endif
704
Joerg Roedel9cabe892009-05-18 16:38:55 +0200705 if (index >= APERTURE_MAX_RANGES)
706 return -ENOMEM;
707
708 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
709 if (!dma_dom->aperture[index])
710 return -ENOMEM;
711
712 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
713 if (!dma_dom->aperture[index]->bitmap)
714 goto out_free;
715
716 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
717
718 if (populate) {
719 unsigned long address = dma_dom->aperture_size;
720 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
721 u64 *pte, *pte_page;
722
723 for (i = 0; i < num_ptes; ++i) {
724 pte = alloc_pte(&dma_dom->domain, address,
725 &pte_page, gfp);
726 if (!pte)
727 goto out_free;
728
729 dma_dom->aperture[index]->pte_pages[i] = pte_page;
730
731 address += APERTURE_RANGE_SIZE / 64;
732 }
733 }
734
735 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
736
Joerg Roedel00cd1222009-05-19 09:52:40 +0200737 /* Intialize the exclusion range if necessary */
738 if (iommu->exclusion_start &&
739 iommu->exclusion_start >= dma_dom->aperture[index]->offset &&
740 iommu->exclusion_start < dma_dom->aperture_size) {
741 unsigned long startpage = iommu->exclusion_start >> PAGE_SHIFT;
742 int pages = iommu_num_pages(iommu->exclusion_start,
743 iommu->exclusion_length,
744 PAGE_SIZE);
745 dma_ops_reserve_addresses(dma_dom, startpage, pages);
746 }
747
748 /*
749 * Check for areas already mapped as present in the new aperture
750 * range and mark those pages as reserved in the allocator. Such
751 * mappings may already exist as a result of requested unity
752 * mappings for devices.
753 */
754 for (i = dma_dom->aperture[index]->offset;
755 i < dma_dom->aperture_size;
756 i += PAGE_SIZE) {
757 u64 *pte = fetch_pte(&dma_dom->domain, i);
758 if (!pte || !IOMMU_PTE_PRESENT(*pte))
759 continue;
760
761 dma_ops_reserve_addresses(dma_dom, i << PAGE_SHIFT, 1);
762 }
763
Joerg Roedel9cabe892009-05-18 16:38:55 +0200764 return 0;
765
766out_free:
767 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
768
769 kfree(dma_dom->aperture[index]);
770 dma_dom->aperture[index] = NULL;
771
772 return -ENOMEM;
773}
774
Joerg Roedel384de722009-05-15 12:30:05 +0200775static unsigned long dma_ops_area_alloc(struct device *dev,
776 struct dma_ops_domain *dom,
777 unsigned int pages,
778 unsigned long align_mask,
779 u64 dma_mask,
780 unsigned long start)
781{
Joerg Roedel803b8cb2009-05-18 15:32:48 +0200782 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +0200783 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
784 int i = start >> APERTURE_RANGE_SHIFT;
785 unsigned long boundary_size;
786 unsigned long address = -1;
787 unsigned long limit;
788
Joerg Roedel803b8cb2009-05-18 15:32:48 +0200789 next_bit >>= PAGE_SHIFT;
790
Joerg Roedel384de722009-05-15 12:30:05 +0200791 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
792 PAGE_SIZE) >> PAGE_SHIFT;
793
794 for (;i < max_index; ++i) {
795 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
796
797 if (dom->aperture[i]->offset >= dma_mask)
798 break;
799
800 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
801 dma_mask >> PAGE_SHIFT);
802
803 address = iommu_area_alloc(dom->aperture[i]->bitmap,
804 limit, next_bit, pages, 0,
805 boundary_size, align_mask);
806 if (address != -1) {
807 address = dom->aperture[i]->offset +
808 (address << PAGE_SHIFT);
Joerg Roedel803b8cb2009-05-18 15:32:48 +0200809 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +0200810 break;
811 }
812
813 next_bit = 0;
814 }
815
816 return address;
817}
818
Joerg Roedeld3086442008-06-26 21:27:57 +0200819static unsigned long dma_ops_alloc_addresses(struct device *dev,
820 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +0200821 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +0200822 unsigned long align_mask,
823 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +0200824{
Joerg Roedeld3086442008-06-26 21:27:57 +0200825 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +0200826
Joerg Roedelfe16f082009-05-22 12:27:53 +0200827#ifdef CONFIG_IOMMU_STRESS
828 dom->next_address = 0;
829 dom->need_flush = true;
830#endif
Joerg Roedeld3086442008-06-26 21:27:57 +0200831
Joerg Roedel384de722009-05-15 12:30:05 +0200832 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb2009-05-18 15:32:48 +0200833 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +0200834
Joerg Roedel1c655772008-09-04 18:40:05 +0200835 if (address == -1) {
Joerg Roedel803b8cb2009-05-18 15:32:48 +0200836 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +0200837 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
838 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +0200839 dom->need_flush = true;
840 }
Joerg Roedeld3086442008-06-26 21:27:57 +0200841
Joerg Roedel384de722009-05-15 12:30:05 +0200842 if (unlikely(address == -1))
Joerg Roedeld3086442008-06-26 21:27:57 +0200843 address = bad_dma_address;
844
845 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
846
847 return address;
848}
849
Joerg Roedel431b2a22008-07-11 17:14:22 +0200850/*
851 * The address free function.
852 *
853 * called with domain->lock held
854 */
Joerg Roedeld3086442008-06-26 21:27:57 +0200855static void dma_ops_free_addresses(struct dma_ops_domain *dom,
856 unsigned long address,
857 unsigned int pages)
858{
Joerg Roedel384de722009-05-15 12:30:05 +0200859 unsigned i = address >> APERTURE_RANGE_SHIFT;
860 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +0100861
Joerg Roedel384de722009-05-15 12:30:05 +0200862 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
863
Joerg Roedel47bccd62009-05-22 12:40:54 +0200864#ifdef CONFIG_IOMMU_STRESS
865 if (i < 4)
866 return;
867#endif
868
Joerg Roedel803b8cb2009-05-18 15:32:48 +0200869 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +0100870 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +0200871
872 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb2009-05-18 15:32:48 +0200873
Joerg Roedel384de722009-05-15 12:30:05 +0200874 iommu_area_free(range->bitmap, address, pages);
875
Joerg Roedeld3086442008-06-26 21:27:57 +0200876}
877
Joerg Roedel431b2a22008-07-11 17:14:22 +0200878/****************************************************************************
879 *
880 * The next functions belong to the domain allocation. A domain is
881 * allocated for every IOMMU as the default domain. If device isolation
882 * is enabled, every device get its own domain. The most important thing
883 * about domains is the page table mapping the DMA address space they
884 * contain.
885 *
886 ****************************************************************************/
887
Joerg Roedelec487d12008-06-26 21:27:58 +0200888static u16 domain_id_alloc(void)
889{
890 unsigned long flags;
891 int id;
892
893 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
894 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
895 BUG_ON(id == 0);
896 if (id > 0 && id < MAX_DOMAIN_ID)
897 __set_bit(id, amd_iommu_pd_alloc_bitmap);
898 else
899 id = 0;
900 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
901
902 return id;
903}
904
Joerg Roedela2acfb72008-12-02 18:28:53 +0100905static void domain_id_free(int id)
906{
907 unsigned long flags;
908
909 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
910 if (id > 0 && id < MAX_DOMAIN_ID)
911 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
912 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
913}
Joerg Roedela2acfb72008-12-02 18:28:53 +0100914
Joerg Roedel431b2a22008-07-11 17:14:22 +0200915/*
916 * Used to reserve address ranges in the aperture (e.g. for exclusion
917 * ranges.
918 */
Joerg Roedelec487d12008-06-26 21:27:58 +0200919static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
920 unsigned long start_page,
921 unsigned int pages)
922{
Joerg Roedel384de722009-05-15 12:30:05 +0200923 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
Joerg Roedelec487d12008-06-26 21:27:58 +0200924
925 if (start_page + pages > last_page)
926 pages = last_page - start_page;
927
Joerg Roedel384de722009-05-15 12:30:05 +0200928 for (i = start_page; i < start_page + pages; ++i) {
929 int index = i / APERTURE_RANGE_PAGES;
930 int page = i % APERTURE_RANGE_PAGES;
931 __set_bit(page, dom->aperture[index]->bitmap);
932 }
Joerg Roedelec487d12008-06-26 21:27:58 +0200933}
934
Joerg Roedel86db2e52008-12-02 18:20:21 +0100935static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +0200936{
937 int i, j;
938 u64 *p1, *p2, *p3;
939
Joerg Roedel86db2e52008-12-02 18:20:21 +0100940 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +0200941
942 if (!p1)
943 return;
944
945 for (i = 0; i < 512; ++i) {
946 if (!IOMMU_PTE_PRESENT(p1[i]))
947 continue;
948
949 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +0100950 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +0200951 if (!IOMMU_PTE_PRESENT(p2[j]))
952 continue;
953 p3 = IOMMU_PTE_PAGE(p2[j]);
954 free_page((unsigned long)p3);
955 }
956
957 free_page((unsigned long)p2);
958 }
959
960 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +0100961
962 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +0200963}
964
Joerg Roedel431b2a22008-07-11 17:14:22 +0200965/*
966 * Free a domain, only used if something went wrong in the
967 * allocation path and we need to free an already allocated page table
968 */
Joerg Roedelec487d12008-06-26 21:27:58 +0200969static void dma_ops_domain_free(struct dma_ops_domain *dom)
970{
Joerg Roedel384de722009-05-15 12:30:05 +0200971 int i;
972
Joerg Roedelec487d12008-06-26 21:27:58 +0200973 if (!dom)
974 return;
975
Joerg Roedel86db2e52008-12-02 18:20:21 +0100976 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +0200977
Joerg Roedel384de722009-05-15 12:30:05 +0200978 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
979 if (!dom->aperture[i])
980 continue;
981 free_page((unsigned long)dom->aperture[i]->bitmap);
982 kfree(dom->aperture[i]);
983 }
Joerg Roedelec487d12008-06-26 21:27:58 +0200984
985 kfree(dom);
986}
987
Joerg Roedel431b2a22008-07-11 17:14:22 +0200988/*
989 * Allocates a new protection domain usable for the dma_ops functions.
990 * It also intializes the page table and the address allocator data
991 * structures required for the dma_ops interface
992 */
Joerg Roedeld9cfed92009-05-19 12:16:29 +0200993static struct dma_ops_domain *dma_ops_domain_alloc(struct amd_iommu *iommu)
Joerg Roedelec487d12008-06-26 21:27:58 +0200994{
995 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +0200996
997 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
998 if (!dma_dom)
999 return NULL;
1000
1001 spin_lock_init(&dma_dom->domain.lock);
1002
1003 dma_dom->domain.id = domain_id_alloc();
1004 if (dma_dom->domain.id == 0)
1005 goto free_dma_dom;
1006 dma_dom->domain.mode = PAGE_MODE_3_LEVEL;
1007 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001008 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001009 dma_dom->domain.priv = dma_dom;
1010 if (!dma_dom->domain.pt_root)
1011 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001012
Joerg Roedel1c655772008-09-04 18:40:05 +02001013 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001014 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001015
Joerg Roedel00cd1222009-05-19 09:52:40 +02001016 if (alloc_new_range(iommu, dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001017 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001018
Joerg Roedel431b2a22008-07-11 17:14:22 +02001019 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001020 * mark the first page as allocated so we never return 0 as
1021 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001022 */
Joerg Roedel384de722009-05-15 12:30:05 +02001023 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001024 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001025
Joerg Roedelec487d12008-06-26 21:27:58 +02001026
1027 return dma_dom;
1028
1029free_dma_dom:
1030 dma_ops_domain_free(dma_dom);
1031
1032 return NULL;
1033}
1034
Joerg Roedel431b2a22008-07-11 17:14:22 +02001035/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001036 * little helper function to check whether a given protection domain is a
1037 * dma_ops domain
1038 */
1039static bool dma_ops_domain(struct protection_domain *domain)
1040{
1041 return domain->flags & PD_DMA_OPS_MASK;
1042}
1043
1044/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001045 * Find out the protection domain structure for a given PCI device. This
1046 * will give us the pointer to the page table root for example.
1047 */
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001048static struct protection_domain *domain_for_device(u16 devid)
1049{
1050 struct protection_domain *dom;
1051 unsigned long flags;
1052
1053 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
1054 dom = amd_iommu_pd_table[devid];
1055 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1056
1057 return dom;
1058}
1059
Joerg Roedel431b2a22008-07-11 17:14:22 +02001060/*
1061 * If a device is not yet associated with a domain, this function does
1062 * assigns it visible for the hardware
1063 */
Joerg Roedelf1179dc2008-12-10 14:39:51 +01001064static void attach_device(struct amd_iommu *iommu,
1065 struct protection_domain *domain,
1066 u16 devid)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001067{
1068 unsigned long flags;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001069 u64 pte_root = virt_to_phys(domain->pt_root);
1070
Joerg Roedel863c74e2008-12-02 17:56:36 +01001071 domain->dev_cnt += 1;
1072
Joerg Roedel38ddf412008-09-11 10:38:32 +02001073 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1074 << DEV_ENTRY_MODE_SHIFT;
1075 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001076
1077 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel38ddf412008-09-11 10:38:32 +02001078 amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
1079 amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001080 amd_iommu_dev_table[devid].data[2] = domain->id;
1081
1082 amd_iommu_pd_table[devid] = domain;
1083 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1084
Chris Wright42a49f92009-06-15 15:42:00 +02001085 /*
1086 * We might boot into a crash-kernel here. The crashed kernel
1087 * left the caches in the IOMMU dirty. So we have to flush
1088 * here to evict all dirty stuff.
1089 */
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001090 iommu_queue_inv_dev_entry(iommu, devid);
Chris Wright42a49f92009-06-15 15:42:00 +02001091 iommu_flush_tlb_pde(iommu, domain->id);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001092}
1093
Joerg Roedel355bf552008-12-08 12:02:41 +01001094/*
1095 * Removes a device from a protection domain (unlocked)
1096 */
1097static void __detach_device(struct protection_domain *domain, u16 devid)
1098{
1099
1100 /* lock domain */
1101 spin_lock(&domain->lock);
1102
1103 /* remove domain from the lookup table */
1104 amd_iommu_pd_table[devid] = NULL;
1105
1106 /* remove entry from the device table seen by the hardware */
1107 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1108 amd_iommu_dev_table[devid].data[1] = 0;
1109 amd_iommu_dev_table[devid].data[2] = 0;
1110
1111 /* decrease reference counter */
1112 domain->dev_cnt -= 1;
1113
1114 /* ready */
1115 spin_unlock(&domain->lock);
1116}
1117
1118/*
1119 * Removes a device from a protection domain (with devtable_lock held)
1120 */
1121static void detach_device(struct protection_domain *domain, u16 devid)
1122{
1123 unsigned long flags;
1124
1125 /* lock device table */
1126 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1127 __detach_device(domain, devid);
1128 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1129}
Joerg Roedele275a2a2008-12-10 18:27:25 +01001130
1131static int device_change_notifier(struct notifier_block *nb,
1132 unsigned long action, void *data)
1133{
1134 struct device *dev = data;
1135 struct pci_dev *pdev = to_pci_dev(dev);
1136 u16 devid = calc_devid(pdev->bus->number, pdev->devfn);
1137 struct protection_domain *domain;
1138 struct dma_ops_domain *dma_domain;
1139 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001140 unsigned long flags;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001141
1142 if (devid > amd_iommu_last_bdf)
1143 goto out;
1144
1145 devid = amd_iommu_alias_table[devid];
1146
1147 iommu = amd_iommu_rlookup_table[devid];
1148 if (iommu == NULL)
1149 goto out;
1150
1151 domain = domain_for_device(devid);
1152
1153 if (domain && !dma_ops_domain(domain))
1154 WARN_ONCE(1, "AMD IOMMU WARNING: device %s already bound "
1155 "to a non-dma-ops domain\n", dev_name(dev));
1156
1157 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07001158 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedele275a2a2008-12-10 18:27:25 +01001159 if (!domain)
1160 goto out;
1161 detach_device(domain, devid);
1162 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001163 case BUS_NOTIFY_ADD_DEVICE:
1164 /* allocate a protection domain if a device is added */
1165 dma_domain = find_protection_domain(devid);
1166 if (dma_domain)
1167 goto out;
Joerg Roedeld9cfed92009-05-19 12:16:29 +02001168 dma_domain = dma_ops_domain_alloc(iommu);
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001169 if (!dma_domain)
1170 goto out;
1171 dma_domain->target_dev = devid;
1172
1173 spin_lock_irqsave(&iommu_pd_list_lock, flags);
1174 list_add_tail(&dma_domain->list, &iommu_pd_list);
1175 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
1176
1177 break;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001178 default:
1179 goto out;
1180 }
1181
1182 iommu_queue_inv_dev_entry(iommu, devid);
1183 iommu_completion_wait(iommu);
1184
1185out:
1186 return 0;
1187}
1188
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05301189static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01001190 .notifier_call = device_change_notifier,
1191};
Joerg Roedel355bf552008-12-08 12:02:41 +01001192
Joerg Roedel431b2a22008-07-11 17:14:22 +02001193/*****************************************************************************
1194 *
1195 * The next functions belong to the dma_ops mapping/unmapping code.
1196 *
1197 *****************************************************************************/
1198
1199/*
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001200 * This function checks if the driver got a valid device from the caller to
1201 * avoid dereferencing invalid pointers.
1202 */
1203static bool check_device(struct device *dev)
1204{
1205 if (!dev || !dev->dma_mask)
1206 return false;
1207
1208 return true;
1209}
1210
1211/*
Joerg Roedelbd60b732008-09-11 10:24:48 +02001212 * In this function the list of preallocated protection domains is traversed to
1213 * find the domain for a specific device
1214 */
1215static struct dma_ops_domain *find_protection_domain(u16 devid)
1216{
1217 struct dma_ops_domain *entry, *ret = NULL;
1218 unsigned long flags;
1219
1220 if (list_empty(&iommu_pd_list))
1221 return NULL;
1222
1223 spin_lock_irqsave(&iommu_pd_list_lock, flags);
1224
1225 list_for_each_entry(entry, &iommu_pd_list, list) {
1226 if (entry->target_dev == devid) {
1227 ret = entry;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001228 break;
1229 }
1230 }
1231
1232 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
1233
1234 return ret;
1235}
1236
1237/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001238 * In the dma_ops path we only have the struct device. This function
1239 * finds the corresponding IOMMU, the protection domain and the
1240 * requestor id for a given device.
1241 * If the device is not yet associated with a domain this is also done
1242 * in this function.
1243 */
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001244static int get_device_resources(struct device *dev,
1245 struct amd_iommu **iommu,
1246 struct protection_domain **domain,
1247 u16 *bdf)
1248{
1249 struct dma_ops_domain *dma_dom;
1250 struct pci_dev *pcidev;
1251 u16 _bdf;
1252
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001253 *iommu = NULL;
1254 *domain = NULL;
1255 *bdf = 0xffff;
1256
1257 if (dev->bus != &pci_bus_type)
1258 return 0;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001259
1260 pcidev = to_pci_dev(dev);
Joerg Roedeld591b0a2008-07-11 17:14:35 +02001261 _bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001262
Joerg Roedel431b2a22008-07-11 17:14:22 +02001263 /* device not translated by any IOMMU in the system? */
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001264 if (_bdf > amd_iommu_last_bdf)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001265 return 0;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001266
1267 *bdf = amd_iommu_alias_table[_bdf];
1268
1269 *iommu = amd_iommu_rlookup_table[*bdf];
1270 if (*iommu == NULL)
1271 return 0;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001272 *domain = domain_for_device(*bdf);
1273 if (*domain == NULL) {
Joerg Roedelbd60b732008-09-11 10:24:48 +02001274 dma_dom = find_protection_domain(*bdf);
1275 if (!dma_dom)
1276 dma_dom = (*iommu)->default_dom;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001277 *domain = &dma_dom->domain;
Joerg Roedelf1179dc2008-12-10 14:39:51 +01001278 attach_device(*iommu, *domain, *bdf);
Joerg Roedele9a22a12009-06-09 12:00:37 +02001279 DUMP_printk("Using protection domain %d for device %s\n",
1280 (*domain)->id, dev_name(dev));
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001281 }
1282
Joerg Roedelf91ba192008-11-25 12:56:12 +01001283 if (domain_for_device(_bdf) == NULL)
Joerg Roedelf1179dc2008-12-10 14:39:51 +01001284 attach_device(*iommu, *domain, _bdf);
Joerg Roedelf91ba192008-11-25 12:56:12 +01001285
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001286 return 1;
1287}
1288
Joerg Roedel431b2a22008-07-11 17:14:22 +02001289/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02001290 * If the pte_page is not yet allocated this function is called
1291 */
1292static u64* alloc_pte(struct protection_domain *dom,
1293 unsigned long address, u64 **pte_page, gfp_t gfp)
1294{
1295 u64 *pte, *page;
1296
1297 pte = &dom->pt_root[IOMMU_PTE_L2_INDEX(address)];
1298
1299 if (!IOMMU_PTE_PRESENT(*pte)) {
1300 page = (u64 *)get_zeroed_page(gfp);
1301 if (!page)
1302 return NULL;
1303 *pte = IOMMU_L2_PDE(virt_to_phys(page));
1304 }
1305
1306 pte = IOMMU_PTE_PAGE(*pte);
1307 pte = &pte[IOMMU_PTE_L1_INDEX(address)];
1308
1309 if (!IOMMU_PTE_PRESENT(*pte)) {
1310 page = (u64 *)get_zeroed_page(gfp);
1311 if (!page)
1312 return NULL;
1313 *pte = IOMMU_L1_PDE(virt_to_phys(page));
1314 }
1315
1316 pte = IOMMU_PTE_PAGE(*pte);
1317
1318 if (pte_page)
1319 *pte_page = pte;
1320
1321 pte = &pte[IOMMU_PTE_L0_INDEX(address)];
1322
1323 return pte;
1324}
1325
1326/*
1327 * This function fetches the PTE for a given address in the aperture
1328 */
1329static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
1330 unsigned long address)
1331{
Joerg Roedel384de722009-05-15 12:30:05 +02001332 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02001333 u64 *pte, *pte_page;
1334
Joerg Roedel384de722009-05-15 12:30:05 +02001335 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1336 if (!aperture)
1337 return NULL;
1338
1339 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02001340 if (!pte) {
1341 pte = alloc_pte(&dom->domain, address, &pte_page, GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02001342 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
1343 } else
1344 pte += IOMMU_PTE_L0_INDEX(address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001345
1346 return pte;
1347}
1348
1349/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001350 * This is the generic map function. It maps one 4kb page at paddr to
1351 * the given address in the DMA address space for the domain.
1352 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02001353static dma_addr_t dma_ops_domain_map(struct amd_iommu *iommu,
1354 struct dma_ops_domain *dom,
1355 unsigned long address,
1356 phys_addr_t paddr,
1357 int direction)
1358{
1359 u64 *pte, __pte;
1360
1361 WARN_ON(address > dom->aperture_size);
1362
1363 paddr &= PAGE_MASK;
1364
Joerg Roedel8bda3092009-05-12 12:02:46 +02001365 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02001366 if (!pte)
1367 return bad_dma_address;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001368
1369 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
1370
1371 if (direction == DMA_TO_DEVICE)
1372 __pte |= IOMMU_PTE_IR;
1373 else if (direction == DMA_FROM_DEVICE)
1374 __pte |= IOMMU_PTE_IW;
1375 else if (direction == DMA_BIDIRECTIONAL)
1376 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
1377
1378 WARN_ON(*pte);
1379
1380 *pte = __pte;
1381
1382 return (dma_addr_t)address;
1383}
1384
Joerg Roedel431b2a22008-07-11 17:14:22 +02001385/*
1386 * The generic unmapping function for on page in the DMA address space.
1387 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02001388static void dma_ops_domain_unmap(struct amd_iommu *iommu,
1389 struct dma_ops_domain *dom,
1390 unsigned long address)
1391{
Joerg Roedel384de722009-05-15 12:30:05 +02001392 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001393 u64 *pte;
1394
1395 if (address >= dom->aperture_size)
1396 return;
1397
Joerg Roedel384de722009-05-15 12:30:05 +02001398 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1399 if (!aperture)
1400 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001401
Joerg Roedel384de722009-05-15 12:30:05 +02001402 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
1403 if (!pte)
1404 return;
1405
Joerg Roedelcb76c322008-06-26 21:28:00 +02001406 pte += IOMMU_PTE_L0_INDEX(address);
1407
1408 WARN_ON(!*pte);
1409
1410 *pte = 0ULL;
1411}
1412
Joerg Roedel431b2a22008-07-11 17:14:22 +02001413/*
1414 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01001415 * contiguous memory region into DMA address space. It is used by all
1416 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001417 * Must be called with the domain lock held.
1418 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02001419static dma_addr_t __map_single(struct device *dev,
1420 struct amd_iommu *iommu,
1421 struct dma_ops_domain *dma_dom,
1422 phys_addr_t paddr,
1423 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001424 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001425 bool align,
1426 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02001427{
1428 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02001429 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001430 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001431 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001432 int i;
1433
Joerg Roedele3c449f2008-10-15 22:02:11 -07001434 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001435 paddr &= PAGE_MASK;
1436
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01001437 INC_STATS_COUNTER(total_map_requests);
1438
Joerg Roedelc1858972008-12-12 15:42:39 +01001439 if (pages > 1)
1440 INC_STATS_COUNTER(cross_page);
1441
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001442 if (align)
1443 align_mask = (1UL << get_order(size)) - 1;
1444
Joerg Roedel11b83882009-05-19 10:23:15 +02001445retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02001446 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
1447 dma_mask);
Joerg Roedel11b83882009-05-19 10:23:15 +02001448 if (unlikely(address == bad_dma_address)) {
1449 /*
1450 * setting next_address here will let the address
1451 * allocator only scan the new allocated range in the
1452 * first run. This is a small optimization.
1453 */
1454 dma_dom->next_address = dma_dom->aperture_size;
1455
1456 if (alloc_new_range(iommu, dma_dom, false, GFP_ATOMIC))
1457 goto out;
1458
1459 /*
1460 * aperture was sucessfully enlarged by 128 MB, try
1461 * allocation again
1462 */
1463 goto retry;
1464 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02001465
1466 start = address;
1467 for (i = 0; i < pages; ++i) {
Joerg Roedel53812c12009-05-12 12:17:38 +02001468 ret = dma_ops_domain_map(iommu, dma_dom, start, paddr, dir);
1469 if (ret == bad_dma_address)
1470 goto out_unmap;
1471
Joerg Roedelcb76c322008-06-26 21:28:00 +02001472 paddr += PAGE_SIZE;
1473 start += PAGE_SIZE;
1474 }
1475 address += offset;
1476
Joerg Roedel5774f7c2008-12-12 15:57:30 +01001477 ADD_STATS_COUNTER(alloced_io_mem, size);
1478
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09001479 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel1c655772008-09-04 18:40:05 +02001480 iommu_flush_tlb(iommu, dma_dom->domain.id);
1481 dma_dom->need_flush = false;
1482 } else if (unlikely(iommu_has_npcache(iommu)))
Joerg Roedel270cab242008-09-04 15:49:46 +02001483 iommu_flush_pages(iommu, dma_dom->domain.id, address, size);
1484
Joerg Roedelcb76c322008-06-26 21:28:00 +02001485out:
1486 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02001487
1488out_unmap:
1489
1490 for (--i; i >= 0; --i) {
1491 start -= PAGE_SIZE;
1492 dma_ops_domain_unmap(iommu, dma_dom, start);
1493 }
1494
1495 dma_ops_free_addresses(dma_dom, address, pages);
1496
1497 return bad_dma_address;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001498}
1499
Joerg Roedel431b2a22008-07-11 17:14:22 +02001500/*
1501 * Does the reverse of the __map_single function. Must be called with
1502 * the domain lock held too
1503 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02001504static void __unmap_single(struct amd_iommu *iommu,
1505 struct dma_ops_domain *dma_dom,
1506 dma_addr_t dma_addr,
1507 size_t size,
1508 int dir)
1509{
1510 dma_addr_t i, start;
1511 unsigned int pages;
1512
Joerg Roedelb8d99052008-12-08 14:40:26 +01001513 if ((dma_addr == bad_dma_address) ||
1514 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02001515 return;
1516
Joerg Roedele3c449f2008-10-15 22:02:11 -07001517 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001518 dma_addr &= PAGE_MASK;
1519 start = dma_addr;
1520
1521 for (i = 0; i < pages; ++i) {
1522 dma_ops_domain_unmap(iommu, dma_dom, start);
1523 start += PAGE_SIZE;
1524 }
1525
Joerg Roedel5774f7c2008-12-12 15:57:30 +01001526 SUB_STATS_COUNTER(alloced_io_mem, size);
1527
Joerg Roedelcb76c322008-06-26 21:28:00 +02001528 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02001529
Joerg Roedel80be3082008-11-06 14:59:05 +01001530 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel1c655772008-09-04 18:40:05 +02001531 iommu_flush_pages(iommu, dma_dom->domain.id, dma_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01001532 dma_dom->need_flush = false;
1533 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02001534}
1535
Joerg Roedel431b2a22008-07-11 17:14:22 +02001536/*
1537 * The exported map_single function for dma_ops.
1538 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09001539static dma_addr_t map_page(struct device *dev, struct page *page,
1540 unsigned long offset, size_t size,
1541 enum dma_data_direction dir,
1542 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02001543{
1544 unsigned long flags;
1545 struct amd_iommu *iommu;
1546 struct protection_domain *domain;
1547 u16 devid;
1548 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02001549 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09001550 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02001551
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01001552 INC_STATS_COUNTER(cnt_map_single);
1553
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001554 if (!check_device(dev))
1555 return bad_dma_address;
1556
Joerg Roedel832a90c2008-09-18 15:54:23 +02001557 dma_mask = *dev->dma_mask;
Joerg Roedel4da70b92008-06-26 21:28:01 +02001558
1559 get_device_resources(dev, &iommu, &domain, &devid);
1560
1561 if (iommu == NULL || domain == NULL)
Joerg Roedel431b2a22008-07-11 17:14:22 +02001562 /* device not handled by any AMD IOMMU */
Joerg Roedel4da70b92008-06-26 21:28:01 +02001563 return (dma_addr_t)paddr;
1564
Joerg Roedel5b28df62008-12-02 17:49:42 +01001565 if (!dma_ops_domain(domain))
1566 return bad_dma_address;
1567
Joerg Roedel4da70b92008-06-26 21:28:01 +02001568 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel832a90c2008-09-18 15:54:23 +02001569 addr = __map_single(dev, iommu, domain->priv, paddr, size, dir, false,
1570 dma_mask);
Joerg Roedel4da70b92008-06-26 21:28:01 +02001571 if (addr == bad_dma_address)
1572 goto out;
1573
Joerg Roedel09ee17e2008-12-03 12:19:27 +01001574 iommu_completion_wait(iommu);
Joerg Roedel4da70b92008-06-26 21:28:01 +02001575
1576out:
1577 spin_unlock_irqrestore(&domain->lock, flags);
1578
1579 return addr;
1580}
1581
Joerg Roedel431b2a22008-07-11 17:14:22 +02001582/*
1583 * The exported unmap_single function for dma_ops.
1584 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09001585static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
1586 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02001587{
1588 unsigned long flags;
1589 struct amd_iommu *iommu;
1590 struct protection_domain *domain;
1591 u16 devid;
1592
Joerg Roedel146a6912008-12-12 15:07:12 +01001593 INC_STATS_COUNTER(cnt_unmap_single);
1594
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001595 if (!check_device(dev) ||
1596 !get_device_resources(dev, &iommu, &domain, &devid))
Joerg Roedel431b2a22008-07-11 17:14:22 +02001597 /* device not handled by any AMD IOMMU */
Joerg Roedel4da70b92008-06-26 21:28:01 +02001598 return;
1599
Joerg Roedel5b28df62008-12-02 17:49:42 +01001600 if (!dma_ops_domain(domain))
1601 return;
1602
Joerg Roedel4da70b92008-06-26 21:28:01 +02001603 spin_lock_irqsave(&domain->lock, flags);
1604
1605 __unmap_single(iommu, domain->priv, dma_addr, size, dir);
1606
Joerg Roedel09ee17e2008-12-03 12:19:27 +01001607 iommu_completion_wait(iommu);
Joerg Roedel4da70b92008-06-26 21:28:01 +02001608
1609 spin_unlock_irqrestore(&domain->lock, flags);
1610}
1611
Joerg Roedel431b2a22008-07-11 17:14:22 +02001612/*
1613 * This is a special map_sg function which is used if we should map a
1614 * device which is not handled by an AMD IOMMU in the system.
1615 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02001616static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
1617 int nelems, int dir)
1618{
1619 struct scatterlist *s;
1620 int i;
1621
1622 for_each_sg(sglist, s, nelems, i) {
1623 s->dma_address = (dma_addr_t)sg_phys(s);
1624 s->dma_length = s->length;
1625 }
1626
1627 return nelems;
1628}
1629
Joerg Roedel431b2a22008-07-11 17:14:22 +02001630/*
1631 * The exported map_sg function for dma_ops (handles scatter-gather
1632 * lists).
1633 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02001634static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09001635 int nelems, enum dma_data_direction dir,
1636 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02001637{
1638 unsigned long flags;
1639 struct amd_iommu *iommu;
1640 struct protection_domain *domain;
1641 u16 devid;
1642 int i;
1643 struct scatterlist *s;
1644 phys_addr_t paddr;
1645 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02001646 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02001647
Joerg Roedeld03f0672008-12-12 15:09:48 +01001648 INC_STATS_COUNTER(cnt_map_sg);
1649
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001650 if (!check_device(dev))
1651 return 0;
1652
Joerg Roedel832a90c2008-09-18 15:54:23 +02001653 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02001654
1655 get_device_resources(dev, &iommu, &domain, &devid);
1656
1657 if (!iommu || !domain)
1658 return map_sg_no_iommu(dev, sglist, nelems, dir);
1659
Joerg Roedel5b28df62008-12-02 17:49:42 +01001660 if (!dma_ops_domain(domain))
1661 return 0;
1662
Joerg Roedel65b050a2008-06-26 21:28:02 +02001663 spin_lock_irqsave(&domain->lock, flags);
1664
1665 for_each_sg(sglist, s, nelems, i) {
1666 paddr = sg_phys(s);
1667
1668 s->dma_address = __map_single(dev, iommu, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001669 paddr, s->length, dir, false,
1670 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02001671
1672 if (s->dma_address) {
1673 s->dma_length = s->length;
1674 mapped_elems++;
1675 } else
1676 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02001677 }
1678
Joerg Roedel09ee17e2008-12-03 12:19:27 +01001679 iommu_completion_wait(iommu);
Joerg Roedel65b050a2008-06-26 21:28:02 +02001680
1681out:
1682 spin_unlock_irqrestore(&domain->lock, flags);
1683
1684 return mapped_elems;
1685unmap:
1686 for_each_sg(sglist, s, mapped_elems, i) {
1687 if (s->dma_address)
1688 __unmap_single(iommu, domain->priv, s->dma_address,
1689 s->dma_length, dir);
1690 s->dma_address = s->dma_length = 0;
1691 }
1692
1693 mapped_elems = 0;
1694
1695 goto out;
1696}
1697
Joerg Roedel431b2a22008-07-11 17:14:22 +02001698/*
1699 * The exported map_sg function for dma_ops (handles scatter-gather
1700 * lists).
1701 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02001702static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09001703 int nelems, enum dma_data_direction dir,
1704 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02001705{
1706 unsigned long flags;
1707 struct amd_iommu *iommu;
1708 struct protection_domain *domain;
1709 struct scatterlist *s;
1710 u16 devid;
1711 int i;
1712
Joerg Roedel55877a62008-12-12 15:12:14 +01001713 INC_STATS_COUNTER(cnt_unmap_sg);
1714
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001715 if (!check_device(dev) ||
1716 !get_device_resources(dev, &iommu, &domain, &devid))
Joerg Roedel65b050a2008-06-26 21:28:02 +02001717 return;
1718
Joerg Roedel5b28df62008-12-02 17:49:42 +01001719 if (!dma_ops_domain(domain))
1720 return;
1721
Joerg Roedel65b050a2008-06-26 21:28:02 +02001722 spin_lock_irqsave(&domain->lock, flags);
1723
1724 for_each_sg(sglist, s, nelems, i) {
1725 __unmap_single(iommu, domain->priv, s->dma_address,
1726 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02001727 s->dma_address = s->dma_length = 0;
1728 }
1729
Joerg Roedel09ee17e2008-12-03 12:19:27 +01001730 iommu_completion_wait(iommu);
Joerg Roedel65b050a2008-06-26 21:28:02 +02001731
1732 spin_unlock_irqrestore(&domain->lock, flags);
1733}
1734
Joerg Roedel431b2a22008-07-11 17:14:22 +02001735/*
1736 * The exported alloc_coherent function for dma_ops.
1737 */
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02001738static void *alloc_coherent(struct device *dev, size_t size,
1739 dma_addr_t *dma_addr, gfp_t flag)
1740{
1741 unsigned long flags;
1742 void *virt_addr;
1743 struct amd_iommu *iommu;
1744 struct protection_domain *domain;
1745 u16 devid;
1746 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02001747 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02001748
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01001749 INC_STATS_COUNTER(cnt_alloc_coherent);
1750
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001751 if (!check_device(dev))
1752 return NULL;
1753
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09001754 if (!get_device_resources(dev, &iommu, &domain, &devid))
1755 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
1756
Joerg Roedelc97ac532008-09-11 10:59:15 +02001757 flag |= __GFP_ZERO;
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02001758 virt_addr = (void *)__get_free_pages(flag, get_order(size));
1759 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05301760 return NULL;
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02001761
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02001762 paddr = virt_to_phys(virt_addr);
1763
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02001764 if (!iommu || !domain) {
1765 *dma_addr = (dma_addr_t)paddr;
1766 return virt_addr;
1767 }
1768
Joerg Roedel5b28df62008-12-02 17:49:42 +01001769 if (!dma_ops_domain(domain))
1770 goto out_free;
1771
Joerg Roedel832a90c2008-09-18 15:54:23 +02001772 if (!dma_mask)
1773 dma_mask = *dev->dma_mask;
1774
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02001775 spin_lock_irqsave(&domain->lock, flags);
1776
1777 *dma_addr = __map_single(dev, iommu, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001778 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02001779
Jiri Slaby367d04c2009-05-28 09:54:48 +02001780 if (*dma_addr == bad_dma_address) {
1781 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01001782 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02001783 }
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02001784
Joerg Roedel09ee17e2008-12-03 12:19:27 +01001785 iommu_completion_wait(iommu);
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02001786
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02001787 spin_unlock_irqrestore(&domain->lock, flags);
1788
1789 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01001790
1791out_free:
1792
1793 free_pages((unsigned long)virt_addr, get_order(size));
1794
1795 return NULL;
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02001796}
1797
Joerg Roedel431b2a22008-07-11 17:14:22 +02001798/*
1799 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001800 */
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02001801static void free_coherent(struct device *dev, size_t size,
1802 void *virt_addr, dma_addr_t dma_addr)
1803{
1804 unsigned long flags;
1805 struct amd_iommu *iommu;
1806 struct protection_domain *domain;
1807 u16 devid;
1808
Joerg Roedel5d31ee72008-12-12 15:16:38 +01001809 INC_STATS_COUNTER(cnt_free_coherent);
1810
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001811 if (!check_device(dev))
1812 return;
1813
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02001814 get_device_resources(dev, &iommu, &domain, &devid);
1815
1816 if (!iommu || !domain)
1817 goto free_mem;
1818
Joerg Roedel5b28df62008-12-02 17:49:42 +01001819 if (!dma_ops_domain(domain))
1820 goto free_mem;
1821
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02001822 spin_lock_irqsave(&domain->lock, flags);
1823
1824 __unmap_single(iommu, domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02001825
Joerg Roedel09ee17e2008-12-03 12:19:27 +01001826 iommu_completion_wait(iommu);
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02001827
1828 spin_unlock_irqrestore(&domain->lock, flags);
1829
1830free_mem:
1831 free_pages((unsigned long)virt_addr, get_order(size));
1832}
1833
Joerg Roedelc432f3d2008-06-26 21:28:04 +02001834/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02001835 * This function is called by the DMA layer to find out if we can handle a
1836 * particular device. It is part of the dma_ops.
1837 */
1838static int amd_iommu_dma_supported(struct device *dev, u64 mask)
1839{
1840 u16 bdf;
1841 struct pci_dev *pcidev;
1842
1843 /* No device or no PCI device */
1844 if (!dev || dev->bus != &pci_bus_type)
1845 return 0;
1846
1847 pcidev = to_pci_dev(dev);
1848
1849 bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
1850
1851 /* Out of our scope? */
1852 if (bdf > amd_iommu_last_bdf)
1853 return 0;
1854
1855 return 1;
1856}
1857
1858/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001859 * The function for pre-allocating protection domains.
1860 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02001861 * If the driver core informs the DMA layer if a driver grabs a device
1862 * we don't need to preallocate the protection domains anymore.
1863 * For now we have to.
1864 */
Jaswinder Singh Rajput0e93dd82008-12-29 21:45:22 +05301865static void prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02001866{
1867 struct pci_dev *dev = NULL;
1868 struct dma_ops_domain *dma_dom;
1869 struct amd_iommu *iommu;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02001870 u16 devid;
1871
1872 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
Joerg Roedeledcb34d2008-12-10 20:01:45 +01001873 devid = calc_devid(dev->bus->number, dev->devfn);
Joerg Roedel3a61ec32008-07-25 13:07:50 +02001874 if (devid > amd_iommu_last_bdf)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02001875 continue;
1876 devid = amd_iommu_alias_table[devid];
1877 if (domain_for_device(devid))
1878 continue;
1879 iommu = amd_iommu_rlookup_table[devid];
1880 if (!iommu)
1881 continue;
Joerg Roedeld9cfed92009-05-19 12:16:29 +02001882 dma_dom = dma_ops_domain_alloc(iommu);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02001883 if (!dma_dom)
1884 continue;
1885 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02001886 dma_dom->target_dev = devid;
1887
1888 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02001889 }
1890}
1891
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09001892static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedel6631ee92008-06-26 21:28:05 +02001893 .alloc_coherent = alloc_coherent,
1894 .free_coherent = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09001895 .map_page = map_page,
1896 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02001897 .map_sg = map_sg,
1898 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02001899 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02001900};
1901
Joerg Roedel431b2a22008-07-11 17:14:22 +02001902/*
1903 * The function which clues the AMD IOMMU driver into dma_ops.
1904 */
Joerg Roedel6631ee92008-06-26 21:28:05 +02001905int __init amd_iommu_init_dma_ops(void)
1906{
1907 struct amd_iommu *iommu;
Joerg Roedel6631ee92008-06-26 21:28:05 +02001908 int ret;
1909
Joerg Roedel431b2a22008-07-11 17:14:22 +02001910 /*
1911 * first allocate a default protection domain for every IOMMU we
1912 * found in the system. Devices not assigned to any other
1913 * protection domain will be assigned to the default one.
1914 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02001915 for_each_iommu(iommu) {
Joerg Roedeld9cfed92009-05-19 12:16:29 +02001916 iommu->default_dom = dma_ops_domain_alloc(iommu);
Joerg Roedel6631ee92008-06-26 21:28:05 +02001917 if (iommu->default_dom == NULL)
1918 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01001919 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02001920 ret = iommu_init_unity_mappings(iommu);
1921 if (ret)
1922 goto free_domains;
1923 }
1924
Joerg Roedel431b2a22008-07-11 17:14:22 +02001925 /*
1926 * If device isolation is enabled, pre-allocate the protection
1927 * domains for each device.
1928 */
Joerg Roedel6631ee92008-06-26 21:28:05 +02001929 if (amd_iommu_isolate)
1930 prealloc_protection_domains();
1931
1932 iommu_detected = 1;
1933 force_iommu = 1;
1934 bad_dma_address = 0;
Ingo Molnar92af4e22008-06-27 10:48:16 +02001935#ifdef CONFIG_GART_IOMMU
Joerg Roedel6631ee92008-06-26 21:28:05 +02001936 gart_iommu_aperture_disabled = 1;
1937 gart_iommu_aperture = 0;
Ingo Molnar92af4e22008-06-27 10:48:16 +02001938#endif
Joerg Roedel6631ee92008-06-26 21:28:05 +02001939
Joerg Roedel431b2a22008-07-11 17:14:22 +02001940 /* Make the driver finally visible to the drivers */
Joerg Roedel6631ee92008-06-26 21:28:05 +02001941 dma_ops = &amd_iommu_dma_ops;
1942
Joerg Roedel26961ef2008-12-03 17:00:17 +01001943 register_iommu(&amd_iommu_ops);
Joerg Roedel26961ef2008-12-03 17:00:17 +01001944
Joerg Roedele275a2a2008-12-10 18:27:25 +01001945 bus_register_notifier(&pci_bus_type, &device_nb);
1946
Joerg Roedel7f265082008-12-12 13:50:21 +01001947 amd_iommu_stats_init();
1948
Joerg Roedel6631ee92008-06-26 21:28:05 +02001949 return 0;
1950
1951free_domains:
1952
Joerg Roedel3bd22172009-05-04 15:06:20 +02001953 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02001954 if (iommu->default_dom)
1955 dma_ops_domain_free(iommu->default_dom);
1956 }
1957
1958 return ret;
1959}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01001960
1961/*****************************************************************************
1962 *
1963 * The following functions belong to the exported interface of AMD IOMMU
1964 *
1965 * This interface allows access to lower level functions of the IOMMU
1966 * like protection domain handling and assignement of devices to domains
1967 * which is not possible with the dma_ops interface.
1968 *
1969 *****************************************************************************/
1970
Joerg Roedel6d98cd82008-12-08 12:05:55 +01001971static void cleanup_domain(struct protection_domain *domain)
1972{
1973 unsigned long flags;
1974 u16 devid;
1975
1976 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1977
1978 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
1979 if (amd_iommu_pd_table[devid] == domain)
1980 __detach_device(domain, devid);
1981
1982 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1983}
1984
Joerg Roedelc156e342008-12-02 18:13:27 +01001985static int amd_iommu_domain_init(struct iommu_domain *dom)
1986{
1987 struct protection_domain *domain;
1988
1989 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
1990 if (!domain)
1991 return -ENOMEM;
1992
1993 spin_lock_init(&domain->lock);
1994 domain->mode = PAGE_MODE_3_LEVEL;
1995 domain->id = domain_id_alloc();
1996 if (!domain->id)
1997 goto out_free;
1998 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
1999 if (!domain->pt_root)
2000 goto out_free;
2001
2002 dom->priv = domain;
2003
2004 return 0;
2005
2006out_free:
2007 kfree(domain);
2008
2009 return -ENOMEM;
2010}
2011
Joerg Roedel98383fc2008-12-02 18:34:12 +01002012static void amd_iommu_domain_destroy(struct iommu_domain *dom)
2013{
2014 struct protection_domain *domain = dom->priv;
2015
2016 if (!domain)
2017 return;
2018
2019 if (domain->dev_cnt > 0)
2020 cleanup_domain(domain);
2021
2022 BUG_ON(domain->dev_cnt != 0);
2023
2024 free_pagetable(domain);
2025
2026 domain_id_free(domain->id);
2027
2028 kfree(domain);
2029
2030 dom->priv = NULL;
2031}
2032
Joerg Roedel684f2882008-12-08 12:07:44 +01002033static void amd_iommu_detach_device(struct iommu_domain *dom,
2034 struct device *dev)
2035{
2036 struct protection_domain *domain = dom->priv;
2037 struct amd_iommu *iommu;
2038 struct pci_dev *pdev;
2039 u16 devid;
2040
2041 if (dev->bus != &pci_bus_type)
2042 return;
2043
2044 pdev = to_pci_dev(dev);
2045
2046 devid = calc_devid(pdev->bus->number, pdev->devfn);
2047
2048 if (devid > 0)
2049 detach_device(domain, devid);
2050
2051 iommu = amd_iommu_rlookup_table[devid];
2052 if (!iommu)
2053 return;
2054
2055 iommu_queue_inv_dev_entry(iommu, devid);
2056 iommu_completion_wait(iommu);
2057}
2058
Joerg Roedel01106062008-12-02 19:34:11 +01002059static int amd_iommu_attach_device(struct iommu_domain *dom,
2060 struct device *dev)
2061{
2062 struct protection_domain *domain = dom->priv;
2063 struct protection_domain *old_domain;
2064 struct amd_iommu *iommu;
2065 struct pci_dev *pdev;
2066 u16 devid;
2067
2068 if (dev->bus != &pci_bus_type)
2069 return -EINVAL;
2070
2071 pdev = to_pci_dev(dev);
2072
2073 devid = calc_devid(pdev->bus->number, pdev->devfn);
2074
2075 if (devid >= amd_iommu_last_bdf ||
2076 devid != amd_iommu_alias_table[devid])
2077 return -EINVAL;
2078
2079 iommu = amd_iommu_rlookup_table[devid];
2080 if (!iommu)
2081 return -EINVAL;
2082
2083 old_domain = domain_for_device(devid);
2084 if (old_domain)
Joerg Roedel71ff3bc2009-06-08 13:47:33 -07002085 detach_device(old_domain, devid);
Joerg Roedel01106062008-12-02 19:34:11 +01002086
2087 attach_device(iommu, domain, devid);
2088
2089 iommu_completion_wait(iommu);
2090
2091 return 0;
2092}
2093
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002094static int amd_iommu_map_range(struct iommu_domain *dom,
2095 unsigned long iova, phys_addr_t paddr,
2096 size_t size, int iommu_prot)
2097{
2098 struct protection_domain *domain = dom->priv;
2099 unsigned long i, npages = iommu_num_pages(paddr, size, PAGE_SIZE);
2100 int prot = 0;
2101 int ret;
2102
2103 if (iommu_prot & IOMMU_READ)
2104 prot |= IOMMU_PROT_IR;
2105 if (iommu_prot & IOMMU_WRITE)
2106 prot |= IOMMU_PROT_IW;
2107
2108 iova &= PAGE_MASK;
2109 paddr &= PAGE_MASK;
2110
2111 for (i = 0; i < npages; ++i) {
2112 ret = iommu_map_page(domain, iova, paddr, prot);
2113 if (ret)
2114 return ret;
2115
2116 iova += PAGE_SIZE;
2117 paddr += PAGE_SIZE;
2118 }
2119
2120 return 0;
2121}
2122
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002123static void amd_iommu_unmap_range(struct iommu_domain *dom,
2124 unsigned long iova, size_t size)
2125{
2126
2127 struct protection_domain *domain = dom->priv;
2128 unsigned long i, npages = iommu_num_pages(iova, size, PAGE_SIZE);
2129
2130 iova &= PAGE_MASK;
2131
2132 for (i = 0; i < npages; ++i) {
2133 iommu_unmap_page(domain, iova);
2134 iova += PAGE_SIZE;
2135 }
2136
2137 iommu_flush_domain(domain->id);
2138}
2139
Joerg Roedel645c4c82008-12-02 20:05:50 +01002140static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2141 unsigned long iova)
2142{
2143 struct protection_domain *domain = dom->priv;
2144 unsigned long offset = iova & ~PAGE_MASK;
2145 phys_addr_t paddr;
2146 u64 *pte;
2147
2148 pte = &domain->pt_root[IOMMU_PTE_L2_INDEX(iova)];
2149
2150 if (!IOMMU_PTE_PRESENT(*pte))
2151 return 0;
2152
2153 pte = IOMMU_PTE_PAGE(*pte);
2154 pte = &pte[IOMMU_PTE_L1_INDEX(iova)];
2155
2156 if (!IOMMU_PTE_PRESENT(*pte))
2157 return 0;
2158
2159 pte = IOMMU_PTE_PAGE(*pte);
2160 pte = &pte[IOMMU_PTE_L0_INDEX(iova)];
2161
2162 if (!IOMMU_PTE_PRESENT(*pte))
2163 return 0;
2164
2165 paddr = *pte & IOMMU_PAGE_MASK;
2166 paddr |= offset;
2167
2168 return paddr;
2169}
2170
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002171static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
2172 unsigned long cap)
2173{
2174 return 0;
2175}
2176
Joerg Roedel26961ef2008-12-03 17:00:17 +01002177static struct iommu_ops amd_iommu_ops = {
2178 .domain_init = amd_iommu_domain_init,
2179 .domain_destroy = amd_iommu_domain_destroy,
2180 .attach_dev = amd_iommu_attach_device,
2181 .detach_dev = amd_iommu_detach_device,
2182 .map = amd_iommu_map_range,
2183 .unmap = amd_iommu_unmap_range,
2184 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002185 .domain_has_cap = amd_iommu_domain_has_cap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002186};
2187