blob: 90b365024c24d6d15289f789e168d7e6f6df80e3 [file] [log] [blame]
Joerg Roedelb6c02712008-06-26 21:27:53 +02001/*
Joerg Roedelbf3118c2009-11-20 13:39:19 +01002 * Copyright (C) 2007-2009 Advanced Micro Devices, Inc.
Joerg Roedelb6c02712008-06-26 21:27:53 +02003 * 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 Roedel6a9401a2009-11-20 13:22:21 +010031#include <asm/amd_iommu_proto.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 Roedel0feae532009-08-26 15:26:30 +020045/*
46 * Domain for untranslated devices - only allocated
47 * if iommu=pt passed on kernel cmd line.
48 */
49static struct protection_domain *pt_domain;
50
Joerg Roedel26961ef2008-12-03 17:00:17 +010051static struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010052
Joerg Roedel431b2a22008-07-11 17:14:22 +020053/*
54 * general struct to manage commands send to an IOMMU
55 */
Joerg Roedeld6449532008-07-11 17:14:28 +020056struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +020057 u32 data[4];
58};
59
Joerg Roedelbd0e5212008-06-26 21:27:56 +020060static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
61 struct unity_map_entry *e);
Joerg Roedel00cd1222009-05-19 09:52:40 +020062static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
63 unsigned long start_page,
64 unsigned int pages);
Joerg Roedela345b232009-09-03 15:01:43 +020065static void reset_iommu_command_buffer(struct amd_iommu *iommu);
Joerg Roedel04bfdd82009-09-02 16:00:23 +020066static void update_domain(struct protection_domain *domain);
Chris Wrightc1eee672009-05-21 00:56:58 -070067
Joerg Roedel15898bb2009-11-24 15:39:42 +010068/****************************************************************************
69 *
70 * Helper functions
71 *
72 ****************************************************************************/
73
74static inline u16 get_device_id(struct device *dev)
75{
76 struct pci_dev *pdev = to_pci_dev(dev);
77
78 return calc_devid(pdev->bus->number, pdev->devfn);
79}
80
Joerg Roedel71c70982009-11-24 16:43:06 +010081/*
82 * In this function the list of preallocated protection domains is traversed to
83 * find the domain for a specific device
84 */
85static struct dma_ops_domain *find_protection_domain(u16 devid)
86{
87 struct dma_ops_domain *entry, *ret = NULL;
88 unsigned long flags;
89 u16 alias = amd_iommu_alias_table[devid];
90
91 if (list_empty(&iommu_pd_list))
92 return NULL;
93
94 spin_lock_irqsave(&iommu_pd_list_lock, flags);
95
96 list_for_each_entry(entry, &iommu_pd_list, list) {
97 if (entry->target_dev == devid ||
98 entry->target_dev == alias) {
99 ret = entry;
100 break;
101 }
102 }
103
104 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
105
106 return ret;
107}
108
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100109/*
110 * This function checks if the driver got a valid device from the caller to
111 * avoid dereferencing invalid pointers.
112 */
113static bool check_device(struct device *dev)
114{
115 u16 devid;
116
117 if (!dev || !dev->dma_mask)
118 return false;
119
120 /* No device or no PCI device */
121 if (!dev || dev->bus != &pci_bus_type)
122 return false;
123
124 devid = get_device_id(dev);
125
126 /* Out of our scope? */
127 if (devid > amd_iommu_last_bdf)
128 return false;
129
130 if (amd_iommu_rlookup_table[devid] == NULL)
131 return false;
132
133 return true;
134}
135
Joerg Roedel7f265082008-12-12 13:50:21 +0100136#ifdef CONFIG_AMD_IOMMU_STATS
137
138/*
139 * Initialization code for statistics collection
140 */
141
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100142DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100143DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100144DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100145DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100146DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100147DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100148DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100149DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100150DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100151DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100152DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100153DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100154
Joerg Roedel7f265082008-12-12 13:50:21 +0100155static struct dentry *stats_dir;
156static struct dentry *de_isolate;
157static struct dentry *de_fflush;
158
159static void amd_iommu_stats_add(struct __iommu_counter *cnt)
160{
161 if (stats_dir == NULL)
162 return;
163
164 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
165 &cnt->value);
166}
167
168static void amd_iommu_stats_init(void)
169{
170 stats_dir = debugfs_create_dir("amd-iommu", NULL);
171 if (stats_dir == NULL)
172 return;
173
174 de_isolate = debugfs_create_bool("isolation", 0444, stats_dir,
175 (u32 *)&amd_iommu_isolate);
176
177 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
178 (u32 *)&amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100179
180 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100181 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100182 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100183 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100184 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100185 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100186 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100187 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100188 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100189 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100190 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100191 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100192}
193
194#endif
195
Joerg Roedel431b2a22008-07-11 17:14:22 +0200196/****************************************************************************
197 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200198 * Interrupt handling functions
199 *
200 ****************************************************************************/
201
Joerg Roedele3e59872009-09-03 14:02:10 +0200202static void dump_dte_entry(u16 devid)
203{
204 int i;
205
206 for (i = 0; i < 8; ++i)
207 pr_err("AMD-Vi: DTE[%d]: %08x\n", i,
208 amd_iommu_dev_table[devid].data[i]);
209}
210
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200211static void dump_command(unsigned long phys_addr)
212{
213 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
214 int i;
215
216 for (i = 0; i < 4; ++i)
217 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
218}
219
Joerg Roedela345b232009-09-03 15:01:43 +0200220static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200221{
222 u32 *event = __evt;
223 int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
224 int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
225 int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
226 int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
227 u64 address = (u64)(((u64)event[3]) << 32) | event[2];
228
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200229 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200230
231 switch (type) {
232 case EVENT_TYPE_ILL_DEV:
233 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
234 "address=0x%016llx flags=0x%04x]\n",
235 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
236 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200237 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200238 break;
239 case EVENT_TYPE_IO_FAULT:
240 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
241 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
242 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
243 domid, address, flags);
244 break;
245 case EVENT_TYPE_DEV_TAB_ERR:
246 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
247 "address=0x%016llx flags=0x%04x]\n",
248 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
249 address, flags);
250 break;
251 case EVENT_TYPE_PAGE_TAB_ERR:
252 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
253 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
254 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
255 domid, address, flags);
256 break;
257 case EVENT_TYPE_ILL_CMD:
258 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedela345b232009-09-03 15:01:43 +0200259 reset_iommu_command_buffer(iommu);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200260 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200261 break;
262 case EVENT_TYPE_CMD_HARD_ERR:
263 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
264 "flags=0x%04x]\n", address, flags);
265 break;
266 case EVENT_TYPE_IOTLB_INV_TO:
267 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
268 "address=0x%016llx]\n",
269 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
270 address);
271 break;
272 case EVENT_TYPE_INV_DEV_REQ:
273 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
274 "address=0x%016llx flags=0x%04x]\n",
275 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
276 address, flags);
277 break;
278 default:
279 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
280 }
281}
282
283static void iommu_poll_events(struct amd_iommu *iommu)
284{
285 u32 head, tail;
286 unsigned long flags;
287
288 spin_lock_irqsave(&iommu->lock, flags);
289
290 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
291 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
292
293 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200294 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200295 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
296 }
297
298 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
299
300 spin_unlock_irqrestore(&iommu->lock, flags);
301}
302
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200303irqreturn_t amd_iommu_int_handler(int irq, void *data)
304{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200305 struct amd_iommu *iommu;
306
Joerg Roedel3bd22172009-05-04 15:06:20 +0200307 for_each_iommu(iommu)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200308 iommu_poll_events(iommu);
309
310 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200311}
312
313/****************************************************************************
314 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200315 * IOMMU command queuing functions
316 *
317 ****************************************************************************/
318
319/*
320 * Writes the command to the IOMMUs command buffer and informs the
321 * hardware about the new command. Must be called with iommu->lock held.
322 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200323static int __iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200324{
325 u32 tail, head;
326 u8 *target;
327
328 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Jiri Kosina8a7c5ef2008-08-19 02:13:55 +0200329 target = iommu->cmd_buf + tail;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200330 memcpy_toio(target, cmd, sizeof(*cmd));
331 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
332 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
333 if (tail == head)
334 return -ENOMEM;
335 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
336
337 return 0;
338}
339
Joerg Roedel431b2a22008-07-11 17:14:22 +0200340/*
341 * General queuing function for commands. Takes iommu->lock and calls
342 * __iommu_queue_command().
343 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200344static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200345{
346 unsigned long flags;
347 int ret;
348
349 spin_lock_irqsave(&iommu->lock, flags);
350 ret = __iommu_queue_command(iommu, cmd);
Joerg Roedel09ee17e2008-12-03 12:19:27 +0100351 if (!ret)
Joerg Roedel0cfd7aa2008-12-10 19:58:00 +0100352 iommu->need_sync = true;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200353 spin_unlock_irqrestore(&iommu->lock, flags);
354
355 return ret;
356}
357
Joerg Roedel431b2a22008-07-11 17:14:22 +0200358/*
Joerg Roedel8d201962008-12-02 20:34:41 +0100359 * This function waits until an IOMMU has completed a completion
360 * wait command
Joerg Roedel431b2a22008-07-11 17:14:22 +0200361 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100362static void __iommu_wait_for_completion(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200363{
Joerg Roedel8d201962008-12-02 20:34:41 +0100364 int ready = 0;
Joerg Roedel519c31b2008-08-14 19:55:15 +0200365 unsigned status = 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100366 unsigned long i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200367
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100368 INC_STATS_COUNTER(compl_wait);
369
Joerg Roedel136f78a2008-07-11 17:14:27 +0200370 while (!ready && (i < EXIT_LOOP_COUNT)) {
371 ++i;
Joerg Roedel519c31b2008-08-14 19:55:15 +0200372 /* wait for the bit to become one */
373 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
374 ready = status & MMIO_STATUS_COM_WAIT_INT_MASK;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200375 }
376
Joerg Roedel519c31b2008-08-14 19:55:15 +0200377 /* set bit back to zero */
378 status &= ~MMIO_STATUS_COM_WAIT_INT_MASK;
379 writel(status, iommu->mmio_base + MMIO_STATUS_OFFSET);
380
Joerg Roedel6a1eddd2009-09-03 15:15:10 +0200381 if (unlikely(i == EXIT_LOOP_COUNT)) {
382 spin_unlock(&iommu->lock);
383 reset_iommu_command_buffer(iommu);
384 spin_lock(&iommu->lock);
385 }
Joerg Roedel8d201962008-12-02 20:34:41 +0100386}
387
388/*
389 * This function queues a completion wait command into the command
390 * buffer of an IOMMU
391 */
392static int __iommu_completion_wait(struct amd_iommu *iommu)
393{
394 struct iommu_cmd cmd;
395
396 memset(&cmd, 0, sizeof(cmd));
397 cmd.data[0] = CMD_COMPL_WAIT_INT_MASK;
398 CMD_SET_TYPE(&cmd, CMD_COMPL_WAIT);
399
400 return __iommu_queue_command(iommu, &cmd);
401}
402
403/*
404 * This function is called whenever we need to ensure that the IOMMU has
405 * completed execution of all commands we sent. It sends a
406 * COMPLETION_WAIT command and waits for it to finish. The IOMMU informs
407 * us about that by writing a value to a physical address we pass with
408 * the command.
409 */
410static int iommu_completion_wait(struct amd_iommu *iommu)
411{
412 int ret = 0;
413 unsigned long flags;
414
415 spin_lock_irqsave(&iommu->lock, flags);
416
417 if (!iommu->need_sync)
418 goto out;
419
420 ret = __iommu_completion_wait(iommu);
421
Joerg Roedel0cfd7aa2008-12-10 19:58:00 +0100422 iommu->need_sync = false;
Joerg Roedel8d201962008-12-02 20:34:41 +0100423
424 if (ret)
425 goto out;
426
427 __iommu_wait_for_completion(iommu);
Joerg Roedel84df8172008-12-17 16:36:44 +0100428
Joerg Roedel7e4f88d2008-09-17 14:19:15 +0200429out:
430 spin_unlock_irqrestore(&iommu->lock, flags);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200431
432 return 0;
433}
434
Joerg Roedel0518a3a2009-11-20 16:00:05 +0100435static void iommu_flush_complete(struct protection_domain *domain)
436{
437 int i;
438
439 for (i = 0; i < amd_iommus_present; ++i) {
440 if (!domain->dev_iommu[i])
441 continue;
442
443 /*
444 * Devices of this domain are behind this IOMMU
445 * We need to wait for completion of all commands.
446 */
447 iommu_completion_wait(amd_iommus[i]);
448 }
449}
450
Joerg Roedel431b2a22008-07-11 17:14:22 +0200451/*
452 * Command send function for invalidating a device table entry
453 */
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200454static int iommu_queue_inv_dev_entry(struct amd_iommu *iommu, u16 devid)
455{
Joerg Roedeld6449532008-07-11 17:14:28 +0200456 struct iommu_cmd cmd;
Joerg Roedelee2fa742008-09-17 13:47:25 +0200457 int ret;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200458
459 BUG_ON(iommu == NULL);
460
461 memset(&cmd, 0, sizeof(cmd));
462 CMD_SET_TYPE(&cmd, CMD_INV_DEV_ENTRY);
463 cmd.data[0] = devid;
464
Joerg Roedelee2fa742008-09-17 13:47:25 +0200465 ret = iommu_queue_command(iommu, &cmd);
466
Joerg Roedelee2fa742008-09-17 13:47:25 +0200467 return ret;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200468}
469
Joerg Roedel237b6f32008-12-02 20:54:37 +0100470static void __iommu_build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
471 u16 domid, int pde, int s)
472{
473 memset(cmd, 0, sizeof(*cmd));
474 address &= PAGE_MASK;
475 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
476 cmd->data[1] |= domid;
477 cmd->data[2] = lower_32_bits(address);
478 cmd->data[3] = upper_32_bits(address);
479 if (s) /* size bit - we flush more than one 4kb page */
480 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
481 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
482 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
483}
484
Joerg Roedel431b2a22008-07-11 17:14:22 +0200485/*
486 * Generic command send function for invalidaing TLB entries
487 */
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200488static int iommu_queue_inv_iommu_pages(struct amd_iommu *iommu,
489 u64 address, u16 domid, int pde, int s)
490{
Joerg Roedeld6449532008-07-11 17:14:28 +0200491 struct iommu_cmd cmd;
Joerg Roedelee2fa742008-09-17 13:47:25 +0200492 int ret;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200493
Joerg Roedel237b6f32008-12-02 20:54:37 +0100494 __iommu_build_inv_iommu_pages(&cmd, address, domid, pde, s);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200495
Joerg Roedelee2fa742008-09-17 13:47:25 +0200496 ret = iommu_queue_command(iommu, &cmd);
497
Joerg Roedelee2fa742008-09-17 13:47:25 +0200498 return ret;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200499}
500
Joerg Roedel431b2a22008-07-11 17:14:22 +0200501/*
502 * TLB invalidation function which is called from the mapping functions.
503 * It invalidates a single PTE if the range to flush is within a single
504 * page. Otherwise it flushes the whole TLB of the IOMMU.
505 */
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100506static void __iommu_flush_pages(struct protection_domain *domain,
507 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200508{
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100509 int s = 0, i;
Joerg Roedeldcd1e922009-11-20 15:30:58 +0100510 unsigned long pages = iommu_num_pages(address, size, PAGE_SIZE);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200511
512 address &= PAGE_MASK;
513
Joerg Roedel999ba412008-07-03 19:35:08 +0200514 if (pages > 1) {
515 /*
516 * If we have to flush more than one page, flush all
517 * TLB entries for this domain
518 */
519 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
520 s = 1;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200521 }
522
Joerg Roedel999ba412008-07-03 19:35:08 +0200523
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100524 for (i = 0; i < amd_iommus_present; ++i) {
525 if (!domain->dev_iommu[i])
526 continue;
527
528 /*
529 * Devices of this domain are behind this IOMMU
530 * We need a TLB flush
531 */
532 iommu_queue_inv_iommu_pages(amd_iommus[i], address,
533 domain->id, pde, s);
534 }
535
536 return;
537}
538
539static void iommu_flush_pages(struct protection_domain *domain,
540 u64 address, size_t size)
541{
542 __iommu_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200543}
Joerg Roedelb6c02712008-06-26 21:27:53 +0200544
Joerg Roedel1c655772008-09-04 18:40:05 +0200545/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedeldcd1e922009-11-20 15:30:58 +0100546static void iommu_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +0200547{
Joerg Roedeldcd1e922009-11-20 15:30:58 +0100548 __iommu_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +0200549}
550
Chris Wright42a49f92009-06-15 15:42:00 +0200551/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedeldcd1e922009-11-20 15:30:58 +0100552static void iommu_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +0200553{
Joerg Roedeldcd1e922009-11-20 15:30:58 +0100554 __iommu_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
Chris Wright42a49f92009-06-15 15:42:00 +0200555}
556
Joerg Roedel43f49602008-12-02 21:01:12 +0100557/*
Joerg Roedel09b42802009-11-20 17:02:44 +0100558 * This function flushes all domains that have devices on the given IOMMU
Joerg Roedel43f49602008-12-02 21:01:12 +0100559 */
Joerg Roedele394d722009-09-03 15:28:33 +0200560static void flush_all_domains_on_iommu(struct amd_iommu *iommu)
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200561{
Joerg Roedel09b42802009-11-20 17:02:44 +0100562 u64 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
563 struct protection_domain *domain;
564 unsigned long flags;
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200565
Joerg Roedel09b42802009-11-20 17:02:44 +0100566 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
567
568 list_for_each_entry(domain, &amd_iommu_pd_list, list) {
569 if (domain->dev_iommu[iommu->index] == 0)
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200570 continue;
Joerg Roedel09b42802009-11-20 17:02:44 +0100571
572 spin_lock(&domain->lock);
573 iommu_queue_inv_iommu_pages(iommu, address, domain->id, 1, 1);
574 iommu_flush_complete(domain);
575 spin_unlock(&domain->lock);
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200576 }
Joerg Roedele394d722009-09-03 15:28:33 +0200577
Joerg Roedel09b42802009-11-20 17:02:44 +0100578 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
Joerg Roedele394d722009-09-03 15:28:33 +0200579}
580
Joerg Roedel09b42802009-11-20 17:02:44 +0100581/*
582 * This function uses heavy locking and may disable irqs for some time. But
583 * this is no issue because it is only called during resume.
584 */
Joerg Roedele394d722009-09-03 15:28:33 +0200585void amd_iommu_flush_all_domains(void)
586{
Joerg Roedele3306662009-11-20 16:48:58 +0100587 struct protection_domain *domain;
Joerg Roedel09b42802009-11-20 17:02:44 +0100588 unsigned long flags;
589
590 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
Joerg Roedele394d722009-09-03 15:28:33 +0200591
Joerg Roedele3306662009-11-20 16:48:58 +0100592 list_for_each_entry(domain, &amd_iommu_pd_list, list) {
Joerg Roedel09b42802009-11-20 17:02:44 +0100593 spin_lock(&domain->lock);
Joerg Roedele3306662009-11-20 16:48:58 +0100594 iommu_flush_tlb_pde(domain);
595 iommu_flush_complete(domain);
Joerg Roedel09b42802009-11-20 17:02:44 +0100596 spin_unlock(&domain->lock);
Joerg Roedele3306662009-11-20 16:48:58 +0100597 }
Joerg Roedel09b42802009-11-20 17:02:44 +0100598
599 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200600}
601
Joerg Roedeld586d782009-09-03 15:39:23 +0200602static void flush_all_devices_for_iommu(struct amd_iommu *iommu)
603{
604 int i;
605
606 for (i = 0; i <= amd_iommu_last_bdf; ++i) {
607 if (iommu != amd_iommu_rlookup_table[i])
608 continue;
609
610 iommu_queue_inv_dev_entry(iommu, i);
611 iommu_completion_wait(iommu);
Joerg Roedel431b2a22008-07-11 17:14:22 +0200612 }
613}
614
Joerg Roedel6a0dbcb2009-09-02 15:41:59 +0200615static void flush_devices_by_domain(struct protection_domain *domain)
Joerg Roedel7d7a1102009-05-05 15:48:10 +0200616{
617 struct amd_iommu *iommu;
618 int i;
619
620 for (i = 0; i <= amd_iommu_last_bdf; ++i) {
Joerg Roedel6a0dbcb2009-09-02 15:41:59 +0200621 if ((domain == NULL && amd_iommu_pd_table[i] == NULL) ||
622 (amd_iommu_pd_table[i] != domain))
Joerg Roedel7d7a1102009-05-05 15:48:10 +0200623 continue;
624
625 iommu = amd_iommu_rlookup_table[i];
626 if (!iommu)
627 continue;
628
629 iommu_queue_inv_dev_entry(iommu, i);
630 iommu_completion_wait(iommu);
631 }
632}
633
Joerg Roedela345b232009-09-03 15:01:43 +0200634static void reset_iommu_command_buffer(struct amd_iommu *iommu)
635{
636 pr_err("AMD-Vi: Resetting IOMMU command buffer\n");
637
Joerg Roedelb26e81b2009-09-03 15:08:09 +0200638 if (iommu->reset_in_progress)
639 panic("AMD-Vi: ILLEGAL_COMMAND_ERROR while resetting command buffer\n");
640
641 iommu->reset_in_progress = true;
642
Joerg Roedela345b232009-09-03 15:01:43 +0200643 amd_iommu_reset_cmd_buffer(iommu);
644 flush_all_devices_for_iommu(iommu);
645 flush_all_domains_on_iommu(iommu);
Joerg Roedelb26e81b2009-09-03 15:08:09 +0200646
647 iommu->reset_in_progress = false;
Joerg Roedela345b232009-09-03 15:01:43 +0200648}
649
Joerg Roedel6a0dbcb2009-09-02 15:41:59 +0200650void amd_iommu_flush_all_devices(void)
651{
652 flush_devices_by_domain(NULL);
653}
654
Joerg Roedel431b2a22008-07-11 17:14:22 +0200655/****************************************************************************
656 *
657 * The functions below are used the create the page table mappings for
658 * unity mapped regions.
659 *
660 ****************************************************************************/
661
662/*
Joerg Roedel308973d2009-11-24 17:43:32 +0100663 * This function is used to add another level to an IO page table. Adding
664 * another level increases the size of the address space by 9 bits to a size up
665 * to 64 bits.
666 */
667static bool increase_address_space(struct protection_domain *domain,
668 gfp_t gfp)
669{
670 u64 *pte;
671
672 if (domain->mode == PAGE_MODE_6_LEVEL)
673 /* address space already 64 bit large */
674 return false;
675
676 pte = (void *)get_zeroed_page(gfp);
677 if (!pte)
678 return false;
679
680 *pte = PM_LEVEL_PDE(domain->mode,
681 virt_to_phys(domain->pt_root));
682 domain->pt_root = pte;
683 domain->mode += 1;
684 domain->updated = true;
685
686 return true;
687}
688
689static u64 *alloc_pte(struct protection_domain *domain,
690 unsigned long address,
691 int end_lvl,
692 u64 **pte_page,
693 gfp_t gfp)
694{
695 u64 *pte, *page;
696 int level;
697
698 while (address > PM_LEVEL_SIZE(domain->mode))
699 increase_address_space(domain, gfp);
700
701 level = domain->mode - 1;
702 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
703
704 while (level > end_lvl) {
705 if (!IOMMU_PTE_PRESENT(*pte)) {
706 page = (u64 *)get_zeroed_page(gfp);
707 if (!page)
708 return NULL;
709 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
710 }
711
712 level -= 1;
713
714 pte = IOMMU_PTE_PAGE(*pte);
715
716 if (pte_page && level == end_lvl)
717 *pte_page = pte;
718
719 pte = &pte[PM_LEVEL_INDEX(level, address)];
720 }
721
722 return pte;
723}
724
725/*
726 * This function checks if there is a PTE for a given dma address. If
727 * there is one, it returns the pointer to it.
728 */
729static u64 *fetch_pte(struct protection_domain *domain,
730 unsigned long address, int map_size)
731{
732 int level;
733 u64 *pte;
734
735 level = domain->mode - 1;
736 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
737
738 while (level > map_size) {
739 if (!IOMMU_PTE_PRESENT(*pte))
740 return NULL;
741
742 level -= 1;
743
744 pte = IOMMU_PTE_PAGE(*pte);
745 pte = &pte[PM_LEVEL_INDEX(level, address)];
746
747 if ((PM_PTE_LEVEL(*pte) == 0) && level != map_size) {
748 pte = NULL;
749 break;
750 }
751 }
752
753 return pte;
754}
755
756/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200757 * Generic mapping functions. It maps a physical address into a DMA
758 * address space. It allocates the page table pages if necessary.
759 * In the future it can be extended to a generic mapping function
760 * supporting all features of AMD IOMMU page tables like level skipping
761 * and full 64 bit address spaces.
762 */
Joerg Roedel38e817f2008-12-02 17:27:52 +0100763static int iommu_map_page(struct protection_domain *dom,
764 unsigned long bus_addr,
765 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200766 int prot,
767 int map_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200768{
Joerg Roedel8bda3092009-05-12 12:02:46 +0200769 u64 __pte, *pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200770
771 bus_addr = PAGE_ALIGN(bus_addr);
Joerg Roedelbb9d4ff2008-12-04 15:59:48 +0100772 phys_addr = PAGE_ALIGN(phys_addr);
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200773
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200774 BUG_ON(!PM_ALIGNED(map_size, bus_addr));
775 BUG_ON(!PM_ALIGNED(map_size, phys_addr));
776
Joerg Roedelbad1cac2009-09-02 16:52:23 +0200777 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200778 return -EINVAL;
779
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200780 pte = alloc_pte(dom, bus_addr, map_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200781
782 if (IOMMU_PTE_PRESENT(*pte))
783 return -EBUSY;
784
785 __pte = phys_addr | IOMMU_PTE_P;
786 if (prot & IOMMU_PROT_IR)
787 __pte |= IOMMU_PTE_IR;
788 if (prot & IOMMU_PROT_IW)
789 __pte |= IOMMU_PTE_IW;
790
791 *pte = __pte;
792
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200793 update_domain(dom);
794
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200795 return 0;
796}
797
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100798static void iommu_unmap_page(struct protection_domain *dom,
Joerg Roedela6b256b2009-09-03 12:21:31 +0200799 unsigned long bus_addr, int map_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100800{
Joerg Roedela6b256b2009-09-03 12:21:31 +0200801 u64 *pte = fetch_pte(dom, bus_addr, map_size);
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100802
Joerg Roedel38a76ee2009-09-02 17:02:47 +0200803 if (pte)
804 *pte = 0;
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100805}
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100806
Joerg Roedel431b2a22008-07-11 17:14:22 +0200807/*
808 * This function checks if a specific unity mapping entry is needed for
809 * this specific IOMMU.
810 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200811static int iommu_for_unity_map(struct amd_iommu *iommu,
812 struct unity_map_entry *entry)
813{
814 u16 bdf, i;
815
816 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
817 bdf = amd_iommu_alias_table[i];
818 if (amd_iommu_rlookup_table[bdf] == iommu)
819 return 1;
820 }
821
822 return 0;
823}
824
Joerg Roedel431b2a22008-07-11 17:14:22 +0200825/*
826 * Init the unity mappings for a specific IOMMU in the system
827 *
828 * Basically iterates over all unity mapping entries and applies them to
829 * the default domain DMA of that IOMMU if necessary.
830 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200831static int iommu_init_unity_mappings(struct amd_iommu *iommu)
832{
833 struct unity_map_entry *entry;
834 int ret;
835
836 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
837 if (!iommu_for_unity_map(iommu, entry))
838 continue;
839 ret = dma_ops_unity_map(iommu->default_dom, entry);
840 if (ret)
841 return ret;
842 }
843
844 return 0;
845}
846
Joerg Roedel431b2a22008-07-11 17:14:22 +0200847/*
848 * This function actually applies the mapping to the page table of the
849 * dma_ops domain.
850 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200851static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
852 struct unity_map_entry *e)
853{
854 u64 addr;
855 int ret;
856
857 for (addr = e->address_start; addr < e->address_end;
858 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200859 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
860 PM_MAP_4k);
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200861 if (ret)
862 return ret;
863 /*
864 * if unity mapping is in aperture range mark the page
865 * as allocated in the aperture
866 */
867 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +0200868 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +0200869 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200870 }
871
872 return 0;
873}
874
Joerg Roedel431b2a22008-07-11 17:14:22 +0200875/*
876 * Inits the unity mappings required for a specific device
877 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200878static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
879 u16 devid)
880{
881 struct unity_map_entry *e;
882 int ret;
883
884 list_for_each_entry(e, &amd_iommu_unity_map, list) {
885 if (!(devid >= e->devid_start && devid <= e->devid_end))
886 continue;
887 ret = dma_ops_unity_map(dma_dom, e);
888 if (ret)
889 return ret;
890 }
891
892 return 0;
893}
894
Joerg Roedel431b2a22008-07-11 17:14:22 +0200895/****************************************************************************
896 *
897 * The next functions belong to the address allocator for the dma_ops
898 * interface functions. They work like the allocators in the other IOMMU
899 * drivers. Its basically a bitmap which marks the allocated pages in
900 * the aperture. Maybe it could be enhanced in the future to a more
901 * efficient allocator.
902 *
903 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +0200904
Joerg Roedel431b2a22008-07-11 17:14:22 +0200905/*
Joerg Roedel384de722009-05-15 12:30:05 +0200906 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200907 *
908 * called with domain->lock held
909 */
Joerg Roedel384de722009-05-15 12:30:05 +0200910
Joerg Roedel9cabe892009-05-18 16:38:55 +0200911/*
912 * This function is used to add a new aperture range to an existing
913 * aperture in case of dma_ops domain allocation or address allocation
914 * failure.
915 */
Joerg Roedel576175c2009-11-23 19:08:46 +0100916static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +0200917 bool populate, gfp_t gfp)
918{
919 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +0100920 struct amd_iommu *iommu;
Joerg Roedel00cd1222009-05-19 09:52:40 +0200921 int i;
Joerg Roedel9cabe892009-05-18 16:38:55 +0200922
Joerg Roedelf5e97052009-05-22 12:31:53 +0200923#ifdef CONFIG_IOMMU_STRESS
924 populate = false;
925#endif
926
Joerg Roedel9cabe892009-05-18 16:38:55 +0200927 if (index >= APERTURE_MAX_RANGES)
928 return -ENOMEM;
929
930 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
931 if (!dma_dom->aperture[index])
932 return -ENOMEM;
933
934 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
935 if (!dma_dom->aperture[index]->bitmap)
936 goto out_free;
937
938 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
939
940 if (populate) {
941 unsigned long address = dma_dom->aperture_size;
942 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
943 u64 *pte, *pte_page;
944
945 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200946 pte = alloc_pte(&dma_dom->domain, address, PM_MAP_4k,
Joerg Roedel9cabe892009-05-18 16:38:55 +0200947 &pte_page, gfp);
948 if (!pte)
949 goto out_free;
950
951 dma_dom->aperture[index]->pte_pages[i] = pte_page;
952
953 address += APERTURE_RANGE_SIZE / 64;
954 }
955 }
956
957 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
958
Joerg Roedel00cd1222009-05-19 09:52:40 +0200959 /* Intialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +0100960 for_each_iommu(iommu) {
961 if (iommu->exclusion_start &&
962 iommu->exclusion_start >= dma_dom->aperture[index]->offset
963 && iommu->exclusion_start < dma_dom->aperture_size) {
964 unsigned long startpage;
965 int pages = iommu_num_pages(iommu->exclusion_start,
966 iommu->exclusion_length,
967 PAGE_SIZE);
968 startpage = iommu->exclusion_start >> PAGE_SHIFT;
969 dma_ops_reserve_addresses(dma_dom, startpage, pages);
970 }
Joerg Roedel00cd1222009-05-19 09:52:40 +0200971 }
972
973 /*
974 * Check for areas already mapped as present in the new aperture
975 * range and mark those pages as reserved in the allocator. Such
976 * mappings may already exist as a result of requested unity
977 * mappings for devices.
978 */
979 for (i = dma_dom->aperture[index]->offset;
980 i < dma_dom->aperture_size;
981 i += PAGE_SIZE) {
Joerg Roedela6b256b2009-09-03 12:21:31 +0200982 u64 *pte = fetch_pte(&dma_dom->domain, i, PM_MAP_4k);
Joerg Roedel00cd1222009-05-19 09:52:40 +0200983 if (!pte || !IOMMU_PTE_PRESENT(*pte))
984 continue;
985
986 dma_ops_reserve_addresses(dma_dom, i << PAGE_SHIFT, 1);
987 }
988
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200989 update_domain(&dma_dom->domain);
990
Joerg Roedel9cabe892009-05-18 16:38:55 +0200991 return 0;
992
993out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200994 update_domain(&dma_dom->domain);
995
Joerg Roedel9cabe892009-05-18 16:38:55 +0200996 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
997
998 kfree(dma_dom->aperture[index]);
999 dma_dom->aperture[index] = NULL;
1000
1001 return -ENOMEM;
1002}
1003
Joerg Roedel384de722009-05-15 12:30:05 +02001004static unsigned long dma_ops_area_alloc(struct device *dev,
1005 struct dma_ops_domain *dom,
1006 unsigned int pages,
1007 unsigned long align_mask,
1008 u64 dma_mask,
1009 unsigned long start)
1010{
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001011 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001012 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1013 int i = start >> APERTURE_RANGE_SHIFT;
1014 unsigned long boundary_size;
1015 unsigned long address = -1;
1016 unsigned long limit;
1017
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001018 next_bit >>= PAGE_SHIFT;
1019
Joerg Roedel384de722009-05-15 12:30:05 +02001020 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1021 PAGE_SIZE) >> PAGE_SHIFT;
1022
1023 for (;i < max_index; ++i) {
1024 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1025
1026 if (dom->aperture[i]->offset >= dma_mask)
1027 break;
1028
1029 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1030 dma_mask >> PAGE_SHIFT);
1031
1032 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1033 limit, next_bit, pages, 0,
1034 boundary_size, align_mask);
1035 if (address != -1) {
1036 address = dom->aperture[i]->offset +
1037 (address << PAGE_SHIFT);
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001038 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001039 break;
1040 }
1041
1042 next_bit = 0;
1043 }
1044
1045 return address;
1046}
1047
Joerg Roedeld3086442008-06-26 21:27:57 +02001048static unsigned long dma_ops_alloc_addresses(struct device *dev,
1049 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001050 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001051 unsigned long align_mask,
1052 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001053{
Joerg Roedeld3086442008-06-26 21:27:57 +02001054 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001055
Joerg Roedelfe16f082009-05-22 12:27:53 +02001056#ifdef CONFIG_IOMMU_STRESS
1057 dom->next_address = 0;
1058 dom->need_flush = true;
1059#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001060
Joerg Roedel384de722009-05-15 12:30:05 +02001061 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001062 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001063
Joerg Roedel1c655772008-09-04 18:40:05 +02001064 if (address == -1) {
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001065 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001066 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1067 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001068 dom->need_flush = true;
1069 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001070
Joerg Roedel384de722009-05-15 12:30:05 +02001071 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001072 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001073
1074 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1075
1076 return address;
1077}
1078
Joerg Roedel431b2a22008-07-11 17:14:22 +02001079/*
1080 * The address free function.
1081 *
1082 * called with domain->lock held
1083 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001084static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1085 unsigned long address,
1086 unsigned int pages)
1087{
Joerg Roedel384de722009-05-15 12:30:05 +02001088 unsigned i = address >> APERTURE_RANGE_SHIFT;
1089 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001090
Joerg Roedel384de722009-05-15 12:30:05 +02001091 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1092
Joerg Roedel47bccd62009-05-22 12:40:54 +02001093#ifdef CONFIG_IOMMU_STRESS
1094 if (i < 4)
1095 return;
1096#endif
1097
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001098 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001099 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001100
1101 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001102
Joerg Roedel384de722009-05-15 12:30:05 +02001103 iommu_area_free(range->bitmap, address, pages);
1104
Joerg Roedeld3086442008-06-26 21:27:57 +02001105}
1106
Joerg Roedel431b2a22008-07-11 17:14:22 +02001107/****************************************************************************
1108 *
1109 * The next functions belong to the domain allocation. A domain is
1110 * allocated for every IOMMU as the default domain. If device isolation
1111 * is enabled, every device get its own domain. The most important thing
1112 * about domains is the page table mapping the DMA address space they
1113 * contain.
1114 *
1115 ****************************************************************************/
1116
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001117/*
1118 * This function adds a protection domain to the global protection domain list
1119 */
1120static void add_domain_to_list(struct protection_domain *domain)
1121{
1122 unsigned long flags;
1123
1124 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1125 list_add(&domain->list, &amd_iommu_pd_list);
1126 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1127}
1128
1129/*
1130 * This function removes a protection domain to the global
1131 * protection domain list
1132 */
1133static void del_domain_from_list(struct protection_domain *domain)
1134{
1135 unsigned long flags;
1136
1137 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1138 list_del(&domain->list);
1139 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1140}
1141
Joerg Roedelec487d12008-06-26 21:27:58 +02001142static u16 domain_id_alloc(void)
1143{
1144 unsigned long flags;
1145 int id;
1146
1147 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1148 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1149 BUG_ON(id == 0);
1150 if (id > 0 && id < MAX_DOMAIN_ID)
1151 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1152 else
1153 id = 0;
1154 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1155
1156 return id;
1157}
1158
Joerg Roedela2acfb72008-12-02 18:28:53 +01001159static void domain_id_free(int id)
1160{
1161 unsigned long flags;
1162
1163 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1164 if (id > 0 && id < MAX_DOMAIN_ID)
1165 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1166 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1167}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001168
Joerg Roedel431b2a22008-07-11 17:14:22 +02001169/*
1170 * Used to reserve address ranges in the aperture (e.g. for exclusion
1171 * ranges.
1172 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001173static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1174 unsigned long start_page,
1175 unsigned int pages)
1176{
Joerg Roedel384de722009-05-15 12:30:05 +02001177 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
Joerg Roedelec487d12008-06-26 21:27:58 +02001178
1179 if (start_page + pages > last_page)
1180 pages = last_page - start_page;
1181
Joerg Roedel384de722009-05-15 12:30:05 +02001182 for (i = start_page; i < start_page + pages; ++i) {
1183 int index = i / APERTURE_RANGE_PAGES;
1184 int page = i % APERTURE_RANGE_PAGES;
1185 __set_bit(page, dom->aperture[index]->bitmap);
1186 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001187}
1188
Joerg Roedel86db2e52008-12-02 18:20:21 +01001189static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001190{
1191 int i, j;
1192 u64 *p1, *p2, *p3;
1193
Joerg Roedel86db2e52008-12-02 18:20:21 +01001194 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001195
1196 if (!p1)
1197 return;
1198
1199 for (i = 0; i < 512; ++i) {
1200 if (!IOMMU_PTE_PRESENT(p1[i]))
1201 continue;
1202
1203 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001204 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001205 if (!IOMMU_PTE_PRESENT(p2[j]))
1206 continue;
1207 p3 = IOMMU_PTE_PAGE(p2[j]);
1208 free_page((unsigned long)p3);
1209 }
1210
1211 free_page((unsigned long)p2);
1212 }
1213
1214 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001215
1216 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001217}
1218
Joerg Roedel431b2a22008-07-11 17:14:22 +02001219/*
1220 * Free a domain, only used if something went wrong in the
1221 * allocation path and we need to free an already allocated page table
1222 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001223static void dma_ops_domain_free(struct dma_ops_domain *dom)
1224{
Joerg Roedel384de722009-05-15 12:30:05 +02001225 int i;
1226
Joerg Roedelec487d12008-06-26 21:27:58 +02001227 if (!dom)
1228 return;
1229
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001230 del_domain_from_list(&dom->domain);
1231
Joerg Roedel86db2e52008-12-02 18:20:21 +01001232 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001233
Joerg Roedel384de722009-05-15 12:30:05 +02001234 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1235 if (!dom->aperture[i])
1236 continue;
1237 free_page((unsigned long)dom->aperture[i]->bitmap);
1238 kfree(dom->aperture[i]);
1239 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001240
1241 kfree(dom);
1242}
1243
Joerg Roedel431b2a22008-07-11 17:14:22 +02001244/*
1245 * Allocates a new protection domain usable for the dma_ops functions.
1246 * It also intializes the page table and the address allocator data
1247 * structures required for the dma_ops interface
1248 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001249static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001250{
1251 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001252
1253 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1254 if (!dma_dom)
1255 return NULL;
1256
1257 spin_lock_init(&dma_dom->domain.lock);
1258
1259 dma_dom->domain.id = domain_id_alloc();
1260 if (dma_dom->domain.id == 0)
1261 goto free_dma_dom;
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001262 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001263 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001264 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001265 dma_dom->domain.priv = dma_dom;
1266 if (!dma_dom->domain.pt_root)
1267 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001268
Joerg Roedel1c655772008-09-04 18:40:05 +02001269 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001270 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001271
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001272 add_domain_to_list(&dma_dom->domain);
1273
Joerg Roedel576175c2009-11-23 19:08:46 +01001274 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001275 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001276
Joerg Roedel431b2a22008-07-11 17:14:22 +02001277 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001278 * mark the first page as allocated so we never return 0 as
1279 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001280 */
Joerg Roedel384de722009-05-15 12:30:05 +02001281 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001282 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001283
Joerg Roedelec487d12008-06-26 21:27:58 +02001284
1285 return dma_dom;
1286
1287free_dma_dom:
1288 dma_ops_domain_free(dma_dom);
1289
1290 return NULL;
1291}
1292
Joerg Roedel431b2a22008-07-11 17:14:22 +02001293/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001294 * little helper function to check whether a given protection domain is a
1295 * dma_ops domain
1296 */
1297static bool dma_ops_domain(struct protection_domain *domain)
1298{
1299 return domain->flags & PD_DMA_OPS_MASK;
1300}
1301
Joerg Roedel407d7332009-09-02 16:07:00 +02001302static void set_dte_entry(u16 devid, struct protection_domain *domain)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001303{
Joerg Roedel15898bb2009-11-24 15:39:42 +01001304 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001305 u64 pte_root = virt_to_phys(domain->pt_root);
Joerg Roedel863c74e2008-12-02 17:56:36 +01001306
Joerg Roedel15898bb2009-11-24 15:39:42 +01001307 BUG_ON(amd_iommu_pd_table[devid] != NULL);
1308
Joerg Roedel38ddf412008-09-11 10:38:32 +02001309 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1310 << DEV_ENTRY_MODE_SHIFT;
1311 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001312
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001313 amd_iommu_dev_table[devid].data[2] = domain->id;
Joerg Roedelaa879ff2009-08-31 16:01:48 +02001314 amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
1315 amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001316
1317 amd_iommu_pd_table[devid] = domain;
Joerg Roedeleba6ac62009-09-01 12:07:08 +02001318
Joerg Roedelc4596112009-11-20 14:57:32 +01001319 /* Do reference counting */
1320 domain->dev_iommu[iommu->index] += 1;
1321 domain->dev_cnt += 1;
Joerg Roedeleba6ac62009-09-01 12:07:08 +02001322
Joerg Roedel15898bb2009-11-24 15:39:42 +01001323 /* Flush the changes DTE entry */
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001324 iommu_queue_inv_dev_entry(iommu, devid);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001325}
1326
Joerg Roedel15898bb2009-11-24 15:39:42 +01001327static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001328{
Joerg Roedel15898bb2009-11-24 15:39:42 +01001329 struct protection_domain *domain = amd_iommu_pd_table[devid];
Joerg Roedelc4596112009-11-20 14:57:32 +01001330 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
1331
Joerg Roedel15898bb2009-11-24 15:39:42 +01001332 BUG_ON(domain == NULL);
Joerg Roedel355bf552008-12-08 12:02:41 +01001333
1334 /* remove domain from the lookup table */
1335 amd_iommu_pd_table[devid] = NULL;
1336
1337 /* remove entry from the device table seen by the hardware */
1338 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1339 amd_iommu_dev_table[devid].data[1] = 0;
1340 amd_iommu_dev_table[devid].data[2] = 0;
1341
Joerg Roedelc5cca142009-10-09 18:31:20 +02001342 amd_iommu_apply_erratum_63(devid);
1343
Joerg Roedelc4596112009-11-20 14:57:32 +01001344 /* decrease reference counters */
1345 domain->dev_iommu[iommu->index] -= 1;
1346 domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001347
Joerg Roedel15898bb2009-11-24 15:39:42 +01001348 iommu_queue_inv_dev_entry(iommu, devid);
1349}
1350
1351/*
1352 * If a device is not yet associated with a domain, this function does
1353 * assigns it visible for the hardware
1354 */
1355static int __attach_device(struct device *dev,
1356 struct protection_domain *domain)
1357{
1358 u16 devid = get_device_id(dev);
1359 u16 alias = amd_iommu_alias_table[devid];
1360
1361 /* lock domain */
1362 spin_lock(&domain->lock);
1363
1364 /* Some sanity checks */
1365 if (amd_iommu_pd_table[alias] != NULL &&
1366 amd_iommu_pd_table[alias] != domain)
1367 return -EBUSY;
1368
1369 if (amd_iommu_pd_table[devid] != NULL &&
1370 amd_iommu_pd_table[devid] != domain)
1371 return -EBUSY;
1372
1373 /* Do real assignment */
1374 if (alias != devid &&
1375 amd_iommu_pd_table[alias] == NULL)
1376 set_dte_entry(alias, domain);
1377
1378 if (amd_iommu_pd_table[devid] == NULL)
1379 set_dte_entry(devid, domain);
1380
Joerg Roedel355bf552008-12-08 12:02:41 +01001381 /* ready */
1382 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001383
Joerg Roedel15898bb2009-11-24 15:39:42 +01001384 return 0;
1385}
1386
1387/*
1388 * If a device is not yet associated with a domain, this function does
1389 * assigns it visible for the hardware
1390 */
1391static int attach_device(struct device *dev,
1392 struct protection_domain *domain)
1393{
1394 unsigned long flags;
1395 int ret;
1396
1397 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1398 ret = __attach_device(dev, domain);
1399 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1400
1401 /*
1402 * We might boot into a crash-kernel here. The crashed kernel
1403 * left the caches in the IOMMU dirty. So we have to flush
1404 * here to evict all dirty stuff.
1405 */
1406 iommu_flush_tlb_pde(domain);
1407
1408 return ret;
1409}
1410
1411/*
1412 * Removes a device from a protection domain (unlocked)
1413 */
1414static void __detach_device(struct device *dev)
1415{
1416 u16 devid = get_device_id(dev);
1417 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
1418
1419 BUG_ON(!iommu);
1420
1421 clear_dte_entry(devid);
1422
Joerg Roedel21129f72009-09-01 11:59:42 +02001423 /*
1424 * If we run in passthrough mode the device must be assigned to the
1425 * passthrough domain if it is detached from any other domain
1426 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01001427 if (iommu_pass_through)
1428 __attach_device(dev, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01001429}
1430
1431/*
1432 * Removes a device from a protection domain (with devtable_lock held)
1433 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01001434static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01001435{
1436 unsigned long flags;
1437
1438 /* lock device table */
1439 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001440 __detach_device(dev);
Joerg Roedel355bf552008-12-08 12:02:41 +01001441 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1442}
Joerg Roedele275a2a2008-12-10 18:27:25 +01001443
Joerg Roedel15898bb2009-11-24 15:39:42 +01001444/*
1445 * Find out the protection domain structure for a given PCI device. This
1446 * will give us the pointer to the page table root for example.
1447 */
1448static struct protection_domain *domain_for_device(struct device *dev)
1449{
1450 struct protection_domain *dom;
1451 unsigned long flags;
1452 u16 devid, alias;
1453
1454 devid = get_device_id(dev);
1455 alias = amd_iommu_alias_table[devid];
1456
1457 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
1458 dom = amd_iommu_pd_table[devid];
1459 if (dom == NULL &&
1460 amd_iommu_pd_table[alias] != NULL) {
1461 __attach_device(dev, amd_iommu_pd_table[alias]);
1462 dom = amd_iommu_pd_table[devid];
1463 }
1464
1465 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1466
1467 return dom;
1468}
1469
Joerg Roedele275a2a2008-12-10 18:27:25 +01001470static int device_change_notifier(struct notifier_block *nb,
1471 unsigned long action, void *data)
1472{
1473 struct device *dev = data;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001474 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001475 struct protection_domain *domain;
1476 struct dma_ops_domain *dma_domain;
1477 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001478 unsigned long flags;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001479
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001480 if (!check_device(dev))
1481 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001482
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001483 devid = get_device_id(dev);
1484 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel15898bb2009-11-24 15:39:42 +01001485 domain = domain_for_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001486
1487 if (domain && !dma_ops_domain(domain))
1488 WARN_ONCE(1, "AMD IOMMU WARNING: device %s already bound "
1489 "to a non-dma-ops domain\n", dev_name(dev));
1490
1491 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07001492 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedele275a2a2008-12-10 18:27:25 +01001493 if (!domain)
1494 goto out;
Joerg Roedela1ca3312009-09-01 12:22:22 +02001495 if (iommu_pass_through)
1496 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001497 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001498 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001499 case BUS_NOTIFY_ADD_DEVICE:
1500 /* allocate a protection domain if a device is added */
1501 dma_domain = find_protection_domain(devid);
1502 if (dma_domain)
1503 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01001504 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001505 if (!dma_domain)
1506 goto out;
1507 dma_domain->target_dev = devid;
1508
1509 spin_lock_irqsave(&iommu_pd_list_lock, flags);
1510 list_add_tail(&dma_domain->list, &iommu_pd_list);
1511 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
1512
1513 break;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001514 default:
1515 goto out;
1516 }
1517
1518 iommu_queue_inv_dev_entry(iommu, devid);
1519 iommu_completion_wait(iommu);
1520
1521out:
1522 return 0;
1523}
1524
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05301525static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01001526 .notifier_call = device_change_notifier,
1527};
Joerg Roedel355bf552008-12-08 12:02:41 +01001528
Joerg Roedel431b2a22008-07-11 17:14:22 +02001529/*****************************************************************************
1530 *
1531 * The next functions belong to the dma_ops mapping/unmapping code.
1532 *
1533 *****************************************************************************/
1534
1535/*
1536 * In the dma_ops path we only have the struct device. This function
1537 * finds the corresponding IOMMU, the protection domain and the
1538 * requestor id for a given device.
1539 * If the device is not yet associated with a domain this is also done
1540 * in this function.
1541 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001542static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001543{
Joerg Roedel94f6d192009-11-24 16:40:02 +01001544 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001545 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01001546 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001547
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001548 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01001549 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001550
Joerg Roedel94f6d192009-11-24 16:40:02 +01001551 domain = domain_for_device(dev);
1552 if (domain != NULL && !dma_ops_domain(domain))
1553 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001554
Joerg Roedel94f6d192009-11-24 16:40:02 +01001555 if (domain != NULL)
1556 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001557
Joerg Roedel15898bb2009-11-24 15:39:42 +01001558 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001559 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001560 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01001561 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
1562 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001563 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01001564 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01001565
Joerg Roedel94f6d192009-11-24 16:40:02 +01001566 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001567}
1568
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001569static void update_device_table(struct protection_domain *domain)
1570{
Joerg Roedel2b681fa2009-09-03 17:14:57 +02001571 unsigned long flags;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001572 int i;
1573
1574 for (i = 0; i <= amd_iommu_last_bdf; ++i) {
1575 if (amd_iommu_pd_table[i] != domain)
1576 continue;
Joerg Roedel2b681fa2009-09-03 17:14:57 +02001577 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001578 set_dte_entry(i, domain);
Joerg Roedel2b681fa2009-09-03 17:14:57 +02001579 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001580 }
1581}
1582
1583static void update_domain(struct protection_domain *domain)
1584{
1585 if (!domain->updated)
1586 return;
1587
1588 update_device_table(domain);
1589 flush_devices_by_domain(domain);
Joerg Roedel601367d2009-11-20 16:08:55 +01001590 iommu_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001591
1592 domain->updated = false;
1593}
1594
Joerg Roedel431b2a22008-07-11 17:14:22 +02001595/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02001596 * This function fetches the PTE for a given address in the aperture
1597 */
1598static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
1599 unsigned long address)
1600{
Joerg Roedel384de722009-05-15 12:30:05 +02001601 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02001602 u64 *pte, *pte_page;
1603
Joerg Roedel384de722009-05-15 12:30:05 +02001604 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1605 if (!aperture)
1606 return NULL;
1607
1608 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02001609 if (!pte) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001610 pte = alloc_pte(&dom->domain, address, PM_MAP_4k, &pte_page,
1611 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02001612 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
1613 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001614 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001615
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001616 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001617
1618 return pte;
1619}
1620
1621/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001622 * This is the generic map function. It maps one 4kb page at paddr to
1623 * the given address in the DMA address space for the domain.
1624 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001625static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001626 unsigned long address,
1627 phys_addr_t paddr,
1628 int direction)
1629{
1630 u64 *pte, __pte;
1631
1632 WARN_ON(address > dom->aperture_size);
1633
1634 paddr &= PAGE_MASK;
1635
Joerg Roedel8bda3092009-05-12 12:02:46 +02001636 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02001637 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001638 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001639
1640 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
1641
1642 if (direction == DMA_TO_DEVICE)
1643 __pte |= IOMMU_PTE_IR;
1644 else if (direction == DMA_FROM_DEVICE)
1645 __pte |= IOMMU_PTE_IW;
1646 else if (direction == DMA_BIDIRECTIONAL)
1647 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
1648
1649 WARN_ON(*pte);
1650
1651 *pte = __pte;
1652
1653 return (dma_addr_t)address;
1654}
1655
Joerg Roedel431b2a22008-07-11 17:14:22 +02001656/*
1657 * The generic unmapping function for on page in the DMA address space.
1658 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001659static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001660 unsigned long address)
1661{
Joerg Roedel384de722009-05-15 12:30:05 +02001662 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001663 u64 *pte;
1664
1665 if (address >= dom->aperture_size)
1666 return;
1667
Joerg Roedel384de722009-05-15 12:30:05 +02001668 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1669 if (!aperture)
1670 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001671
Joerg Roedel384de722009-05-15 12:30:05 +02001672 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
1673 if (!pte)
1674 return;
1675
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001676 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001677
1678 WARN_ON(!*pte);
1679
1680 *pte = 0ULL;
1681}
1682
Joerg Roedel431b2a22008-07-11 17:14:22 +02001683/*
1684 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01001685 * contiguous memory region into DMA address space. It is used by all
1686 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001687 * Must be called with the domain lock held.
1688 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02001689static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001690 struct dma_ops_domain *dma_dom,
1691 phys_addr_t paddr,
1692 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001693 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001694 bool align,
1695 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02001696{
1697 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02001698 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001699 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001700 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001701 int i;
1702
Joerg Roedele3c449f2008-10-15 22:02:11 -07001703 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001704 paddr &= PAGE_MASK;
1705
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01001706 INC_STATS_COUNTER(total_map_requests);
1707
Joerg Roedelc1858972008-12-12 15:42:39 +01001708 if (pages > 1)
1709 INC_STATS_COUNTER(cross_page);
1710
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001711 if (align)
1712 align_mask = (1UL << get_order(size)) - 1;
1713
Joerg Roedel11b83882009-05-19 10:23:15 +02001714retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02001715 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
1716 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001717 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02001718 /*
1719 * setting next_address here will let the address
1720 * allocator only scan the new allocated range in the
1721 * first run. This is a small optimization.
1722 */
1723 dma_dom->next_address = dma_dom->aperture_size;
1724
Joerg Roedel576175c2009-11-23 19:08:46 +01001725 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02001726 goto out;
1727
1728 /*
1729 * aperture was sucessfully enlarged by 128 MB, try
1730 * allocation again
1731 */
1732 goto retry;
1733 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02001734
1735 start = address;
1736 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01001737 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001738 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02001739 goto out_unmap;
1740
Joerg Roedelcb76c322008-06-26 21:28:00 +02001741 paddr += PAGE_SIZE;
1742 start += PAGE_SIZE;
1743 }
1744 address += offset;
1745
Joerg Roedel5774f7c2008-12-12 15:57:30 +01001746 ADD_STATS_COUNTER(alloced_io_mem, size);
1747
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09001748 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedeldcd1e922009-11-20 15:30:58 +01001749 iommu_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02001750 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01001751 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001752 iommu_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02001753
Joerg Roedelcb76c322008-06-26 21:28:00 +02001754out:
1755 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02001756
1757out_unmap:
1758
1759 for (--i; i >= 0; --i) {
1760 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01001761 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02001762 }
1763
1764 dma_ops_free_addresses(dma_dom, address, pages);
1765
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001766 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001767}
1768
Joerg Roedel431b2a22008-07-11 17:14:22 +02001769/*
1770 * Does the reverse of the __map_single function. Must be called with
1771 * the domain lock held too
1772 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01001773static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001774 dma_addr_t dma_addr,
1775 size_t size,
1776 int dir)
1777{
1778 dma_addr_t i, start;
1779 unsigned int pages;
1780
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001781 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01001782 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02001783 return;
1784
Joerg Roedele3c449f2008-10-15 22:02:11 -07001785 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001786 dma_addr &= PAGE_MASK;
1787 start = dma_addr;
1788
1789 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01001790 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001791 start += PAGE_SIZE;
1792 }
1793
Joerg Roedel5774f7c2008-12-12 15:57:30 +01001794 SUB_STATS_COUNTER(alloced_io_mem, size);
1795
Joerg Roedelcb76c322008-06-26 21:28:00 +02001796 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02001797
Joerg Roedel80be3082008-11-06 14:59:05 +01001798 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001799 iommu_flush_pages(&dma_dom->domain, dma_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01001800 dma_dom->need_flush = false;
1801 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02001802}
1803
Joerg Roedel431b2a22008-07-11 17:14:22 +02001804/*
1805 * The exported map_single function for dma_ops.
1806 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09001807static dma_addr_t map_page(struct device *dev, struct page *page,
1808 unsigned long offset, size_t size,
1809 enum dma_data_direction dir,
1810 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02001811{
1812 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02001813 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02001814 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02001815 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09001816 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02001817
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01001818 INC_STATS_COUNTER(cnt_map_single);
1819
Joerg Roedel94f6d192009-11-24 16:40:02 +01001820 domain = get_domain(dev);
1821 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02001822 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01001823 else if (IS_ERR(domain))
1824 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02001825
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001826 dma_mask = *dev->dma_mask;
1827
Joerg Roedel4da70b92008-06-26 21:28:01 +02001828 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01001829
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01001830 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001831 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001832 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02001833 goto out;
1834
Joerg Roedel0518a3a2009-11-20 16:00:05 +01001835 iommu_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02001836
1837out:
1838 spin_unlock_irqrestore(&domain->lock, flags);
1839
1840 return addr;
1841}
1842
Joerg Roedel431b2a22008-07-11 17:14:22 +02001843/*
1844 * The exported unmap_single function for dma_ops.
1845 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09001846static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
1847 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02001848{
1849 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02001850 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02001851
Joerg Roedel146a6912008-12-12 15:07:12 +01001852 INC_STATS_COUNTER(cnt_unmap_single);
1853
Joerg Roedel94f6d192009-11-24 16:40:02 +01001854 domain = get_domain(dev);
1855 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01001856 return;
1857
Joerg Roedel4da70b92008-06-26 21:28:01 +02001858 spin_lock_irqsave(&domain->lock, flags);
1859
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01001860 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02001861
Joerg Roedel0518a3a2009-11-20 16:00:05 +01001862 iommu_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02001863
1864 spin_unlock_irqrestore(&domain->lock, flags);
1865}
1866
Joerg Roedel431b2a22008-07-11 17:14:22 +02001867/*
1868 * This is a special map_sg function which is used if we should map a
1869 * device which is not handled by an AMD IOMMU in the system.
1870 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02001871static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
1872 int nelems, int dir)
1873{
1874 struct scatterlist *s;
1875 int i;
1876
1877 for_each_sg(sglist, s, nelems, i) {
1878 s->dma_address = (dma_addr_t)sg_phys(s);
1879 s->dma_length = s->length;
1880 }
1881
1882 return nelems;
1883}
1884
Joerg Roedel431b2a22008-07-11 17:14:22 +02001885/*
1886 * The exported map_sg function for dma_ops (handles scatter-gather
1887 * lists).
1888 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02001889static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09001890 int nelems, enum dma_data_direction dir,
1891 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02001892{
1893 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02001894 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02001895 int i;
1896 struct scatterlist *s;
1897 phys_addr_t paddr;
1898 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02001899 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02001900
Joerg Roedeld03f067a2008-12-12 15:09:48 +01001901 INC_STATS_COUNTER(cnt_map_sg);
1902
Joerg Roedel94f6d192009-11-24 16:40:02 +01001903 domain = get_domain(dev);
1904 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001905 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01001906 else if (IS_ERR(domain))
1907 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001908
Joerg Roedel832a90c2008-09-18 15:54:23 +02001909 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02001910
Joerg Roedel65b050a2008-06-26 21:28:02 +02001911 spin_lock_irqsave(&domain->lock, flags);
1912
1913 for_each_sg(sglist, s, nelems, i) {
1914 paddr = sg_phys(s);
1915
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01001916 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001917 paddr, s->length, dir, false,
1918 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02001919
1920 if (s->dma_address) {
1921 s->dma_length = s->length;
1922 mapped_elems++;
1923 } else
1924 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02001925 }
1926
Joerg Roedel0518a3a2009-11-20 16:00:05 +01001927 iommu_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02001928
1929out:
1930 spin_unlock_irqrestore(&domain->lock, flags);
1931
1932 return mapped_elems;
1933unmap:
1934 for_each_sg(sglist, s, mapped_elems, i) {
1935 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01001936 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02001937 s->dma_length, dir);
1938 s->dma_address = s->dma_length = 0;
1939 }
1940
1941 mapped_elems = 0;
1942
1943 goto out;
1944}
1945
Joerg Roedel431b2a22008-07-11 17:14:22 +02001946/*
1947 * The exported map_sg function for dma_ops (handles scatter-gather
1948 * lists).
1949 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02001950static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09001951 int nelems, enum dma_data_direction dir,
1952 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02001953{
1954 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02001955 struct protection_domain *domain;
1956 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02001957 int i;
1958
Joerg Roedel55877a62008-12-12 15:12:14 +01001959 INC_STATS_COUNTER(cnt_unmap_sg);
1960
Joerg Roedel94f6d192009-11-24 16:40:02 +01001961 domain = get_domain(dev);
1962 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01001963 return;
1964
Joerg Roedel65b050a2008-06-26 21:28:02 +02001965 spin_lock_irqsave(&domain->lock, flags);
1966
1967 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01001968 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02001969 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02001970 s->dma_address = s->dma_length = 0;
1971 }
1972
Joerg Roedel0518a3a2009-11-20 16:00:05 +01001973 iommu_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02001974
1975 spin_unlock_irqrestore(&domain->lock, flags);
1976}
1977
Joerg Roedel431b2a22008-07-11 17:14:22 +02001978/*
1979 * The exported alloc_coherent function for dma_ops.
1980 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02001981static void *alloc_coherent(struct device *dev, size_t size,
1982 dma_addr_t *dma_addr, gfp_t flag)
1983{
1984 unsigned long flags;
1985 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02001986 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02001987 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02001988 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02001989
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01001990 INC_STATS_COUNTER(cnt_alloc_coherent);
1991
Joerg Roedel94f6d192009-11-24 16:40:02 +01001992 domain = get_domain(dev);
1993 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001994 virt_addr = (void *)__get_free_pages(flag, get_order(size));
1995 *dma_addr = __pa(virt_addr);
1996 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01001997 } else if (IS_ERR(domain))
1998 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001999
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002000 dma_mask = dev->coherent_dma_mask;
2001 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2002 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002003
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002004 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2005 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302006 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002007
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002008 paddr = virt_to_phys(virt_addr);
2009
Joerg Roedel832a90c2008-09-18 15:54:23 +02002010 if (!dma_mask)
2011 dma_mask = *dev->dma_mask;
2012
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002013 spin_lock_irqsave(&domain->lock, flags);
2014
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002015 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002016 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002017
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002018 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002019 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002020 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002021 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002022
Joerg Roedel0518a3a2009-11-20 16:00:05 +01002023 iommu_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002024
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002025 spin_unlock_irqrestore(&domain->lock, flags);
2026
2027 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002028
2029out_free:
2030
2031 free_pages((unsigned long)virt_addr, get_order(size));
2032
2033 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002034}
2035
Joerg Roedel431b2a22008-07-11 17:14:22 +02002036/*
2037 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002038 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002039static void free_coherent(struct device *dev, size_t size,
2040 void *virt_addr, dma_addr_t dma_addr)
2041{
2042 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002043 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002044
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002045 INC_STATS_COUNTER(cnt_free_coherent);
2046
Joerg Roedel94f6d192009-11-24 16:40:02 +01002047 domain = get_domain(dev);
2048 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002049 goto free_mem;
2050
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002051 spin_lock_irqsave(&domain->lock, flags);
2052
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002053 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002054
Joerg Roedel0518a3a2009-11-20 16:00:05 +01002055 iommu_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002056
2057 spin_unlock_irqrestore(&domain->lock, flags);
2058
2059free_mem:
2060 free_pages((unsigned long)virt_addr, get_order(size));
2061}
2062
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002063/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002064 * This function is called by the DMA layer to find out if we can handle a
2065 * particular device. It is part of the dma_ops.
2066 */
2067static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2068{
Joerg Roedel420aef82009-11-23 16:14:57 +01002069 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002070}
2071
2072/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002073 * The function for pre-allocating protection domains.
2074 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002075 * If the driver core informs the DMA layer if a driver grabs a device
2076 * we don't need to preallocate the protection domains anymore.
2077 * For now we have to.
2078 */
Jaswinder Singh Rajput0e93dd82008-12-29 21:45:22 +05302079static void prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002080{
2081 struct pci_dev *dev = NULL;
2082 struct dma_ops_domain *dma_dom;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002083 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002084
2085 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002086
2087 /* Do we handle this device? */
2088 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002089 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002090
2091 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002092 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002093 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002094
2095 devid = get_device_id(&dev->dev);
2096
Joerg Roedel87a64d52009-11-24 17:26:43 +01002097 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002098 if (!dma_dom)
2099 continue;
2100 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002101 dma_dom->target_dev = devid;
2102
Joerg Roedel15898bb2009-11-24 15:39:42 +01002103 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01002104
Joerg Roedelbd60b732008-09-11 10:24:48 +02002105 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002106 }
2107}
2108
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002109static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002110 .alloc_coherent = alloc_coherent,
2111 .free_coherent = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002112 .map_page = map_page,
2113 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002114 .map_sg = map_sg,
2115 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002116 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002117};
2118
Joerg Roedel431b2a22008-07-11 17:14:22 +02002119/*
2120 * The function which clues the AMD IOMMU driver into dma_ops.
2121 */
Joerg Roedel6631ee92008-06-26 21:28:05 +02002122int __init amd_iommu_init_dma_ops(void)
2123{
2124 struct amd_iommu *iommu;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002125 int ret;
2126
Joerg Roedel431b2a22008-07-11 17:14:22 +02002127 /*
2128 * first allocate a default protection domain for every IOMMU we
2129 * found in the system. Devices not assigned to any other
2130 * protection domain will be assigned to the default one.
2131 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02002132 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01002133 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002134 if (iommu->default_dom == NULL)
2135 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01002136 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002137 ret = iommu_init_unity_mappings(iommu);
2138 if (ret)
2139 goto free_domains;
2140 }
2141
Joerg Roedel431b2a22008-07-11 17:14:22 +02002142 /*
2143 * If device isolation is enabled, pre-allocate the protection
2144 * domains for each device.
2145 */
Joerg Roedel6631ee92008-06-26 21:28:05 +02002146 if (amd_iommu_isolate)
2147 prealloc_protection_domains();
2148
2149 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002150 swiotlb = 0;
Ingo Molnar92af4e22008-06-27 10:48:16 +02002151#ifdef CONFIG_GART_IOMMU
Joerg Roedel6631ee92008-06-26 21:28:05 +02002152 gart_iommu_aperture_disabled = 1;
2153 gart_iommu_aperture = 0;
Ingo Molnar92af4e22008-06-27 10:48:16 +02002154#endif
Joerg Roedel6631ee92008-06-26 21:28:05 +02002155
Joerg Roedel431b2a22008-07-11 17:14:22 +02002156 /* Make the driver finally visible to the drivers */
Joerg Roedel6631ee92008-06-26 21:28:05 +02002157 dma_ops = &amd_iommu_dma_ops;
2158
Joerg Roedel26961ef2008-12-03 17:00:17 +01002159 register_iommu(&amd_iommu_ops);
Joerg Roedel26961ef2008-12-03 17:00:17 +01002160
Joerg Roedele275a2a2008-12-10 18:27:25 +01002161 bus_register_notifier(&pci_bus_type, &device_nb);
2162
Joerg Roedel7f265082008-12-12 13:50:21 +01002163 amd_iommu_stats_init();
2164
Joerg Roedel6631ee92008-06-26 21:28:05 +02002165 return 0;
2166
2167free_domains:
2168
Joerg Roedel3bd22172009-05-04 15:06:20 +02002169 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002170 if (iommu->default_dom)
2171 dma_ops_domain_free(iommu->default_dom);
2172 }
2173
2174 return ret;
2175}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002176
2177/*****************************************************************************
2178 *
2179 * The following functions belong to the exported interface of AMD IOMMU
2180 *
2181 * This interface allows access to lower level functions of the IOMMU
2182 * like protection domain handling and assignement of devices to domains
2183 * which is not possible with the dma_ops interface.
2184 *
2185 *****************************************************************************/
2186
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002187static void cleanup_domain(struct protection_domain *domain)
2188{
2189 unsigned long flags;
2190 u16 devid;
2191
2192 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2193
2194 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
2195 if (amd_iommu_pd_table[devid] == domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002196 clear_dte_entry(devid);
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002197
2198 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2199}
2200
Joerg Roedel26508152009-08-26 16:52:40 +02002201static void protection_domain_free(struct protection_domain *domain)
2202{
2203 if (!domain)
2204 return;
2205
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002206 del_domain_from_list(domain);
2207
Joerg Roedel26508152009-08-26 16:52:40 +02002208 if (domain->id)
2209 domain_id_free(domain->id);
2210
2211 kfree(domain);
2212}
2213
2214static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002215{
2216 struct protection_domain *domain;
2217
2218 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2219 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002220 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002221
2222 spin_lock_init(&domain->lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01002223 domain->id = domain_id_alloc();
2224 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02002225 goto out_err;
2226
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002227 add_domain_to_list(domain);
2228
Joerg Roedel26508152009-08-26 16:52:40 +02002229 return domain;
2230
2231out_err:
2232 kfree(domain);
2233
2234 return NULL;
2235}
2236
2237static int amd_iommu_domain_init(struct iommu_domain *dom)
2238{
2239 struct protection_domain *domain;
2240
2241 domain = protection_domain_alloc();
2242 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01002243 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02002244
2245 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002246 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2247 if (!domain->pt_root)
2248 goto out_free;
2249
2250 dom->priv = domain;
2251
2252 return 0;
2253
2254out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02002255 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01002256
2257 return -ENOMEM;
2258}
2259
Joerg Roedel98383fc2008-12-02 18:34:12 +01002260static void amd_iommu_domain_destroy(struct iommu_domain *dom)
2261{
2262 struct protection_domain *domain = dom->priv;
2263
2264 if (!domain)
2265 return;
2266
2267 if (domain->dev_cnt > 0)
2268 cleanup_domain(domain);
2269
2270 BUG_ON(domain->dev_cnt != 0);
2271
2272 free_pagetable(domain);
2273
2274 domain_id_free(domain->id);
2275
2276 kfree(domain);
2277
2278 dom->priv = NULL;
2279}
2280
Joerg Roedel684f2882008-12-08 12:07:44 +01002281static void amd_iommu_detach_device(struct iommu_domain *dom,
2282 struct device *dev)
2283{
Joerg Roedel684f2882008-12-08 12:07:44 +01002284 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002285 u16 devid;
2286
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002287 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01002288 return;
2289
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002290 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002291
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002292 if (amd_iommu_pd_table[devid] != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002293 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002294
2295 iommu = amd_iommu_rlookup_table[devid];
2296 if (!iommu)
2297 return;
2298
2299 iommu_queue_inv_dev_entry(iommu, devid);
2300 iommu_completion_wait(iommu);
2301}
2302
Joerg Roedel01106062008-12-02 19:34:11 +01002303static int amd_iommu_attach_device(struct iommu_domain *dom,
2304 struct device *dev)
2305{
2306 struct protection_domain *domain = dom->priv;
2307 struct protection_domain *old_domain;
2308 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002309 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002310 u16 devid;
2311
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002312 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01002313 return -EINVAL;
2314
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002315 devid = get_device_id(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002316
2317 iommu = amd_iommu_rlookup_table[devid];
2318 if (!iommu)
2319 return -EINVAL;
2320
Joerg Roedel15898bb2009-11-24 15:39:42 +01002321 old_domain = amd_iommu_pd_table[devid];
Joerg Roedel01106062008-12-02 19:34:11 +01002322 if (old_domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002323 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002324
Joerg Roedel15898bb2009-11-24 15:39:42 +01002325 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01002326
2327 iommu_completion_wait(iommu);
2328
Joerg Roedel15898bb2009-11-24 15:39:42 +01002329 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002330}
2331
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002332static int amd_iommu_map_range(struct iommu_domain *dom,
2333 unsigned long iova, phys_addr_t paddr,
2334 size_t size, int iommu_prot)
2335{
2336 struct protection_domain *domain = dom->priv;
2337 unsigned long i, npages = iommu_num_pages(paddr, size, PAGE_SIZE);
2338 int prot = 0;
2339 int ret;
2340
2341 if (iommu_prot & IOMMU_READ)
2342 prot |= IOMMU_PROT_IR;
2343 if (iommu_prot & IOMMU_WRITE)
2344 prot |= IOMMU_PROT_IW;
2345
2346 iova &= PAGE_MASK;
2347 paddr &= PAGE_MASK;
2348
2349 for (i = 0; i < npages; ++i) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002350 ret = iommu_map_page(domain, iova, paddr, prot, PM_MAP_4k);
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002351 if (ret)
2352 return ret;
2353
2354 iova += PAGE_SIZE;
2355 paddr += PAGE_SIZE;
2356 }
2357
2358 return 0;
2359}
2360
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002361static void amd_iommu_unmap_range(struct iommu_domain *dom,
2362 unsigned long iova, size_t size)
2363{
2364
2365 struct protection_domain *domain = dom->priv;
2366 unsigned long i, npages = iommu_num_pages(iova, size, PAGE_SIZE);
2367
2368 iova &= PAGE_MASK;
2369
2370 for (i = 0; i < npages; ++i) {
Joerg Roedela6b256b2009-09-03 12:21:31 +02002371 iommu_unmap_page(domain, iova, PM_MAP_4k);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002372 iova += PAGE_SIZE;
2373 }
2374
Joerg Roedel601367d2009-11-20 16:08:55 +01002375 iommu_flush_tlb_pde(domain);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002376}
2377
Joerg Roedel645c4c82008-12-02 20:05:50 +01002378static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2379 unsigned long iova)
2380{
2381 struct protection_domain *domain = dom->priv;
2382 unsigned long offset = iova & ~PAGE_MASK;
2383 phys_addr_t paddr;
2384 u64 *pte;
2385
Joerg Roedela6b256b2009-09-03 12:21:31 +02002386 pte = fetch_pte(domain, iova, PM_MAP_4k);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002387
Joerg Roedela6d41a42009-09-02 17:08:55 +02002388 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01002389 return 0;
2390
2391 paddr = *pte & IOMMU_PAGE_MASK;
2392 paddr |= offset;
2393
2394 return paddr;
2395}
2396
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002397static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
2398 unsigned long cap)
2399{
2400 return 0;
2401}
2402
Joerg Roedel26961ef2008-12-03 17:00:17 +01002403static struct iommu_ops amd_iommu_ops = {
2404 .domain_init = amd_iommu_domain_init,
2405 .domain_destroy = amd_iommu_domain_destroy,
2406 .attach_dev = amd_iommu_attach_device,
2407 .detach_dev = amd_iommu_detach_device,
2408 .map = amd_iommu_map_range,
2409 .unmap = amd_iommu_unmap_range,
2410 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002411 .domain_has_cap = amd_iommu_domain_has_cap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002412};
2413
Joerg Roedel0feae532009-08-26 15:26:30 +02002414/*****************************************************************************
2415 *
2416 * The next functions do a basic initialization of IOMMU for pass through
2417 * mode
2418 *
2419 * In passthrough mode the IOMMU is initialized and enabled but not used for
2420 * DMA-API translation.
2421 *
2422 *****************************************************************************/
2423
2424int __init amd_iommu_init_passthrough(void)
2425{
Joerg Roedel15898bb2009-11-24 15:39:42 +01002426 struct amd_iommu *iommu;
Joerg Roedel0feae532009-08-26 15:26:30 +02002427 struct pci_dev *dev = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002428 u16 devid;
Joerg Roedel0feae532009-08-26 15:26:30 +02002429
2430 /* allocate passthroug domain */
2431 pt_domain = protection_domain_alloc();
2432 if (!pt_domain)
2433 return -ENOMEM;
2434
2435 pt_domain->mode |= PAGE_MODE_NONE;
2436
2437 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
Joerg Roedel0feae532009-08-26 15:26:30 +02002438
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002439 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02002440 continue;
2441
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002442 devid = get_device_id(&dev->dev);
2443
Joerg Roedel15898bb2009-11-24 15:39:42 +01002444 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02002445 if (!iommu)
2446 continue;
2447
Joerg Roedel15898bb2009-11-24 15:39:42 +01002448 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02002449 }
2450
2451 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
2452
2453 return 0;
2454}