blob: 873e7e1ead7b6f233bd79a56ad646f217f233053 [file] [log] [blame]
Joerg Roedelb6c02712008-06-26 21:27:53 +02001/*
Joerg Roedel5d0d7152010-10-13 11:13:21 +02002 * Copyright (C) 2007-2010 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>
Joerg Roedelcb41ed82011-04-05 11:00:53 +020021#include <linux/pci-ats.h>
Akinobu Mitaa66022c2009-12-15 16:48:28 -080022#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Joerg Roedel7f265082008-12-12 13:50:21 +010024#include <linux/debugfs.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020025#include <linux/scatterlist.h>
FUJITA Tomonori51491362009-01-05 23:47:25 +090026#include <linux/dma-mapping.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020027#include <linux/iommu-helper.h>
Joerg Roedelc156e342008-12-02 18:13:27 +010028#include <linux/iommu.h>
Joerg Roedel815b33f2011-04-06 17:26:49 +020029#include <linux/delay.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020030#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090031#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010032#include <asm/gart.h>
Joerg Roedel6a9401a2009-11-20 13:22:21 +010033#include <asm/amd_iommu_proto.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020034#include <asm/amd_iommu_types.h>
Joerg Roedelc6da9922008-06-26 21:28:06 +020035#include <asm/amd_iommu.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020036
37#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
38
Joerg Roedel815b33f2011-04-06 17:26:49 +020039#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020040
Joerg Roedelb6c02712008-06-26 21:27:53 +020041static DEFINE_RWLOCK(amd_iommu_devtable_lock);
42
Joerg Roedelbd60b732008-09-11 10:24:48 +020043/* A list of preallocated protection domains */
44static LIST_HEAD(iommu_pd_list);
45static DEFINE_SPINLOCK(iommu_pd_list_lock);
46
Joerg Roedel0feae532009-08-26 15:26:30 +020047/*
48 * Domain for untranslated devices - only allocated
49 * if iommu=pt passed on kernel cmd line.
50 */
51static struct protection_domain *pt_domain;
52
Joerg Roedel26961ef2008-12-03 17:00:17 +010053static struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010054
Joerg Roedel431b2a22008-07-11 17:14:22 +020055/*
56 * general struct to manage commands send to an IOMMU
57 */
Joerg Roedeld6449532008-07-11 17:14:28 +020058struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +020059 u32 data[4];
60};
61
Joerg Roedel04bfdd82009-09-02 16:00:23 +020062static void update_domain(struct protection_domain *domain);
Chris Wrightc1eee672009-05-21 00:56:58 -070063
Joerg Roedel15898bb2009-11-24 15:39:42 +010064/****************************************************************************
65 *
66 * Helper functions
67 *
68 ****************************************************************************/
69
70static inline u16 get_device_id(struct device *dev)
71{
72 struct pci_dev *pdev = to_pci_dev(dev);
73
74 return calc_devid(pdev->bus->number, pdev->devfn);
75}
76
Joerg Roedel657cbb62009-11-23 15:26:46 +010077static struct iommu_dev_data *get_dev_data(struct device *dev)
78{
79 return dev->archdata.iommu;
80}
81
Joerg Roedel71c70982009-11-24 16:43:06 +010082/*
83 * In this function the list of preallocated protection domains is traversed to
84 * find the domain for a specific device
85 */
86static struct dma_ops_domain *find_protection_domain(u16 devid)
87{
88 struct dma_ops_domain *entry, *ret = NULL;
89 unsigned long flags;
90 u16 alias = amd_iommu_alias_table[devid];
91
92 if (list_empty(&iommu_pd_list))
93 return NULL;
94
95 spin_lock_irqsave(&iommu_pd_list_lock, flags);
96
97 list_for_each_entry(entry, &iommu_pd_list, list) {
98 if (entry->target_dev == devid ||
99 entry->target_dev == alias) {
100 ret = entry;
101 break;
102 }
103 }
104
105 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
106
107 return ret;
108}
109
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100110/*
111 * This function checks if the driver got a valid device from the caller to
112 * avoid dereferencing invalid pointers.
113 */
114static bool check_device(struct device *dev)
115{
116 u16 devid;
117
118 if (!dev || !dev->dma_mask)
119 return false;
120
121 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100122 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100123 return false;
124
125 devid = get_device_id(dev);
126
127 /* Out of our scope? */
128 if (devid > amd_iommu_last_bdf)
129 return false;
130
131 if (amd_iommu_rlookup_table[devid] == NULL)
132 return false;
133
134 return true;
135}
136
Joerg Roedel657cbb62009-11-23 15:26:46 +0100137static int iommu_init_device(struct device *dev)
138{
139 struct iommu_dev_data *dev_data;
140 struct pci_dev *pdev;
141 u16 devid, alias;
142
143 if (dev->archdata.iommu)
144 return 0;
145
146 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
147 if (!dev_data)
148 return -ENOMEM;
149
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100150 dev_data->dev = dev;
151
Joerg Roedel657cbb62009-11-23 15:26:46 +0100152 devid = get_device_id(dev);
153 alias = amd_iommu_alias_table[devid];
154 pdev = pci_get_bus_and_slot(PCI_BUS(alias), alias & 0xff);
155 if (pdev)
156 dev_data->alias = &pdev->dev;
157
Joerg Roedel24100052009-11-25 15:59:57 +0100158 atomic_set(&dev_data->bind, 0);
159
Joerg Roedel657cbb62009-11-23 15:26:46 +0100160 dev->archdata.iommu = dev_data;
161
162
163 return 0;
164}
165
166static void iommu_uninit_device(struct device *dev)
167{
168 kfree(dev->archdata.iommu);
169}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100170
171void __init amd_iommu_uninit_devices(void)
172{
173 struct pci_dev *pdev = NULL;
174
175 for_each_pci_dev(pdev) {
176
177 if (!check_device(&pdev->dev))
178 continue;
179
180 iommu_uninit_device(&pdev->dev);
181 }
182}
183
184int __init amd_iommu_init_devices(void)
185{
186 struct pci_dev *pdev = NULL;
187 int ret = 0;
188
189 for_each_pci_dev(pdev) {
190
191 if (!check_device(&pdev->dev))
192 continue;
193
194 ret = iommu_init_device(&pdev->dev);
195 if (ret)
196 goto out_free;
197 }
198
199 return 0;
200
201out_free:
202
203 amd_iommu_uninit_devices();
204
205 return ret;
206}
Joerg Roedel7f265082008-12-12 13:50:21 +0100207#ifdef CONFIG_AMD_IOMMU_STATS
208
209/*
210 * Initialization code for statistics collection
211 */
212
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100213DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100214DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100215DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100216DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100217DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100218DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100219DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100220DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100221DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100222DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100223DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100224DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100225
Joerg Roedel7f265082008-12-12 13:50:21 +0100226static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100227static struct dentry *de_fflush;
228
229static void amd_iommu_stats_add(struct __iommu_counter *cnt)
230{
231 if (stats_dir == NULL)
232 return;
233
234 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
235 &cnt->value);
236}
237
238static void amd_iommu_stats_init(void)
239{
240 stats_dir = debugfs_create_dir("amd-iommu", NULL);
241 if (stats_dir == NULL)
242 return;
243
Joerg Roedel7f265082008-12-12 13:50:21 +0100244 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
245 (u32 *)&amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100246
247 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100248 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100249 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100250 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100251 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100252 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100253 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100254 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100255 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100256 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100257 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100258 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100259}
260
261#endif
262
Joerg Roedel431b2a22008-07-11 17:14:22 +0200263/****************************************************************************
264 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200265 * Interrupt handling functions
266 *
267 ****************************************************************************/
268
Joerg Roedele3e59872009-09-03 14:02:10 +0200269static void dump_dte_entry(u16 devid)
270{
271 int i;
272
273 for (i = 0; i < 8; ++i)
274 pr_err("AMD-Vi: DTE[%d]: %08x\n", i,
275 amd_iommu_dev_table[devid].data[i]);
276}
277
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200278static void dump_command(unsigned long phys_addr)
279{
280 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
281 int i;
282
283 for (i = 0; i < 4; ++i)
284 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
285}
286
Joerg Roedela345b232009-09-03 15:01:43 +0200287static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200288{
289 u32 *event = __evt;
290 int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
291 int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
292 int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
293 int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
294 u64 address = (u64)(((u64)event[3]) << 32) | event[2];
295
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200296 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200297
298 switch (type) {
299 case EVENT_TYPE_ILL_DEV:
300 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
301 "address=0x%016llx flags=0x%04x]\n",
302 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
303 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200304 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200305 break;
306 case EVENT_TYPE_IO_FAULT:
307 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
308 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
309 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
310 domid, address, flags);
311 break;
312 case EVENT_TYPE_DEV_TAB_ERR:
313 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
314 "address=0x%016llx flags=0x%04x]\n",
315 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
316 address, flags);
317 break;
318 case EVENT_TYPE_PAGE_TAB_ERR:
319 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
320 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
321 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
322 domid, address, flags);
323 break;
324 case EVENT_TYPE_ILL_CMD:
325 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200326 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200327 break;
328 case EVENT_TYPE_CMD_HARD_ERR:
329 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
330 "flags=0x%04x]\n", address, flags);
331 break;
332 case EVENT_TYPE_IOTLB_INV_TO:
333 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
334 "address=0x%016llx]\n",
335 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
336 address);
337 break;
338 case EVENT_TYPE_INV_DEV_REQ:
339 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
340 "address=0x%016llx flags=0x%04x]\n",
341 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
342 address, flags);
343 break;
344 default:
345 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
346 }
347}
348
349static void iommu_poll_events(struct amd_iommu *iommu)
350{
351 u32 head, tail;
352 unsigned long flags;
353
354 spin_lock_irqsave(&iommu->lock, flags);
355
356 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
357 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
358
359 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200360 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200361 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
362 }
363
364 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
365
366 spin_unlock_irqrestore(&iommu->lock, flags);
367}
368
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200369irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200370{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200371 struct amd_iommu *iommu;
372
Joerg Roedel3bd22172009-05-04 15:06:20 +0200373 for_each_iommu(iommu)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200374 iommu_poll_events(iommu);
375
376 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200377}
378
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200379irqreturn_t amd_iommu_int_handler(int irq, void *data)
380{
381 return IRQ_WAKE_THREAD;
382}
383
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200384/****************************************************************************
385 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200386 * IOMMU command queuing functions
387 *
388 ****************************************************************************/
389
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200390static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200391{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200392 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200393
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200394 while (*sem == 0 && i < LOOP_TIMEOUT) {
395 udelay(1);
396 i += 1;
397 }
398
399 if (i == LOOP_TIMEOUT) {
400 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
401 return -EIO;
402 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200403
404 return 0;
405}
406
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200407static void copy_cmd_to_buffer(struct amd_iommu *iommu,
408 struct iommu_cmd *cmd,
409 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200410{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200411 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200412
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200413 target = iommu->cmd_buf + tail;
414 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200415
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200416 /* Copy command to buffer */
417 memcpy(target, cmd, sizeof(*cmd));
418
419 /* Tell the IOMMU about it */
420 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
421}
422
Joerg Roedel815b33f2011-04-06 17:26:49 +0200423static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200424{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200425 WARN_ON(address & 0x7ULL);
426
Joerg Roedelded46732011-04-06 10:53:48 +0200427 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200428 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
429 cmd->data[1] = upper_32_bits(__pa(address));
430 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200431 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
432}
433
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200434static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
435{
436 memset(cmd, 0, sizeof(*cmd));
437 cmd->data[0] = devid;
438 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
439}
440
Joerg Roedel11b64022011-04-06 11:49:28 +0200441static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
442 size_t size, u16 domid, int pde)
443{
444 u64 pages;
445 int s;
446
447 pages = iommu_num_pages(address, size, PAGE_SIZE);
448 s = 0;
449
450 if (pages > 1) {
451 /*
452 * If we have to flush more than one page, flush all
453 * TLB entries for this domain
454 */
455 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
456 s = 1;
457 }
458
459 address &= PAGE_MASK;
460
461 memset(cmd, 0, sizeof(*cmd));
462 cmd->data[1] |= domid;
463 cmd->data[2] = lower_32_bits(address);
464 cmd->data[3] = upper_32_bits(address);
465 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
466 if (s) /* size bit - we flush more than one 4kb page */
467 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
468 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
469 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
470}
471
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200472static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
473 u64 address, size_t size)
474{
475 u64 pages;
476 int s;
477
478 pages = iommu_num_pages(address, size, PAGE_SIZE);
479 s = 0;
480
481 if (pages > 1) {
482 /*
483 * If we have to flush more than one page, flush all
484 * TLB entries for this domain
485 */
486 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
487 s = 1;
488 }
489
490 address &= PAGE_MASK;
491
492 memset(cmd, 0, sizeof(*cmd));
493 cmd->data[0] = devid;
494 cmd->data[0] |= (qdep & 0xff) << 24;
495 cmd->data[1] = devid;
496 cmd->data[2] = lower_32_bits(address);
497 cmd->data[3] = upper_32_bits(address);
498 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
499 if (s)
500 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
501}
502
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200503static void build_inv_all(struct iommu_cmd *cmd)
504{
505 memset(cmd, 0, sizeof(*cmd));
506 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200507}
508
Joerg Roedel431b2a22008-07-11 17:14:22 +0200509/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200510 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200511 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200512 */
Joerg Roedel815b33f2011-04-06 17:26:49 +0200513static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200514{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200515 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200516 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200517
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200518 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100519
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200520again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200521 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200522
523 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
524 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
525 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
526 left = (head - next_tail) % iommu->cmd_buf_size;
527
528 if (left <= 2) {
529 struct iommu_cmd sync_cmd;
530 volatile u64 sem = 0;
531 int ret;
532
533 build_completion_wait(&sync_cmd, (u64)&sem);
534 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
535
536 spin_unlock_irqrestore(&iommu->lock, flags);
537
538 if ((ret = wait_on_sem(&sem)) != 0)
539 return ret;
540
541 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200542 }
543
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200544 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200545
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200546 /* We need to sync now to make sure all commands are processed */
Joerg Roedel815b33f2011-04-06 17:26:49 +0200547 iommu->need_sync = true;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200548
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200549 spin_unlock_irqrestore(&iommu->lock, flags);
550
Joerg Roedel815b33f2011-04-06 17:26:49 +0200551 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100552}
553
554/*
555 * This function queues a completion wait command into the command
556 * buffer of an IOMMU
557 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100558static int iommu_completion_wait(struct amd_iommu *iommu)
559{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200560 struct iommu_cmd cmd;
561 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200562 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100563
564 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200565 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100566
Joerg Roedel815b33f2011-04-06 17:26:49 +0200567 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100568
Joerg Roedel815b33f2011-04-06 17:26:49 +0200569 ret = iommu_queue_command(iommu, &cmd);
Joerg Roedel8d201962008-12-02 20:34:41 +0100570 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200571 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100572
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200573 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200574}
575
Joerg Roedeld8c13082011-04-06 18:51:26 +0200576static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200577{
578 struct iommu_cmd cmd;
579
Joerg Roedeld8c13082011-04-06 18:51:26 +0200580 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200581
Joerg Roedeld8c13082011-04-06 18:51:26 +0200582 return iommu_queue_command(iommu, &cmd);
583}
584
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200585static void iommu_flush_dte_all(struct amd_iommu *iommu)
586{
587 u32 devid;
588
589 for (devid = 0; devid <= 0xffff; ++devid)
590 iommu_flush_dte(iommu, devid);
591
592 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200593}
594
595/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200596 * This function uses heavy locking and may disable irqs for some time. But
597 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200598 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200599static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200600{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200601 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200602
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200603 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
604 struct iommu_cmd cmd;
605 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
606 dom_id, 1);
607 iommu_queue_command(iommu, &cmd);
608 }
Joerg Roedel431b2a22008-07-11 17:14:22 +0200609
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200610 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200611}
612
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200613static void iommu_flush_all(struct amd_iommu *iommu)
614{
615 struct iommu_cmd cmd;
616
617 build_inv_all(&cmd);
618
619 iommu_queue_command(iommu, &cmd);
620 iommu_completion_wait(iommu);
621}
622
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200623void iommu_flush_all_caches(struct amd_iommu *iommu)
624{
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200625 if (iommu_feature(iommu, FEATURE_IA)) {
626 iommu_flush_all(iommu);
627 } else {
628 iommu_flush_dte_all(iommu);
629 iommu_flush_tlb_all(iommu);
630 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200631}
632
Joerg Roedel431b2a22008-07-11 17:14:22 +0200633/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200634 * Command send function for flushing on-device TLB
635 */
636static int device_flush_iotlb(struct device *dev, u64 address, size_t size)
637{
638 struct pci_dev *pdev = to_pci_dev(dev);
639 struct amd_iommu *iommu;
640 struct iommu_cmd cmd;
641 u16 devid;
642 int qdep;
643
644 qdep = pci_ats_queue_depth(pdev);
645 devid = get_device_id(dev);
646 iommu = amd_iommu_rlookup_table[devid];
647
648 build_inv_iotlb_pages(&cmd, devid, qdep, address, size);
649
650 return iommu_queue_command(iommu, &cmd);
651}
652
653/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200654 * Command send function for invalidating a device table entry
655 */
Joerg Roedeld8c13082011-04-06 18:51:26 +0200656static int device_flush_dte(struct device *dev)
Joerg Roedel3fa43652009-11-26 15:04:38 +0100657{
658 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200659 struct pci_dev *pdev;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100660 u16 devid;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200661 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100662
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200663 pdev = to_pci_dev(dev);
Joerg Roedel3fa43652009-11-26 15:04:38 +0100664 devid = get_device_id(dev);
665 iommu = amd_iommu_rlookup_table[devid];
666
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200667 ret = iommu_flush_dte(iommu, devid);
668 if (ret)
669 return ret;
670
671 if (pci_ats_enabled(pdev))
672 ret = device_flush_iotlb(dev, 0, ~0UL);
673
674 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100675}
676
Joerg Roedel431b2a22008-07-11 17:14:22 +0200677/*
678 * TLB invalidation function which is called from the mapping functions.
679 * It invalidates a single PTE if the range to flush is within a single
680 * page. Otherwise it flushes the whole TLB of the IOMMU.
681 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200682static void __domain_flush_pages(struct protection_domain *domain,
683 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200684{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200685 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +0200686 struct iommu_cmd cmd;
687 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200688
Joerg Roedel11b64022011-04-06 11:49:28 +0200689 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +0200690
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100691 for (i = 0; i < amd_iommus_present; ++i) {
692 if (!domain->dev_iommu[i])
693 continue;
694
695 /*
696 * Devices of this domain are behind this IOMMU
697 * We need a TLB flush
698 */
Joerg Roedel11b64022011-04-06 11:49:28 +0200699 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100700 }
701
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200702 list_for_each_entry(dev_data, &domain->dev_list, list) {
703 struct pci_dev *pdev = to_pci_dev(dev_data->dev);
704
705 if (!pci_ats_enabled(pdev))
706 continue;
707
708 ret |= device_flush_iotlb(dev_data->dev, address, size);
709 }
710
Joerg Roedel11b64022011-04-06 11:49:28 +0200711 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100712}
713
Joerg Roedel17b124b2011-04-06 18:01:35 +0200714static void domain_flush_pages(struct protection_domain *domain,
715 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100716{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200717 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200718}
Joerg Roedelb6c02712008-06-26 21:27:53 +0200719
Joerg Roedel1c655772008-09-04 18:40:05 +0200720/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200721static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +0200722{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200723 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +0200724}
725
Chris Wright42a49f92009-06-15 15:42:00 +0200726/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200727static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +0200728{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200729 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
730}
731
732static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +0200733{
734 int i;
735
736 for (i = 0; i < amd_iommus_present; ++i) {
737 if (!domain->dev_iommu[i])
738 continue;
739
740 /*
741 * Devices of this domain are behind this IOMMU
742 * We need to wait for completion of all commands.
743 */
744 iommu_completion_wait(amd_iommus[i]);
745 }
746}
747
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100748
Joerg Roedel43f49602008-12-02 21:01:12 +0100749/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100750 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +0100751 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200752static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200753{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100754 struct iommu_dev_data *dev_data;
755 unsigned long flags;
756
757 spin_lock_irqsave(&domain->lock, flags);
758
759 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedeld8c13082011-04-06 18:51:26 +0200760 device_flush_dte(dev_data->dev);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100761
762 spin_unlock_irqrestore(&domain->lock, flags);
763}
764
Joerg Roedel431b2a22008-07-11 17:14:22 +0200765/****************************************************************************
766 *
767 * The functions below are used the create the page table mappings for
768 * unity mapped regions.
769 *
770 ****************************************************************************/
771
772/*
Joerg Roedel308973d2009-11-24 17:43:32 +0100773 * This function is used to add another level to an IO page table. Adding
774 * another level increases the size of the address space by 9 bits to a size up
775 * to 64 bits.
776 */
777static bool increase_address_space(struct protection_domain *domain,
778 gfp_t gfp)
779{
780 u64 *pte;
781
782 if (domain->mode == PAGE_MODE_6_LEVEL)
783 /* address space already 64 bit large */
784 return false;
785
786 pte = (void *)get_zeroed_page(gfp);
787 if (!pte)
788 return false;
789
790 *pte = PM_LEVEL_PDE(domain->mode,
791 virt_to_phys(domain->pt_root));
792 domain->pt_root = pte;
793 domain->mode += 1;
794 domain->updated = true;
795
796 return true;
797}
798
799static u64 *alloc_pte(struct protection_domain *domain,
800 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100801 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +0100802 u64 **pte_page,
803 gfp_t gfp)
804{
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100805 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +0100806 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100807
808 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +0100809
810 while (address > PM_LEVEL_SIZE(domain->mode))
811 increase_address_space(domain, gfp);
812
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100813 level = domain->mode - 1;
814 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
815 address = PAGE_SIZE_ALIGN(address, page_size);
816 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +0100817
818 while (level > end_lvl) {
819 if (!IOMMU_PTE_PRESENT(*pte)) {
820 page = (u64 *)get_zeroed_page(gfp);
821 if (!page)
822 return NULL;
823 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
824 }
825
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100826 /* No level skipping support yet */
827 if (PM_PTE_LEVEL(*pte) != level)
828 return NULL;
829
Joerg Roedel308973d2009-11-24 17:43:32 +0100830 level -= 1;
831
832 pte = IOMMU_PTE_PAGE(*pte);
833
834 if (pte_page && level == end_lvl)
835 *pte_page = pte;
836
837 pte = &pte[PM_LEVEL_INDEX(level, address)];
838 }
839
840 return pte;
841}
842
843/*
844 * This function checks if there is a PTE for a given dma address. If
845 * there is one, it returns the pointer to it.
846 */
Joerg Roedel24cd7722010-01-19 17:27:39 +0100847static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +0100848{
849 int level;
850 u64 *pte;
851
Joerg Roedel24cd7722010-01-19 17:27:39 +0100852 if (address > PM_LEVEL_SIZE(domain->mode))
853 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +0100854
Joerg Roedel24cd7722010-01-19 17:27:39 +0100855 level = domain->mode - 1;
856 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
857
858 while (level > 0) {
859
860 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +0100861 if (!IOMMU_PTE_PRESENT(*pte))
862 return NULL;
863
Joerg Roedel24cd7722010-01-19 17:27:39 +0100864 /* Large PTE */
865 if (PM_PTE_LEVEL(*pte) == 0x07) {
866 unsigned long pte_mask, __pte;
867
868 /*
869 * If we have a series of large PTEs, make
870 * sure to return a pointer to the first one.
871 */
872 pte_mask = PTE_PAGE_SIZE(*pte);
873 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
874 __pte = ((unsigned long)pte) & pte_mask;
875
876 return (u64 *)__pte;
877 }
878
879 /* No level skipping support yet */
880 if (PM_PTE_LEVEL(*pte) != level)
881 return NULL;
882
Joerg Roedel308973d2009-11-24 17:43:32 +0100883 level -= 1;
884
Joerg Roedel24cd7722010-01-19 17:27:39 +0100885 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +0100886 pte = IOMMU_PTE_PAGE(*pte);
887 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +0100888 }
889
890 return pte;
891}
892
893/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200894 * Generic mapping functions. It maps a physical address into a DMA
895 * address space. It allocates the page table pages if necessary.
896 * In the future it can be extended to a generic mapping function
897 * supporting all features of AMD IOMMU page tables like level skipping
898 * and full 64 bit address spaces.
899 */
Joerg Roedel38e817f2008-12-02 17:27:52 +0100900static int iommu_map_page(struct protection_domain *dom,
901 unsigned long bus_addr,
902 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200903 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100904 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200905{
Joerg Roedel8bda3092009-05-12 12:02:46 +0200906 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100907 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200908
Joerg Roedelbad1cac2009-09-02 16:52:23 +0200909 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200910 return -EINVAL;
911
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100912 bus_addr = PAGE_ALIGN(bus_addr);
913 phys_addr = PAGE_ALIGN(phys_addr);
914 count = PAGE_SIZE_PTE_COUNT(page_size);
915 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200916
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100917 for (i = 0; i < count; ++i)
918 if (IOMMU_PTE_PRESENT(pte[i]))
919 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200920
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100921 if (page_size > PAGE_SIZE) {
922 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
923 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
924 } else
925 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
926
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200927 if (prot & IOMMU_PROT_IR)
928 __pte |= IOMMU_PTE_IR;
929 if (prot & IOMMU_PROT_IW)
930 __pte |= IOMMU_PTE_IW;
931
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100932 for (i = 0; i < count; ++i)
933 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200934
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200935 update_domain(dom);
936
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200937 return 0;
938}
939
Joerg Roedel24cd7722010-01-19 17:27:39 +0100940static unsigned long iommu_unmap_page(struct protection_domain *dom,
941 unsigned long bus_addr,
942 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100943{
Joerg Roedel24cd7722010-01-19 17:27:39 +0100944 unsigned long long unmap_size, unmapped;
945 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100946
Joerg Roedel24cd7722010-01-19 17:27:39 +0100947 BUG_ON(!is_power_of_2(page_size));
948
949 unmapped = 0;
950
951 while (unmapped < page_size) {
952
953 pte = fetch_pte(dom, bus_addr);
954
955 if (!pte) {
956 /*
957 * No PTE for this address
958 * move forward in 4kb steps
959 */
960 unmap_size = PAGE_SIZE;
961 } else if (PM_PTE_LEVEL(*pte) == 0) {
962 /* 4kb PTE found for this address */
963 unmap_size = PAGE_SIZE;
964 *pte = 0ULL;
965 } else {
966 int count, i;
967
968 /* Large PTE found which maps this address */
969 unmap_size = PTE_PAGE_SIZE(*pte);
970 count = PAGE_SIZE_PTE_COUNT(unmap_size);
971 for (i = 0; i < count; i++)
972 pte[i] = 0ULL;
973 }
974
975 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
976 unmapped += unmap_size;
977 }
978
979 BUG_ON(!is_power_of_2(unmapped));
980
981 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100982}
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100983
Joerg Roedel431b2a22008-07-11 17:14:22 +0200984/*
985 * This function checks if a specific unity mapping entry is needed for
986 * this specific IOMMU.
987 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200988static int iommu_for_unity_map(struct amd_iommu *iommu,
989 struct unity_map_entry *entry)
990{
991 u16 bdf, i;
992
993 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
994 bdf = amd_iommu_alias_table[i];
995 if (amd_iommu_rlookup_table[bdf] == iommu)
996 return 1;
997 }
998
999 return 0;
1000}
1001
Joerg Roedel431b2a22008-07-11 17:14:22 +02001002/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001003 * This function actually applies the mapping to the page table of the
1004 * dma_ops domain.
1005 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001006static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1007 struct unity_map_entry *e)
1008{
1009 u64 addr;
1010 int ret;
1011
1012 for (addr = e->address_start; addr < e->address_end;
1013 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001014 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001015 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001016 if (ret)
1017 return ret;
1018 /*
1019 * if unity mapping is in aperture range mark the page
1020 * as allocated in the aperture
1021 */
1022 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001023 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001024 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001025 }
1026
1027 return 0;
1028}
1029
Joerg Roedel431b2a22008-07-11 17:14:22 +02001030/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001031 * Init the unity mappings for a specific IOMMU in the system
1032 *
1033 * Basically iterates over all unity mapping entries and applies them to
1034 * the default domain DMA of that IOMMU if necessary.
1035 */
1036static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1037{
1038 struct unity_map_entry *entry;
1039 int ret;
1040
1041 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1042 if (!iommu_for_unity_map(iommu, entry))
1043 continue;
1044 ret = dma_ops_unity_map(iommu->default_dom, entry);
1045 if (ret)
1046 return ret;
1047 }
1048
1049 return 0;
1050}
1051
1052/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001053 * Inits the unity mappings required for a specific device
1054 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001055static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1056 u16 devid)
1057{
1058 struct unity_map_entry *e;
1059 int ret;
1060
1061 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1062 if (!(devid >= e->devid_start && devid <= e->devid_end))
1063 continue;
1064 ret = dma_ops_unity_map(dma_dom, e);
1065 if (ret)
1066 return ret;
1067 }
1068
1069 return 0;
1070}
1071
Joerg Roedel431b2a22008-07-11 17:14:22 +02001072/****************************************************************************
1073 *
1074 * The next functions belong to the address allocator for the dma_ops
1075 * interface functions. They work like the allocators in the other IOMMU
1076 * drivers. Its basically a bitmap which marks the allocated pages in
1077 * the aperture. Maybe it could be enhanced in the future to a more
1078 * efficient allocator.
1079 *
1080 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001081
Joerg Roedel431b2a22008-07-11 17:14:22 +02001082/*
Joerg Roedel384de722009-05-15 12:30:05 +02001083 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001084 *
1085 * called with domain->lock held
1086 */
Joerg Roedel384de722009-05-15 12:30:05 +02001087
Joerg Roedel9cabe892009-05-18 16:38:55 +02001088/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001089 * Used to reserve address ranges in the aperture (e.g. for exclusion
1090 * ranges.
1091 */
1092static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1093 unsigned long start_page,
1094 unsigned int pages)
1095{
1096 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1097
1098 if (start_page + pages > last_page)
1099 pages = last_page - start_page;
1100
1101 for (i = start_page; i < start_page + pages; ++i) {
1102 int index = i / APERTURE_RANGE_PAGES;
1103 int page = i % APERTURE_RANGE_PAGES;
1104 __set_bit(page, dom->aperture[index]->bitmap);
1105 }
1106}
1107
1108/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001109 * This function is used to add a new aperture range to an existing
1110 * aperture in case of dma_ops domain allocation or address allocation
1111 * failure.
1112 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001113static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001114 bool populate, gfp_t gfp)
1115{
1116 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001117 struct amd_iommu *iommu;
Joerg Roedeld91afd12010-01-22 16:40:20 +01001118 unsigned long i;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001119
Joerg Roedelf5e97052009-05-22 12:31:53 +02001120#ifdef CONFIG_IOMMU_STRESS
1121 populate = false;
1122#endif
1123
Joerg Roedel9cabe892009-05-18 16:38:55 +02001124 if (index >= APERTURE_MAX_RANGES)
1125 return -ENOMEM;
1126
1127 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1128 if (!dma_dom->aperture[index])
1129 return -ENOMEM;
1130
1131 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1132 if (!dma_dom->aperture[index]->bitmap)
1133 goto out_free;
1134
1135 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1136
1137 if (populate) {
1138 unsigned long address = dma_dom->aperture_size;
1139 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1140 u64 *pte, *pte_page;
1141
1142 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001143 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001144 &pte_page, gfp);
1145 if (!pte)
1146 goto out_free;
1147
1148 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1149
1150 address += APERTURE_RANGE_SIZE / 64;
1151 }
1152 }
1153
1154 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1155
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001156 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001157 for_each_iommu(iommu) {
1158 if (iommu->exclusion_start &&
1159 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1160 && iommu->exclusion_start < dma_dom->aperture_size) {
1161 unsigned long startpage;
1162 int pages = iommu_num_pages(iommu->exclusion_start,
1163 iommu->exclusion_length,
1164 PAGE_SIZE);
1165 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1166 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1167 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001168 }
1169
1170 /*
1171 * Check for areas already mapped as present in the new aperture
1172 * range and mark those pages as reserved in the allocator. Such
1173 * mappings may already exist as a result of requested unity
1174 * mappings for devices.
1175 */
1176 for (i = dma_dom->aperture[index]->offset;
1177 i < dma_dom->aperture_size;
1178 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001179 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001180 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1181 continue;
1182
1183 dma_ops_reserve_addresses(dma_dom, i << PAGE_SHIFT, 1);
1184 }
1185
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001186 update_domain(&dma_dom->domain);
1187
Joerg Roedel9cabe892009-05-18 16:38:55 +02001188 return 0;
1189
1190out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001191 update_domain(&dma_dom->domain);
1192
Joerg Roedel9cabe892009-05-18 16:38:55 +02001193 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1194
1195 kfree(dma_dom->aperture[index]);
1196 dma_dom->aperture[index] = NULL;
1197
1198 return -ENOMEM;
1199}
1200
Joerg Roedel384de722009-05-15 12:30:05 +02001201static unsigned long dma_ops_area_alloc(struct device *dev,
1202 struct dma_ops_domain *dom,
1203 unsigned int pages,
1204 unsigned long align_mask,
1205 u64 dma_mask,
1206 unsigned long start)
1207{
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001208 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001209 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1210 int i = start >> APERTURE_RANGE_SHIFT;
1211 unsigned long boundary_size;
1212 unsigned long address = -1;
1213 unsigned long limit;
1214
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001215 next_bit >>= PAGE_SHIFT;
1216
Joerg Roedel384de722009-05-15 12:30:05 +02001217 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1218 PAGE_SIZE) >> PAGE_SHIFT;
1219
1220 for (;i < max_index; ++i) {
1221 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1222
1223 if (dom->aperture[i]->offset >= dma_mask)
1224 break;
1225
1226 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1227 dma_mask >> PAGE_SHIFT);
1228
1229 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1230 limit, next_bit, pages, 0,
1231 boundary_size, align_mask);
1232 if (address != -1) {
1233 address = dom->aperture[i]->offset +
1234 (address << PAGE_SHIFT);
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001235 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001236 break;
1237 }
1238
1239 next_bit = 0;
1240 }
1241
1242 return address;
1243}
1244
Joerg Roedeld3086442008-06-26 21:27:57 +02001245static unsigned long dma_ops_alloc_addresses(struct device *dev,
1246 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001247 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001248 unsigned long align_mask,
1249 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001250{
Joerg Roedeld3086442008-06-26 21:27:57 +02001251 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001252
Joerg Roedelfe16f082009-05-22 12:27:53 +02001253#ifdef CONFIG_IOMMU_STRESS
1254 dom->next_address = 0;
1255 dom->need_flush = true;
1256#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001257
Joerg Roedel384de722009-05-15 12:30:05 +02001258 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001259 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001260
Joerg Roedel1c655772008-09-04 18:40:05 +02001261 if (address == -1) {
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001262 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001263 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1264 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001265 dom->need_flush = true;
1266 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001267
Joerg Roedel384de722009-05-15 12:30:05 +02001268 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001269 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001270
1271 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1272
1273 return address;
1274}
1275
Joerg Roedel431b2a22008-07-11 17:14:22 +02001276/*
1277 * The address free function.
1278 *
1279 * called with domain->lock held
1280 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001281static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1282 unsigned long address,
1283 unsigned int pages)
1284{
Joerg Roedel384de722009-05-15 12:30:05 +02001285 unsigned i = address >> APERTURE_RANGE_SHIFT;
1286 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001287
Joerg Roedel384de722009-05-15 12:30:05 +02001288 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1289
Joerg Roedel47bccd62009-05-22 12:40:54 +02001290#ifdef CONFIG_IOMMU_STRESS
1291 if (i < 4)
1292 return;
1293#endif
1294
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001295 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001296 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001297
1298 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001299
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001300 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001301
Joerg Roedeld3086442008-06-26 21:27:57 +02001302}
1303
Joerg Roedel431b2a22008-07-11 17:14:22 +02001304/****************************************************************************
1305 *
1306 * The next functions belong to the domain allocation. A domain is
1307 * allocated for every IOMMU as the default domain. If device isolation
1308 * is enabled, every device get its own domain. The most important thing
1309 * about domains is the page table mapping the DMA address space they
1310 * contain.
1311 *
1312 ****************************************************************************/
1313
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001314/*
1315 * This function adds a protection domain to the global protection domain list
1316 */
1317static void add_domain_to_list(struct protection_domain *domain)
1318{
1319 unsigned long flags;
1320
1321 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1322 list_add(&domain->list, &amd_iommu_pd_list);
1323 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1324}
1325
1326/*
1327 * This function removes a protection domain to the global
1328 * protection domain list
1329 */
1330static void del_domain_from_list(struct protection_domain *domain)
1331{
1332 unsigned long flags;
1333
1334 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1335 list_del(&domain->list);
1336 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1337}
1338
Joerg Roedelec487d12008-06-26 21:27:58 +02001339static u16 domain_id_alloc(void)
1340{
1341 unsigned long flags;
1342 int id;
1343
1344 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1345 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1346 BUG_ON(id == 0);
1347 if (id > 0 && id < MAX_DOMAIN_ID)
1348 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1349 else
1350 id = 0;
1351 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1352
1353 return id;
1354}
1355
Joerg Roedela2acfb72008-12-02 18:28:53 +01001356static void domain_id_free(int id)
1357{
1358 unsigned long flags;
1359
1360 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1361 if (id > 0 && id < MAX_DOMAIN_ID)
1362 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1363 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1364}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001365
Joerg Roedel86db2e52008-12-02 18:20:21 +01001366static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001367{
1368 int i, j;
1369 u64 *p1, *p2, *p3;
1370
Joerg Roedel86db2e52008-12-02 18:20:21 +01001371 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001372
1373 if (!p1)
1374 return;
1375
1376 for (i = 0; i < 512; ++i) {
1377 if (!IOMMU_PTE_PRESENT(p1[i]))
1378 continue;
1379
1380 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001381 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001382 if (!IOMMU_PTE_PRESENT(p2[j]))
1383 continue;
1384 p3 = IOMMU_PTE_PAGE(p2[j]);
1385 free_page((unsigned long)p3);
1386 }
1387
1388 free_page((unsigned long)p2);
1389 }
1390
1391 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001392
1393 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001394}
1395
Joerg Roedel431b2a22008-07-11 17:14:22 +02001396/*
1397 * Free a domain, only used if something went wrong in the
1398 * allocation path and we need to free an already allocated page table
1399 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001400static void dma_ops_domain_free(struct dma_ops_domain *dom)
1401{
Joerg Roedel384de722009-05-15 12:30:05 +02001402 int i;
1403
Joerg Roedelec487d12008-06-26 21:27:58 +02001404 if (!dom)
1405 return;
1406
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001407 del_domain_from_list(&dom->domain);
1408
Joerg Roedel86db2e52008-12-02 18:20:21 +01001409 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001410
Joerg Roedel384de722009-05-15 12:30:05 +02001411 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1412 if (!dom->aperture[i])
1413 continue;
1414 free_page((unsigned long)dom->aperture[i]->bitmap);
1415 kfree(dom->aperture[i]);
1416 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001417
1418 kfree(dom);
1419}
1420
Joerg Roedel431b2a22008-07-11 17:14:22 +02001421/*
1422 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001423 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001424 * structures required for the dma_ops interface
1425 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001426static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001427{
1428 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001429
1430 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1431 if (!dma_dom)
1432 return NULL;
1433
1434 spin_lock_init(&dma_dom->domain.lock);
1435
1436 dma_dom->domain.id = domain_id_alloc();
1437 if (dma_dom->domain.id == 0)
1438 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001439 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001440 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001441 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001442 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001443 dma_dom->domain.priv = dma_dom;
1444 if (!dma_dom->domain.pt_root)
1445 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001446
Joerg Roedel1c655772008-09-04 18:40:05 +02001447 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001448 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001449
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001450 add_domain_to_list(&dma_dom->domain);
1451
Joerg Roedel576175c2009-11-23 19:08:46 +01001452 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001453 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001454
Joerg Roedel431b2a22008-07-11 17:14:22 +02001455 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001456 * mark the first page as allocated so we never return 0 as
1457 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001458 */
Joerg Roedel384de722009-05-15 12:30:05 +02001459 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001460 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001461
Joerg Roedelec487d12008-06-26 21:27:58 +02001462
1463 return dma_dom;
1464
1465free_dma_dom:
1466 dma_ops_domain_free(dma_dom);
1467
1468 return NULL;
1469}
1470
Joerg Roedel431b2a22008-07-11 17:14:22 +02001471/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001472 * little helper function to check whether a given protection domain is a
1473 * dma_ops domain
1474 */
1475static bool dma_ops_domain(struct protection_domain *domain)
1476{
1477 return domain->flags & PD_DMA_OPS_MASK;
1478}
1479
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001480static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001481{
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001482 u64 pte_root = virt_to_phys(domain->pt_root);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001483 u32 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001484
Joerg Roedel38ddf412008-09-11 10:38:32 +02001485 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1486 << DEV_ENTRY_MODE_SHIFT;
1487 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001488
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001489 if (ats)
1490 flags |= DTE_FLAG_IOTLB;
1491
1492 amd_iommu_dev_table[devid].data[3] |= flags;
1493 amd_iommu_dev_table[devid].data[2] = domain->id;
1494 amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
1495 amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001496}
1497
Joerg Roedel15898bb2009-11-24 15:39:42 +01001498static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001499{
Joerg Roedel355bf552008-12-08 12:02:41 +01001500 /* remove entry from the device table seen by the hardware */
1501 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1502 amd_iommu_dev_table[devid].data[1] = 0;
1503 amd_iommu_dev_table[devid].data[2] = 0;
1504
Joerg Roedelc5cca142009-10-09 18:31:20 +02001505 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001506}
1507
1508static void do_attach(struct device *dev, struct protection_domain *domain)
1509{
1510 struct iommu_dev_data *dev_data;
1511 struct amd_iommu *iommu;
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001512 struct pci_dev *pdev;
1513 bool ats = false;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001514 u16 devid;
1515
1516 devid = get_device_id(dev);
1517 iommu = amd_iommu_rlookup_table[devid];
1518 dev_data = get_dev_data(dev);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001519 pdev = to_pci_dev(dev);
1520
1521 if (amd_iommu_iotlb_sup)
1522 ats = pci_ats_enabled(pdev);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001523
1524 /* Update data structures */
1525 dev_data->domain = domain;
1526 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001527 set_dte_entry(devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001528
1529 /* Do reference counting */
1530 domain->dev_iommu[iommu->index] += 1;
1531 domain->dev_cnt += 1;
1532
1533 /* Flush the DTE entry */
Joerg Roedeld8c13082011-04-06 18:51:26 +02001534 device_flush_dte(dev);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001535}
1536
1537static void do_detach(struct device *dev)
1538{
1539 struct iommu_dev_data *dev_data;
1540 struct amd_iommu *iommu;
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001541 struct pci_dev *pdev;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001542 u16 devid;
1543
1544 devid = get_device_id(dev);
1545 iommu = amd_iommu_rlookup_table[devid];
1546 dev_data = get_dev_data(dev);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001547 pdev = to_pci_dev(dev);
Joerg Roedelc5cca142009-10-09 18:31:20 +02001548
Joerg Roedelc4596112009-11-20 14:57:32 +01001549 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001550 dev_data->domain->dev_iommu[iommu->index] -= 1;
1551 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001552
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001553 /* Update data structures */
1554 dev_data->domain = NULL;
1555 list_del(&dev_data->list);
1556 clear_dte_entry(devid);
1557
1558 /* Flush the DTE entry */
Joerg Roedeld8c13082011-04-06 18:51:26 +02001559 device_flush_dte(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001560}
1561
1562/*
1563 * If a device is not yet associated with a domain, this function does
1564 * assigns it visible for the hardware
1565 */
1566static int __attach_device(struct device *dev,
1567 struct protection_domain *domain)
1568{
Joerg Roedel657cbb62009-11-23 15:26:46 +01001569 struct iommu_dev_data *dev_data, *alias_data;
Julia Lawall84fe6c12010-05-27 12:31:51 +02001570 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001571
Joerg Roedel657cbb62009-11-23 15:26:46 +01001572 dev_data = get_dev_data(dev);
1573 alias_data = get_dev_data(dev_data->alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001574
Joerg Roedel657cbb62009-11-23 15:26:46 +01001575 if (!alias_data)
1576 return -EINVAL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001577
1578 /* lock domain */
1579 spin_lock(&domain->lock);
1580
1581 /* Some sanity checks */
Julia Lawall84fe6c12010-05-27 12:31:51 +02001582 ret = -EBUSY;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001583 if (alias_data->domain != NULL &&
1584 alias_data->domain != domain)
Julia Lawall84fe6c12010-05-27 12:31:51 +02001585 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001586
Joerg Roedel657cbb62009-11-23 15:26:46 +01001587 if (dev_data->domain != NULL &&
1588 dev_data->domain != domain)
Julia Lawall84fe6c12010-05-27 12:31:51 +02001589 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001590
1591 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001592 if (dev_data->alias != dev) {
1593 alias_data = get_dev_data(dev_data->alias);
1594 if (alias_data->domain == NULL)
1595 do_attach(dev_data->alias, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01001596
1597 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001598 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01001599
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001600 if (dev_data->domain == NULL)
1601 do_attach(dev, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001602
Joerg Roedel24100052009-11-25 15:59:57 +01001603 atomic_inc(&dev_data->bind);
1604
Julia Lawall84fe6c12010-05-27 12:31:51 +02001605 ret = 0;
1606
1607out_unlock:
1608
Joerg Roedel355bf552008-12-08 12:02:41 +01001609 /* ready */
1610 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001611
Julia Lawall84fe6c12010-05-27 12:31:51 +02001612 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001613}
1614
1615/*
1616 * If a device is not yet associated with a domain, this function does
1617 * assigns it visible for the hardware
1618 */
1619static int attach_device(struct device *dev,
1620 struct protection_domain *domain)
1621{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001622 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001623 unsigned long flags;
1624 int ret;
1625
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001626 if (amd_iommu_iotlb_sup)
1627 pci_enable_ats(pdev, PAGE_SHIFT);
1628
Joerg Roedel15898bb2009-11-24 15:39:42 +01001629 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1630 ret = __attach_device(dev, domain);
1631 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1632
1633 /*
1634 * We might boot into a crash-kernel here. The crashed kernel
1635 * left the caches in the IOMMU dirty. So we have to flush
1636 * here to evict all dirty stuff.
1637 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001638 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001639
1640 return ret;
1641}
1642
1643/*
1644 * Removes a device from a protection domain (unlocked)
1645 */
1646static void __detach_device(struct device *dev)
1647{
Joerg Roedel657cbb62009-11-23 15:26:46 +01001648 struct iommu_dev_data *dev_data = get_dev_data(dev);
Joerg Roedel24100052009-11-25 15:59:57 +01001649 struct iommu_dev_data *alias_data;
Joerg Roedel2ca76272010-01-22 16:45:31 +01001650 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001651 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001652
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001653 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001654
Joerg Roedel2ca76272010-01-22 16:45:31 +01001655 domain = dev_data->domain;
1656
1657 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01001658
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001659 if (dev_data->alias != dev) {
Joerg Roedel24100052009-11-25 15:59:57 +01001660 alias_data = get_dev_data(dev_data->alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001661 if (atomic_dec_and_test(&alias_data->bind))
1662 do_detach(dev_data->alias);
Joerg Roedel24100052009-11-25 15:59:57 +01001663 }
1664
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001665 if (atomic_dec_and_test(&dev_data->bind))
1666 do_detach(dev);
1667
Joerg Roedel2ca76272010-01-22 16:45:31 +01001668 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001669
Joerg Roedel21129f72009-09-01 11:59:42 +02001670 /*
1671 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001672 * passthrough domain if it is detached from any other domain.
1673 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02001674 */
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001675 if (iommu_pass_through &&
1676 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedel15898bb2009-11-24 15:39:42 +01001677 __attach_device(dev, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01001678}
1679
1680/*
1681 * Removes a device from a protection domain (with devtable_lock held)
1682 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01001683static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01001684{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001685 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedel355bf552008-12-08 12:02:41 +01001686 unsigned long flags;
1687
1688 /* lock device table */
1689 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001690 __detach_device(dev);
Joerg Roedel355bf552008-12-08 12:02:41 +01001691 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001692
1693 if (amd_iommu_iotlb_sup && pci_ats_enabled(pdev))
1694 pci_disable_ats(pdev);
Joerg Roedel355bf552008-12-08 12:02:41 +01001695}
Joerg Roedele275a2a2008-12-10 18:27:25 +01001696
Joerg Roedel15898bb2009-11-24 15:39:42 +01001697/*
1698 * Find out the protection domain structure for a given PCI device. This
1699 * will give us the pointer to the page table root for example.
1700 */
1701static struct protection_domain *domain_for_device(struct device *dev)
1702{
1703 struct protection_domain *dom;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001704 struct iommu_dev_data *dev_data, *alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001705 unsigned long flags;
1706 u16 devid, alias;
1707
Joerg Roedel657cbb62009-11-23 15:26:46 +01001708 devid = get_device_id(dev);
1709 alias = amd_iommu_alias_table[devid];
1710 dev_data = get_dev_data(dev);
1711 alias_data = get_dev_data(dev_data->alias);
1712 if (!alias_data)
1713 return NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001714
1715 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001716 dom = dev_data->domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001717 if (dom == NULL &&
Joerg Roedel657cbb62009-11-23 15:26:46 +01001718 alias_data->domain != NULL) {
1719 __attach_device(dev, alias_data->domain);
1720 dom = alias_data->domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001721 }
1722
1723 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1724
1725 return dom;
1726}
1727
Joerg Roedele275a2a2008-12-10 18:27:25 +01001728static int device_change_notifier(struct notifier_block *nb,
1729 unsigned long action, void *data)
1730{
1731 struct device *dev = data;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001732 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001733 struct protection_domain *domain;
1734 struct dma_ops_domain *dma_domain;
1735 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001736 unsigned long flags;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001737
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001738 if (!check_device(dev))
1739 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001740
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001741 devid = get_device_id(dev);
1742 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01001743
1744 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07001745 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001746
1747 domain = domain_for_device(dev);
1748
Joerg Roedele275a2a2008-12-10 18:27:25 +01001749 if (!domain)
1750 goto out;
Joerg Roedela1ca3312009-09-01 12:22:22 +02001751 if (iommu_pass_through)
1752 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001753 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001754 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001755 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001756
1757 iommu_init_device(dev);
1758
1759 domain = domain_for_device(dev);
1760
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001761 /* allocate a protection domain if a device is added */
1762 dma_domain = find_protection_domain(devid);
1763 if (dma_domain)
1764 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01001765 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001766 if (!dma_domain)
1767 goto out;
1768 dma_domain->target_dev = devid;
1769
1770 spin_lock_irqsave(&iommu_pd_list_lock, flags);
1771 list_add_tail(&dma_domain->list, &iommu_pd_list);
1772 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
1773
1774 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001775 case BUS_NOTIFY_DEL_DEVICE:
1776
1777 iommu_uninit_device(dev);
1778
Joerg Roedele275a2a2008-12-10 18:27:25 +01001779 default:
1780 goto out;
1781 }
1782
Joerg Roedeld8c13082011-04-06 18:51:26 +02001783 device_flush_dte(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001784 iommu_completion_wait(iommu);
1785
1786out:
1787 return 0;
1788}
1789
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05301790static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01001791 .notifier_call = device_change_notifier,
1792};
Joerg Roedel355bf552008-12-08 12:02:41 +01001793
Joerg Roedel8638c492009-12-10 11:12:25 +01001794void amd_iommu_init_notifier(void)
1795{
1796 bus_register_notifier(&pci_bus_type, &device_nb);
1797}
1798
Joerg Roedel431b2a22008-07-11 17:14:22 +02001799/*****************************************************************************
1800 *
1801 * The next functions belong to the dma_ops mapping/unmapping code.
1802 *
1803 *****************************************************************************/
1804
1805/*
1806 * In the dma_ops path we only have the struct device. This function
1807 * finds the corresponding IOMMU, the protection domain and the
1808 * requestor id for a given device.
1809 * If the device is not yet associated with a domain this is also done
1810 * in this function.
1811 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001812static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001813{
Joerg Roedel94f6d192009-11-24 16:40:02 +01001814 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001815 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01001816 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001817
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001818 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01001819 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001820
Joerg Roedel94f6d192009-11-24 16:40:02 +01001821 domain = domain_for_device(dev);
1822 if (domain != NULL && !dma_ops_domain(domain))
1823 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001824
Joerg Roedel94f6d192009-11-24 16:40:02 +01001825 if (domain != NULL)
1826 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001827
Joerg Roedel15898bb2009-11-24 15:39:42 +01001828 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001829 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001830 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01001831 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
1832 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001833 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01001834 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01001835
Joerg Roedel94f6d192009-11-24 16:40:02 +01001836 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001837}
1838
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001839static void update_device_table(struct protection_domain *domain)
1840{
Joerg Roedel492667d2009-11-27 13:25:47 +01001841 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001842
Joerg Roedel492667d2009-11-27 13:25:47 +01001843 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001844 struct pci_dev *pdev = to_pci_dev(dev_data->dev);
Joerg Roedel492667d2009-11-27 13:25:47 +01001845 u16 devid = get_device_id(dev_data->dev);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001846 set_dte_entry(devid, domain, pci_ats_enabled(pdev));
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001847 }
1848}
1849
1850static void update_domain(struct protection_domain *domain)
1851{
1852 if (!domain->updated)
1853 return;
1854
1855 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02001856
1857 domain_flush_devices(domain);
1858 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001859
1860 domain->updated = false;
1861}
1862
Joerg Roedel431b2a22008-07-11 17:14:22 +02001863/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02001864 * This function fetches the PTE for a given address in the aperture
1865 */
1866static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
1867 unsigned long address)
1868{
Joerg Roedel384de722009-05-15 12:30:05 +02001869 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02001870 u64 *pte, *pte_page;
1871
Joerg Roedel384de722009-05-15 12:30:05 +02001872 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1873 if (!aperture)
1874 return NULL;
1875
1876 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02001877 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001878 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001879 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02001880 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
1881 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001882 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001883
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001884 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001885
1886 return pte;
1887}
1888
1889/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001890 * This is the generic map function. It maps one 4kb page at paddr to
1891 * the given address in the DMA address space for the domain.
1892 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001893static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001894 unsigned long address,
1895 phys_addr_t paddr,
1896 int direction)
1897{
1898 u64 *pte, __pte;
1899
1900 WARN_ON(address > dom->aperture_size);
1901
1902 paddr &= PAGE_MASK;
1903
Joerg Roedel8bda3092009-05-12 12:02:46 +02001904 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02001905 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001906 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001907
1908 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
1909
1910 if (direction == DMA_TO_DEVICE)
1911 __pte |= IOMMU_PTE_IR;
1912 else if (direction == DMA_FROM_DEVICE)
1913 __pte |= IOMMU_PTE_IW;
1914 else if (direction == DMA_BIDIRECTIONAL)
1915 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
1916
1917 WARN_ON(*pte);
1918
1919 *pte = __pte;
1920
1921 return (dma_addr_t)address;
1922}
1923
Joerg Roedel431b2a22008-07-11 17:14:22 +02001924/*
1925 * The generic unmapping function for on page in the DMA address space.
1926 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001927static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001928 unsigned long address)
1929{
Joerg Roedel384de722009-05-15 12:30:05 +02001930 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001931 u64 *pte;
1932
1933 if (address >= dom->aperture_size)
1934 return;
1935
Joerg Roedel384de722009-05-15 12:30:05 +02001936 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1937 if (!aperture)
1938 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001939
Joerg Roedel384de722009-05-15 12:30:05 +02001940 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
1941 if (!pte)
1942 return;
1943
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001944 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001945
1946 WARN_ON(!*pte);
1947
1948 *pte = 0ULL;
1949}
1950
Joerg Roedel431b2a22008-07-11 17:14:22 +02001951/*
1952 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01001953 * contiguous memory region into DMA address space. It is used by all
1954 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001955 * Must be called with the domain lock held.
1956 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02001957static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001958 struct dma_ops_domain *dma_dom,
1959 phys_addr_t paddr,
1960 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001961 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001962 bool align,
1963 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02001964{
1965 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02001966 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001967 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001968 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001969 int i;
1970
Joerg Roedele3c449f2008-10-15 22:02:11 -07001971 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001972 paddr &= PAGE_MASK;
1973
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01001974 INC_STATS_COUNTER(total_map_requests);
1975
Joerg Roedelc1858972008-12-12 15:42:39 +01001976 if (pages > 1)
1977 INC_STATS_COUNTER(cross_page);
1978
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001979 if (align)
1980 align_mask = (1UL << get_order(size)) - 1;
1981
Joerg Roedel11b83882009-05-19 10:23:15 +02001982retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02001983 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
1984 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001985 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02001986 /*
1987 * setting next_address here will let the address
1988 * allocator only scan the new allocated range in the
1989 * first run. This is a small optimization.
1990 */
1991 dma_dom->next_address = dma_dom->aperture_size;
1992
Joerg Roedel576175c2009-11-23 19:08:46 +01001993 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02001994 goto out;
1995
1996 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001997 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02001998 * allocation again
1999 */
2000 goto retry;
2001 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002002
2003 start = address;
2004 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002005 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002006 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002007 goto out_unmap;
2008
Joerg Roedelcb76c322008-06-26 21:28:00 +02002009 paddr += PAGE_SIZE;
2010 start += PAGE_SIZE;
2011 }
2012 address += offset;
2013
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002014 ADD_STATS_COUNTER(alloced_io_mem, size);
2015
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002016 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002017 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002018 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002019 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002020 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002021
Joerg Roedelcb76c322008-06-26 21:28:00 +02002022out:
2023 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002024
2025out_unmap:
2026
2027 for (--i; i >= 0; --i) {
2028 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002029 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002030 }
2031
2032 dma_ops_free_addresses(dma_dom, address, pages);
2033
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002034 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002035}
2036
Joerg Roedel431b2a22008-07-11 17:14:22 +02002037/*
2038 * Does the reverse of the __map_single function. Must be called with
2039 * the domain lock held too
2040 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002041static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002042 dma_addr_t dma_addr,
2043 size_t size,
2044 int dir)
2045{
Joerg Roedel04e04632010-09-23 16:12:48 +02002046 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002047 dma_addr_t i, start;
2048 unsigned int pages;
2049
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002050 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002051 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002052 return;
2053
Joerg Roedel04e04632010-09-23 16:12:48 +02002054 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002055 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002056 dma_addr &= PAGE_MASK;
2057 start = dma_addr;
2058
2059 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002060 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002061 start += PAGE_SIZE;
2062 }
2063
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002064 SUB_STATS_COUNTER(alloced_io_mem, size);
2065
Joerg Roedelcb76c322008-06-26 21:28:00 +02002066 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002067
Joerg Roedel80be3082008-11-06 14:59:05 +01002068 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002069 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002070 dma_dom->need_flush = false;
2071 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002072}
2073
Joerg Roedel431b2a22008-07-11 17:14:22 +02002074/*
2075 * The exported map_single function for dma_ops.
2076 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002077static dma_addr_t map_page(struct device *dev, struct page *page,
2078 unsigned long offset, size_t size,
2079 enum dma_data_direction dir,
2080 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002081{
2082 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002083 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002084 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002085 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002086 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002087
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002088 INC_STATS_COUNTER(cnt_map_single);
2089
Joerg Roedel94f6d192009-11-24 16:40:02 +01002090 domain = get_domain(dev);
2091 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002092 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002093 else if (IS_ERR(domain))
2094 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002095
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002096 dma_mask = *dev->dma_mask;
2097
Joerg Roedel4da70b92008-06-26 21:28:01 +02002098 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002099
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002100 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002101 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002102 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002103 goto out;
2104
Joerg Roedel17b124b2011-04-06 18:01:35 +02002105 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002106
2107out:
2108 spin_unlock_irqrestore(&domain->lock, flags);
2109
2110 return addr;
2111}
2112
Joerg Roedel431b2a22008-07-11 17:14:22 +02002113/*
2114 * The exported unmap_single function for dma_ops.
2115 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002116static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2117 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002118{
2119 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002120 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002121
Joerg Roedel146a6912008-12-12 15:07:12 +01002122 INC_STATS_COUNTER(cnt_unmap_single);
2123
Joerg Roedel94f6d192009-11-24 16:40:02 +01002124 domain = get_domain(dev);
2125 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002126 return;
2127
Joerg Roedel4da70b92008-06-26 21:28:01 +02002128 spin_lock_irqsave(&domain->lock, flags);
2129
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002130 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002131
Joerg Roedel17b124b2011-04-06 18:01:35 +02002132 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002133
2134 spin_unlock_irqrestore(&domain->lock, flags);
2135}
2136
Joerg Roedel431b2a22008-07-11 17:14:22 +02002137/*
2138 * This is a special map_sg function which is used if we should map a
2139 * device which is not handled by an AMD IOMMU in the system.
2140 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002141static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2142 int nelems, int dir)
2143{
2144 struct scatterlist *s;
2145 int i;
2146
2147 for_each_sg(sglist, s, nelems, i) {
2148 s->dma_address = (dma_addr_t)sg_phys(s);
2149 s->dma_length = s->length;
2150 }
2151
2152 return nelems;
2153}
2154
Joerg Roedel431b2a22008-07-11 17:14:22 +02002155/*
2156 * The exported map_sg function for dma_ops (handles scatter-gather
2157 * lists).
2158 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002159static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002160 int nelems, enum dma_data_direction dir,
2161 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002162{
2163 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002164 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002165 int i;
2166 struct scatterlist *s;
2167 phys_addr_t paddr;
2168 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002169 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002170
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002171 INC_STATS_COUNTER(cnt_map_sg);
2172
Joerg Roedel94f6d192009-11-24 16:40:02 +01002173 domain = get_domain(dev);
2174 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002175 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002176 else if (IS_ERR(domain))
2177 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002178
Joerg Roedel832a90c2008-09-18 15:54:23 +02002179 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002180
Joerg Roedel65b050a2008-06-26 21:28:02 +02002181 spin_lock_irqsave(&domain->lock, flags);
2182
2183 for_each_sg(sglist, s, nelems, i) {
2184 paddr = sg_phys(s);
2185
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002186 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002187 paddr, s->length, dir, false,
2188 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002189
2190 if (s->dma_address) {
2191 s->dma_length = s->length;
2192 mapped_elems++;
2193 } else
2194 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002195 }
2196
Joerg Roedel17b124b2011-04-06 18:01:35 +02002197 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002198
2199out:
2200 spin_unlock_irqrestore(&domain->lock, flags);
2201
2202 return mapped_elems;
2203unmap:
2204 for_each_sg(sglist, s, mapped_elems, i) {
2205 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002206 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002207 s->dma_length, dir);
2208 s->dma_address = s->dma_length = 0;
2209 }
2210
2211 mapped_elems = 0;
2212
2213 goto out;
2214}
2215
Joerg Roedel431b2a22008-07-11 17:14:22 +02002216/*
2217 * The exported map_sg function for dma_ops (handles scatter-gather
2218 * lists).
2219 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002220static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002221 int nelems, enum dma_data_direction dir,
2222 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002223{
2224 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002225 struct protection_domain *domain;
2226 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002227 int i;
2228
Joerg Roedel55877a62008-12-12 15:12:14 +01002229 INC_STATS_COUNTER(cnt_unmap_sg);
2230
Joerg Roedel94f6d192009-11-24 16:40:02 +01002231 domain = get_domain(dev);
2232 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002233 return;
2234
Joerg Roedel65b050a2008-06-26 21:28:02 +02002235 spin_lock_irqsave(&domain->lock, flags);
2236
2237 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002238 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002239 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002240 s->dma_address = s->dma_length = 0;
2241 }
2242
Joerg Roedel17b124b2011-04-06 18:01:35 +02002243 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002244
2245 spin_unlock_irqrestore(&domain->lock, flags);
2246}
2247
Joerg Roedel431b2a22008-07-11 17:14:22 +02002248/*
2249 * The exported alloc_coherent function for dma_ops.
2250 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002251static void *alloc_coherent(struct device *dev, size_t size,
2252 dma_addr_t *dma_addr, gfp_t flag)
2253{
2254 unsigned long flags;
2255 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002256 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002257 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002258 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002259
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002260 INC_STATS_COUNTER(cnt_alloc_coherent);
2261
Joerg Roedel94f6d192009-11-24 16:40:02 +01002262 domain = get_domain(dev);
2263 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002264 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2265 *dma_addr = __pa(virt_addr);
2266 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002267 } else if (IS_ERR(domain))
2268 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002269
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002270 dma_mask = dev->coherent_dma_mask;
2271 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2272 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002273
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002274 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2275 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302276 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002277
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002278 paddr = virt_to_phys(virt_addr);
2279
Joerg Roedel832a90c2008-09-18 15:54:23 +02002280 if (!dma_mask)
2281 dma_mask = *dev->dma_mask;
2282
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002283 spin_lock_irqsave(&domain->lock, flags);
2284
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002285 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002286 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002287
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002288 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002289 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002290 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002291 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002292
Joerg Roedel17b124b2011-04-06 18:01:35 +02002293 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002294
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002295 spin_unlock_irqrestore(&domain->lock, flags);
2296
2297 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002298
2299out_free:
2300
2301 free_pages((unsigned long)virt_addr, get_order(size));
2302
2303 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002304}
2305
Joerg Roedel431b2a22008-07-11 17:14:22 +02002306/*
2307 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002308 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002309static void free_coherent(struct device *dev, size_t size,
2310 void *virt_addr, dma_addr_t dma_addr)
2311{
2312 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002313 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002314
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002315 INC_STATS_COUNTER(cnt_free_coherent);
2316
Joerg Roedel94f6d192009-11-24 16:40:02 +01002317 domain = get_domain(dev);
2318 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002319 goto free_mem;
2320
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002321 spin_lock_irqsave(&domain->lock, flags);
2322
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002323 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002324
Joerg Roedel17b124b2011-04-06 18:01:35 +02002325 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002326
2327 spin_unlock_irqrestore(&domain->lock, flags);
2328
2329free_mem:
2330 free_pages((unsigned long)virt_addr, get_order(size));
2331}
2332
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002333/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002334 * This function is called by the DMA layer to find out if we can handle a
2335 * particular device. It is part of the dma_ops.
2336 */
2337static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2338{
Joerg Roedel420aef82009-11-23 16:14:57 +01002339 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002340}
2341
2342/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002343 * The function for pre-allocating protection domains.
2344 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002345 * If the driver core informs the DMA layer if a driver grabs a device
2346 * we don't need to preallocate the protection domains anymore.
2347 * For now we have to.
2348 */
Jaswinder Singh Rajput0e93dd82008-12-29 21:45:22 +05302349static void prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002350{
2351 struct pci_dev *dev = NULL;
2352 struct dma_ops_domain *dma_dom;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002353 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002354
Chris Wrightd18c69d2010-04-02 18:27:55 -07002355 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002356
2357 /* Do we handle this device? */
2358 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002359 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002360
2361 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002362 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002363 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002364
2365 devid = get_device_id(&dev->dev);
2366
Joerg Roedel87a64d52009-11-24 17:26:43 +01002367 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002368 if (!dma_dom)
2369 continue;
2370 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002371 dma_dom->target_dev = devid;
2372
Joerg Roedel15898bb2009-11-24 15:39:42 +01002373 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01002374
Joerg Roedelbd60b732008-09-11 10:24:48 +02002375 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002376 }
2377}
2378
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002379static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002380 .alloc_coherent = alloc_coherent,
2381 .free_coherent = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002382 .map_page = map_page,
2383 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002384 .map_sg = map_sg,
2385 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002386 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002387};
2388
Joerg Roedel431b2a22008-07-11 17:14:22 +02002389/*
2390 * The function which clues the AMD IOMMU driver into dma_ops.
2391 */
Joerg Roedelf5325092010-01-22 17:44:35 +01002392
2393void __init amd_iommu_init_api(void)
2394{
2395 register_iommu(&amd_iommu_ops);
2396}
2397
Joerg Roedel6631ee92008-06-26 21:28:05 +02002398int __init amd_iommu_init_dma_ops(void)
2399{
2400 struct amd_iommu *iommu;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002401 int ret;
2402
Joerg Roedel431b2a22008-07-11 17:14:22 +02002403 /*
2404 * first allocate a default protection domain for every IOMMU we
2405 * found in the system. Devices not assigned to any other
2406 * protection domain will be assigned to the default one.
2407 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02002408 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01002409 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002410 if (iommu->default_dom == NULL)
2411 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01002412 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002413 ret = iommu_init_unity_mappings(iommu);
2414 if (ret)
2415 goto free_domains;
2416 }
2417
Joerg Roedel431b2a22008-07-11 17:14:22 +02002418 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01002419 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002420 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01002421 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002422
2423 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002424 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002425
Joerg Roedel431b2a22008-07-11 17:14:22 +02002426 /* Make the driver finally visible to the drivers */
Joerg Roedel6631ee92008-06-26 21:28:05 +02002427 dma_ops = &amd_iommu_dma_ops;
2428
Joerg Roedel7f265082008-12-12 13:50:21 +01002429 amd_iommu_stats_init();
2430
Joerg Roedel6631ee92008-06-26 21:28:05 +02002431 return 0;
2432
2433free_domains:
2434
Joerg Roedel3bd22172009-05-04 15:06:20 +02002435 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002436 if (iommu->default_dom)
2437 dma_ops_domain_free(iommu->default_dom);
2438 }
2439
2440 return ret;
2441}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002442
2443/*****************************************************************************
2444 *
2445 * The following functions belong to the exported interface of AMD IOMMU
2446 *
2447 * This interface allows access to lower level functions of the IOMMU
2448 * like protection domain handling and assignement of devices to domains
2449 * which is not possible with the dma_ops interface.
2450 *
2451 *****************************************************************************/
2452
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002453static void cleanup_domain(struct protection_domain *domain)
2454{
Joerg Roedel492667d2009-11-27 13:25:47 +01002455 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002456 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002457
2458 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2459
Joerg Roedel492667d2009-11-27 13:25:47 +01002460 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
2461 struct device *dev = dev_data->dev;
2462
Chris Wright04e856c2010-02-17 08:51:20 -08002463 __detach_device(dev);
Joerg Roedel492667d2009-11-27 13:25:47 +01002464 atomic_set(&dev_data->bind, 0);
2465 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002466
2467 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2468}
2469
Joerg Roedel26508152009-08-26 16:52:40 +02002470static void protection_domain_free(struct protection_domain *domain)
2471{
2472 if (!domain)
2473 return;
2474
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002475 del_domain_from_list(domain);
2476
Joerg Roedel26508152009-08-26 16:52:40 +02002477 if (domain->id)
2478 domain_id_free(domain->id);
2479
2480 kfree(domain);
2481}
2482
2483static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002484{
2485 struct protection_domain *domain;
2486
2487 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2488 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002489 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002490
2491 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002492 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01002493 domain->id = domain_id_alloc();
2494 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02002495 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002496 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02002497
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002498 add_domain_to_list(domain);
2499
Joerg Roedel26508152009-08-26 16:52:40 +02002500 return domain;
2501
2502out_err:
2503 kfree(domain);
2504
2505 return NULL;
2506}
2507
2508static int amd_iommu_domain_init(struct iommu_domain *dom)
2509{
2510 struct protection_domain *domain;
2511
2512 domain = protection_domain_alloc();
2513 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01002514 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02002515
2516 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002517 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2518 if (!domain->pt_root)
2519 goto out_free;
2520
2521 dom->priv = domain;
2522
2523 return 0;
2524
2525out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02002526 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01002527
2528 return -ENOMEM;
2529}
2530
Joerg Roedel98383fc2008-12-02 18:34:12 +01002531static void amd_iommu_domain_destroy(struct iommu_domain *dom)
2532{
2533 struct protection_domain *domain = dom->priv;
2534
2535 if (!domain)
2536 return;
2537
2538 if (domain->dev_cnt > 0)
2539 cleanup_domain(domain);
2540
2541 BUG_ON(domain->dev_cnt != 0);
2542
2543 free_pagetable(domain);
2544
Joerg Roedel8b408fe2010-03-08 14:20:07 +01002545 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002546
2547 dom->priv = NULL;
2548}
2549
Joerg Roedel684f2882008-12-08 12:07:44 +01002550static void amd_iommu_detach_device(struct iommu_domain *dom,
2551 struct device *dev)
2552{
Joerg Roedel657cbb62009-11-23 15:26:46 +01002553 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002554 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002555 u16 devid;
2556
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002557 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01002558 return;
2559
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002560 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002561
Joerg Roedel657cbb62009-11-23 15:26:46 +01002562 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002563 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002564
2565 iommu = amd_iommu_rlookup_table[devid];
2566 if (!iommu)
2567 return;
2568
Joerg Roedeld8c13082011-04-06 18:51:26 +02002569 device_flush_dte(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002570 iommu_completion_wait(iommu);
2571}
2572
Joerg Roedel01106062008-12-02 19:34:11 +01002573static int amd_iommu_attach_device(struct iommu_domain *dom,
2574 struct device *dev)
2575{
2576 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002577 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01002578 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002579 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002580 u16 devid;
2581
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002582 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01002583 return -EINVAL;
2584
Joerg Roedel657cbb62009-11-23 15:26:46 +01002585 dev_data = dev->archdata.iommu;
2586
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002587 devid = get_device_id(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002588
2589 iommu = amd_iommu_rlookup_table[devid];
2590 if (!iommu)
2591 return -EINVAL;
2592
Joerg Roedel657cbb62009-11-23 15:26:46 +01002593 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002594 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002595
Joerg Roedel15898bb2009-11-24 15:39:42 +01002596 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01002597
2598 iommu_completion_wait(iommu);
2599
Joerg Roedel15898bb2009-11-24 15:39:42 +01002600 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002601}
2602
Joerg Roedel468e2362010-01-21 16:37:36 +01002603static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
2604 phys_addr_t paddr, int gfp_order, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002605{
Joerg Roedel468e2362010-01-21 16:37:36 +01002606 unsigned long page_size = 0x1000UL << gfp_order;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002607 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002608 int prot = 0;
2609 int ret;
2610
2611 if (iommu_prot & IOMMU_READ)
2612 prot |= IOMMU_PROT_IR;
2613 if (iommu_prot & IOMMU_WRITE)
2614 prot |= IOMMU_PROT_IW;
2615
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002616 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002617 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002618 mutex_unlock(&domain->api_lock);
2619
Joerg Roedel795e74f72010-05-11 17:40:57 +02002620 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002621}
2622
Joerg Roedel468e2362010-01-21 16:37:36 +01002623static int amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2624 int gfp_order)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002625{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002626 struct protection_domain *domain = dom->priv;
Joerg Roedel468e2362010-01-21 16:37:36 +01002627 unsigned long page_size, unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002628
Joerg Roedel468e2362010-01-21 16:37:36 +01002629 page_size = 0x1000UL << gfp_order;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002630
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002631 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01002632 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002633 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002634
Joerg Roedel17b124b2011-04-06 18:01:35 +02002635 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002636
Joerg Roedel468e2362010-01-21 16:37:36 +01002637 return get_order(unmap_size);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002638}
2639
Joerg Roedel645c4c82008-12-02 20:05:50 +01002640static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2641 unsigned long iova)
2642{
2643 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002644 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002645 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002646 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002647
Joerg Roedel24cd7722010-01-19 17:27:39 +01002648 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002649
Joerg Roedela6d41a42009-09-02 17:08:55 +02002650 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01002651 return 0;
2652
Joerg Roedelf03152b2010-01-21 16:15:24 +01002653 if (PM_PTE_LEVEL(*pte) == 0)
2654 offset_mask = PAGE_SIZE - 1;
2655 else
2656 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
2657
2658 __pte = *pte & PM_ADDR_MASK;
2659 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002660
2661 return paddr;
2662}
2663
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002664static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
2665 unsigned long cap)
2666{
Joerg Roedel80a506b2010-07-27 17:14:24 +02002667 switch (cap) {
2668 case IOMMU_CAP_CACHE_COHERENCY:
2669 return 1;
2670 }
2671
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002672 return 0;
2673}
2674
Joerg Roedel26961ef2008-12-03 17:00:17 +01002675static struct iommu_ops amd_iommu_ops = {
2676 .domain_init = amd_iommu_domain_init,
2677 .domain_destroy = amd_iommu_domain_destroy,
2678 .attach_dev = amd_iommu_attach_device,
2679 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01002680 .map = amd_iommu_map,
2681 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002682 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002683 .domain_has_cap = amd_iommu_domain_has_cap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002684};
2685
Joerg Roedel0feae532009-08-26 15:26:30 +02002686/*****************************************************************************
2687 *
2688 * The next functions do a basic initialization of IOMMU for pass through
2689 * mode
2690 *
2691 * In passthrough mode the IOMMU is initialized and enabled but not used for
2692 * DMA-API translation.
2693 *
2694 *****************************************************************************/
2695
2696int __init amd_iommu_init_passthrough(void)
2697{
Joerg Roedel15898bb2009-11-24 15:39:42 +01002698 struct amd_iommu *iommu;
Joerg Roedel0feae532009-08-26 15:26:30 +02002699 struct pci_dev *dev = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002700 u16 devid;
Joerg Roedel0feae532009-08-26 15:26:30 +02002701
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002702 /* allocate passthrough domain */
Joerg Roedel0feae532009-08-26 15:26:30 +02002703 pt_domain = protection_domain_alloc();
2704 if (!pt_domain)
2705 return -ENOMEM;
2706
2707 pt_domain->mode |= PAGE_MODE_NONE;
2708
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04002709 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002710 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02002711 continue;
2712
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002713 devid = get_device_id(&dev->dev);
2714
Joerg Roedel15898bb2009-11-24 15:39:42 +01002715 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02002716 if (!iommu)
2717 continue;
2718
Joerg Roedel15898bb2009-11-24 15:39:42 +01002719 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02002720 }
2721
2722 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
2723
2724 return 0;
2725}