blob: 112412d733abea86b8340d337cf1fb320c1a29ca [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>
25#include <linux/iommu-helper.h>
Joerg Roedelc156e342008-12-02 18:13:27 +010026#ifdef CONFIG_IOMMU_API
27#include <linux/iommu.h>
28#endif
Joerg Roedelb6c02712008-06-26 21:27:53 +020029#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090030#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010031#include <asm/gart.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020032#include <asm/amd_iommu_types.h>
Joerg Roedelc6da9922008-06-26 21:28:06 +020033#include <asm/amd_iommu.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020034
35#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
36
Joerg Roedel136f78a2008-07-11 17:14:27 +020037#define EXIT_LOOP_COUNT 10000000
38
Joerg Roedelb6c02712008-06-26 21:27:53 +020039static DEFINE_RWLOCK(amd_iommu_devtable_lock);
40
Joerg Roedelbd60b732008-09-11 10:24:48 +020041/* A list of preallocated protection domains */
42static LIST_HEAD(iommu_pd_list);
43static DEFINE_SPINLOCK(iommu_pd_list_lock);
44
Joerg Roedel26961ef2008-12-03 17:00:17 +010045#ifdef CONFIG_IOMMU_API
46static struct iommu_ops amd_iommu_ops;
47#endif
48
Joerg Roedel431b2a22008-07-11 17:14:22 +020049/*
50 * general struct to manage commands send to an IOMMU
51 */
Joerg Roedeld6449532008-07-11 17:14:28 +020052struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +020053 u32 data[4];
54};
55
Joerg Roedelbd0e5212008-06-26 21:27:56 +020056static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
57 struct unity_map_entry *e);
Joerg Roedele275a2a2008-12-10 18:27:25 +010058static struct dma_ops_domain *find_protection_domain(u16 devid);
59
Joerg Roedelbd0e5212008-06-26 21:27:56 +020060
Joerg Roedel7f265082008-12-12 13:50:21 +010061#ifdef CONFIG_AMD_IOMMU_STATS
62
63/*
64 * Initialization code for statistics collection
65 */
66
Joerg Roedelda49f6d2008-12-12 14:59:58 +010067DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +010068DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +010069DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +010070DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +010071DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +010072DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +010073DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelda49f6d2008-12-12 14:59:58 +010074
Joerg Roedel7f265082008-12-12 13:50:21 +010075static struct dentry *stats_dir;
76static struct dentry *de_isolate;
77static struct dentry *de_fflush;
78
79static void amd_iommu_stats_add(struct __iommu_counter *cnt)
80{
81 if (stats_dir == NULL)
82 return;
83
84 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
85 &cnt->value);
86}
87
88static void amd_iommu_stats_init(void)
89{
90 stats_dir = debugfs_create_dir("amd-iommu", NULL);
91 if (stats_dir == NULL)
92 return;
93
94 de_isolate = debugfs_create_bool("isolation", 0444, stats_dir,
95 (u32 *)&amd_iommu_isolate);
96
97 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
98 (u32 *)&amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +010099
100 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100101 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100102 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100103 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100104 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100105 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100106 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedel7f265082008-12-12 13:50:21 +0100107}
108
109#endif
110
Joerg Roedel431b2a22008-07-11 17:14:22 +0200111/* returns !0 if the IOMMU is caching non-present entries in its TLB */
Joerg Roedel4da70b92008-06-26 21:28:01 +0200112static int iommu_has_npcache(struct amd_iommu *iommu)
113{
Joerg Roedelae9b9402008-10-30 17:43:57 +0100114 return iommu->cap & (1UL << IOMMU_CAP_NPCACHE);
Joerg Roedel4da70b92008-06-26 21:28:01 +0200115}
116
Joerg Roedel431b2a22008-07-11 17:14:22 +0200117/****************************************************************************
118 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200119 * Interrupt handling functions
120 *
121 ****************************************************************************/
122
Joerg Roedel90008ee2008-09-09 16:41:05 +0200123static void iommu_print_event(void *__evt)
124{
125 u32 *event = __evt;
126 int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
127 int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
128 int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
129 int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
130 u64 address = (u64)(((u64)event[3]) << 32) | event[2];
131
132 printk(KERN_ERR "AMD IOMMU: Event logged [");
133
134 switch (type) {
135 case EVENT_TYPE_ILL_DEV:
136 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
137 "address=0x%016llx flags=0x%04x]\n",
138 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
139 address, flags);
140 break;
141 case EVENT_TYPE_IO_FAULT:
142 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
143 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
144 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
145 domid, address, flags);
146 break;
147 case EVENT_TYPE_DEV_TAB_ERR:
148 printk("DEV_TAB_HARDWARE_ERROR 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_PAGE_TAB_ERR:
154 printk("PAGE_TAB_HARDWARE_ERROR 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_ILL_CMD:
160 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
161 break;
162 case EVENT_TYPE_CMD_HARD_ERR:
163 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
164 "flags=0x%04x]\n", address, flags);
165 break;
166 case EVENT_TYPE_IOTLB_INV_TO:
167 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
168 "address=0x%016llx]\n",
169 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
170 address);
171 break;
172 case EVENT_TYPE_INV_DEV_REQ:
173 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
174 "address=0x%016llx flags=0x%04x]\n",
175 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
176 address, flags);
177 break;
178 default:
179 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
180 }
181}
182
183static void iommu_poll_events(struct amd_iommu *iommu)
184{
185 u32 head, tail;
186 unsigned long flags;
187
188 spin_lock_irqsave(&iommu->lock, flags);
189
190 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
191 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
192
193 while (head != tail) {
194 iommu_print_event(iommu->evt_buf + head);
195 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
196 }
197
198 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
199
200 spin_unlock_irqrestore(&iommu->lock, flags);
201}
202
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200203irqreturn_t amd_iommu_int_handler(int irq, void *data)
204{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200205 struct amd_iommu *iommu;
206
207 list_for_each_entry(iommu, &amd_iommu_list, list)
208 iommu_poll_events(iommu);
209
210 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200211}
212
213/****************************************************************************
214 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200215 * IOMMU command queuing functions
216 *
217 ****************************************************************************/
218
219/*
220 * Writes the command to the IOMMUs command buffer and informs the
221 * hardware about the new command. Must be called with iommu->lock held.
222 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200223static int __iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200224{
225 u32 tail, head;
226 u8 *target;
227
228 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Jiri Kosina8a7c5ef2008-08-19 02:13:55 +0200229 target = iommu->cmd_buf + tail;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200230 memcpy_toio(target, cmd, sizeof(*cmd));
231 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
232 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
233 if (tail == head)
234 return -ENOMEM;
235 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
236
237 return 0;
238}
239
Joerg Roedel431b2a22008-07-11 17:14:22 +0200240/*
241 * General queuing function for commands. Takes iommu->lock and calls
242 * __iommu_queue_command().
243 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200244static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200245{
246 unsigned long flags;
247 int ret;
248
249 spin_lock_irqsave(&iommu->lock, flags);
250 ret = __iommu_queue_command(iommu, cmd);
Joerg Roedel09ee17e2008-12-03 12:19:27 +0100251 if (!ret)
Joerg Roedel0cfd7aa2008-12-10 19:58:00 +0100252 iommu->need_sync = true;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200253 spin_unlock_irqrestore(&iommu->lock, flags);
254
255 return ret;
256}
257
Joerg Roedel431b2a22008-07-11 17:14:22 +0200258/*
Joerg Roedel8d201962008-12-02 20:34:41 +0100259 * This function waits until an IOMMU has completed a completion
260 * wait command
Joerg Roedel431b2a22008-07-11 17:14:22 +0200261 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100262static void __iommu_wait_for_completion(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200263{
Joerg Roedel8d201962008-12-02 20:34:41 +0100264 int ready = 0;
Joerg Roedel519c31b2008-08-14 19:55:15 +0200265 unsigned status = 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100266 unsigned long i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200267
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100268 INC_STATS_COUNTER(compl_wait);
269
Joerg Roedel136f78a2008-07-11 17:14:27 +0200270 while (!ready && (i < EXIT_LOOP_COUNT)) {
271 ++i;
Joerg Roedel519c31b2008-08-14 19:55:15 +0200272 /* wait for the bit to become one */
273 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
274 ready = status & MMIO_STATUS_COM_WAIT_INT_MASK;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200275 }
276
Joerg Roedel519c31b2008-08-14 19:55:15 +0200277 /* set bit back to zero */
278 status &= ~MMIO_STATUS_COM_WAIT_INT_MASK;
279 writel(status, iommu->mmio_base + MMIO_STATUS_OFFSET);
280
Joerg Roedel84df8172008-12-17 16:36:44 +0100281 if (unlikely(i == EXIT_LOOP_COUNT))
282 panic("AMD IOMMU: Completion wait loop failed\n");
Joerg Roedel8d201962008-12-02 20:34:41 +0100283}
284
285/*
286 * This function queues a completion wait command into the command
287 * buffer of an IOMMU
288 */
289static int __iommu_completion_wait(struct amd_iommu *iommu)
290{
291 struct iommu_cmd cmd;
292
293 memset(&cmd, 0, sizeof(cmd));
294 cmd.data[0] = CMD_COMPL_WAIT_INT_MASK;
295 CMD_SET_TYPE(&cmd, CMD_COMPL_WAIT);
296
297 return __iommu_queue_command(iommu, &cmd);
298}
299
300/*
301 * This function is called whenever we need to ensure that the IOMMU has
302 * completed execution of all commands we sent. It sends a
303 * COMPLETION_WAIT command and waits for it to finish. The IOMMU informs
304 * us about that by writing a value to a physical address we pass with
305 * the command.
306 */
307static int iommu_completion_wait(struct amd_iommu *iommu)
308{
309 int ret = 0;
310 unsigned long flags;
311
312 spin_lock_irqsave(&iommu->lock, flags);
313
314 if (!iommu->need_sync)
315 goto out;
316
317 ret = __iommu_completion_wait(iommu);
318
Joerg Roedel0cfd7aa2008-12-10 19:58:00 +0100319 iommu->need_sync = false;
Joerg Roedel8d201962008-12-02 20:34:41 +0100320
321 if (ret)
322 goto out;
323
324 __iommu_wait_for_completion(iommu);
Joerg Roedel84df8172008-12-17 16:36:44 +0100325
Joerg Roedel7e4f88d2008-09-17 14:19:15 +0200326out:
327 spin_unlock_irqrestore(&iommu->lock, flags);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200328
329 return 0;
330}
331
Joerg Roedel431b2a22008-07-11 17:14:22 +0200332/*
333 * Command send function for invalidating a device table entry
334 */
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200335static int iommu_queue_inv_dev_entry(struct amd_iommu *iommu, u16 devid)
336{
Joerg Roedeld6449532008-07-11 17:14:28 +0200337 struct iommu_cmd cmd;
Joerg Roedelee2fa742008-09-17 13:47:25 +0200338 int ret;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200339
340 BUG_ON(iommu == NULL);
341
342 memset(&cmd, 0, sizeof(cmd));
343 CMD_SET_TYPE(&cmd, CMD_INV_DEV_ENTRY);
344 cmd.data[0] = devid;
345
Joerg Roedelee2fa742008-09-17 13:47:25 +0200346 ret = iommu_queue_command(iommu, &cmd);
347
Joerg Roedelee2fa742008-09-17 13:47:25 +0200348 return ret;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200349}
350
Joerg Roedel237b6f32008-12-02 20:54:37 +0100351static void __iommu_build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
352 u16 domid, int pde, int s)
353{
354 memset(cmd, 0, sizeof(*cmd));
355 address &= PAGE_MASK;
356 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
357 cmd->data[1] |= domid;
358 cmd->data[2] = lower_32_bits(address);
359 cmd->data[3] = upper_32_bits(address);
360 if (s) /* size bit - we flush more than one 4kb page */
361 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
362 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
363 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
364}
365
Joerg Roedel431b2a22008-07-11 17:14:22 +0200366/*
367 * Generic command send function for invalidaing TLB entries
368 */
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200369static int iommu_queue_inv_iommu_pages(struct amd_iommu *iommu,
370 u64 address, u16 domid, int pde, int s)
371{
Joerg Roedeld6449532008-07-11 17:14:28 +0200372 struct iommu_cmd cmd;
Joerg Roedelee2fa742008-09-17 13:47:25 +0200373 int ret;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200374
Joerg Roedel237b6f32008-12-02 20:54:37 +0100375 __iommu_build_inv_iommu_pages(&cmd, address, domid, pde, s);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200376
Joerg Roedelee2fa742008-09-17 13:47:25 +0200377 ret = iommu_queue_command(iommu, &cmd);
378
Joerg Roedelee2fa742008-09-17 13:47:25 +0200379 return ret;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200380}
381
Joerg Roedel431b2a22008-07-11 17:14:22 +0200382/*
383 * TLB invalidation function which is called from the mapping functions.
384 * It invalidates a single PTE if the range to flush is within a single
385 * page. Otherwise it flushes the whole TLB of the IOMMU.
386 */
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200387static int iommu_flush_pages(struct amd_iommu *iommu, u16 domid,
388 u64 address, size_t size)
389{
Joerg Roedel999ba412008-07-03 19:35:08 +0200390 int s = 0;
Joerg Roedele3c449f2008-10-15 22:02:11 -0700391 unsigned pages = iommu_num_pages(address, size, PAGE_SIZE);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200392
393 address &= PAGE_MASK;
394
Joerg Roedel999ba412008-07-03 19:35:08 +0200395 if (pages > 1) {
396 /*
397 * If we have to flush more than one page, flush all
398 * TLB entries for this domain
399 */
400 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
401 s = 1;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200402 }
403
Joerg Roedel999ba412008-07-03 19:35:08 +0200404 iommu_queue_inv_iommu_pages(iommu, address, domid, 0, s);
405
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200406 return 0;
407}
Joerg Roedelb6c02712008-06-26 21:27:53 +0200408
Joerg Roedel1c655772008-09-04 18:40:05 +0200409/* Flush the whole IO/TLB for a given protection domain */
410static void iommu_flush_tlb(struct amd_iommu *iommu, u16 domid)
411{
412 u64 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
413
414 iommu_queue_inv_iommu_pages(iommu, address, domid, 0, 1);
415}
416
Joerg Roedel43f49602008-12-02 21:01:12 +0100417#ifdef CONFIG_IOMMU_API
418/*
419 * This function is used to flush the IO/TLB for a given protection domain
420 * on every IOMMU in the system
421 */
422static void iommu_flush_domain(u16 domid)
423{
424 unsigned long flags;
425 struct amd_iommu *iommu;
426 struct iommu_cmd cmd;
427
428 __iommu_build_inv_iommu_pages(&cmd, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
429 domid, 1, 1);
430
431 list_for_each_entry(iommu, &amd_iommu_list, list) {
432 spin_lock_irqsave(&iommu->lock, flags);
433 __iommu_queue_command(iommu, &cmd);
434 __iommu_completion_wait(iommu);
435 __iommu_wait_for_completion(iommu);
436 spin_unlock_irqrestore(&iommu->lock, flags);
437 }
438}
439#endif
440
Joerg Roedel431b2a22008-07-11 17:14:22 +0200441/****************************************************************************
442 *
443 * The functions below are used the create the page table mappings for
444 * unity mapped regions.
445 *
446 ****************************************************************************/
447
448/*
449 * Generic mapping functions. It maps a physical address into a DMA
450 * address space. It allocates the page table pages if necessary.
451 * In the future it can be extended to a generic mapping function
452 * supporting all features of AMD IOMMU page tables like level skipping
453 * and full 64 bit address spaces.
454 */
Joerg Roedel38e817f2008-12-02 17:27:52 +0100455static int iommu_map_page(struct protection_domain *dom,
456 unsigned long bus_addr,
457 unsigned long phys_addr,
458 int prot)
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200459{
460 u64 __pte, *pte, *page;
461
462 bus_addr = PAGE_ALIGN(bus_addr);
Joerg Roedelbb9d4ff2008-12-04 15:59:48 +0100463 phys_addr = PAGE_ALIGN(phys_addr);
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200464
465 /* only support 512GB address spaces for now */
466 if (bus_addr > IOMMU_MAP_SIZE_L3 || !(prot & IOMMU_PROT_MASK))
467 return -EINVAL;
468
469 pte = &dom->pt_root[IOMMU_PTE_L2_INDEX(bus_addr)];
470
471 if (!IOMMU_PTE_PRESENT(*pte)) {
472 page = (u64 *)get_zeroed_page(GFP_KERNEL);
473 if (!page)
474 return -ENOMEM;
475 *pte = IOMMU_L2_PDE(virt_to_phys(page));
476 }
477
478 pte = IOMMU_PTE_PAGE(*pte);
479 pte = &pte[IOMMU_PTE_L1_INDEX(bus_addr)];
480
481 if (!IOMMU_PTE_PRESENT(*pte)) {
482 page = (u64 *)get_zeroed_page(GFP_KERNEL);
483 if (!page)
484 return -ENOMEM;
485 *pte = IOMMU_L1_PDE(virt_to_phys(page));
486 }
487
488 pte = IOMMU_PTE_PAGE(*pte);
489 pte = &pte[IOMMU_PTE_L0_INDEX(bus_addr)];
490
491 if (IOMMU_PTE_PRESENT(*pte))
492 return -EBUSY;
493
494 __pte = phys_addr | IOMMU_PTE_P;
495 if (prot & IOMMU_PROT_IR)
496 __pte |= IOMMU_PTE_IR;
497 if (prot & IOMMU_PROT_IW)
498 __pte |= IOMMU_PTE_IW;
499
500 *pte = __pte;
501
502 return 0;
503}
504
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100505#ifdef CONFIG_IOMMU_API
506static void iommu_unmap_page(struct protection_domain *dom,
507 unsigned long bus_addr)
508{
509 u64 *pte;
510
511 pte = &dom->pt_root[IOMMU_PTE_L2_INDEX(bus_addr)];
512
513 if (!IOMMU_PTE_PRESENT(*pte))
514 return;
515
516 pte = IOMMU_PTE_PAGE(*pte);
517 pte = &pte[IOMMU_PTE_L1_INDEX(bus_addr)];
518
519 if (!IOMMU_PTE_PRESENT(*pte))
520 return;
521
522 pte = IOMMU_PTE_PAGE(*pte);
523 pte = &pte[IOMMU_PTE_L1_INDEX(bus_addr)];
524
525 *pte = 0;
526}
527#endif
528
Joerg Roedel431b2a22008-07-11 17:14:22 +0200529/*
530 * This function checks if a specific unity mapping entry is needed for
531 * this specific IOMMU.
532 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200533static int iommu_for_unity_map(struct amd_iommu *iommu,
534 struct unity_map_entry *entry)
535{
536 u16 bdf, i;
537
538 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
539 bdf = amd_iommu_alias_table[i];
540 if (amd_iommu_rlookup_table[bdf] == iommu)
541 return 1;
542 }
543
544 return 0;
545}
546
Joerg Roedel431b2a22008-07-11 17:14:22 +0200547/*
548 * Init the unity mappings for a specific IOMMU in the system
549 *
550 * Basically iterates over all unity mapping entries and applies them to
551 * the default domain DMA of that IOMMU if necessary.
552 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200553static int iommu_init_unity_mappings(struct amd_iommu *iommu)
554{
555 struct unity_map_entry *entry;
556 int ret;
557
558 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
559 if (!iommu_for_unity_map(iommu, entry))
560 continue;
561 ret = dma_ops_unity_map(iommu->default_dom, entry);
562 if (ret)
563 return ret;
564 }
565
566 return 0;
567}
568
Joerg Roedel431b2a22008-07-11 17:14:22 +0200569/*
570 * This function actually applies the mapping to the page table of the
571 * dma_ops domain.
572 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200573static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
574 struct unity_map_entry *e)
575{
576 u64 addr;
577 int ret;
578
579 for (addr = e->address_start; addr < e->address_end;
580 addr += PAGE_SIZE) {
Joerg Roedel38e817f2008-12-02 17:27:52 +0100581 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot);
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200582 if (ret)
583 return ret;
584 /*
585 * if unity mapping is in aperture range mark the page
586 * as allocated in the aperture
587 */
588 if (addr < dma_dom->aperture_size)
589 __set_bit(addr >> PAGE_SHIFT, dma_dom->bitmap);
590 }
591
592 return 0;
593}
594
Joerg Roedel431b2a22008-07-11 17:14:22 +0200595/*
596 * Inits the unity mappings required for a specific device
597 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200598static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
599 u16 devid)
600{
601 struct unity_map_entry *e;
602 int ret;
603
604 list_for_each_entry(e, &amd_iommu_unity_map, list) {
605 if (!(devid >= e->devid_start && devid <= e->devid_end))
606 continue;
607 ret = dma_ops_unity_map(dma_dom, e);
608 if (ret)
609 return ret;
610 }
611
612 return 0;
613}
614
Joerg Roedel431b2a22008-07-11 17:14:22 +0200615/****************************************************************************
616 *
617 * The next functions belong to the address allocator for the dma_ops
618 * interface functions. They work like the allocators in the other IOMMU
619 * drivers. Its basically a bitmap which marks the allocated pages in
620 * the aperture. Maybe it could be enhanced in the future to a more
621 * efficient allocator.
622 *
623 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +0200624
Joerg Roedel431b2a22008-07-11 17:14:22 +0200625/*
626 * The address allocator core function.
627 *
628 * called with domain->lock held
629 */
Joerg Roedeld3086442008-06-26 21:27:57 +0200630static unsigned long dma_ops_alloc_addresses(struct device *dev,
631 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +0200632 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +0200633 unsigned long align_mask,
634 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +0200635{
FUJITA Tomonori40becd82008-09-29 00:06:36 +0900636 unsigned long limit;
Joerg Roedeld3086442008-06-26 21:27:57 +0200637 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +0200638 unsigned long boundary_size;
639
640 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
641 PAGE_SIZE) >> PAGE_SHIFT;
FUJITA Tomonori40becd82008-09-29 00:06:36 +0900642 limit = iommu_device_max_index(dom->aperture_size >> PAGE_SHIFT, 0,
643 dma_mask >> PAGE_SHIFT);
Joerg Roedeld3086442008-06-26 21:27:57 +0200644
Joerg Roedel1c655772008-09-04 18:40:05 +0200645 if (dom->next_bit >= limit) {
Joerg Roedeld3086442008-06-26 21:27:57 +0200646 dom->next_bit = 0;
Joerg Roedel1c655772008-09-04 18:40:05 +0200647 dom->need_flush = true;
648 }
Joerg Roedeld3086442008-06-26 21:27:57 +0200649
650 address = iommu_area_alloc(dom->bitmap, limit, dom->next_bit, pages,
Joerg Roedel6d4f3432008-09-04 19:18:02 +0200651 0 , boundary_size, align_mask);
Joerg Roedel1c655772008-09-04 18:40:05 +0200652 if (address == -1) {
Joerg Roedeld3086442008-06-26 21:27:57 +0200653 address = iommu_area_alloc(dom->bitmap, limit, 0, pages,
Joerg Roedel6d4f3432008-09-04 19:18:02 +0200654 0, boundary_size, align_mask);
Joerg Roedel1c655772008-09-04 18:40:05 +0200655 dom->need_flush = true;
656 }
Joerg Roedeld3086442008-06-26 21:27:57 +0200657
658 if (likely(address != -1)) {
Joerg Roedeld3086442008-06-26 21:27:57 +0200659 dom->next_bit = address + pages;
660 address <<= PAGE_SHIFT;
661 } else
662 address = bad_dma_address;
663
664 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
665
666 return address;
667}
668
Joerg Roedel431b2a22008-07-11 17:14:22 +0200669/*
670 * The address free function.
671 *
672 * called with domain->lock held
673 */
Joerg Roedeld3086442008-06-26 21:27:57 +0200674static void dma_ops_free_addresses(struct dma_ops_domain *dom,
675 unsigned long address,
676 unsigned int pages)
677{
678 address >>= PAGE_SHIFT;
679 iommu_area_free(dom->bitmap, address, pages);
Joerg Roedel80be3082008-11-06 14:59:05 +0100680
Joerg Roedel8501c452008-11-17 19:11:46 +0100681 if (address >= dom->next_bit)
Joerg Roedel80be3082008-11-06 14:59:05 +0100682 dom->need_flush = true;
Joerg Roedeld3086442008-06-26 21:27:57 +0200683}
684
Joerg Roedel431b2a22008-07-11 17:14:22 +0200685/****************************************************************************
686 *
687 * The next functions belong to the domain allocation. A domain is
688 * allocated for every IOMMU as the default domain. If device isolation
689 * is enabled, every device get its own domain. The most important thing
690 * about domains is the page table mapping the DMA address space they
691 * contain.
692 *
693 ****************************************************************************/
694
Joerg Roedelec487d12008-06-26 21:27:58 +0200695static u16 domain_id_alloc(void)
696{
697 unsigned long flags;
698 int id;
699
700 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
701 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
702 BUG_ON(id == 0);
703 if (id > 0 && id < MAX_DOMAIN_ID)
704 __set_bit(id, amd_iommu_pd_alloc_bitmap);
705 else
706 id = 0;
707 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
708
709 return id;
710}
711
Joerg Roedela2acfb72008-12-02 18:28:53 +0100712#ifdef CONFIG_IOMMU_API
713static void domain_id_free(int id)
714{
715 unsigned long flags;
716
717 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
718 if (id > 0 && id < MAX_DOMAIN_ID)
719 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
720 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
721}
722#endif
723
Joerg Roedel431b2a22008-07-11 17:14:22 +0200724/*
725 * Used to reserve address ranges in the aperture (e.g. for exclusion
726 * ranges.
727 */
Joerg Roedelec487d12008-06-26 21:27:58 +0200728static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
729 unsigned long start_page,
730 unsigned int pages)
731{
732 unsigned int last_page = dom->aperture_size >> PAGE_SHIFT;
733
734 if (start_page + pages > last_page)
735 pages = last_page - start_page;
736
FUJITA Tomonorid26dbc52008-09-22 22:35:07 +0900737 iommu_area_reserve(dom->bitmap, start_page, pages);
Joerg Roedelec487d12008-06-26 21:27:58 +0200738}
739
Joerg Roedel86db2e52008-12-02 18:20:21 +0100740static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +0200741{
742 int i, j;
743 u64 *p1, *p2, *p3;
744
Joerg Roedel86db2e52008-12-02 18:20:21 +0100745 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +0200746
747 if (!p1)
748 return;
749
750 for (i = 0; i < 512; ++i) {
751 if (!IOMMU_PTE_PRESENT(p1[i]))
752 continue;
753
754 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +0100755 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +0200756 if (!IOMMU_PTE_PRESENT(p2[j]))
757 continue;
758 p3 = IOMMU_PTE_PAGE(p2[j]);
759 free_page((unsigned long)p3);
760 }
761
762 free_page((unsigned long)p2);
763 }
764
765 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +0100766
767 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +0200768}
769
Joerg Roedel431b2a22008-07-11 17:14:22 +0200770/*
771 * Free a domain, only used if something went wrong in the
772 * allocation path and we need to free an already allocated page table
773 */
Joerg Roedelec487d12008-06-26 21:27:58 +0200774static void dma_ops_domain_free(struct dma_ops_domain *dom)
775{
776 if (!dom)
777 return;
778
Joerg Roedel86db2e52008-12-02 18:20:21 +0100779 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +0200780
781 kfree(dom->pte_pages);
782
783 kfree(dom->bitmap);
784
785 kfree(dom);
786}
787
Joerg Roedel431b2a22008-07-11 17:14:22 +0200788/*
789 * Allocates a new protection domain usable for the dma_ops functions.
790 * It also intializes the page table and the address allocator data
791 * structures required for the dma_ops interface
792 */
Joerg Roedelec487d12008-06-26 21:27:58 +0200793static struct dma_ops_domain *dma_ops_domain_alloc(struct amd_iommu *iommu,
794 unsigned order)
795{
796 struct dma_ops_domain *dma_dom;
797 unsigned i, num_pte_pages;
798 u64 *l2_pde;
799 u64 address;
800
801 /*
802 * Currently the DMA aperture must be between 32 MB and 1GB in size
803 */
804 if ((order < 25) || (order > 30))
805 return NULL;
806
807 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
808 if (!dma_dom)
809 return NULL;
810
811 spin_lock_init(&dma_dom->domain.lock);
812
813 dma_dom->domain.id = domain_id_alloc();
814 if (dma_dom->domain.id == 0)
815 goto free_dma_dom;
816 dma_dom->domain.mode = PAGE_MODE_3_LEVEL;
817 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +0100818 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +0200819 dma_dom->domain.priv = dma_dom;
820 if (!dma_dom->domain.pt_root)
821 goto free_dma_dom;
822 dma_dom->aperture_size = (1ULL << order);
823 dma_dom->bitmap = kzalloc(dma_dom->aperture_size / (PAGE_SIZE * 8),
824 GFP_KERNEL);
825 if (!dma_dom->bitmap)
826 goto free_dma_dom;
827 /*
828 * mark the first page as allocated so we never return 0 as
829 * a valid dma-address. So we can use 0 as error value
830 */
831 dma_dom->bitmap[0] = 1;
832 dma_dom->next_bit = 0;
833
Joerg Roedel1c655772008-09-04 18:40:05 +0200834 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +0200835 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +0200836
Joerg Roedel431b2a22008-07-11 17:14:22 +0200837 /* Intialize the exclusion range if necessary */
Joerg Roedelec487d12008-06-26 21:27:58 +0200838 if (iommu->exclusion_start &&
839 iommu->exclusion_start < dma_dom->aperture_size) {
840 unsigned long startpage = iommu->exclusion_start >> PAGE_SHIFT;
Joerg Roedele3c449f2008-10-15 22:02:11 -0700841 int pages = iommu_num_pages(iommu->exclusion_start,
842 iommu->exclusion_length,
843 PAGE_SIZE);
Joerg Roedelec487d12008-06-26 21:27:58 +0200844 dma_ops_reserve_addresses(dma_dom, startpage, pages);
845 }
846
Joerg Roedel431b2a22008-07-11 17:14:22 +0200847 /*
848 * At the last step, build the page tables so we don't need to
849 * allocate page table pages in the dma_ops mapping/unmapping
850 * path.
851 */
Joerg Roedelec487d12008-06-26 21:27:58 +0200852 num_pte_pages = dma_dom->aperture_size / (PAGE_SIZE * 512);
853 dma_dom->pte_pages = kzalloc(num_pte_pages * sizeof(void *),
854 GFP_KERNEL);
855 if (!dma_dom->pte_pages)
856 goto free_dma_dom;
857
858 l2_pde = (u64 *)get_zeroed_page(GFP_KERNEL);
859 if (l2_pde == NULL)
860 goto free_dma_dom;
861
862 dma_dom->domain.pt_root[0] = IOMMU_L2_PDE(virt_to_phys(l2_pde));
863
864 for (i = 0; i < num_pte_pages; ++i) {
865 dma_dom->pte_pages[i] = (u64 *)get_zeroed_page(GFP_KERNEL);
866 if (!dma_dom->pte_pages[i])
867 goto free_dma_dom;
868 address = virt_to_phys(dma_dom->pte_pages[i]);
869 l2_pde[i] = IOMMU_L1_PDE(address);
870 }
871
872 return dma_dom;
873
874free_dma_dom:
875 dma_ops_domain_free(dma_dom);
876
877 return NULL;
878}
879
Joerg Roedel431b2a22008-07-11 17:14:22 +0200880/*
Joerg Roedel5b28df62008-12-02 17:49:42 +0100881 * little helper function to check whether a given protection domain is a
882 * dma_ops domain
883 */
884static bool dma_ops_domain(struct protection_domain *domain)
885{
886 return domain->flags & PD_DMA_OPS_MASK;
887}
888
889/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200890 * Find out the protection domain structure for a given PCI device. This
891 * will give us the pointer to the page table root for example.
892 */
Joerg Roedelb20ac0d2008-06-26 21:27:59 +0200893static struct protection_domain *domain_for_device(u16 devid)
894{
895 struct protection_domain *dom;
896 unsigned long flags;
897
898 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
899 dom = amd_iommu_pd_table[devid];
900 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
901
902 return dom;
903}
904
Joerg Roedel431b2a22008-07-11 17:14:22 +0200905/*
906 * If a device is not yet associated with a domain, this function does
907 * assigns it visible for the hardware
908 */
Joerg Roedelf1179dc2008-12-10 14:39:51 +0100909static void attach_device(struct amd_iommu *iommu,
910 struct protection_domain *domain,
911 u16 devid)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +0200912{
913 unsigned long flags;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +0200914 u64 pte_root = virt_to_phys(domain->pt_root);
915
Joerg Roedel863c74e2008-12-02 17:56:36 +0100916 domain->dev_cnt += 1;
917
Joerg Roedel38ddf412008-09-11 10:38:32 +0200918 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
919 << DEV_ENTRY_MODE_SHIFT;
920 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +0200921
922 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel38ddf412008-09-11 10:38:32 +0200923 amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
924 amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +0200925 amd_iommu_dev_table[devid].data[2] = domain->id;
926
927 amd_iommu_pd_table[devid] = domain;
928 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
929
930 iommu_queue_inv_dev_entry(iommu, devid);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +0200931}
932
Joerg Roedel355bf552008-12-08 12:02:41 +0100933/*
934 * Removes a device from a protection domain (unlocked)
935 */
936static void __detach_device(struct protection_domain *domain, u16 devid)
937{
938
939 /* lock domain */
940 spin_lock(&domain->lock);
941
942 /* remove domain from the lookup table */
943 amd_iommu_pd_table[devid] = NULL;
944
945 /* remove entry from the device table seen by the hardware */
946 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
947 amd_iommu_dev_table[devid].data[1] = 0;
948 amd_iommu_dev_table[devid].data[2] = 0;
949
950 /* decrease reference counter */
951 domain->dev_cnt -= 1;
952
953 /* ready */
954 spin_unlock(&domain->lock);
955}
956
957/*
958 * Removes a device from a protection domain (with devtable_lock held)
959 */
960static void detach_device(struct protection_domain *domain, u16 devid)
961{
962 unsigned long flags;
963
964 /* lock device table */
965 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
966 __detach_device(domain, devid);
967 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
968}
Joerg Roedele275a2a2008-12-10 18:27:25 +0100969
970static int device_change_notifier(struct notifier_block *nb,
971 unsigned long action, void *data)
972{
973 struct device *dev = data;
974 struct pci_dev *pdev = to_pci_dev(dev);
975 u16 devid = calc_devid(pdev->bus->number, pdev->devfn);
976 struct protection_domain *domain;
977 struct dma_ops_domain *dma_domain;
978 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +0100979 int order = amd_iommu_aperture_order;
980 unsigned long flags;
Joerg Roedele275a2a2008-12-10 18:27:25 +0100981
982 if (devid > amd_iommu_last_bdf)
983 goto out;
984
985 devid = amd_iommu_alias_table[devid];
986
987 iommu = amd_iommu_rlookup_table[devid];
988 if (iommu == NULL)
989 goto out;
990
991 domain = domain_for_device(devid);
992
993 if (domain && !dma_ops_domain(domain))
994 WARN_ONCE(1, "AMD IOMMU WARNING: device %s already bound "
995 "to a non-dma-ops domain\n", dev_name(dev));
996
997 switch (action) {
998 case BUS_NOTIFY_BOUND_DRIVER:
999 if (domain)
1000 goto out;
1001 dma_domain = find_protection_domain(devid);
1002 if (!dma_domain)
1003 dma_domain = iommu->default_dom;
1004 attach_device(iommu, &dma_domain->domain, devid);
1005 printk(KERN_INFO "AMD IOMMU: Using protection domain %d for "
1006 "device %s\n", dma_domain->domain.id, dev_name(dev));
1007 break;
1008 case BUS_NOTIFY_UNBIND_DRIVER:
1009 if (!domain)
1010 goto out;
1011 detach_device(domain, devid);
1012 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001013 case BUS_NOTIFY_ADD_DEVICE:
1014 /* allocate a protection domain if a device is added */
1015 dma_domain = find_protection_domain(devid);
1016 if (dma_domain)
1017 goto out;
1018 dma_domain = dma_ops_domain_alloc(iommu, order);
1019 if (!dma_domain)
1020 goto out;
1021 dma_domain->target_dev = devid;
1022
1023 spin_lock_irqsave(&iommu_pd_list_lock, flags);
1024 list_add_tail(&dma_domain->list, &iommu_pd_list);
1025 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
1026
1027 break;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001028 default:
1029 goto out;
1030 }
1031
1032 iommu_queue_inv_dev_entry(iommu, devid);
1033 iommu_completion_wait(iommu);
1034
1035out:
1036 return 0;
1037}
1038
1039struct notifier_block device_nb = {
1040 .notifier_call = device_change_notifier,
1041};
Joerg Roedel355bf552008-12-08 12:02:41 +01001042
Joerg Roedel431b2a22008-07-11 17:14:22 +02001043/*****************************************************************************
1044 *
1045 * The next functions belong to the dma_ops mapping/unmapping code.
1046 *
1047 *****************************************************************************/
1048
1049/*
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001050 * This function checks if the driver got a valid device from the caller to
1051 * avoid dereferencing invalid pointers.
1052 */
1053static bool check_device(struct device *dev)
1054{
1055 if (!dev || !dev->dma_mask)
1056 return false;
1057
1058 return true;
1059}
1060
1061/*
Joerg Roedelbd60b732008-09-11 10:24:48 +02001062 * In this function the list of preallocated protection domains is traversed to
1063 * find the domain for a specific device
1064 */
1065static struct dma_ops_domain *find_protection_domain(u16 devid)
1066{
1067 struct dma_ops_domain *entry, *ret = NULL;
1068 unsigned long flags;
1069
1070 if (list_empty(&iommu_pd_list))
1071 return NULL;
1072
1073 spin_lock_irqsave(&iommu_pd_list_lock, flags);
1074
1075 list_for_each_entry(entry, &iommu_pd_list, list) {
1076 if (entry->target_dev == devid) {
1077 ret = entry;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001078 break;
1079 }
1080 }
1081
1082 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
1083
1084 return ret;
1085}
1086
1087/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001088 * In the dma_ops path we only have the struct device. This function
1089 * finds the corresponding IOMMU, the protection domain and the
1090 * requestor id for a given device.
1091 * If the device is not yet associated with a domain this is also done
1092 * in this function.
1093 */
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001094static int get_device_resources(struct device *dev,
1095 struct amd_iommu **iommu,
1096 struct protection_domain **domain,
1097 u16 *bdf)
1098{
1099 struct dma_ops_domain *dma_dom;
1100 struct pci_dev *pcidev;
1101 u16 _bdf;
1102
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001103 *iommu = NULL;
1104 *domain = NULL;
1105 *bdf = 0xffff;
1106
1107 if (dev->bus != &pci_bus_type)
1108 return 0;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001109
1110 pcidev = to_pci_dev(dev);
Joerg Roedeld591b0a2008-07-11 17:14:35 +02001111 _bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001112
Joerg Roedel431b2a22008-07-11 17:14:22 +02001113 /* device not translated by any IOMMU in the system? */
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001114 if (_bdf > amd_iommu_last_bdf)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001115 return 0;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001116
1117 *bdf = amd_iommu_alias_table[_bdf];
1118
1119 *iommu = amd_iommu_rlookup_table[*bdf];
1120 if (*iommu == NULL)
1121 return 0;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001122 *domain = domain_for_device(*bdf);
1123 if (*domain == NULL) {
Joerg Roedelbd60b732008-09-11 10:24:48 +02001124 dma_dom = find_protection_domain(*bdf);
1125 if (!dma_dom)
1126 dma_dom = (*iommu)->default_dom;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001127 *domain = &dma_dom->domain;
Joerg Roedelf1179dc2008-12-10 14:39:51 +01001128 attach_device(*iommu, *domain, *bdf);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001129 printk(KERN_INFO "AMD IOMMU: Using protection domain %d for "
Joerg Roedelab896722008-12-10 19:43:07 +01001130 "device %s\n", (*domain)->id, dev_name(dev));
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001131 }
1132
Joerg Roedelf91ba192008-11-25 12:56:12 +01001133 if (domain_for_device(_bdf) == NULL)
Joerg Roedelf1179dc2008-12-10 14:39:51 +01001134 attach_device(*iommu, *domain, _bdf);
Joerg Roedelf91ba192008-11-25 12:56:12 +01001135
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001136 return 1;
1137}
1138
Joerg Roedel431b2a22008-07-11 17:14:22 +02001139/*
1140 * This is the generic map function. It maps one 4kb page at paddr to
1141 * the given address in the DMA address space for the domain.
1142 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02001143static dma_addr_t dma_ops_domain_map(struct amd_iommu *iommu,
1144 struct dma_ops_domain *dom,
1145 unsigned long address,
1146 phys_addr_t paddr,
1147 int direction)
1148{
1149 u64 *pte, __pte;
1150
1151 WARN_ON(address > dom->aperture_size);
1152
1153 paddr &= PAGE_MASK;
1154
1155 pte = dom->pte_pages[IOMMU_PTE_L1_INDEX(address)];
1156 pte += IOMMU_PTE_L0_INDEX(address);
1157
1158 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
1159
1160 if (direction == DMA_TO_DEVICE)
1161 __pte |= IOMMU_PTE_IR;
1162 else if (direction == DMA_FROM_DEVICE)
1163 __pte |= IOMMU_PTE_IW;
1164 else if (direction == DMA_BIDIRECTIONAL)
1165 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
1166
1167 WARN_ON(*pte);
1168
1169 *pte = __pte;
1170
1171 return (dma_addr_t)address;
1172}
1173
Joerg Roedel431b2a22008-07-11 17:14:22 +02001174/*
1175 * The generic unmapping function for on page in the DMA address space.
1176 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02001177static void dma_ops_domain_unmap(struct amd_iommu *iommu,
1178 struct dma_ops_domain *dom,
1179 unsigned long address)
1180{
1181 u64 *pte;
1182
1183 if (address >= dom->aperture_size)
1184 return;
1185
Joerg Roedel8ad909c2008-12-08 14:37:20 +01001186 WARN_ON(address & ~PAGE_MASK || address >= dom->aperture_size);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001187
1188 pte = dom->pte_pages[IOMMU_PTE_L1_INDEX(address)];
1189 pte += IOMMU_PTE_L0_INDEX(address);
1190
1191 WARN_ON(!*pte);
1192
1193 *pte = 0ULL;
1194}
1195
Joerg Roedel431b2a22008-07-11 17:14:22 +02001196/*
1197 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01001198 * contiguous memory region into DMA address space. It is used by all
1199 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001200 * Must be called with the domain lock held.
1201 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02001202static dma_addr_t __map_single(struct device *dev,
1203 struct amd_iommu *iommu,
1204 struct dma_ops_domain *dma_dom,
1205 phys_addr_t paddr,
1206 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001207 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001208 bool align,
1209 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02001210{
1211 dma_addr_t offset = paddr & ~PAGE_MASK;
1212 dma_addr_t address, start;
1213 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001214 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001215 int i;
1216
Joerg Roedele3c449f2008-10-15 22:02:11 -07001217 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001218 paddr &= PAGE_MASK;
1219
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001220 if (align)
1221 align_mask = (1UL << get_order(size)) - 1;
1222
Joerg Roedel832a90c2008-09-18 15:54:23 +02001223 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
1224 dma_mask);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001225 if (unlikely(address == bad_dma_address))
1226 goto out;
1227
1228 start = address;
1229 for (i = 0; i < pages; ++i) {
1230 dma_ops_domain_map(iommu, dma_dom, start, paddr, dir);
1231 paddr += PAGE_SIZE;
1232 start += PAGE_SIZE;
1233 }
1234 address += offset;
1235
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09001236 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel1c655772008-09-04 18:40:05 +02001237 iommu_flush_tlb(iommu, dma_dom->domain.id);
1238 dma_dom->need_flush = false;
1239 } else if (unlikely(iommu_has_npcache(iommu)))
Joerg Roedel270cab242008-09-04 15:49:46 +02001240 iommu_flush_pages(iommu, dma_dom->domain.id, address, size);
1241
Joerg Roedelcb76c322008-06-26 21:28:00 +02001242out:
1243 return address;
1244}
1245
Joerg Roedel431b2a22008-07-11 17:14:22 +02001246/*
1247 * Does the reverse of the __map_single function. Must be called with
1248 * the domain lock held too
1249 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02001250static void __unmap_single(struct amd_iommu *iommu,
1251 struct dma_ops_domain *dma_dom,
1252 dma_addr_t dma_addr,
1253 size_t size,
1254 int dir)
1255{
1256 dma_addr_t i, start;
1257 unsigned int pages;
1258
Joerg Roedelb8d99052008-12-08 14:40:26 +01001259 if ((dma_addr == bad_dma_address) ||
1260 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02001261 return;
1262
Joerg Roedele3c449f2008-10-15 22:02:11 -07001263 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001264 dma_addr &= PAGE_MASK;
1265 start = dma_addr;
1266
1267 for (i = 0; i < pages; ++i) {
1268 dma_ops_domain_unmap(iommu, dma_dom, start);
1269 start += PAGE_SIZE;
1270 }
1271
1272 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02001273
Joerg Roedel80be3082008-11-06 14:59:05 +01001274 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel1c655772008-09-04 18:40:05 +02001275 iommu_flush_pages(iommu, dma_dom->domain.id, dma_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01001276 dma_dom->need_flush = false;
1277 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02001278}
1279
Joerg Roedel431b2a22008-07-11 17:14:22 +02001280/*
1281 * The exported map_single function for dma_ops.
1282 */
Joerg Roedel4da70b92008-06-26 21:28:01 +02001283static dma_addr_t map_single(struct device *dev, phys_addr_t paddr,
1284 size_t size, int dir)
1285{
1286 unsigned long flags;
1287 struct amd_iommu *iommu;
1288 struct protection_domain *domain;
1289 u16 devid;
1290 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02001291 u64 dma_mask;
Joerg Roedel4da70b92008-06-26 21:28:01 +02001292
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01001293 INC_STATS_COUNTER(cnt_map_single);
1294
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001295 if (!check_device(dev))
1296 return bad_dma_address;
1297
Joerg Roedel832a90c2008-09-18 15:54:23 +02001298 dma_mask = *dev->dma_mask;
Joerg Roedel4da70b92008-06-26 21:28:01 +02001299
1300 get_device_resources(dev, &iommu, &domain, &devid);
1301
1302 if (iommu == NULL || domain == NULL)
Joerg Roedel431b2a22008-07-11 17:14:22 +02001303 /* device not handled by any AMD IOMMU */
Joerg Roedel4da70b92008-06-26 21:28:01 +02001304 return (dma_addr_t)paddr;
1305
Joerg Roedel5b28df62008-12-02 17:49:42 +01001306 if (!dma_ops_domain(domain))
1307 return bad_dma_address;
1308
Joerg Roedel4da70b92008-06-26 21:28:01 +02001309 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel832a90c2008-09-18 15:54:23 +02001310 addr = __map_single(dev, iommu, domain->priv, paddr, size, dir, false,
1311 dma_mask);
Joerg Roedel4da70b92008-06-26 21:28:01 +02001312 if (addr == bad_dma_address)
1313 goto out;
1314
Joerg Roedel09ee17e2008-12-03 12:19:27 +01001315 iommu_completion_wait(iommu);
Joerg Roedel4da70b92008-06-26 21:28:01 +02001316
1317out:
1318 spin_unlock_irqrestore(&domain->lock, flags);
1319
1320 return addr;
1321}
1322
Joerg Roedel431b2a22008-07-11 17:14:22 +02001323/*
1324 * The exported unmap_single function for dma_ops.
1325 */
Joerg Roedel4da70b92008-06-26 21:28:01 +02001326static void unmap_single(struct device *dev, dma_addr_t dma_addr,
1327 size_t size, int dir)
1328{
1329 unsigned long flags;
1330 struct amd_iommu *iommu;
1331 struct protection_domain *domain;
1332 u16 devid;
1333
Joerg Roedel146a6912008-12-12 15:07:12 +01001334 INC_STATS_COUNTER(cnt_unmap_single);
1335
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001336 if (!check_device(dev) ||
1337 !get_device_resources(dev, &iommu, &domain, &devid))
Joerg Roedel431b2a22008-07-11 17:14:22 +02001338 /* device not handled by any AMD IOMMU */
Joerg Roedel4da70b92008-06-26 21:28:01 +02001339 return;
1340
Joerg Roedel5b28df62008-12-02 17:49:42 +01001341 if (!dma_ops_domain(domain))
1342 return;
1343
Joerg Roedel4da70b92008-06-26 21:28:01 +02001344 spin_lock_irqsave(&domain->lock, flags);
1345
1346 __unmap_single(iommu, domain->priv, dma_addr, size, dir);
1347
Joerg Roedel09ee17e2008-12-03 12:19:27 +01001348 iommu_completion_wait(iommu);
Joerg Roedel4da70b92008-06-26 21:28:01 +02001349
1350 spin_unlock_irqrestore(&domain->lock, flags);
1351}
1352
Joerg Roedel431b2a22008-07-11 17:14:22 +02001353/*
1354 * This is a special map_sg function which is used if we should map a
1355 * device which is not handled by an AMD IOMMU in the system.
1356 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02001357static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
1358 int nelems, int dir)
1359{
1360 struct scatterlist *s;
1361 int i;
1362
1363 for_each_sg(sglist, s, nelems, i) {
1364 s->dma_address = (dma_addr_t)sg_phys(s);
1365 s->dma_length = s->length;
1366 }
1367
1368 return nelems;
1369}
1370
Joerg Roedel431b2a22008-07-11 17:14:22 +02001371/*
1372 * The exported map_sg function for dma_ops (handles scatter-gather
1373 * lists).
1374 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02001375static int map_sg(struct device *dev, struct scatterlist *sglist,
1376 int nelems, int dir)
1377{
1378 unsigned long flags;
1379 struct amd_iommu *iommu;
1380 struct protection_domain *domain;
1381 u16 devid;
1382 int i;
1383 struct scatterlist *s;
1384 phys_addr_t paddr;
1385 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02001386 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02001387
Joerg Roedeld03f0672008-12-12 15:09:48 +01001388 INC_STATS_COUNTER(cnt_map_sg);
1389
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001390 if (!check_device(dev))
1391 return 0;
1392
Joerg Roedel832a90c2008-09-18 15:54:23 +02001393 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02001394
1395 get_device_resources(dev, &iommu, &domain, &devid);
1396
1397 if (!iommu || !domain)
1398 return map_sg_no_iommu(dev, sglist, nelems, dir);
1399
Joerg Roedel5b28df62008-12-02 17:49:42 +01001400 if (!dma_ops_domain(domain))
1401 return 0;
1402
Joerg Roedel65b050a2008-06-26 21:28:02 +02001403 spin_lock_irqsave(&domain->lock, flags);
1404
1405 for_each_sg(sglist, s, nelems, i) {
1406 paddr = sg_phys(s);
1407
1408 s->dma_address = __map_single(dev, iommu, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001409 paddr, s->length, dir, false,
1410 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02001411
1412 if (s->dma_address) {
1413 s->dma_length = s->length;
1414 mapped_elems++;
1415 } else
1416 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02001417 }
1418
Joerg Roedel09ee17e2008-12-03 12:19:27 +01001419 iommu_completion_wait(iommu);
Joerg Roedel65b050a2008-06-26 21:28:02 +02001420
1421out:
1422 spin_unlock_irqrestore(&domain->lock, flags);
1423
1424 return mapped_elems;
1425unmap:
1426 for_each_sg(sglist, s, mapped_elems, i) {
1427 if (s->dma_address)
1428 __unmap_single(iommu, domain->priv, s->dma_address,
1429 s->dma_length, dir);
1430 s->dma_address = s->dma_length = 0;
1431 }
1432
1433 mapped_elems = 0;
1434
1435 goto out;
1436}
1437
Joerg Roedel431b2a22008-07-11 17:14:22 +02001438/*
1439 * The exported map_sg function for dma_ops (handles scatter-gather
1440 * lists).
1441 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02001442static void unmap_sg(struct device *dev, struct scatterlist *sglist,
1443 int nelems, int dir)
1444{
1445 unsigned long flags;
1446 struct amd_iommu *iommu;
1447 struct protection_domain *domain;
1448 struct scatterlist *s;
1449 u16 devid;
1450 int i;
1451
Joerg Roedel55877a62008-12-12 15:12:14 +01001452 INC_STATS_COUNTER(cnt_unmap_sg);
1453
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001454 if (!check_device(dev) ||
1455 !get_device_resources(dev, &iommu, &domain, &devid))
Joerg Roedel65b050a2008-06-26 21:28:02 +02001456 return;
1457
Joerg Roedel5b28df62008-12-02 17:49:42 +01001458 if (!dma_ops_domain(domain))
1459 return;
1460
Joerg Roedel65b050a2008-06-26 21:28:02 +02001461 spin_lock_irqsave(&domain->lock, flags);
1462
1463 for_each_sg(sglist, s, nelems, i) {
1464 __unmap_single(iommu, domain->priv, s->dma_address,
1465 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02001466 s->dma_address = s->dma_length = 0;
1467 }
1468
Joerg Roedel09ee17e2008-12-03 12:19:27 +01001469 iommu_completion_wait(iommu);
Joerg Roedel65b050a2008-06-26 21:28:02 +02001470
1471 spin_unlock_irqrestore(&domain->lock, flags);
1472}
1473
Joerg Roedel431b2a22008-07-11 17:14:22 +02001474/*
1475 * The exported alloc_coherent function for dma_ops.
1476 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02001477static void *alloc_coherent(struct device *dev, size_t size,
1478 dma_addr_t *dma_addr, gfp_t flag)
1479{
1480 unsigned long flags;
1481 void *virt_addr;
1482 struct amd_iommu *iommu;
1483 struct protection_domain *domain;
1484 u16 devid;
1485 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02001486 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02001487
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01001488 INC_STATS_COUNTER(cnt_alloc_coherent);
1489
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001490 if (!check_device(dev))
1491 return NULL;
1492
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09001493 if (!get_device_resources(dev, &iommu, &domain, &devid))
1494 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
1495
Joerg Roedelc97ac532008-09-11 10:59:15 +02001496 flag |= __GFP_ZERO;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02001497 virt_addr = (void *)__get_free_pages(flag, get_order(size));
1498 if (!virt_addr)
1499 return 0;
1500
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02001501 paddr = virt_to_phys(virt_addr);
1502
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02001503 if (!iommu || !domain) {
1504 *dma_addr = (dma_addr_t)paddr;
1505 return virt_addr;
1506 }
1507
Joerg Roedel5b28df62008-12-02 17:49:42 +01001508 if (!dma_ops_domain(domain))
1509 goto out_free;
1510
Joerg Roedel832a90c2008-09-18 15:54:23 +02001511 if (!dma_mask)
1512 dma_mask = *dev->dma_mask;
1513
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02001514 spin_lock_irqsave(&domain->lock, flags);
1515
1516 *dma_addr = __map_single(dev, iommu, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001517 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02001518
Joerg Roedel5b28df62008-12-02 17:49:42 +01001519 if (*dma_addr == bad_dma_address)
1520 goto out_free;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02001521
Joerg Roedel09ee17e2008-12-03 12:19:27 +01001522 iommu_completion_wait(iommu);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02001523
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02001524 spin_unlock_irqrestore(&domain->lock, flags);
1525
1526 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01001527
1528out_free:
1529
1530 free_pages((unsigned long)virt_addr, get_order(size));
1531
1532 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02001533}
1534
Joerg Roedel431b2a22008-07-11 17:14:22 +02001535/*
1536 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001537 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02001538static void free_coherent(struct device *dev, size_t size,
1539 void *virt_addr, dma_addr_t dma_addr)
1540{
1541 unsigned long flags;
1542 struct amd_iommu *iommu;
1543 struct protection_domain *domain;
1544 u16 devid;
1545
Joerg Roedel5d31ee72008-12-12 15:16:38 +01001546 INC_STATS_COUNTER(cnt_free_coherent);
1547
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001548 if (!check_device(dev))
1549 return;
1550
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02001551 get_device_resources(dev, &iommu, &domain, &devid);
1552
1553 if (!iommu || !domain)
1554 goto free_mem;
1555
Joerg Roedel5b28df62008-12-02 17:49:42 +01001556 if (!dma_ops_domain(domain))
1557 goto free_mem;
1558
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02001559 spin_lock_irqsave(&domain->lock, flags);
1560
1561 __unmap_single(iommu, domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02001562
Joerg Roedel09ee17e2008-12-03 12:19:27 +01001563 iommu_completion_wait(iommu);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02001564
1565 spin_unlock_irqrestore(&domain->lock, flags);
1566
1567free_mem:
1568 free_pages((unsigned long)virt_addr, get_order(size));
1569}
1570
Joerg Roedelc432f3d2008-06-26 21:28:04 +02001571/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02001572 * This function is called by the DMA layer to find out if we can handle a
1573 * particular device. It is part of the dma_ops.
1574 */
1575static int amd_iommu_dma_supported(struct device *dev, u64 mask)
1576{
1577 u16 bdf;
1578 struct pci_dev *pcidev;
1579
1580 /* No device or no PCI device */
1581 if (!dev || dev->bus != &pci_bus_type)
1582 return 0;
1583
1584 pcidev = to_pci_dev(dev);
1585
1586 bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
1587
1588 /* Out of our scope? */
1589 if (bdf > amd_iommu_last_bdf)
1590 return 0;
1591
1592 return 1;
1593}
1594
1595/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001596 * The function for pre-allocating protection domains.
1597 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02001598 * If the driver core informs the DMA layer if a driver grabs a device
1599 * we don't need to preallocate the protection domains anymore.
1600 * For now we have to.
1601 */
1602void prealloc_protection_domains(void)
1603{
1604 struct pci_dev *dev = NULL;
1605 struct dma_ops_domain *dma_dom;
1606 struct amd_iommu *iommu;
1607 int order = amd_iommu_aperture_order;
1608 u16 devid;
1609
1610 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
Joerg Roedeledcb34d2008-12-10 20:01:45 +01001611 devid = calc_devid(dev->bus->number, dev->devfn);
Joerg Roedel3a61ec32008-07-25 13:07:50 +02001612 if (devid > amd_iommu_last_bdf)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02001613 continue;
1614 devid = amd_iommu_alias_table[devid];
1615 if (domain_for_device(devid))
1616 continue;
1617 iommu = amd_iommu_rlookup_table[devid];
1618 if (!iommu)
1619 continue;
1620 dma_dom = dma_ops_domain_alloc(iommu, order);
1621 if (!dma_dom)
1622 continue;
1623 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02001624 dma_dom->target_dev = devid;
1625
1626 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02001627 }
1628}
1629
Joerg Roedel6631ee92008-06-26 21:28:05 +02001630static struct dma_mapping_ops amd_iommu_dma_ops = {
1631 .alloc_coherent = alloc_coherent,
1632 .free_coherent = free_coherent,
1633 .map_single = map_single,
1634 .unmap_single = unmap_single,
1635 .map_sg = map_sg,
1636 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02001637 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02001638};
1639
Joerg Roedel431b2a22008-07-11 17:14:22 +02001640/*
1641 * The function which clues the AMD IOMMU driver into dma_ops.
1642 */
Joerg Roedel6631ee92008-06-26 21:28:05 +02001643int __init amd_iommu_init_dma_ops(void)
1644{
1645 struct amd_iommu *iommu;
1646 int order = amd_iommu_aperture_order;
1647 int ret;
1648
Joerg Roedel431b2a22008-07-11 17:14:22 +02001649 /*
1650 * first allocate a default protection domain for every IOMMU we
1651 * found in the system. Devices not assigned to any other
1652 * protection domain will be assigned to the default one.
1653 */
Joerg Roedel6631ee92008-06-26 21:28:05 +02001654 list_for_each_entry(iommu, &amd_iommu_list, list) {
1655 iommu->default_dom = dma_ops_domain_alloc(iommu, order);
1656 if (iommu->default_dom == NULL)
1657 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01001658 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02001659 ret = iommu_init_unity_mappings(iommu);
1660 if (ret)
1661 goto free_domains;
1662 }
1663
Joerg Roedel431b2a22008-07-11 17:14:22 +02001664 /*
1665 * If device isolation is enabled, pre-allocate the protection
1666 * domains for each device.
1667 */
Joerg Roedel6631ee92008-06-26 21:28:05 +02001668 if (amd_iommu_isolate)
1669 prealloc_protection_domains();
1670
1671 iommu_detected = 1;
1672 force_iommu = 1;
1673 bad_dma_address = 0;
Ingo Molnar92af4e22008-06-27 10:48:16 +02001674#ifdef CONFIG_GART_IOMMU
Joerg Roedel6631ee92008-06-26 21:28:05 +02001675 gart_iommu_aperture_disabled = 1;
1676 gart_iommu_aperture = 0;
Ingo Molnar92af4e22008-06-27 10:48:16 +02001677#endif
Joerg Roedel6631ee92008-06-26 21:28:05 +02001678
Joerg Roedel431b2a22008-07-11 17:14:22 +02001679 /* Make the driver finally visible to the drivers */
Joerg Roedel6631ee92008-06-26 21:28:05 +02001680 dma_ops = &amd_iommu_dma_ops;
1681
Joerg Roedel26961ef2008-12-03 17:00:17 +01001682#ifdef CONFIG_IOMMU_API
1683 register_iommu(&amd_iommu_ops);
1684#endif
1685
Joerg Roedele275a2a2008-12-10 18:27:25 +01001686 bus_register_notifier(&pci_bus_type, &device_nb);
1687
Joerg Roedel7f265082008-12-12 13:50:21 +01001688 amd_iommu_stats_init();
1689
Joerg Roedel6631ee92008-06-26 21:28:05 +02001690 return 0;
1691
1692free_domains:
1693
1694 list_for_each_entry(iommu, &amd_iommu_list, list) {
1695 if (iommu->default_dom)
1696 dma_ops_domain_free(iommu->default_dom);
1697 }
1698
1699 return ret;
1700}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01001701
1702/*****************************************************************************
1703 *
1704 * The following functions belong to the exported interface of AMD IOMMU
1705 *
1706 * This interface allows access to lower level functions of the IOMMU
1707 * like protection domain handling and assignement of devices to domains
1708 * which is not possible with the dma_ops interface.
1709 *
1710 *****************************************************************************/
1711
1712#ifdef CONFIG_IOMMU_API
1713
1714static void cleanup_domain(struct protection_domain *domain)
1715{
1716 unsigned long flags;
1717 u16 devid;
1718
1719 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1720
1721 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
1722 if (amd_iommu_pd_table[devid] == domain)
1723 __detach_device(domain, devid);
1724
1725 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1726}
1727
Joerg Roedelc156e342008-12-02 18:13:27 +01001728static int amd_iommu_domain_init(struct iommu_domain *dom)
1729{
1730 struct protection_domain *domain;
1731
1732 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
1733 if (!domain)
1734 return -ENOMEM;
1735
1736 spin_lock_init(&domain->lock);
1737 domain->mode = PAGE_MODE_3_LEVEL;
1738 domain->id = domain_id_alloc();
1739 if (!domain->id)
1740 goto out_free;
1741 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
1742 if (!domain->pt_root)
1743 goto out_free;
1744
1745 dom->priv = domain;
1746
1747 return 0;
1748
1749out_free:
1750 kfree(domain);
1751
1752 return -ENOMEM;
1753}
1754
Joerg Roedel98383fc2008-12-02 18:34:12 +01001755static void amd_iommu_domain_destroy(struct iommu_domain *dom)
1756{
1757 struct protection_domain *domain = dom->priv;
1758
1759 if (!domain)
1760 return;
1761
1762 if (domain->dev_cnt > 0)
1763 cleanup_domain(domain);
1764
1765 BUG_ON(domain->dev_cnt != 0);
1766
1767 free_pagetable(domain);
1768
1769 domain_id_free(domain->id);
1770
1771 kfree(domain);
1772
1773 dom->priv = NULL;
1774}
1775
Joerg Roedel684f2882008-12-08 12:07:44 +01001776static void amd_iommu_detach_device(struct iommu_domain *dom,
1777 struct device *dev)
1778{
1779 struct protection_domain *domain = dom->priv;
1780 struct amd_iommu *iommu;
1781 struct pci_dev *pdev;
1782 u16 devid;
1783
1784 if (dev->bus != &pci_bus_type)
1785 return;
1786
1787 pdev = to_pci_dev(dev);
1788
1789 devid = calc_devid(pdev->bus->number, pdev->devfn);
1790
1791 if (devid > 0)
1792 detach_device(domain, devid);
1793
1794 iommu = amd_iommu_rlookup_table[devid];
1795 if (!iommu)
1796 return;
1797
1798 iommu_queue_inv_dev_entry(iommu, devid);
1799 iommu_completion_wait(iommu);
1800}
1801
Joerg Roedel01106062008-12-02 19:34:11 +01001802static int amd_iommu_attach_device(struct iommu_domain *dom,
1803 struct device *dev)
1804{
1805 struct protection_domain *domain = dom->priv;
1806 struct protection_domain *old_domain;
1807 struct amd_iommu *iommu;
1808 struct pci_dev *pdev;
1809 u16 devid;
1810
1811 if (dev->bus != &pci_bus_type)
1812 return -EINVAL;
1813
1814 pdev = to_pci_dev(dev);
1815
1816 devid = calc_devid(pdev->bus->number, pdev->devfn);
1817
1818 if (devid >= amd_iommu_last_bdf ||
1819 devid != amd_iommu_alias_table[devid])
1820 return -EINVAL;
1821
1822 iommu = amd_iommu_rlookup_table[devid];
1823 if (!iommu)
1824 return -EINVAL;
1825
1826 old_domain = domain_for_device(devid);
1827 if (old_domain)
1828 return -EBUSY;
1829
1830 attach_device(iommu, domain, devid);
1831
1832 iommu_completion_wait(iommu);
1833
1834 return 0;
1835}
1836
Joerg Roedelc6229ca2008-12-02 19:48:43 +01001837static int amd_iommu_map_range(struct iommu_domain *dom,
1838 unsigned long iova, phys_addr_t paddr,
1839 size_t size, int iommu_prot)
1840{
1841 struct protection_domain *domain = dom->priv;
1842 unsigned long i, npages = iommu_num_pages(paddr, size, PAGE_SIZE);
1843 int prot = 0;
1844 int ret;
1845
1846 if (iommu_prot & IOMMU_READ)
1847 prot |= IOMMU_PROT_IR;
1848 if (iommu_prot & IOMMU_WRITE)
1849 prot |= IOMMU_PROT_IW;
1850
1851 iova &= PAGE_MASK;
1852 paddr &= PAGE_MASK;
1853
1854 for (i = 0; i < npages; ++i) {
1855 ret = iommu_map_page(domain, iova, paddr, prot);
1856 if (ret)
1857 return ret;
1858
1859 iova += PAGE_SIZE;
1860 paddr += PAGE_SIZE;
1861 }
1862
1863 return 0;
1864}
1865
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001866static void amd_iommu_unmap_range(struct iommu_domain *dom,
1867 unsigned long iova, size_t size)
1868{
1869
1870 struct protection_domain *domain = dom->priv;
1871 unsigned long i, npages = iommu_num_pages(iova, size, PAGE_SIZE);
1872
1873 iova &= PAGE_MASK;
1874
1875 for (i = 0; i < npages; ++i) {
1876 iommu_unmap_page(domain, iova);
1877 iova += PAGE_SIZE;
1878 }
1879
1880 iommu_flush_domain(domain->id);
1881}
1882
Joerg Roedel645c4c82008-12-02 20:05:50 +01001883static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
1884 unsigned long iova)
1885{
1886 struct protection_domain *domain = dom->priv;
1887 unsigned long offset = iova & ~PAGE_MASK;
1888 phys_addr_t paddr;
1889 u64 *pte;
1890
1891 pte = &domain->pt_root[IOMMU_PTE_L2_INDEX(iova)];
1892
1893 if (!IOMMU_PTE_PRESENT(*pte))
1894 return 0;
1895
1896 pte = IOMMU_PTE_PAGE(*pte);
1897 pte = &pte[IOMMU_PTE_L1_INDEX(iova)];
1898
1899 if (!IOMMU_PTE_PRESENT(*pte))
1900 return 0;
1901
1902 pte = IOMMU_PTE_PAGE(*pte);
1903 pte = &pte[IOMMU_PTE_L0_INDEX(iova)];
1904
1905 if (!IOMMU_PTE_PRESENT(*pte))
1906 return 0;
1907
1908 paddr = *pte & IOMMU_PAGE_MASK;
1909 paddr |= offset;
1910
1911 return paddr;
1912}
1913
Joerg Roedel26961ef2008-12-03 17:00:17 +01001914static struct iommu_ops amd_iommu_ops = {
1915 .domain_init = amd_iommu_domain_init,
1916 .domain_destroy = amd_iommu_domain_destroy,
1917 .attach_dev = amd_iommu_attach_device,
1918 .detach_dev = amd_iommu_detach_device,
1919 .map = amd_iommu_map_range,
1920 .unmap = amd_iommu_unmap_range,
1921 .iova_to_phys = amd_iommu_iova_to_phys,
1922};
1923
Joerg Roedel6d98cd82008-12-08 12:05:55 +01001924#endif