blob: b2c309b07427e53e1889e85ea8270e904f34ba0b [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 Roedel27c21272011-05-30 15:56:24 +020033#include <asm/dma.h>
Joerg Roedel6a9401a2009-11-20 13:22:21 +010034#include <asm/amd_iommu_proto.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020035#include <asm/amd_iommu_types.h>
Joerg Roedelc6da9922008-06-26 21:28:06 +020036#include <asm/amd_iommu.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020037
38#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
39
Joerg Roedel815b33f2011-04-06 17:26:49 +020040#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020041
Joerg Roedelb6c02712008-06-26 21:27:53 +020042static DEFINE_RWLOCK(amd_iommu_devtable_lock);
43
Joerg Roedelbd60b732008-09-11 10:24:48 +020044/* A list of preallocated protection domains */
45static LIST_HEAD(iommu_pd_list);
46static DEFINE_SPINLOCK(iommu_pd_list_lock);
47
Joerg Roedel0feae532009-08-26 15:26:30 +020048/*
49 * Domain for untranslated devices - only allocated
50 * if iommu=pt passed on kernel cmd line.
51 */
52static struct protection_domain *pt_domain;
53
Joerg Roedel26961ef2008-12-03 17:00:17 +010054static struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010055
Joerg Roedel431b2a22008-07-11 17:14:22 +020056/*
57 * general struct to manage commands send to an IOMMU
58 */
Joerg Roedeld6449532008-07-11 17:14:28 +020059struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +020060 u32 data[4];
61};
62
Joerg Roedel04bfdd82009-09-02 16:00:23 +020063static void update_domain(struct protection_domain *domain);
Chris Wrightc1eee672009-05-21 00:56:58 -070064
Joerg Roedel15898bb2009-11-24 15:39:42 +010065/****************************************************************************
66 *
67 * Helper functions
68 *
69 ****************************************************************************/
70
71static inline u16 get_device_id(struct device *dev)
72{
73 struct pci_dev *pdev = to_pci_dev(dev);
74
75 return calc_devid(pdev->bus->number, pdev->devfn);
76}
77
Joerg Roedel657cbb62009-11-23 15:26:46 +010078static struct iommu_dev_data *get_dev_data(struct device *dev)
79{
80 return dev->archdata.iommu;
81}
82
Joerg Roedel71c70982009-11-24 16:43:06 +010083/*
84 * In this function the list of preallocated protection domains is traversed to
85 * find the domain for a specific device
86 */
87static struct dma_ops_domain *find_protection_domain(u16 devid)
88{
89 struct dma_ops_domain *entry, *ret = NULL;
90 unsigned long flags;
91 u16 alias = amd_iommu_alias_table[devid];
92
93 if (list_empty(&iommu_pd_list))
94 return NULL;
95
96 spin_lock_irqsave(&iommu_pd_list_lock, flags);
97
98 list_for_each_entry(entry, &iommu_pd_list, list) {
99 if (entry->target_dev == devid ||
100 entry->target_dev == alias) {
101 ret = entry;
102 break;
103 }
104 }
105
106 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
107
108 return ret;
109}
110
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100111/*
112 * This function checks if the driver got a valid device from the caller to
113 * avoid dereferencing invalid pointers.
114 */
115static bool check_device(struct device *dev)
116{
117 u16 devid;
118
119 if (!dev || !dev->dma_mask)
120 return false;
121
122 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100123 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100124 return false;
125
126 devid = get_device_id(dev);
127
128 /* Out of our scope? */
129 if (devid > amd_iommu_last_bdf)
130 return false;
131
132 if (amd_iommu_rlookup_table[devid] == NULL)
133 return false;
134
135 return true;
136}
137
Joerg Roedel657cbb62009-11-23 15:26:46 +0100138static int iommu_init_device(struct device *dev)
139{
140 struct iommu_dev_data *dev_data;
141 struct pci_dev *pdev;
142 u16 devid, alias;
143
144 if (dev->archdata.iommu)
145 return 0;
146
147 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
148 if (!dev_data)
149 return -ENOMEM;
150
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100151 dev_data->dev = dev;
152
Joerg Roedel657cbb62009-11-23 15:26:46 +0100153 devid = get_device_id(dev);
154 alias = amd_iommu_alias_table[devid];
155 pdev = pci_get_bus_and_slot(PCI_BUS(alias), alias & 0xff);
156 if (pdev)
157 dev_data->alias = &pdev->dev;
158
Joerg Roedel24100052009-11-25 15:59:57 +0100159 atomic_set(&dev_data->bind, 0);
160
Joerg Roedel657cbb62009-11-23 15:26:46 +0100161 dev->archdata.iommu = dev_data;
162
163
164 return 0;
165}
166
167static void iommu_uninit_device(struct device *dev)
168{
169 kfree(dev->archdata.iommu);
170}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100171
172void __init amd_iommu_uninit_devices(void)
173{
174 struct pci_dev *pdev = NULL;
175
176 for_each_pci_dev(pdev) {
177
178 if (!check_device(&pdev->dev))
179 continue;
180
181 iommu_uninit_device(&pdev->dev);
182 }
183}
184
185int __init amd_iommu_init_devices(void)
186{
187 struct pci_dev *pdev = NULL;
188 int ret = 0;
189
190 for_each_pci_dev(pdev) {
191
192 if (!check_device(&pdev->dev))
193 continue;
194
195 ret = iommu_init_device(&pdev->dev);
196 if (ret)
197 goto out_free;
198 }
199
200 return 0;
201
202out_free:
203
204 amd_iommu_uninit_devices();
205
206 return ret;
207}
Joerg Roedel7f265082008-12-12 13:50:21 +0100208#ifdef CONFIG_AMD_IOMMU_STATS
209
210/*
211 * Initialization code for statistics collection
212 */
213
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100214DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100215DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100216DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100217DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100218DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100219DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100220DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100221DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100222DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100223DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100224DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100225DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100226
Joerg Roedel7f265082008-12-12 13:50:21 +0100227static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100228static struct dentry *de_fflush;
229
230static void amd_iommu_stats_add(struct __iommu_counter *cnt)
231{
232 if (stats_dir == NULL)
233 return;
234
235 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
236 &cnt->value);
237}
238
239static void amd_iommu_stats_init(void)
240{
241 stats_dir = debugfs_create_dir("amd-iommu", NULL);
242 if (stats_dir == NULL)
243 return;
244
Joerg Roedel7f265082008-12-12 13:50:21 +0100245 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
246 (u32 *)&amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100247
248 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100249 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100250 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100251 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100252 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100253 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100254 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100255 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100256 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100257 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100258 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100259 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100260}
261
262#endif
263
Joerg Roedel431b2a22008-07-11 17:14:22 +0200264/****************************************************************************
265 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200266 * Interrupt handling functions
267 *
268 ****************************************************************************/
269
Joerg Roedele3e59872009-09-03 14:02:10 +0200270static void dump_dte_entry(u16 devid)
271{
272 int i;
273
274 for (i = 0; i < 8; ++i)
275 pr_err("AMD-Vi: DTE[%d]: %08x\n", i,
276 amd_iommu_dev_table[devid].data[i]);
277}
278
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200279static void dump_command(unsigned long phys_addr)
280{
281 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
282 int i;
283
284 for (i = 0; i < 4; ++i)
285 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
286}
287
Joerg Roedela345b232009-09-03 15:01:43 +0200288static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200289{
290 u32 *event = __evt;
291 int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
292 int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
293 int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
294 int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
295 u64 address = (u64)(((u64)event[3]) << 32) | event[2];
296
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200297 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200298
299 switch (type) {
300 case EVENT_TYPE_ILL_DEV:
301 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
302 "address=0x%016llx flags=0x%04x]\n",
303 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
304 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200305 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200306 break;
307 case EVENT_TYPE_IO_FAULT:
308 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
309 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
310 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
311 domid, address, flags);
312 break;
313 case EVENT_TYPE_DEV_TAB_ERR:
314 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
315 "address=0x%016llx flags=0x%04x]\n",
316 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
317 address, flags);
318 break;
319 case EVENT_TYPE_PAGE_TAB_ERR:
320 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
321 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
322 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
323 domid, address, flags);
324 break;
325 case EVENT_TYPE_ILL_CMD:
326 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200327 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200328 break;
329 case EVENT_TYPE_CMD_HARD_ERR:
330 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
331 "flags=0x%04x]\n", address, flags);
332 break;
333 case EVENT_TYPE_IOTLB_INV_TO:
334 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
335 "address=0x%016llx]\n",
336 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
337 address);
338 break;
339 case EVENT_TYPE_INV_DEV_REQ:
340 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
341 "address=0x%016llx flags=0x%04x]\n",
342 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
343 address, flags);
344 break;
345 default:
346 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
347 }
348}
349
350static void iommu_poll_events(struct amd_iommu *iommu)
351{
352 u32 head, tail;
353 unsigned long flags;
354
355 spin_lock_irqsave(&iommu->lock, flags);
356
357 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
358 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
359
360 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200361 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200362 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
363 }
364
365 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
366
367 spin_unlock_irqrestore(&iommu->lock, flags);
368}
369
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200370irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200371{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200372 struct amd_iommu *iommu;
373
Joerg Roedel3bd22172009-05-04 15:06:20 +0200374 for_each_iommu(iommu)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200375 iommu_poll_events(iommu);
376
377 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200378}
379
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200380irqreturn_t amd_iommu_int_handler(int irq, void *data)
381{
382 return IRQ_WAKE_THREAD;
383}
384
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200385/****************************************************************************
386 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200387 * IOMMU command queuing functions
388 *
389 ****************************************************************************/
390
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200391static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200392{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200393 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200394
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200395 while (*sem == 0 && i < LOOP_TIMEOUT) {
396 udelay(1);
397 i += 1;
398 }
399
400 if (i == LOOP_TIMEOUT) {
401 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
402 return -EIO;
403 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200404
405 return 0;
406}
407
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200408static void copy_cmd_to_buffer(struct amd_iommu *iommu,
409 struct iommu_cmd *cmd,
410 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200411{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200412 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200413
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200414 target = iommu->cmd_buf + tail;
415 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200416
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200417 /* Copy command to buffer */
418 memcpy(target, cmd, sizeof(*cmd));
419
420 /* Tell the IOMMU about it */
421 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
422}
423
Joerg Roedel815b33f2011-04-06 17:26:49 +0200424static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200425{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200426 WARN_ON(address & 0x7ULL);
427
Joerg Roedelded46732011-04-06 10:53:48 +0200428 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200429 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
430 cmd->data[1] = upper_32_bits(__pa(address));
431 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200432 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
433}
434
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200435static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
436{
437 memset(cmd, 0, sizeof(*cmd));
438 cmd->data[0] = devid;
439 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
440}
441
Joerg Roedel11b64022011-04-06 11:49:28 +0200442static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
443 size_t size, u16 domid, int pde)
444{
445 u64 pages;
446 int s;
447
448 pages = iommu_num_pages(address, size, PAGE_SIZE);
449 s = 0;
450
451 if (pages > 1) {
452 /*
453 * If we have to flush more than one page, flush all
454 * TLB entries for this domain
455 */
456 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
457 s = 1;
458 }
459
460 address &= PAGE_MASK;
461
462 memset(cmd, 0, sizeof(*cmd));
463 cmd->data[1] |= domid;
464 cmd->data[2] = lower_32_bits(address);
465 cmd->data[3] = upper_32_bits(address);
466 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
467 if (s) /* size bit - we flush more than one 4kb page */
468 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
469 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
470 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
471}
472
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200473static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
474 u64 address, size_t size)
475{
476 u64 pages;
477 int s;
478
479 pages = iommu_num_pages(address, size, PAGE_SIZE);
480 s = 0;
481
482 if (pages > 1) {
483 /*
484 * If we have to flush more than one page, flush all
485 * TLB entries for this domain
486 */
487 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
488 s = 1;
489 }
490
491 address &= PAGE_MASK;
492
493 memset(cmd, 0, sizeof(*cmd));
494 cmd->data[0] = devid;
495 cmd->data[0] |= (qdep & 0xff) << 24;
496 cmd->data[1] = devid;
497 cmd->data[2] = lower_32_bits(address);
498 cmd->data[3] = upper_32_bits(address);
499 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
500 if (s)
501 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
502}
503
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200504static void build_inv_all(struct iommu_cmd *cmd)
505{
506 memset(cmd, 0, sizeof(*cmd));
507 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200508}
509
Joerg Roedel431b2a22008-07-11 17:14:22 +0200510/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200511 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200512 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200513 */
Joerg Roedel815b33f2011-04-06 17:26:49 +0200514static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200515{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200516 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200517 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200518
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200519 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100520
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200521again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200522 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200523
524 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
525 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
526 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
527 left = (head - next_tail) % iommu->cmd_buf_size;
528
529 if (left <= 2) {
530 struct iommu_cmd sync_cmd;
531 volatile u64 sem = 0;
532 int ret;
533
534 build_completion_wait(&sync_cmd, (u64)&sem);
535 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
536
537 spin_unlock_irqrestore(&iommu->lock, flags);
538
539 if ((ret = wait_on_sem(&sem)) != 0)
540 return ret;
541
542 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200543 }
544
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200545 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200546
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200547 /* We need to sync now to make sure all commands are processed */
Joerg Roedel815b33f2011-04-06 17:26:49 +0200548 iommu->need_sync = true;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200549
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200550 spin_unlock_irqrestore(&iommu->lock, flags);
551
Joerg Roedel815b33f2011-04-06 17:26:49 +0200552 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100553}
554
555/*
556 * This function queues a completion wait command into the command
557 * buffer of an IOMMU
558 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100559static int iommu_completion_wait(struct amd_iommu *iommu)
560{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200561 struct iommu_cmd cmd;
562 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200563 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100564
565 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200566 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100567
Joerg Roedel815b33f2011-04-06 17:26:49 +0200568 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100569
Joerg Roedel815b33f2011-04-06 17:26:49 +0200570 ret = iommu_queue_command(iommu, &cmd);
Joerg Roedel8d201962008-12-02 20:34:41 +0100571 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200572 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100573
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200574 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200575}
576
Joerg Roedeld8c13082011-04-06 18:51:26 +0200577static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200578{
579 struct iommu_cmd cmd;
580
Joerg Roedeld8c13082011-04-06 18:51:26 +0200581 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200582
Joerg Roedeld8c13082011-04-06 18:51:26 +0200583 return iommu_queue_command(iommu, &cmd);
584}
585
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200586static void iommu_flush_dte_all(struct amd_iommu *iommu)
587{
588 u32 devid;
589
590 for (devid = 0; devid <= 0xffff; ++devid)
591 iommu_flush_dte(iommu, devid);
592
593 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200594}
595
596/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200597 * This function uses heavy locking and may disable irqs for some time. But
598 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200599 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200600static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200601{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200602 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200603
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200604 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
605 struct iommu_cmd cmd;
606 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
607 dom_id, 1);
608 iommu_queue_command(iommu, &cmd);
609 }
Joerg Roedel431b2a22008-07-11 17:14:22 +0200610
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200611 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200612}
613
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200614static void iommu_flush_all(struct amd_iommu *iommu)
615{
616 struct iommu_cmd cmd;
617
618 build_inv_all(&cmd);
619
620 iommu_queue_command(iommu, &cmd);
621 iommu_completion_wait(iommu);
622}
623
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200624void iommu_flush_all_caches(struct amd_iommu *iommu)
625{
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200626 if (iommu_feature(iommu, FEATURE_IA)) {
627 iommu_flush_all(iommu);
628 } else {
629 iommu_flush_dte_all(iommu);
630 iommu_flush_tlb_all(iommu);
631 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200632}
633
Joerg Roedel431b2a22008-07-11 17:14:22 +0200634/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200635 * Command send function for flushing on-device TLB
636 */
637static int device_flush_iotlb(struct device *dev, u64 address, size_t size)
638{
639 struct pci_dev *pdev = to_pci_dev(dev);
640 struct amd_iommu *iommu;
641 struct iommu_cmd cmd;
642 u16 devid;
643 int qdep;
644
645 qdep = pci_ats_queue_depth(pdev);
646 devid = get_device_id(dev);
647 iommu = amd_iommu_rlookup_table[devid];
648
649 build_inv_iotlb_pages(&cmd, devid, qdep, address, size);
650
651 return iommu_queue_command(iommu, &cmd);
652}
653
654/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200655 * Command send function for invalidating a device table entry
656 */
Joerg Roedeld8c13082011-04-06 18:51:26 +0200657static int device_flush_dte(struct device *dev)
Joerg Roedel3fa43652009-11-26 15:04:38 +0100658{
659 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200660 struct pci_dev *pdev;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100661 u16 devid;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200662 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100663
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200664 pdev = to_pci_dev(dev);
Joerg Roedel3fa43652009-11-26 15:04:38 +0100665 devid = get_device_id(dev);
666 iommu = amd_iommu_rlookup_table[devid];
667
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200668 ret = iommu_flush_dte(iommu, devid);
669 if (ret)
670 return ret;
671
672 if (pci_ats_enabled(pdev))
673 ret = device_flush_iotlb(dev, 0, ~0UL);
674
675 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100676}
677
Joerg Roedel431b2a22008-07-11 17:14:22 +0200678/*
679 * TLB invalidation function which is called from the mapping functions.
680 * It invalidates a single PTE if the range to flush is within a single
681 * page. Otherwise it flushes the whole TLB of the IOMMU.
682 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200683static void __domain_flush_pages(struct protection_domain *domain,
684 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200685{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200686 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +0200687 struct iommu_cmd cmd;
688 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200689
Joerg Roedel11b64022011-04-06 11:49:28 +0200690 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +0200691
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100692 for (i = 0; i < amd_iommus_present; ++i) {
693 if (!domain->dev_iommu[i])
694 continue;
695
696 /*
697 * Devices of this domain are behind this IOMMU
698 * We need a TLB flush
699 */
Joerg Roedel11b64022011-04-06 11:49:28 +0200700 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100701 }
702
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200703 list_for_each_entry(dev_data, &domain->dev_list, list) {
704 struct pci_dev *pdev = to_pci_dev(dev_data->dev);
705
706 if (!pci_ats_enabled(pdev))
707 continue;
708
709 ret |= device_flush_iotlb(dev_data->dev, address, size);
710 }
711
Joerg Roedel11b64022011-04-06 11:49:28 +0200712 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100713}
714
Joerg Roedel17b124b2011-04-06 18:01:35 +0200715static void domain_flush_pages(struct protection_domain *domain,
716 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100717{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200718 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200719}
Joerg Roedelb6c02712008-06-26 21:27:53 +0200720
Joerg Roedel1c655772008-09-04 18:40:05 +0200721/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200722static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +0200723{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200724 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +0200725}
726
Chris Wright42a49f92009-06-15 15:42:00 +0200727/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200728static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +0200729{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200730 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
731}
732
733static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +0200734{
735 int i;
736
737 for (i = 0; i < amd_iommus_present; ++i) {
738 if (!domain->dev_iommu[i])
739 continue;
740
741 /*
742 * Devices of this domain are behind this IOMMU
743 * We need to wait for completion of all commands.
744 */
745 iommu_completion_wait(amd_iommus[i]);
746 }
747}
748
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100749
Joerg Roedel43f49602008-12-02 21:01:12 +0100750/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100751 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +0100752 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200753static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200754{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100755 struct iommu_dev_data *dev_data;
756 unsigned long flags;
757
758 spin_lock_irqsave(&domain->lock, flags);
759
760 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedeld8c13082011-04-06 18:51:26 +0200761 device_flush_dte(dev_data->dev);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100762
763 spin_unlock_irqrestore(&domain->lock, flags);
764}
765
Joerg Roedel431b2a22008-07-11 17:14:22 +0200766/****************************************************************************
767 *
768 * The functions below are used the create the page table mappings for
769 * unity mapped regions.
770 *
771 ****************************************************************************/
772
773/*
Joerg Roedel308973d2009-11-24 17:43:32 +0100774 * This function is used to add another level to an IO page table. Adding
775 * another level increases the size of the address space by 9 bits to a size up
776 * to 64 bits.
777 */
778static bool increase_address_space(struct protection_domain *domain,
779 gfp_t gfp)
780{
781 u64 *pte;
782
783 if (domain->mode == PAGE_MODE_6_LEVEL)
784 /* address space already 64 bit large */
785 return false;
786
787 pte = (void *)get_zeroed_page(gfp);
788 if (!pte)
789 return false;
790
791 *pte = PM_LEVEL_PDE(domain->mode,
792 virt_to_phys(domain->pt_root));
793 domain->pt_root = pte;
794 domain->mode += 1;
795 domain->updated = true;
796
797 return true;
798}
799
800static u64 *alloc_pte(struct protection_domain *domain,
801 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100802 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +0100803 u64 **pte_page,
804 gfp_t gfp)
805{
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100806 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +0100807 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100808
809 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +0100810
811 while (address > PM_LEVEL_SIZE(domain->mode))
812 increase_address_space(domain, gfp);
813
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100814 level = domain->mode - 1;
815 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
816 address = PAGE_SIZE_ALIGN(address, page_size);
817 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +0100818
819 while (level > end_lvl) {
820 if (!IOMMU_PTE_PRESENT(*pte)) {
821 page = (u64 *)get_zeroed_page(gfp);
822 if (!page)
823 return NULL;
824 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
825 }
826
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100827 /* No level skipping support yet */
828 if (PM_PTE_LEVEL(*pte) != level)
829 return NULL;
830
Joerg Roedel308973d2009-11-24 17:43:32 +0100831 level -= 1;
832
833 pte = IOMMU_PTE_PAGE(*pte);
834
835 if (pte_page && level == end_lvl)
836 *pte_page = pte;
837
838 pte = &pte[PM_LEVEL_INDEX(level, address)];
839 }
840
841 return pte;
842}
843
844/*
845 * This function checks if there is a PTE for a given dma address. If
846 * there is one, it returns the pointer to it.
847 */
Joerg Roedel24cd7722010-01-19 17:27:39 +0100848static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +0100849{
850 int level;
851 u64 *pte;
852
Joerg Roedel24cd7722010-01-19 17:27:39 +0100853 if (address > PM_LEVEL_SIZE(domain->mode))
854 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +0100855
Joerg Roedel24cd7722010-01-19 17:27:39 +0100856 level = domain->mode - 1;
857 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
858
859 while (level > 0) {
860
861 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +0100862 if (!IOMMU_PTE_PRESENT(*pte))
863 return NULL;
864
Joerg Roedel24cd7722010-01-19 17:27:39 +0100865 /* Large PTE */
866 if (PM_PTE_LEVEL(*pte) == 0x07) {
867 unsigned long pte_mask, __pte;
868
869 /*
870 * If we have a series of large PTEs, make
871 * sure to return a pointer to the first one.
872 */
873 pte_mask = PTE_PAGE_SIZE(*pte);
874 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
875 __pte = ((unsigned long)pte) & pte_mask;
876
877 return (u64 *)__pte;
878 }
879
880 /* No level skipping support yet */
881 if (PM_PTE_LEVEL(*pte) != level)
882 return NULL;
883
Joerg Roedel308973d2009-11-24 17:43:32 +0100884 level -= 1;
885
Joerg Roedel24cd7722010-01-19 17:27:39 +0100886 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +0100887 pte = IOMMU_PTE_PAGE(*pte);
888 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +0100889 }
890
891 return pte;
892}
893
894/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200895 * Generic mapping functions. It maps a physical address into a DMA
896 * address space. It allocates the page table pages if necessary.
897 * In the future it can be extended to a generic mapping function
898 * supporting all features of AMD IOMMU page tables like level skipping
899 * and full 64 bit address spaces.
900 */
Joerg Roedel38e817f2008-12-02 17:27:52 +0100901static int iommu_map_page(struct protection_domain *dom,
902 unsigned long bus_addr,
903 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200904 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100905 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200906{
Joerg Roedel8bda3092009-05-12 12:02:46 +0200907 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100908 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200909
Joerg Roedelbad1cac2009-09-02 16:52:23 +0200910 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200911 return -EINVAL;
912
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100913 bus_addr = PAGE_ALIGN(bus_addr);
914 phys_addr = PAGE_ALIGN(phys_addr);
915 count = PAGE_SIZE_PTE_COUNT(page_size);
916 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200917
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100918 for (i = 0; i < count; ++i)
919 if (IOMMU_PTE_PRESENT(pte[i]))
920 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200921
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100922 if (page_size > PAGE_SIZE) {
923 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
924 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
925 } else
926 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
927
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200928 if (prot & IOMMU_PROT_IR)
929 __pte |= IOMMU_PTE_IR;
930 if (prot & IOMMU_PROT_IW)
931 __pte |= IOMMU_PTE_IW;
932
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100933 for (i = 0; i < count; ++i)
934 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200935
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200936 update_domain(dom);
937
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200938 return 0;
939}
940
Joerg Roedel24cd7722010-01-19 17:27:39 +0100941static unsigned long iommu_unmap_page(struct protection_domain *dom,
942 unsigned long bus_addr,
943 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100944{
Joerg Roedel24cd7722010-01-19 17:27:39 +0100945 unsigned long long unmap_size, unmapped;
946 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100947
Joerg Roedel24cd7722010-01-19 17:27:39 +0100948 BUG_ON(!is_power_of_2(page_size));
949
950 unmapped = 0;
951
952 while (unmapped < page_size) {
953
954 pte = fetch_pte(dom, bus_addr);
955
956 if (!pte) {
957 /*
958 * No PTE for this address
959 * move forward in 4kb steps
960 */
961 unmap_size = PAGE_SIZE;
962 } else if (PM_PTE_LEVEL(*pte) == 0) {
963 /* 4kb PTE found for this address */
964 unmap_size = PAGE_SIZE;
965 *pte = 0ULL;
966 } else {
967 int count, i;
968
969 /* Large PTE found which maps this address */
970 unmap_size = PTE_PAGE_SIZE(*pte);
971 count = PAGE_SIZE_PTE_COUNT(unmap_size);
972 for (i = 0; i < count; i++)
973 pte[i] = 0ULL;
974 }
975
976 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
977 unmapped += unmap_size;
978 }
979
980 BUG_ON(!is_power_of_2(unmapped));
981
982 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100983}
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100984
Joerg Roedel431b2a22008-07-11 17:14:22 +0200985/*
986 * This function checks if a specific unity mapping entry is needed for
987 * this specific IOMMU.
988 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200989static int iommu_for_unity_map(struct amd_iommu *iommu,
990 struct unity_map_entry *entry)
991{
992 u16 bdf, i;
993
994 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
995 bdf = amd_iommu_alias_table[i];
996 if (amd_iommu_rlookup_table[bdf] == iommu)
997 return 1;
998 }
999
1000 return 0;
1001}
1002
Joerg Roedel431b2a22008-07-11 17:14:22 +02001003/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001004 * This function actually applies the mapping to the page table of the
1005 * dma_ops domain.
1006 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001007static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1008 struct unity_map_entry *e)
1009{
1010 u64 addr;
1011 int ret;
1012
1013 for (addr = e->address_start; addr < e->address_end;
1014 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001015 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001016 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001017 if (ret)
1018 return ret;
1019 /*
1020 * if unity mapping is in aperture range mark the page
1021 * as allocated in the aperture
1022 */
1023 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001024 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001025 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001026 }
1027
1028 return 0;
1029}
1030
Joerg Roedel431b2a22008-07-11 17:14:22 +02001031/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001032 * Init the unity mappings for a specific IOMMU in the system
1033 *
1034 * Basically iterates over all unity mapping entries and applies them to
1035 * the default domain DMA of that IOMMU if necessary.
1036 */
1037static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1038{
1039 struct unity_map_entry *entry;
1040 int ret;
1041
1042 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1043 if (!iommu_for_unity_map(iommu, entry))
1044 continue;
1045 ret = dma_ops_unity_map(iommu->default_dom, entry);
1046 if (ret)
1047 return ret;
1048 }
1049
1050 return 0;
1051}
1052
1053/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001054 * Inits the unity mappings required for a specific device
1055 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001056static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1057 u16 devid)
1058{
1059 struct unity_map_entry *e;
1060 int ret;
1061
1062 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1063 if (!(devid >= e->devid_start && devid <= e->devid_end))
1064 continue;
1065 ret = dma_ops_unity_map(dma_dom, e);
1066 if (ret)
1067 return ret;
1068 }
1069
1070 return 0;
1071}
1072
Joerg Roedel431b2a22008-07-11 17:14:22 +02001073/****************************************************************************
1074 *
1075 * The next functions belong to the address allocator for the dma_ops
1076 * interface functions. They work like the allocators in the other IOMMU
1077 * drivers. Its basically a bitmap which marks the allocated pages in
1078 * the aperture. Maybe it could be enhanced in the future to a more
1079 * efficient allocator.
1080 *
1081 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001082
Joerg Roedel431b2a22008-07-11 17:14:22 +02001083/*
Joerg Roedel384de722009-05-15 12:30:05 +02001084 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001085 *
1086 * called with domain->lock held
1087 */
Joerg Roedel384de722009-05-15 12:30:05 +02001088
Joerg Roedel9cabe892009-05-18 16:38:55 +02001089/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001090 * Used to reserve address ranges in the aperture (e.g. for exclusion
1091 * ranges.
1092 */
1093static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1094 unsigned long start_page,
1095 unsigned int pages)
1096{
1097 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1098
1099 if (start_page + pages > last_page)
1100 pages = last_page - start_page;
1101
1102 for (i = start_page; i < start_page + pages; ++i) {
1103 int index = i / APERTURE_RANGE_PAGES;
1104 int page = i % APERTURE_RANGE_PAGES;
1105 __set_bit(page, dom->aperture[index]->bitmap);
1106 }
1107}
1108
1109/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001110 * This function is used to add a new aperture range to an existing
1111 * aperture in case of dma_ops domain allocation or address allocation
1112 * failure.
1113 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001114static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001115 bool populate, gfp_t gfp)
1116{
1117 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001118 struct amd_iommu *iommu;
Joerg Roedeld91afd12010-01-22 16:40:20 +01001119 unsigned long i;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001120
Joerg Roedelf5e97052009-05-22 12:31:53 +02001121#ifdef CONFIG_IOMMU_STRESS
1122 populate = false;
1123#endif
1124
Joerg Roedel9cabe892009-05-18 16:38:55 +02001125 if (index >= APERTURE_MAX_RANGES)
1126 return -ENOMEM;
1127
1128 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1129 if (!dma_dom->aperture[index])
1130 return -ENOMEM;
1131
1132 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1133 if (!dma_dom->aperture[index]->bitmap)
1134 goto out_free;
1135
1136 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1137
1138 if (populate) {
1139 unsigned long address = dma_dom->aperture_size;
1140 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1141 u64 *pte, *pte_page;
1142
1143 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001144 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001145 &pte_page, gfp);
1146 if (!pte)
1147 goto out_free;
1148
1149 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1150
1151 address += APERTURE_RANGE_SIZE / 64;
1152 }
1153 }
1154
1155 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1156
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001157 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001158 for_each_iommu(iommu) {
1159 if (iommu->exclusion_start &&
1160 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1161 && iommu->exclusion_start < dma_dom->aperture_size) {
1162 unsigned long startpage;
1163 int pages = iommu_num_pages(iommu->exclusion_start,
1164 iommu->exclusion_length,
1165 PAGE_SIZE);
1166 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1167 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1168 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001169 }
1170
1171 /*
1172 * Check for areas already mapped as present in the new aperture
1173 * range and mark those pages as reserved in the allocator. Such
1174 * mappings may already exist as a result of requested unity
1175 * mappings for devices.
1176 */
1177 for (i = dma_dom->aperture[index]->offset;
1178 i < dma_dom->aperture_size;
1179 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001180 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001181 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1182 continue;
1183
1184 dma_ops_reserve_addresses(dma_dom, i << PAGE_SHIFT, 1);
1185 }
1186
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001187 update_domain(&dma_dom->domain);
1188
Joerg Roedel9cabe892009-05-18 16:38:55 +02001189 return 0;
1190
1191out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001192 update_domain(&dma_dom->domain);
1193
Joerg Roedel9cabe892009-05-18 16:38:55 +02001194 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1195
1196 kfree(dma_dom->aperture[index]);
1197 dma_dom->aperture[index] = NULL;
1198
1199 return -ENOMEM;
1200}
1201
Joerg Roedel384de722009-05-15 12:30:05 +02001202static unsigned long dma_ops_area_alloc(struct device *dev,
1203 struct dma_ops_domain *dom,
1204 unsigned int pages,
1205 unsigned long align_mask,
1206 u64 dma_mask,
1207 unsigned long start)
1208{
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001209 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001210 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1211 int i = start >> APERTURE_RANGE_SHIFT;
1212 unsigned long boundary_size;
1213 unsigned long address = -1;
1214 unsigned long limit;
1215
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001216 next_bit >>= PAGE_SHIFT;
1217
Joerg Roedel384de722009-05-15 12:30:05 +02001218 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1219 PAGE_SIZE) >> PAGE_SHIFT;
1220
1221 for (;i < max_index; ++i) {
1222 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1223
1224 if (dom->aperture[i]->offset >= dma_mask)
1225 break;
1226
1227 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1228 dma_mask >> PAGE_SHIFT);
1229
1230 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1231 limit, next_bit, pages, 0,
1232 boundary_size, align_mask);
1233 if (address != -1) {
1234 address = dom->aperture[i]->offset +
1235 (address << PAGE_SHIFT);
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001236 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001237 break;
1238 }
1239
1240 next_bit = 0;
1241 }
1242
1243 return address;
1244}
1245
Joerg Roedeld3086442008-06-26 21:27:57 +02001246static unsigned long dma_ops_alloc_addresses(struct device *dev,
1247 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001248 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001249 unsigned long align_mask,
1250 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001251{
Joerg Roedeld3086442008-06-26 21:27:57 +02001252 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001253
Joerg Roedelfe16f082009-05-22 12:27:53 +02001254#ifdef CONFIG_IOMMU_STRESS
1255 dom->next_address = 0;
1256 dom->need_flush = true;
1257#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001258
Joerg Roedel384de722009-05-15 12:30:05 +02001259 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001260 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001261
Joerg Roedel1c655772008-09-04 18:40:05 +02001262 if (address == -1) {
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001263 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001264 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1265 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001266 dom->need_flush = true;
1267 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001268
Joerg Roedel384de722009-05-15 12:30:05 +02001269 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001270 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001271
1272 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1273
1274 return address;
1275}
1276
Joerg Roedel431b2a22008-07-11 17:14:22 +02001277/*
1278 * The address free function.
1279 *
1280 * called with domain->lock held
1281 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001282static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1283 unsigned long address,
1284 unsigned int pages)
1285{
Joerg Roedel384de722009-05-15 12:30:05 +02001286 unsigned i = address >> APERTURE_RANGE_SHIFT;
1287 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001288
Joerg Roedel384de722009-05-15 12:30:05 +02001289 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1290
Joerg Roedel47bccd62009-05-22 12:40:54 +02001291#ifdef CONFIG_IOMMU_STRESS
1292 if (i < 4)
1293 return;
1294#endif
1295
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001296 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001297 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001298
1299 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001300
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001301 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001302
Joerg Roedeld3086442008-06-26 21:27:57 +02001303}
1304
Joerg Roedel431b2a22008-07-11 17:14:22 +02001305/****************************************************************************
1306 *
1307 * The next functions belong to the domain allocation. A domain is
1308 * allocated for every IOMMU as the default domain. If device isolation
1309 * is enabled, every device get its own domain. The most important thing
1310 * about domains is the page table mapping the DMA address space they
1311 * contain.
1312 *
1313 ****************************************************************************/
1314
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001315/*
1316 * This function adds a protection domain to the global protection domain list
1317 */
1318static void add_domain_to_list(struct protection_domain *domain)
1319{
1320 unsigned long flags;
1321
1322 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1323 list_add(&domain->list, &amd_iommu_pd_list);
1324 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1325}
1326
1327/*
1328 * This function removes a protection domain to the global
1329 * protection domain list
1330 */
1331static void del_domain_from_list(struct protection_domain *domain)
1332{
1333 unsigned long flags;
1334
1335 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1336 list_del(&domain->list);
1337 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1338}
1339
Joerg Roedelec487d12008-06-26 21:27:58 +02001340static u16 domain_id_alloc(void)
1341{
1342 unsigned long flags;
1343 int id;
1344
1345 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1346 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1347 BUG_ON(id == 0);
1348 if (id > 0 && id < MAX_DOMAIN_ID)
1349 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1350 else
1351 id = 0;
1352 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1353
1354 return id;
1355}
1356
Joerg Roedela2acfb72008-12-02 18:28:53 +01001357static void domain_id_free(int id)
1358{
1359 unsigned long flags;
1360
1361 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1362 if (id > 0 && id < MAX_DOMAIN_ID)
1363 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1364 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1365}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001366
Joerg Roedel86db2e52008-12-02 18:20:21 +01001367static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001368{
1369 int i, j;
1370 u64 *p1, *p2, *p3;
1371
Joerg Roedel86db2e52008-12-02 18:20:21 +01001372 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001373
1374 if (!p1)
1375 return;
1376
1377 for (i = 0; i < 512; ++i) {
1378 if (!IOMMU_PTE_PRESENT(p1[i]))
1379 continue;
1380
1381 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001382 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001383 if (!IOMMU_PTE_PRESENT(p2[j]))
1384 continue;
1385 p3 = IOMMU_PTE_PAGE(p2[j]);
1386 free_page((unsigned long)p3);
1387 }
1388
1389 free_page((unsigned long)p2);
1390 }
1391
1392 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001393
1394 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001395}
1396
Joerg Roedel431b2a22008-07-11 17:14:22 +02001397/*
1398 * Free a domain, only used if something went wrong in the
1399 * allocation path and we need to free an already allocated page table
1400 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001401static void dma_ops_domain_free(struct dma_ops_domain *dom)
1402{
Joerg Roedel384de722009-05-15 12:30:05 +02001403 int i;
1404
Joerg Roedelec487d12008-06-26 21:27:58 +02001405 if (!dom)
1406 return;
1407
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001408 del_domain_from_list(&dom->domain);
1409
Joerg Roedel86db2e52008-12-02 18:20:21 +01001410 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001411
Joerg Roedel384de722009-05-15 12:30:05 +02001412 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1413 if (!dom->aperture[i])
1414 continue;
1415 free_page((unsigned long)dom->aperture[i]->bitmap);
1416 kfree(dom->aperture[i]);
1417 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001418
1419 kfree(dom);
1420}
1421
Joerg Roedel431b2a22008-07-11 17:14:22 +02001422/*
1423 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001424 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001425 * structures required for the dma_ops interface
1426 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001427static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001428{
1429 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001430
1431 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1432 if (!dma_dom)
1433 return NULL;
1434
1435 spin_lock_init(&dma_dom->domain.lock);
1436
1437 dma_dom->domain.id = domain_id_alloc();
1438 if (dma_dom->domain.id == 0)
1439 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001440 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001441 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001442 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001443 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001444 dma_dom->domain.priv = dma_dom;
1445 if (!dma_dom->domain.pt_root)
1446 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001447
Joerg Roedel1c655772008-09-04 18:40:05 +02001448 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001449 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001450
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001451 add_domain_to_list(&dma_dom->domain);
1452
Joerg Roedel576175c2009-11-23 19:08:46 +01001453 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001454 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001455
Joerg Roedel431b2a22008-07-11 17:14:22 +02001456 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001457 * mark the first page as allocated so we never return 0 as
1458 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001459 */
Joerg Roedel384de722009-05-15 12:30:05 +02001460 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001461 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001462
Joerg Roedelec487d12008-06-26 21:27:58 +02001463
1464 return dma_dom;
1465
1466free_dma_dom:
1467 dma_ops_domain_free(dma_dom);
1468
1469 return NULL;
1470}
1471
Joerg Roedel431b2a22008-07-11 17:14:22 +02001472/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001473 * little helper function to check whether a given protection domain is a
1474 * dma_ops domain
1475 */
1476static bool dma_ops_domain(struct protection_domain *domain)
1477{
1478 return domain->flags & PD_DMA_OPS_MASK;
1479}
1480
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001481static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001482{
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001483 u64 pte_root = virt_to_phys(domain->pt_root);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001484 u32 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001485
Joerg Roedel38ddf412008-09-11 10:38:32 +02001486 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1487 << DEV_ENTRY_MODE_SHIFT;
1488 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001489
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001490 if (ats)
1491 flags |= DTE_FLAG_IOTLB;
1492
1493 amd_iommu_dev_table[devid].data[3] |= flags;
1494 amd_iommu_dev_table[devid].data[2] = domain->id;
1495 amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
1496 amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001497}
1498
Joerg Roedel15898bb2009-11-24 15:39:42 +01001499static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001500{
Joerg Roedel355bf552008-12-08 12:02:41 +01001501 /* remove entry from the device table seen by the hardware */
1502 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1503 amd_iommu_dev_table[devid].data[1] = 0;
1504 amd_iommu_dev_table[devid].data[2] = 0;
1505
Joerg Roedelc5cca142009-10-09 18:31:20 +02001506 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001507}
1508
1509static void do_attach(struct device *dev, struct protection_domain *domain)
1510{
1511 struct iommu_dev_data *dev_data;
1512 struct amd_iommu *iommu;
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001513 struct pci_dev *pdev;
1514 bool ats = false;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001515 u16 devid;
1516
1517 devid = get_device_id(dev);
1518 iommu = amd_iommu_rlookup_table[devid];
1519 dev_data = get_dev_data(dev);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001520 pdev = to_pci_dev(dev);
1521
1522 if (amd_iommu_iotlb_sup)
1523 ats = pci_ats_enabled(pdev);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001524
1525 /* Update data structures */
1526 dev_data->domain = domain;
1527 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001528 set_dte_entry(devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001529
1530 /* Do reference counting */
1531 domain->dev_iommu[iommu->index] += 1;
1532 domain->dev_cnt += 1;
1533
1534 /* Flush the DTE entry */
Joerg Roedeld8c13082011-04-06 18:51:26 +02001535 device_flush_dte(dev);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001536}
1537
1538static void do_detach(struct device *dev)
1539{
1540 struct iommu_dev_data *dev_data;
1541 struct amd_iommu *iommu;
1542 u16 devid;
1543
1544 devid = get_device_id(dev);
1545 iommu = amd_iommu_rlookup_table[devid];
1546 dev_data = get_dev_data(dev);
Joerg Roedelc5cca142009-10-09 18:31:20 +02001547
Joerg Roedelc4596112009-11-20 14:57:32 +01001548 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001549 dev_data->domain->dev_iommu[iommu->index] -= 1;
1550 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001551
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001552 /* Update data structures */
1553 dev_data->domain = NULL;
1554 list_del(&dev_data->list);
1555 clear_dte_entry(devid);
1556
1557 /* Flush the DTE entry */
Joerg Roedeld8c13082011-04-06 18:51:26 +02001558 device_flush_dte(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001559}
1560
1561/*
1562 * If a device is not yet associated with a domain, this function does
1563 * assigns it visible for the hardware
1564 */
1565static int __attach_device(struct device *dev,
1566 struct protection_domain *domain)
1567{
Joerg Roedel657cbb62009-11-23 15:26:46 +01001568 struct iommu_dev_data *dev_data, *alias_data;
Julia Lawall84fe6c12010-05-27 12:31:51 +02001569 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001570
Joerg Roedel657cbb62009-11-23 15:26:46 +01001571 dev_data = get_dev_data(dev);
1572 alias_data = get_dev_data(dev_data->alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001573
Joerg Roedel657cbb62009-11-23 15:26:46 +01001574 if (!alias_data)
1575 return -EINVAL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001576
1577 /* lock domain */
1578 spin_lock(&domain->lock);
1579
1580 /* Some sanity checks */
Julia Lawall84fe6c12010-05-27 12:31:51 +02001581 ret = -EBUSY;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001582 if (alias_data->domain != NULL &&
1583 alias_data->domain != domain)
Julia Lawall84fe6c12010-05-27 12:31:51 +02001584 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001585
Joerg Roedel657cbb62009-11-23 15:26:46 +01001586 if (dev_data->domain != NULL &&
1587 dev_data->domain != domain)
Julia Lawall84fe6c12010-05-27 12:31:51 +02001588 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001589
1590 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001591 if (dev_data->alias != dev) {
1592 alias_data = get_dev_data(dev_data->alias);
1593 if (alias_data->domain == NULL)
1594 do_attach(dev_data->alias, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01001595
1596 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001597 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01001598
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001599 if (dev_data->domain == NULL)
1600 do_attach(dev, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001601
Joerg Roedel24100052009-11-25 15:59:57 +01001602 atomic_inc(&dev_data->bind);
1603
Julia Lawall84fe6c12010-05-27 12:31:51 +02001604 ret = 0;
1605
1606out_unlock:
1607
Joerg Roedel355bf552008-12-08 12:02:41 +01001608 /* ready */
1609 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001610
Julia Lawall84fe6c12010-05-27 12:31:51 +02001611 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001612}
1613
1614/*
1615 * If a device is not yet associated with a domain, this function does
1616 * assigns it visible for the hardware
1617 */
1618static int attach_device(struct device *dev,
1619 struct protection_domain *domain)
1620{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001621 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001622 unsigned long flags;
1623 int ret;
1624
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001625 if (amd_iommu_iotlb_sup)
1626 pci_enable_ats(pdev, PAGE_SHIFT);
1627
Joerg Roedel15898bb2009-11-24 15:39:42 +01001628 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1629 ret = __attach_device(dev, domain);
1630 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1631
1632 /*
1633 * We might boot into a crash-kernel here. The crashed kernel
1634 * left the caches in the IOMMU dirty. So we have to flush
1635 * here to evict all dirty stuff.
1636 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001637 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001638
1639 return ret;
1640}
1641
1642/*
1643 * Removes a device from a protection domain (unlocked)
1644 */
1645static void __detach_device(struct device *dev)
1646{
Joerg Roedel657cbb62009-11-23 15:26:46 +01001647 struct iommu_dev_data *dev_data = get_dev_data(dev);
Joerg Roedel24100052009-11-25 15:59:57 +01001648 struct iommu_dev_data *alias_data;
Joerg Roedel2ca76272010-01-22 16:45:31 +01001649 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001650 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001651
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001652 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001653
Joerg Roedel2ca76272010-01-22 16:45:31 +01001654 domain = dev_data->domain;
1655
1656 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01001657
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001658 if (dev_data->alias != dev) {
Joerg Roedel24100052009-11-25 15:59:57 +01001659 alias_data = get_dev_data(dev_data->alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001660 if (atomic_dec_and_test(&alias_data->bind))
1661 do_detach(dev_data->alias);
Joerg Roedel24100052009-11-25 15:59:57 +01001662 }
1663
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001664 if (atomic_dec_and_test(&dev_data->bind))
1665 do_detach(dev);
1666
Joerg Roedel2ca76272010-01-22 16:45:31 +01001667 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001668
Joerg Roedel21129f72009-09-01 11:59:42 +02001669 /*
1670 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001671 * passthrough domain if it is detached from any other domain.
1672 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02001673 */
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001674 if (iommu_pass_through &&
1675 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedel15898bb2009-11-24 15:39:42 +01001676 __attach_device(dev, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01001677}
1678
1679/*
1680 * Removes a device from a protection domain (with devtable_lock held)
1681 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01001682static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01001683{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001684 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedel355bf552008-12-08 12:02:41 +01001685 unsigned long flags;
1686
1687 /* lock device table */
1688 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001689 __detach_device(dev);
Joerg Roedel355bf552008-12-08 12:02:41 +01001690 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001691
1692 if (amd_iommu_iotlb_sup && pci_ats_enabled(pdev))
1693 pci_disable_ats(pdev);
Joerg Roedel355bf552008-12-08 12:02:41 +01001694}
Joerg Roedele275a2a2008-12-10 18:27:25 +01001695
Joerg Roedel15898bb2009-11-24 15:39:42 +01001696/*
1697 * Find out the protection domain structure for a given PCI device. This
1698 * will give us the pointer to the page table root for example.
1699 */
1700static struct protection_domain *domain_for_device(struct device *dev)
1701{
1702 struct protection_domain *dom;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001703 struct iommu_dev_data *dev_data, *alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001704 unsigned long flags;
Gustavo F. Padovan6ec5ff42011-05-20 16:13:00 -03001705 u16 devid;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001706
Joerg Roedel657cbb62009-11-23 15:26:46 +01001707 devid = get_device_id(dev);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001708 dev_data = get_dev_data(dev);
1709 alias_data = get_dev_data(dev_data->alias);
1710 if (!alias_data)
1711 return NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001712
1713 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001714 dom = dev_data->domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001715 if (dom == NULL &&
Joerg Roedel657cbb62009-11-23 15:26:46 +01001716 alias_data->domain != NULL) {
1717 __attach_device(dev, alias_data->domain);
1718 dom = alias_data->domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001719 }
1720
1721 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1722
1723 return dom;
1724}
1725
Joerg Roedele275a2a2008-12-10 18:27:25 +01001726static int device_change_notifier(struct notifier_block *nb,
1727 unsigned long action, void *data)
1728{
1729 struct device *dev = data;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001730 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001731 struct protection_domain *domain;
1732 struct dma_ops_domain *dma_domain;
1733 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001734 unsigned long flags;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001735
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001736 if (!check_device(dev))
1737 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001738
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001739 devid = get_device_id(dev);
1740 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01001741
1742 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07001743 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001744
1745 domain = domain_for_device(dev);
1746
Joerg Roedele275a2a2008-12-10 18:27:25 +01001747 if (!domain)
1748 goto out;
Joerg Roedela1ca3312009-09-01 12:22:22 +02001749 if (iommu_pass_through)
1750 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001751 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001752 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001753 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001754
1755 iommu_init_device(dev);
1756
1757 domain = domain_for_device(dev);
1758
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001759 /* allocate a protection domain if a device is added */
1760 dma_domain = find_protection_domain(devid);
1761 if (dma_domain)
1762 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01001763 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001764 if (!dma_domain)
1765 goto out;
1766 dma_domain->target_dev = devid;
1767
1768 spin_lock_irqsave(&iommu_pd_list_lock, flags);
1769 list_add_tail(&dma_domain->list, &iommu_pd_list);
1770 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
1771
1772 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001773 case BUS_NOTIFY_DEL_DEVICE:
1774
1775 iommu_uninit_device(dev);
1776
Joerg Roedele275a2a2008-12-10 18:27:25 +01001777 default:
1778 goto out;
1779 }
1780
Joerg Roedeld8c13082011-04-06 18:51:26 +02001781 device_flush_dte(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001782 iommu_completion_wait(iommu);
1783
1784out:
1785 return 0;
1786}
1787
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05301788static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01001789 .notifier_call = device_change_notifier,
1790};
Joerg Roedel355bf552008-12-08 12:02:41 +01001791
Joerg Roedel8638c492009-12-10 11:12:25 +01001792void amd_iommu_init_notifier(void)
1793{
1794 bus_register_notifier(&pci_bus_type, &device_nb);
1795}
1796
Joerg Roedel431b2a22008-07-11 17:14:22 +02001797/*****************************************************************************
1798 *
1799 * The next functions belong to the dma_ops mapping/unmapping code.
1800 *
1801 *****************************************************************************/
1802
1803/*
1804 * In the dma_ops path we only have the struct device. This function
1805 * finds the corresponding IOMMU, the protection domain and the
1806 * requestor id for a given device.
1807 * If the device is not yet associated with a domain this is also done
1808 * in this function.
1809 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001810static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001811{
Joerg Roedel94f6d192009-11-24 16:40:02 +01001812 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001813 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01001814 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001815
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001816 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01001817 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001818
Joerg Roedel94f6d192009-11-24 16:40:02 +01001819 domain = domain_for_device(dev);
1820 if (domain != NULL && !dma_ops_domain(domain))
1821 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001822
Joerg Roedel94f6d192009-11-24 16:40:02 +01001823 if (domain != NULL)
1824 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001825
Joerg Roedel15898bb2009-11-24 15:39:42 +01001826 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001827 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001828 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01001829 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
1830 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001831 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01001832 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01001833
Joerg Roedel94f6d192009-11-24 16:40:02 +01001834 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001835}
1836
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001837static void update_device_table(struct protection_domain *domain)
1838{
Joerg Roedel492667d2009-11-27 13:25:47 +01001839 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001840
Joerg Roedel492667d2009-11-27 13:25:47 +01001841 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001842 struct pci_dev *pdev = to_pci_dev(dev_data->dev);
Joerg Roedel492667d2009-11-27 13:25:47 +01001843 u16 devid = get_device_id(dev_data->dev);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001844 set_dte_entry(devid, domain, pci_ats_enabled(pdev));
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001845 }
1846}
1847
1848static void update_domain(struct protection_domain *domain)
1849{
1850 if (!domain->updated)
1851 return;
1852
1853 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02001854
1855 domain_flush_devices(domain);
1856 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001857
1858 domain->updated = false;
1859}
1860
Joerg Roedel431b2a22008-07-11 17:14:22 +02001861/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02001862 * This function fetches the PTE for a given address in the aperture
1863 */
1864static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
1865 unsigned long address)
1866{
Joerg Roedel384de722009-05-15 12:30:05 +02001867 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02001868 u64 *pte, *pte_page;
1869
Joerg Roedel384de722009-05-15 12:30:05 +02001870 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1871 if (!aperture)
1872 return NULL;
1873
1874 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02001875 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001876 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001877 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02001878 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
1879 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001880 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001881
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001882 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001883
1884 return pte;
1885}
1886
1887/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001888 * This is the generic map function. It maps one 4kb page at paddr to
1889 * the given address in the DMA address space for the domain.
1890 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001891static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001892 unsigned long address,
1893 phys_addr_t paddr,
1894 int direction)
1895{
1896 u64 *pte, __pte;
1897
1898 WARN_ON(address > dom->aperture_size);
1899
1900 paddr &= PAGE_MASK;
1901
Joerg Roedel8bda3092009-05-12 12:02:46 +02001902 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02001903 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001904 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001905
1906 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
1907
1908 if (direction == DMA_TO_DEVICE)
1909 __pte |= IOMMU_PTE_IR;
1910 else if (direction == DMA_FROM_DEVICE)
1911 __pte |= IOMMU_PTE_IW;
1912 else if (direction == DMA_BIDIRECTIONAL)
1913 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
1914
1915 WARN_ON(*pte);
1916
1917 *pte = __pte;
1918
1919 return (dma_addr_t)address;
1920}
1921
Joerg Roedel431b2a22008-07-11 17:14:22 +02001922/*
1923 * The generic unmapping function for on page in the DMA address space.
1924 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001925static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001926 unsigned long address)
1927{
Joerg Roedel384de722009-05-15 12:30:05 +02001928 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001929 u64 *pte;
1930
1931 if (address >= dom->aperture_size)
1932 return;
1933
Joerg Roedel384de722009-05-15 12:30:05 +02001934 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1935 if (!aperture)
1936 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001937
Joerg Roedel384de722009-05-15 12:30:05 +02001938 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
1939 if (!pte)
1940 return;
1941
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001942 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001943
1944 WARN_ON(!*pte);
1945
1946 *pte = 0ULL;
1947}
1948
Joerg Roedel431b2a22008-07-11 17:14:22 +02001949/*
1950 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01001951 * contiguous memory region into DMA address space. It is used by all
1952 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001953 * Must be called with the domain lock held.
1954 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02001955static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001956 struct dma_ops_domain *dma_dom,
1957 phys_addr_t paddr,
1958 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001959 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001960 bool align,
1961 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02001962{
1963 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02001964 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001965 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001966 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001967 int i;
1968
Joerg Roedele3c449f2008-10-15 22:02:11 -07001969 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001970 paddr &= PAGE_MASK;
1971
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01001972 INC_STATS_COUNTER(total_map_requests);
1973
Joerg Roedelc1858972008-12-12 15:42:39 +01001974 if (pages > 1)
1975 INC_STATS_COUNTER(cross_page);
1976
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001977 if (align)
1978 align_mask = (1UL << get_order(size)) - 1;
1979
Joerg Roedel11b83882009-05-19 10:23:15 +02001980retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02001981 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
1982 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001983 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02001984 /*
1985 * setting next_address here will let the address
1986 * allocator only scan the new allocated range in the
1987 * first run. This is a small optimization.
1988 */
1989 dma_dom->next_address = dma_dom->aperture_size;
1990
Joerg Roedel576175c2009-11-23 19:08:46 +01001991 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02001992 goto out;
1993
1994 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001995 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02001996 * allocation again
1997 */
1998 goto retry;
1999 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002000
2001 start = address;
2002 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002003 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002004 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002005 goto out_unmap;
2006
Joerg Roedelcb76c322008-06-26 21:28:00 +02002007 paddr += PAGE_SIZE;
2008 start += PAGE_SIZE;
2009 }
2010 address += offset;
2011
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002012 ADD_STATS_COUNTER(alloced_io_mem, size);
2013
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002014 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002015 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002016 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002017 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002018 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002019
Joerg Roedelcb76c322008-06-26 21:28:00 +02002020out:
2021 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002022
2023out_unmap:
2024
2025 for (--i; i >= 0; --i) {
2026 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002027 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002028 }
2029
2030 dma_ops_free_addresses(dma_dom, address, pages);
2031
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002032 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002033}
2034
Joerg Roedel431b2a22008-07-11 17:14:22 +02002035/*
2036 * Does the reverse of the __map_single function. Must be called with
2037 * the domain lock held too
2038 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002039static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002040 dma_addr_t dma_addr,
2041 size_t size,
2042 int dir)
2043{
Joerg Roedel04e04632010-09-23 16:12:48 +02002044 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002045 dma_addr_t i, start;
2046 unsigned int pages;
2047
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002048 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002049 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002050 return;
2051
Joerg Roedel04e04632010-09-23 16:12:48 +02002052 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002053 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002054 dma_addr &= PAGE_MASK;
2055 start = dma_addr;
2056
2057 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002058 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002059 start += PAGE_SIZE;
2060 }
2061
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002062 SUB_STATS_COUNTER(alloced_io_mem, size);
2063
Joerg Roedelcb76c322008-06-26 21:28:00 +02002064 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002065
Joerg Roedel80be3082008-11-06 14:59:05 +01002066 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002067 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002068 dma_dom->need_flush = false;
2069 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002070}
2071
Joerg Roedel431b2a22008-07-11 17:14:22 +02002072/*
2073 * The exported map_single function for dma_ops.
2074 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002075static dma_addr_t map_page(struct device *dev, struct page *page,
2076 unsigned long offset, size_t size,
2077 enum dma_data_direction dir,
2078 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002079{
2080 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002081 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002082 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002083 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002084 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002085
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002086 INC_STATS_COUNTER(cnt_map_single);
2087
Joerg Roedel94f6d192009-11-24 16:40:02 +01002088 domain = get_domain(dev);
2089 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002090 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002091 else if (IS_ERR(domain))
2092 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002093
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002094 dma_mask = *dev->dma_mask;
2095
Joerg Roedel4da70b92008-06-26 21:28:01 +02002096 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002097
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002098 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002099 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002100 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002101 goto out;
2102
Joerg Roedel17b124b2011-04-06 18:01:35 +02002103 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002104
2105out:
2106 spin_unlock_irqrestore(&domain->lock, flags);
2107
2108 return addr;
2109}
2110
Joerg Roedel431b2a22008-07-11 17:14:22 +02002111/*
2112 * The exported unmap_single function for dma_ops.
2113 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002114static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2115 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002116{
2117 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002118 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002119
Joerg Roedel146a6912008-12-12 15:07:12 +01002120 INC_STATS_COUNTER(cnt_unmap_single);
2121
Joerg Roedel94f6d192009-11-24 16:40:02 +01002122 domain = get_domain(dev);
2123 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002124 return;
2125
Joerg Roedel4da70b92008-06-26 21:28:01 +02002126 spin_lock_irqsave(&domain->lock, flags);
2127
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002128 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002129
Joerg Roedel17b124b2011-04-06 18:01:35 +02002130 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002131
2132 spin_unlock_irqrestore(&domain->lock, flags);
2133}
2134
Joerg Roedel431b2a22008-07-11 17:14:22 +02002135/*
2136 * This is a special map_sg function which is used if we should map a
2137 * device which is not handled by an AMD IOMMU in the system.
2138 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002139static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2140 int nelems, int dir)
2141{
2142 struct scatterlist *s;
2143 int i;
2144
2145 for_each_sg(sglist, s, nelems, i) {
2146 s->dma_address = (dma_addr_t)sg_phys(s);
2147 s->dma_length = s->length;
2148 }
2149
2150 return nelems;
2151}
2152
Joerg Roedel431b2a22008-07-11 17:14:22 +02002153/*
2154 * The exported map_sg function for dma_ops (handles scatter-gather
2155 * lists).
2156 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002157static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002158 int nelems, enum dma_data_direction dir,
2159 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002160{
2161 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002162 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002163 int i;
2164 struct scatterlist *s;
2165 phys_addr_t paddr;
2166 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002167 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002168
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002169 INC_STATS_COUNTER(cnt_map_sg);
2170
Joerg Roedel94f6d192009-11-24 16:40:02 +01002171 domain = get_domain(dev);
2172 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002173 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002174 else if (IS_ERR(domain))
2175 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002176
Joerg Roedel832a90c2008-09-18 15:54:23 +02002177 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002178
Joerg Roedel65b050a2008-06-26 21:28:02 +02002179 spin_lock_irqsave(&domain->lock, flags);
2180
2181 for_each_sg(sglist, s, nelems, i) {
2182 paddr = sg_phys(s);
2183
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002184 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002185 paddr, s->length, dir, false,
2186 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002187
2188 if (s->dma_address) {
2189 s->dma_length = s->length;
2190 mapped_elems++;
2191 } else
2192 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002193 }
2194
Joerg Roedel17b124b2011-04-06 18:01:35 +02002195 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002196
2197out:
2198 spin_unlock_irqrestore(&domain->lock, flags);
2199
2200 return mapped_elems;
2201unmap:
2202 for_each_sg(sglist, s, mapped_elems, i) {
2203 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002204 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002205 s->dma_length, dir);
2206 s->dma_address = s->dma_length = 0;
2207 }
2208
2209 mapped_elems = 0;
2210
2211 goto out;
2212}
2213
Joerg Roedel431b2a22008-07-11 17:14:22 +02002214/*
2215 * The exported map_sg function for dma_ops (handles scatter-gather
2216 * lists).
2217 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002218static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002219 int nelems, enum dma_data_direction dir,
2220 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002221{
2222 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002223 struct protection_domain *domain;
2224 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002225 int i;
2226
Joerg Roedel55877a62008-12-12 15:12:14 +01002227 INC_STATS_COUNTER(cnt_unmap_sg);
2228
Joerg Roedel94f6d192009-11-24 16:40:02 +01002229 domain = get_domain(dev);
2230 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002231 return;
2232
Joerg Roedel65b050a2008-06-26 21:28:02 +02002233 spin_lock_irqsave(&domain->lock, flags);
2234
2235 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002236 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002237 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002238 s->dma_address = s->dma_length = 0;
2239 }
2240
Joerg Roedel17b124b2011-04-06 18:01:35 +02002241 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002242
2243 spin_unlock_irqrestore(&domain->lock, flags);
2244}
2245
Joerg Roedel431b2a22008-07-11 17:14:22 +02002246/*
2247 * The exported alloc_coherent function for dma_ops.
2248 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002249static void *alloc_coherent(struct device *dev, size_t size,
2250 dma_addr_t *dma_addr, gfp_t flag)
2251{
2252 unsigned long flags;
2253 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002254 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002255 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002256 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002257
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002258 INC_STATS_COUNTER(cnt_alloc_coherent);
2259
Joerg Roedel94f6d192009-11-24 16:40:02 +01002260 domain = get_domain(dev);
2261 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002262 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2263 *dma_addr = __pa(virt_addr);
2264 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002265 } else if (IS_ERR(domain))
2266 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002267
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002268 dma_mask = dev->coherent_dma_mask;
2269 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2270 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002271
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002272 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2273 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302274 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002275
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002276 paddr = virt_to_phys(virt_addr);
2277
Joerg Roedel832a90c2008-09-18 15:54:23 +02002278 if (!dma_mask)
2279 dma_mask = *dev->dma_mask;
2280
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002281 spin_lock_irqsave(&domain->lock, flags);
2282
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002283 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002284 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002285
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002286 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002287 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002288 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002289 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002290
Joerg Roedel17b124b2011-04-06 18:01:35 +02002291 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002292
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002293 spin_unlock_irqrestore(&domain->lock, flags);
2294
2295 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002296
2297out_free:
2298
2299 free_pages((unsigned long)virt_addr, get_order(size));
2300
2301 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002302}
2303
Joerg Roedel431b2a22008-07-11 17:14:22 +02002304/*
2305 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002306 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002307static void free_coherent(struct device *dev, size_t size,
2308 void *virt_addr, dma_addr_t dma_addr)
2309{
2310 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002311 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002312
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002313 INC_STATS_COUNTER(cnt_free_coherent);
2314
Joerg Roedel94f6d192009-11-24 16:40:02 +01002315 domain = get_domain(dev);
2316 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002317 goto free_mem;
2318
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002319 spin_lock_irqsave(&domain->lock, flags);
2320
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002321 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002322
Joerg Roedel17b124b2011-04-06 18:01:35 +02002323 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002324
2325 spin_unlock_irqrestore(&domain->lock, flags);
2326
2327free_mem:
2328 free_pages((unsigned long)virt_addr, get_order(size));
2329}
2330
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002331/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002332 * This function is called by the DMA layer to find out if we can handle a
2333 * particular device. It is part of the dma_ops.
2334 */
2335static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2336{
Joerg Roedel420aef82009-11-23 16:14:57 +01002337 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002338}
2339
2340/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002341 * The function for pre-allocating protection domains.
2342 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002343 * If the driver core informs the DMA layer if a driver grabs a device
2344 * we don't need to preallocate the protection domains anymore.
2345 * For now we have to.
2346 */
Jaswinder Singh Rajput0e93dd82008-12-29 21:45:22 +05302347static void prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002348{
2349 struct pci_dev *dev = NULL;
2350 struct dma_ops_domain *dma_dom;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002351 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002352
Chris Wrightd18c69d2010-04-02 18:27:55 -07002353 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002354
2355 /* Do we handle this device? */
2356 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002357 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002358
2359 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002360 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002361 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002362
2363 devid = get_device_id(&dev->dev);
2364
Joerg Roedel87a64d52009-11-24 17:26:43 +01002365 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002366 if (!dma_dom)
2367 continue;
2368 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002369 dma_dom->target_dev = devid;
2370
Joerg Roedel15898bb2009-11-24 15:39:42 +01002371 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01002372
Joerg Roedelbd60b732008-09-11 10:24:48 +02002373 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002374 }
2375}
2376
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002377static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002378 .alloc_coherent = alloc_coherent,
2379 .free_coherent = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002380 .map_page = map_page,
2381 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002382 .map_sg = map_sg,
2383 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002384 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002385};
2386
Joerg Roedel27c21272011-05-30 15:56:24 +02002387static unsigned device_dma_ops_init(void)
2388{
2389 struct pci_dev *pdev = NULL;
2390 unsigned unhandled = 0;
2391
2392 for_each_pci_dev(pdev) {
2393 if (!check_device(&pdev->dev)) {
2394 unhandled += 1;
2395 continue;
2396 }
2397
2398 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
2399 }
2400
2401 return unhandled;
2402}
2403
Joerg Roedel431b2a22008-07-11 17:14:22 +02002404/*
2405 * The function which clues the AMD IOMMU driver into dma_ops.
2406 */
Joerg Roedelf5325092010-01-22 17:44:35 +01002407
2408void __init amd_iommu_init_api(void)
2409{
2410 register_iommu(&amd_iommu_ops);
2411}
2412
Joerg Roedel6631ee92008-06-26 21:28:05 +02002413int __init amd_iommu_init_dma_ops(void)
2414{
2415 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02002416 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002417
Joerg Roedel431b2a22008-07-11 17:14:22 +02002418 /*
2419 * first allocate a default protection domain for every IOMMU we
2420 * found in the system. Devices not assigned to any other
2421 * protection domain will be assigned to the default one.
2422 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02002423 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01002424 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002425 if (iommu->default_dom == NULL)
2426 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01002427 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002428 ret = iommu_init_unity_mappings(iommu);
2429 if (ret)
2430 goto free_domains;
2431 }
2432
Joerg Roedel431b2a22008-07-11 17:14:22 +02002433 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01002434 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002435 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01002436 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002437
2438 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002439 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002440
Joerg Roedel431b2a22008-07-11 17:14:22 +02002441 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02002442 unhandled = device_dma_ops_init();
2443 if (unhandled && max_pfn > MAX_DMA32_PFN) {
2444 /* There are unhandled devices - initialize swiotlb for them */
2445 swiotlb = 1;
2446 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02002447
Joerg Roedel7f265082008-12-12 13:50:21 +01002448 amd_iommu_stats_init();
2449
Joerg Roedel6631ee92008-06-26 21:28:05 +02002450 return 0;
2451
2452free_domains:
2453
Joerg Roedel3bd22172009-05-04 15:06:20 +02002454 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002455 if (iommu->default_dom)
2456 dma_ops_domain_free(iommu->default_dom);
2457 }
2458
2459 return ret;
2460}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002461
2462/*****************************************************************************
2463 *
2464 * The following functions belong to the exported interface of AMD IOMMU
2465 *
2466 * This interface allows access to lower level functions of the IOMMU
2467 * like protection domain handling and assignement of devices to domains
2468 * which is not possible with the dma_ops interface.
2469 *
2470 *****************************************************************************/
2471
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002472static void cleanup_domain(struct protection_domain *domain)
2473{
Joerg Roedel492667d2009-11-27 13:25:47 +01002474 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002475 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002476
2477 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2478
Joerg Roedel492667d2009-11-27 13:25:47 +01002479 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
2480 struct device *dev = dev_data->dev;
2481
Chris Wright04e856c2010-02-17 08:51:20 -08002482 __detach_device(dev);
Joerg Roedel492667d2009-11-27 13:25:47 +01002483 atomic_set(&dev_data->bind, 0);
2484 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002485
2486 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2487}
2488
Joerg Roedel26508152009-08-26 16:52:40 +02002489static void protection_domain_free(struct protection_domain *domain)
2490{
2491 if (!domain)
2492 return;
2493
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002494 del_domain_from_list(domain);
2495
Joerg Roedel26508152009-08-26 16:52:40 +02002496 if (domain->id)
2497 domain_id_free(domain->id);
2498
2499 kfree(domain);
2500}
2501
2502static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002503{
2504 struct protection_domain *domain;
2505
2506 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2507 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002508 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002509
2510 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002511 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01002512 domain->id = domain_id_alloc();
2513 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02002514 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002515 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02002516
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002517 add_domain_to_list(domain);
2518
Joerg Roedel26508152009-08-26 16:52:40 +02002519 return domain;
2520
2521out_err:
2522 kfree(domain);
2523
2524 return NULL;
2525}
2526
2527static int amd_iommu_domain_init(struct iommu_domain *dom)
2528{
2529 struct protection_domain *domain;
2530
2531 domain = protection_domain_alloc();
2532 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01002533 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02002534
2535 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002536 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2537 if (!domain->pt_root)
2538 goto out_free;
2539
2540 dom->priv = domain;
2541
2542 return 0;
2543
2544out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02002545 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01002546
2547 return -ENOMEM;
2548}
2549
Joerg Roedel98383fc2008-12-02 18:34:12 +01002550static void amd_iommu_domain_destroy(struct iommu_domain *dom)
2551{
2552 struct protection_domain *domain = dom->priv;
2553
2554 if (!domain)
2555 return;
2556
2557 if (domain->dev_cnt > 0)
2558 cleanup_domain(domain);
2559
2560 BUG_ON(domain->dev_cnt != 0);
2561
2562 free_pagetable(domain);
2563
Joerg Roedel8b408fe2010-03-08 14:20:07 +01002564 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002565
2566 dom->priv = NULL;
2567}
2568
Joerg Roedel684f2882008-12-08 12:07:44 +01002569static void amd_iommu_detach_device(struct iommu_domain *dom,
2570 struct device *dev)
2571{
Joerg Roedel657cbb62009-11-23 15:26:46 +01002572 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002573 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002574 u16 devid;
2575
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002576 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01002577 return;
2578
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002579 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002580
Joerg Roedel657cbb62009-11-23 15:26:46 +01002581 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002582 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002583
2584 iommu = amd_iommu_rlookup_table[devid];
2585 if (!iommu)
2586 return;
2587
Joerg Roedeld8c13082011-04-06 18:51:26 +02002588 device_flush_dte(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002589 iommu_completion_wait(iommu);
2590}
2591
Joerg Roedel01106062008-12-02 19:34:11 +01002592static int amd_iommu_attach_device(struct iommu_domain *dom,
2593 struct device *dev)
2594{
2595 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002596 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01002597 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002598 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002599 u16 devid;
2600
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002601 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01002602 return -EINVAL;
2603
Joerg Roedel657cbb62009-11-23 15:26:46 +01002604 dev_data = dev->archdata.iommu;
2605
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002606 devid = get_device_id(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002607
2608 iommu = amd_iommu_rlookup_table[devid];
2609 if (!iommu)
2610 return -EINVAL;
2611
Joerg Roedel657cbb62009-11-23 15:26:46 +01002612 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002613 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002614
Joerg Roedel15898bb2009-11-24 15:39:42 +01002615 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01002616
2617 iommu_completion_wait(iommu);
2618
Joerg Roedel15898bb2009-11-24 15:39:42 +01002619 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002620}
2621
Joerg Roedel468e2362010-01-21 16:37:36 +01002622static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
2623 phys_addr_t paddr, int gfp_order, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002624{
Joerg Roedel468e2362010-01-21 16:37:36 +01002625 unsigned long page_size = 0x1000UL << gfp_order;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002626 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002627 int prot = 0;
2628 int ret;
2629
2630 if (iommu_prot & IOMMU_READ)
2631 prot |= IOMMU_PROT_IR;
2632 if (iommu_prot & IOMMU_WRITE)
2633 prot |= IOMMU_PROT_IW;
2634
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002635 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002636 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002637 mutex_unlock(&domain->api_lock);
2638
Joerg Roedel795e74f72010-05-11 17:40:57 +02002639 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002640}
2641
Joerg Roedel468e2362010-01-21 16:37:36 +01002642static int amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2643 int gfp_order)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002644{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002645 struct protection_domain *domain = dom->priv;
Joerg Roedel468e2362010-01-21 16:37:36 +01002646 unsigned long page_size, unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002647
Joerg Roedel468e2362010-01-21 16:37:36 +01002648 page_size = 0x1000UL << gfp_order;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002649
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002650 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01002651 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002652 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002653
Joerg Roedel17b124b2011-04-06 18:01:35 +02002654 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002655
Joerg Roedel468e2362010-01-21 16:37:36 +01002656 return get_order(unmap_size);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002657}
2658
Joerg Roedel645c4c82008-12-02 20:05:50 +01002659static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2660 unsigned long iova)
2661{
2662 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002663 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002664 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002665 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002666
Joerg Roedel24cd7722010-01-19 17:27:39 +01002667 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002668
Joerg Roedela6d41a42009-09-02 17:08:55 +02002669 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01002670 return 0;
2671
Joerg Roedelf03152b2010-01-21 16:15:24 +01002672 if (PM_PTE_LEVEL(*pte) == 0)
2673 offset_mask = PAGE_SIZE - 1;
2674 else
2675 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
2676
2677 __pte = *pte & PM_ADDR_MASK;
2678 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002679
2680 return paddr;
2681}
2682
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002683static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
2684 unsigned long cap)
2685{
Joerg Roedel80a506b2010-07-27 17:14:24 +02002686 switch (cap) {
2687 case IOMMU_CAP_CACHE_COHERENCY:
2688 return 1;
2689 }
2690
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002691 return 0;
2692}
2693
Joerg Roedel26961ef2008-12-03 17:00:17 +01002694static struct iommu_ops amd_iommu_ops = {
2695 .domain_init = amd_iommu_domain_init,
2696 .domain_destroy = amd_iommu_domain_destroy,
2697 .attach_dev = amd_iommu_attach_device,
2698 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01002699 .map = amd_iommu_map,
2700 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002701 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002702 .domain_has_cap = amd_iommu_domain_has_cap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002703};
2704
Joerg Roedel0feae532009-08-26 15:26:30 +02002705/*****************************************************************************
2706 *
2707 * The next functions do a basic initialization of IOMMU for pass through
2708 * mode
2709 *
2710 * In passthrough mode the IOMMU is initialized and enabled but not used for
2711 * DMA-API translation.
2712 *
2713 *****************************************************************************/
2714
2715int __init amd_iommu_init_passthrough(void)
2716{
Joerg Roedel15898bb2009-11-24 15:39:42 +01002717 struct amd_iommu *iommu;
Joerg Roedel0feae532009-08-26 15:26:30 +02002718 struct pci_dev *dev = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002719 u16 devid;
Joerg Roedel0feae532009-08-26 15:26:30 +02002720
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002721 /* allocate passthrough domain */
Joerg Roedel0feae532009-08-26 15:26:30 +02002722 pt_domain = protection_domain_alloc();
2723 if (!pt_domain)
2724 return -ENOMEM;
2725
2726 pt_domain->mode |= PAGE_MODE_NONE;
2727
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04002728 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002729 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02002730 continue;
2731
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002732 devid = get_device_id(&dev->dev);
2733
Joerg Roedel15898bb2009-11-24 15:39:42 +01002734 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02002735 if (!iommu)
2736 continue;
2737
Joerg Roedel15898bb2009-11-24 15:39:42 +01002738 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02002739 }
2740
2741 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
2742
2743 return 0;
2744}