blob: bfd75ff5d5bda6083651eaa23cfc86e8f3a843de [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;
Joerg Roedel26018872011-06-06 16:50:14 +0200158 else {
159 kfree(dev_data);
160 return -ENOTSUPP;
161 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100162
Joerg Roedel24100052009-11-25 15:59:57 +0100163 atomic_set(&dev_data->bind, 0);
164
Joerg Roedel657cbb62009-11-23 15:26:46 +0100165 dev->archdata.iommu = dev_data;
166
167
168 return 0;
169}
170
Joerg Roedel26018872011-06-06 16:50:14 +0200171static void iommu_ignore_device(struct device *dev)
172{
173 u16 devid, alias;
174
175 devid = get_device_id(dev);
176 alias = amd_iommu_alias_table[devid];
177
178 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
179 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
180
181 amd_iommu_rlookup_table[devid] = NULL;
182 amd_iommu_rlookup_table[alias] = NULL;
183}
184
Joerg Roedel657cbb62009-11-23 15:26:46 +0100185static void iommu_uninit_device(struct device *dev)
186{
187 kfree(dev->archdata.iommu);
188}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100189
190void __init amd_iommu_uninit_devices(void)
191{
192 struct pci_dev *pdev = NULL;
193
194 for_each_pci_dev(pdev) {
195
196 if (!check_device(&pdev->dev))
197 continue;
198
199 iommu_uninit_device(&pdev->dev);
200 }
201}
202
203int __init amd_iommu_init_devices(void)
204{
205 struct pci_dev *pdev = NULL;
206 int ret = 0;
207
208 for_each_pci_dev(pdev) {
209
210 if (!check_device(&pdev->dev))
211 continue;
212
213 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200214 if (ret == -ENOTSUPP)
215 iommu_ignore_device(&pdev->dev);
216 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100217 goto out_free;
218 }
219
220 return 0;
221
222out_free:
223
224 amd_iommu_uninit_devices();
225
226 return ret;
227}
Joerg Roedel7f265082008-12-12 13:50:21 +0100228#ifdef CONFIG_AMD_IOMMU_STATS
229
230/*
231 * Initialization code for statistics collection
232 */
233
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100234DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100235DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100236DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100237DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100238DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100239DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100240DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100241DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100242DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100243DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100244DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100245DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100246
Joerg Roedel7f265082008-12-12 13:50:21 +0100247static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100248static struct dentry *de_fflush;
249
250static void amd_iommu_stats_add(struct __iommu_counter *cnt)
251{
252 if (stats_dir == NULL)
253 return;
254
255 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
256 &cnt->value);
257}
258
259static void amd_iommu_stats_init(void)
260{
261 stats_dir = debugfs_create_dir("amd-iommu", NULL);
262 if (stats_dir == NULL)
263 return;
264
Joerg Roedel7f265082008-12-12 13:50:21 +0100265 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
266 (u32 *)&amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100267
268 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100269 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100270 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100271 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100272 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100273 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100274 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100275 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100276 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100277 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100278 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100279 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100280}
281
282#endif
283
Joerg Roedel431b2a22008-07-11 17:14:22 +0200284/****************************************************************************
285 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200286 * Interrupt handling functions
287 *
288 ****************************************************************************/
289
Joerg Roedele3e59872009-09-03 14:02:10 +0200290static void dump_dte_entry(u16 devid)
291{
292 int i;
293
294 for (i = 0; i < 8; ++i)
295 pr_err("AMD-Vi: DTE[%d]: %08x\n", i,
296 amd_iommu_dev_table[devid].data[i]);
297}
298
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200299static void dump_command(unsigned long phys_addr)
300{
301 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
302 int i;
303
304 for (i = 0; i < 4; ++i)
305 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
306}
307
Joerg Roedela345b232009-09-03 15:01:43 +0200308static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200309{
310 u32 *event = __evt;
311 int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
312 int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
313 int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
314 int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
315 u64 address = (u64)(((u64)event[3]) << 32) | event[2];
316
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200317 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200318
319 switch (type) {
320 case EVENT_TYPE_ILL_DEV:
321 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
322 "address=0x%016llx flags=0x%04x]\n",
323 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
324 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200325 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200326 break;
327 case EVENT_TYPE_IO_FAULT:
328 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
329 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
330 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
331 domid, address, flags);
332 break;
333 case EVENT_TYPE_DEV_TAB_ERR:
334 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
335 "address=0x%016llx flags=0x%04x]\n",
336 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
337 address, flags);
338 break;
339 case EVENT_TYPE_PAGE_TAB_ERR:
340 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
341 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
342 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
343 domid, address, flags);
344 break;
345 case EVENT_TYPE_ILL_CMD:
346 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200347 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200348 break;
349 case EVENT_TYPE_CMD_HARD_ERR:
350 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
351 "flags=0x%04x]\n", address, flags);
352 break;
353 case EVENT_TYPE_IOTLB_INV_TO:
354 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
355 "address=0x%016llx]\n",
356 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
357 address);
358 break;
359 case EVENT_TYPE_INV_DEV_REQ:
360 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
361 "address=0x%016llx flags=0x%04x]\n",
362 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
363 address, flags);
364 break;
365 default:
366 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
367 }
368}
369
370static void iommu_poll_events(struct amd_iommu *iommu)
371{
372 u32 head, tail;
373 unsigned long flags;
374
375 spin_lock_irqsave(&iommu->lock, flags);
376
377 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
378 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
379
380 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200381 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200382 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
383 }
384
385 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
386
387 spin_unlock_irqrestore(&iommu->lock, flags);
388}
389
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200390irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200391{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200392 struct amd_iommu *iommu;
393
Joerg Roedel3bd22172009-05-04 15:06:20 +0200394 for_each_iommu(iommu)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200395 iommu_poll_events(iommu);
396
397 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200398}
399
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200400irqreturn_t amd_iommu_int_handler(int irq, void *data)
401{
402 return IRQ_WAKE_THREAD;
403}
404
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200405/****************************************************************************
406 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200407 * IOMMU command queuing functions
408 *
409 ****************************************************************************/
410
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200411static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200412{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200413 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200414
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200415 while (*sem == 0 && i < LOOP_TIMEOUT) {
416 udelay(1);
417 i += 1;
418 }
419
420 if (i == LOOP_TIMEOUT) {
421 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
422 return -EIO;
423 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200424
425 return 0;
426}
427
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200428static void copy_cmd_to_buffer(struct amd_iommu *iommu,
429 struct iommu_cmd *cmd,
430 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200431{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200432 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200433
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200434 target = iommu->cmd_buf + tail;
435 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200436
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200437 /* Copy command to buffer */
438 memcpy(target, cmd, sizeof(*cmd));
439
440 /* Tell the IOMMU about it */
441 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
442}
443
Joerg Roedel815b33f2011-04-06 17:26:49 +0200444static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200445{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200446 WARN_ON(address & 0x7ULL);
447
Joerg Roedelded46732011-04-06 10:53:48 +0200448 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200449 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
450 cmd->data[1] = upper_32_bits(__pa(address));
451 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200452 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
453}
454
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200455static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
456{
457 memset(cmd, 0, sizeof(*cmd));
458 cmd->data[0] = devid;
459 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
460}
461
Joerg Roedel11b64022011-04-06 11:49:28 +0200462static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
463 size_t size, u16 domid, int pde)
464{
465 u64 pages;
466 int s;
467
468 pages = iommu_num_pages(address, size, PAGE_SIZE);
469 s = 0;
470
471 if (pages > 1) {
472 /*
473 * If we have to flush more than one page, flush all
474 * TLB entries for this domain
475 */
476 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
477 s = 1;
478 }
479
480 address &= PAGE_MASK;
481
482 memset(cmd, 0, sizeof(*cmd));
483 cmd->data[1] |= domid;
484 cmd->data[2] = lower_32_bits(address);
485 cmd->data[3] = upper_32_bits(address);
486 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
487 if (s) /* size bit - we flush more than one 4kb page */
488 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
489 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
490 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
491}
492
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200493static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
494 u64 address, size_t size)
495{
496 u64 pages;
497 int s;
498
499 pages = iommu_num_pages(address, size, PAGE_SIZE);
500 s = 0;
501
502 if (pages > 1) {
503 /*
504 * If we have to flush more than one page, flush all
505 * TLB entries for this domain
506 */
507 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
508 s = 1;
509 }
510
511 address &= PAGE_MASK;
512
513 memset(cmd, 0, sizeof(*cmd));
514 cmd->data[0] = devid;
515 cmd->data[0] |= (qdep & 0xff) << 24;
516 cmd->data[1] = devid;
517 cmd->data[2] = lower_32_bits(address);
518 cmd->data[3] = upper_32_bits(address);
519 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
520 if (s)
521 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
522}
523
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200524static void build_inv_all(struct iommu_cmd *cmd)
525{
526 memset(cmd, 0, sizeof(*cmd));
527 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200528}
529
Joerg Roedel431b2a22008-07-11 17:14:22 +0200530/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200531 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200532 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200533 */
Joerg Roedel054b93a2011-09-02 14:10:32 +0200534static int iommu_queue_command_sync(struct amd_iommu *iommu,
535 struct iommu_cmd *cmd,
536 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200537{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200538 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200539 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200540
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200541 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100542
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200543again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200544 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200545
546 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
547 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
548 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
549 left = (head - next_tail) % iommu->cmd_buf_size;
550
551 if (left <= 2) {
552 struct iommu_cmd sync_cmd;
553 volatile u64 sem = 0;
554 int ret;
555
556 build_completion_wait(&sync_cmd, (u64)&sem);
557 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
558
559 spin_unlock_irqrestore(&iommu->lock, flags);
560
561 if ((ret = wait_on_sem(&sem)) != 0)
562 return ret;
563
564 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200565 }
566
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200567 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200568
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200569 /* We need to sync now to make sure all commands are processed */
Joerg Roedel054b93a2011-09-02 14:10:32 +0200570 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200571
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200572 spin_unlock_irqrestore(&iommu->lock, flags);
573
Joerg Roedel815b33f2011-04-06 17:26:49 +0200574 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100575}
576
Joerg Roedel054b93a2011-09-02 14:10:32 +0200577static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
578{
579 return iommu_queue_command_sync(iommu, cmd, true);
580}
581
Joerg Roedel8d201962008-12-02 20:34:41 +0100582/*
583 * This function queues a completion wait command into the command
584 * buffer of an IOMMU
585 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100586static int iommu_completion_wait(struct amd_iommu *iommu)
587{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200588 struct iommu_cmd cmd;
589 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200590 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100591
592 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200593 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100594
Joerg Roedel815b33f2011-04-06 17:26:49 +0200595 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100596
Joerg Roedel054b93a2011-09-02 14:10:32 +0200597 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +0100598 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200599 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100600
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200601 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200602}
603
Joerg Roedeld8c13082011-04-06 18:51:26 +0200604static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200605{
606 struct iommu_cmd cmd;
607
Joerg Roedeld8c13082011-04-06 18:51:26 +0200608 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200609
Joerg Roedeld8c13082011-04-06 18:51:26 +0200610 return iommu_queue_command(iommu, &cmd);
611}
612
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200613static void iommu_flush_dte_all(struct amd_iommu *iommu)
614{
615 u32 devid;
616
617 for (devid = 0; devid <= 0xffff; ++devid)
618 iommu_flush_dte(iommu, devid);
619
620 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200621}
622
623/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200624 * This function uses heavy locking and may disable irqs for some time. But
625 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200626 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200627static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200628{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200629 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200630
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200631 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
632 struct iommu_cmd cmd;
633 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
634 dom_id, 1);
635 iommu_queue_command(iommu, &cmd);
636 }
Joerg Roedel431b2a22008-07-11 17:14:22 +0200637
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200638 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200639}
640
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200641static void iommu_flush_all(struct amd_iommu *iommu)
642{
643 struct iommu_cmd cmd;
644
645 build_inv_all(&cmd);
646
647 iommu_queue_command(iommu, &cmd);
648 iommu_completion_wait(iommu);
649}
650
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200651void iommu_flush_all_caches(struct amd_iommu *iommu)
652{
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200653 if (iommu_feature(iommu, FEATURE_IA)) {
654 iommu_flush_all(iommu);
655 } else {
656 iommu_flush_dte_all(iommu);
657 iommu_flush_tlb_all(iommu);
658 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200659}
660
Joerg Roedel431b2a22008-07-11 17:14:22 +0200661/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200662 * Command send function for flushing on-device TLB
663 */
664static int device_flush_iotlb(struct device *dev, u64 address, size_t size)
665{
666 struct pci_dev *pdev = to_pci_dev(dev);
667 struct amd_iommu *iommu;
668 struct iommu_cmd cmd;
669 u16 devid;
670 int qdep;
671
672 qdep = pci_ats_queue_depth(pdev);
673 devid = get_device_id(dev);
674 iommu = amd_iommu_rlookup_table[devid];
675
676 build_inv_iotlb_pages(&cmd, devid, qdep, address, size);
677
678 return iommu_queue_command(iommu, &cmd);
679}
680
681/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200682 * Command send function for invalidating a device table entry
683 */
Joerg Roedeld8c13082011-04-06 18:51:26 +0200684static int device_flush_dte(struct device *dev)
Joerg Roedel3fa43652009-11-26 15:04:38 +0100685{
686 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200687 struct pci_dev *pdev;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100688 u16 devid;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200689 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100690
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200691 pdev = to_pci_dev(dev);
Joerg Roedel3fa43652009-11-26 15:04:38 +0100692 devid = get_device_id(dev);
693 iommu = amd_iommu_rlookup_table[devid];
694
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200695 ret = iommu_flush_dte(iommu, devid);
696 if (ret)
697 return ret;
698
699 if (pci_ats_enabled(pdev))
700 ret = device_flush_iotlb(dev, 0, ~0UL);
701
702 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100703}
704
Joerg Roedel431b2a22008-07-11 17:14:22 +0200705/*
706 * TLB invalidation function which is called from the mapping functions.
707 * It invalidates a single PTE if the range to flush is within a single
708 * page. Otherwise it flushes the whole TLB of the IOMMU.
709 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200710static void __domain_flush_pages(struct protection_domain *domain,
711 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200712{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200713 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +0200714 struct iommu_cmd cmd;
715 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200716
Joerg Roedel11b64022011-04-06 11:49:28 +0200717 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +0200718
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100719 for (i = 0; i < amd_iommus_present; ++i) {
720 if (!domain->dev_iommu[i])
721 continue;
722
723 /*
724 * Devices of this domain are behind this IOMMU
725 * We need a TLB flush
726 */
Joerg Roedel11b64022011-04-06 11:49:28 +0200727 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100728 }
729
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200730 list_for_each_entry(dev_data, &domain->dev_list, list) {
731 struct pci_dev *pdev = to_pci_dev(dev_data->dev);
732
733 if (!pci_ats_enabled(pdev))
734 continue;
735
736 ret |= device_flush_iotlb(dev_data->dev, address, size);
737 }
738
Joerg Roedel11b64022011-04-06 11:49:28 +0200739 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100740}
741
Joerg Roedel17b124b2011-04-06 18:01:35 +0200742static void domain_flush_pages(struct protection_domain *domain,
743 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100744{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200745 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200746}
Joerg Roedelb6c02712008-06-26 21:27:53 +0200747
Joerg Roedel1c655772008-09-04 18:40:05 +0200748/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200749static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +0200750{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200751 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +0200752}
753
Chris Wright42a49f92009-06-15 15:42:00 +0200754/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200755static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +0200756{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200757 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
758}
759
760static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +0200761{
762 int i;
763
764 for (i = 0; i < amd_iommus_present; ++i) {
765 if (!domain->dev_iommu[i])
766 continue;
767
768 /*
769 * Devices of this domain are behind this IOMMU
770 * We need to wait for completion of all commands.
771 */
772 iommu_completion_wait(amd_iommus[i]);
773 }
774}
775
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100776
Joerg Roedel43f49602008-12-02 21:01:12 +0100777/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100778 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +0100779 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200780static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200781{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100782 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100783
784 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedeld8c13082011-04-06 18:51:26 +0200785 device_flush_dte(dev_data->dev);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100786}
787
Joerg Roedel431b2a22008-07-11 17:14:22 +0200788/****************************************************************************
789 *
790 * The functions below are used the create the page table mappings for
791 * unity mapped regions.
792 *
793 ****************************************************************************/
794
795/*
Joerg Roedel308973d2009-11-24 17:43:32 +0100796 * This function is used to add another level to an IO page table. Adding
797 * another level increases the size of the address space by 9 bits to a size up
798 * to 64 bits.
799 */
800static bool increase_address_space(struct protection_domain *domain,
801 gfp_t gfp)
802{
803 u64 *pte;
804
805 if (domain->mode == PAGE_MODE_6_LEVEL)
806 /* address space already 64 bit large */
807 return false;
808
809 pte = (void *)get_zeroed_page(gfp);
810 if (!pte)
811 return false;
812
813 *pte = PM_LEVEL_PDE(domain->mode,
814 virt_to_phys(domain->pt_root));
815 domain->pt_root = pte;
816 domain->mode += 1;
817 domain->updated = true;
818
819 return true;
820}
821
822static u64 *alloc_pte(struct protection_domain *domain,
823 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100824 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +0100825 u64 **pte_page,
826 gfp_t gfp)
827{
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100828 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +0100829 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100830
831 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +0100832
833 while (address > PM_LEVEL_SIZE(domain->mode))
834 increase_address_space(domain, gfp);
835
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100836 level = domain->mode - 1;
837 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
838 address = PAGE_SIZE_ALIGN(address, page_size);
839 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +0100840
841 while (level > end_lvl) {
842 if (!IOMMU_PTE_PRESENT(*pte)) {
843 page = (u64 *)get_zeroed_page(gfp);
844 if (!page)
845 return NULL;
846 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
847 }
848
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100849 /* No level skipping support yet */
850 if (PM_PTE_LEVEL(*pte) != level)
851 return NULL;
852
Joerg Roedel308973d2009-11-24 17:43:32 +0100853 level -= 1;
854
855 pte = IOMMU_PTE_PAGE(*pte);
856
857 if (pte_page && level == end_lvl)
858 *pte_page = pte;
859
860 pte = &pte[PM_LEVEL_INDEX(level, address)];
861 }
862
863 return pte;
864}
865
866/*
867 * This function checks if there is a PTE for a given dma address. If
868 * there is one, it returns the pointer to it.
869 */
Joerg Roedel24cd7722010-01-19 17:27:39 +0100870static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +0100871{
872 int level;
873 u64 *pte;
874
Joerg Roedel24cd7722010-01-19 17:27:39 +0100875 if (address > PM_LEVEL_SIZE(domain->mode))
876 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +0100877
Joerg Roedel24cd7722010-01-19 17:27:39 +0100878 level = domain->mode - 1;
879 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
880
881 while (level > 0) {
882
883 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +0100884 if (!IOMMU_PTE_PRESENT(*pte))
885 return NULL;
886
Joerg Roedel24cd7722010-01-19 17:27:39 +0100887 /* Large PTE */
888 if (PM_PTE_LEVEL(*pte) == 0x07) {
889 unsigned long pte_mask, __pte;
890
891 /*
892 * If we have a series of large PTEs, make
893 * sure to return a pointer to the first one.
894 */
895 pte_mask = PTE_PAGE_SIZE(*pte);
896 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
897 __pte = ((unsigned long)pte) & pte_mask;
898
899 return (u64 *)__pte;
900 }
901
902 /* No level skipping support yet */
903 if (PM_PTE_LEVEL(*pte) != level)
904 return NULL;
905
Joerg Roedel308973d2009-11-24 17:43:32 +0100906 level -= 1;
907
Joerg Roedel24cd7722010-01-19 17:27:39 +0100908 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +0100909 pte = IOMMU_PTE_PAGE(*pte);
910 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +0100911 }
912
913 return pte;
914}
915
916/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200917 * Generic mapping functions. It maps a physical address into a DMA
918 * address space. It allocates the page table pages if necessary.
919 * In the future it can be extended to a generic mapping function
920 * supporting all features of AMD IOMMU page tables like level skipping
921 * and full 64 bit address spaces.
922 */
Joerg Roedel38e817f2008-12-02 17:27:52 +0100923static int iommu_map_page(struct protection_domain *dom,
924 unsigned long bus_addr,
925 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200926 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100927 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200928{
Joerg Roedel8bda3092009-05-12 12:02:46 +0200929 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100930 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200931
Joerg Roedelbad1cac2009-09-02 16:52:23 +0200932 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200933 return -EINVAL;
934
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100935 bus_addr = PAGE_ALIGN(bus_addr);
936 phys_addr = PAGE_ALIGN(phys_addr);
937 count = PAGE_SIZE_PTE_COUNT(page_size);
938 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200939
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100940 for (i = 0; i < count; ++i)
941 if (IOMMU_PTE_PRESENT(pte[i]))
942 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200943
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100944 if (page_size > PAGE_SIZE) {
945 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
946 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
947 } else
948 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
949
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200950 if (prot & IOMMU_PROT_IR)
951 __pte |= IOMMU_PTE_IR;
952 if (prot & IOMMU_PROT_IW)
953 __pte |= IOMMU_PTE_IW;
954
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100955 for (i = 0; i < count; ++i)
956 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200957
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200958 update_domain(dom);
959
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200960 return 0;
961}
962
Joerg Roedel24cd7722010-01-19 17:27:39 +0100963static unsigned long iommu_unmap_page(struct protection_domain *dom,
964 unsigned long bus_addr,
965 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100966{
Joerg Roedel24cd7722010-01-19 17:27:39 +0100967 unsigned long long unmap_size, unmapped;
968 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100969
Joerg Roedel24cd7722010-01-19 17:27:39 +0100970 BUG_ON(!is_power_of_2(page_size));
971
972 unmapped = 0;
973
974 while (unmapped < page_size) {
975
976 pte = fetch_pte(dom, bus_addr);
977
978 if (!pte) {
979 /*
980 * No PTE for this address
981 * move forward in 4kb steps
982 */
983 unmap_size = PAGE_SIZE;
984 } else if (PM_PTE_LEVEL(*pte) == 0) {
985 /* 4kb PTE found for this address */
986 unmap_size = PAGE_SIZE;
987 *pte = 0ULL;
988 } else {
989 int count, i;
990
991 /* Large PTE found which maps this address */
992 unmap_size = PTE_PAGE_SIZE(*pte);
993 count = PAGE_SIZE_PTE_COUNT(unmap_size);
994 for (i = 0; i < count; i++)
995 pte[i] = 0ULL;
996 }
997
998 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
999 unmapped += unmap_size;
1000 }
1001
1002 BUG_ON(!is_power_of_2(unmapped));
1003
1004 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001005}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001006
Joerg Roedel431b2a22008-07-11 17:14:22 +02001007/*
1008 * This function checks if a specific unity mapping entry is needed for
1009 * this specific IOMMU.
1010 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001011static int iommu_for_unity_map(struct amd_iommu *iommu,
1012 struct unity_map_entry *entry)
1013{
1014 u16 bdf, i;
1015
1016 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1017 bdf = amd_iommu_alias_table[i];
1018 if (amd_iommu_rlookup_table[bdf] == iommu)
1019 return 1;
1020 }
1021
1022 return 0;
1023}
1024
Joerg Roedel431b2a22008-07-11 17:14:22 +02001025/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001026 * This function actually applies the mapping to the page table of the
1027 * dma_ops domain.
1028 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001029static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1030 struct unity_map_entry *e)
1031{
1032 u64 addr;
1033 int ret;
1034
1035 for (addr = e->address_start; addr < e->address_end;
1036 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001037 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001038 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001039 if (ret)
1040 return ret;
1041 /*
1042 * if unity mapping is in aperture range mark the page
1043 * as allocated in the aperture
1044 */
1045 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001046 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001047 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001048 }
1049
1050 return 0;
1051}
1052
Joerg Roedel431b2a22008-07-11 17:14:22 +02001053/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001054 * Init the unity mappings for a specific IOMMU in the system
1055 *
1056 * Basically iterates over all unity mapping entries and applies them to
1057 * the default domain DMA of that IOMMU if necessary.
1058 */
1059static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1060{
1061 struct unity_map_entry *entry;
1062 int ret;
1063
1064 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1065 if (!iommu_for_unity_map(iommu, entry))
1066 continue;
1067 ret = dma_ops_unity_map(iommu->default_dom, entry);
1068 if (ret)
1069 return ret;
1070 }
1071
1072 return 0;
1073}
1074
1075/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001076 * Inits the unity mappings required for a specific device
1077 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001078static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1079 u16 devid)
1080{
1081 struct unity_map_entry *e;
1082 int ret;
1083
1084 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1085 if (!(devid >= e->devid_start && devid <= e->devid_end))
1086 continue;
1087 ret = dma_ops_unity_map(dma_dom, e);
1088 if (ret)
1089 return ret;
1090 }
1091
1092 return 0;
1093}
1094
Joerg Roedel431b2a22008-07-11 17:14:22 +02001095/****************************************************************************
1096 *
1097 * The next functions belong to the address allocator for the dma_ops
1098 * interface functions. They work like the allocators in the other IOMMU
1099 * drivers. Its basically a bitmap which marks the allocated pages in
1100 * the aperture. Maybe it could be enhanced in the future to a more
1101 * efficient allocator.
1102 *
1103 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001104
Joerg Roedel431b2a22008-07-11 17:14:22 +02001105/*
Joerg Roedel384de722009-05-15 12:30:05 +02001106 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001107 *
1108 * called with domain->lock held
1109 */
Joerg Roedel384de722009-05-15 12:30:05 +02001110
Joerg Roedel9cabe892009-05-18 16:38:55 +02001111/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001112 * Used to reserve address ranges in the aperture (e.g. for exclusion
1113 * ranges.
1114 */
1115static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1116 unsigned long start_page,
1117 unsigned int pages)
1118{
1119 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1120
1121 if (start_page + pages > last_page)
1122 pages = last_page - start_page;
1123
1124 for (i = start_page; i < start_page + pages; ++i) {
1125 int index = i / APERTURE_RANGE_PAGES;
1126 int page = i % APERTURE_RANGE_PAGES;
1127 __set_bit(page, dom->aperture[index]->bitmap);
1128 }
1129}
1130
1131/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001132 * This function is used to add a new aperture range to an existing
1133 * aperture in case of dma_ops domain allocation or address allocation
1134 * failure.
1135 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001136static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001137 bool populate, gfp_t gfp)
1138{
1139 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001140 struct amd_iommu *iommu;
Joerg Roedeld91afd12010-01-22 16:40:20 +01001141 unsigned long i;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001142
Joerg Roedelf5e97052009-05-22 12:31:53 +02001143#ifdef CONFIG_IOMMU_STRESS
1144 populate = false;
1145#endif
1146
Joerg Roedel9cabe892009-05-18 16:38:55 +02001147 if (index >= APERTURE_MAX_RANGES)
1148 return -ENOMEM;
1149
1150 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1151 if (!dma_dom->aperture[index])
1152 return -ENOMEM;
1153
1154 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1155 if (!dma_dom->aperture[index]->bitmap)
1156 goto out_free;
1157
1158 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1159
1160 if (populate) {
1161 unsigned long address = dma_dom->aperture_size;
1162 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1163 u64 *pte, *pte_page;
1164
1165 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001166 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001167 &pte_page, gfp);
1168 if (!pte)
1169 goto out_free;
1170
1171 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1172
1173 address += APERTURE_RANGE_SIZE / 64;
1174 }
1175 }
1176
1177 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1178
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001179 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001180 for_each_iommu(iommu) {
1181 if (iommu->exclusion_start &&
1182 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1183 && iommu->exclusion_start < dma_dom->aperture_size) {
1184 unsigned long startpage;
1185 int pages = iommu_num_pages(iommu->exclusion_start,
1186 iommu->exclusion_length,
1187 PAGE_SIZE);
1188 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1189 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1190 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001191 }
1192
1193 /*
1194 * Check for areas already mapped as present in the new aperture
1195 * range and mark those pages as reserved in the allocator. Such
1196 * mappings may already exist as a result of requested unity
1197 * mappings for devices.
1198 */
1199 for (i = dma_dom->aperture[index]->offset;
1200 i < dma_dom->aperture_size;
1201 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001202 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001203 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1204 continue;
1205
Joerg Roedel9bdb6662011-10-11 17:41:32 +02001206 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001207 }
1208
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001209 update_domain(&dma_dom->domain);
1210
Joerg Roedel9cabe892009-05-18 16:38:55 +02001211 return 0;
1212
1213out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001214 update_domain(&dma_dom->domain);
1215
Joerg Roedel9cabe892009-05-18 16:38:55 +02001216 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1217
1218 kfree(dma_dom->aperture[index]);
1219 dma_dom->aperture[index] = NULL;
1220
1221 return -ENOMEM;
1222}
1223
Joerg Roedel384de722009-05-15 12:30:05 +02001224static unsigned long dma_ops_area_alloc(struct device *dev,
1225 struct dma_ops_domain *dom,
1226 unsigned int pages,
1227 unsigned long align_mask,
1228 u64 dma_mask,
1229 unsigned long start)
1230{
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001231 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001232 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1233 int i = start >> APERTURE_RANGE_SHIFT;
1234 unsigned long boundary_size;
1235 unsigned long address = -1;
1236 unsigned long limit;
1237
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001238 next_bit >>= PAGE_SHIFT;
1239
Joerg Roedel384de722009-05-15 12:30:05 +02001240 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1241 PAGE_SIZE) >> PAGE_SHIFT;
1242
1243 for (;i < max_index; ++i) {
1244 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1245
1246 if (dom->aperture[i]->offset >= dma_mask)
1247 break;
1248
1249 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1250 dma_mask >> PAGE_SHIFT);
1251
1252 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1253 limit, next_bit, pages, 0,
1254 boundary_size, align_mask);
1255 if (address != -1) {
1256 address = dom->aperture[i]->offset +
1257 (address << PAGE_SHIFT);
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001258 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001259 break;
1260 }
1261
1262 next_bit = 0;
1263 }
1264
1265 return address;
1266}
1267
Joerg Roedeld3086442008-06-26 21:27:57 +02001268static unsigned long dma_ops_alloc_addresses(struct device *dev,
1269 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001270 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001271 unsigned long align_mask,
1272 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001273{
Joerg Roedeld3086442008-06-26 21:27:57 +02001274 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001275
Joerg Roedelfe16f082009-05-22 12:27:53 +02001276#ifdef CONFIG_IOMMU_STRESS
1277 dom->next_address = 0;
1278 dom->need_flush = true;
1279#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001280
Joerg Roedel384de722009-05-15 12:30:05 +02001281 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001282 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001283
Joerg Roedel1c655772008-09-04 18:40:05 +02001284 if (address == -1) {
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001285 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001286 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1287 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001288 dom->need_flush = true;
1289 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001290
Joerg Roedel384de722009-05-15 12:30:05 +02001291 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001292 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001293
1294 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1295
1296 return address;
1297}
1298
Joerg Roedel431b2a22008-07-11 17:14:22 +02001299/*
1300 * The address free function.
1301 *
1302 * called with domain->lock held
1303 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001304static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1305 unsigned long address,
1306 unsigned int pages)
1307{
Joerg Roedel384de722009-05-15 12:30:05 +02001308 unsigned i = address >> APERTURE_RANGE_SHIFT;
1309 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001310
Joerg Roedel384de722009-05-15 12:30:05 +02001311 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1312
Joerg Roedel47bccd62009-05-22 12:40:54 +02001313#ifdef CONFIG_IOMMU_STRESS
1314 if (i < 4)
1315 return;
1316#endif
1317
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001318 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001319 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001320
1321 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001322
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001323 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001324
Joerg Roedeld3086442008-06-26 21:27:57 +02001325}
1326
Joerg Roedel431b2a22008-07-11 17:14:22 +02001327/****************************************************************************
1328 *
1329 * The next functions belong to the domain allocation. A domain is
1330 * allocated for every IOMMU as the default domain. If device isolation
1331 * is enabled, every device get its own domain. The most important thing
1332 * about domains is the page table mapping the DMA address space they
1333 * contain.
1334 *
1335 ****************************************************************************/
1336
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001337/*
1338 * This function adds a protection domain to the global protection domain list
1339 */
1340static void add_domain_to_list(struct protection_domain *domain)
1341{
1342 unsigned long flags;
1343
1344 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1345 list_add(&domain->list, &amd_iommu_pd_list);
1346 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1347}
1348
1349/*
1350 * This function removes a protection domain to the global
1351 * protection domain list
1352 */
1353static void del_domain_from_list(struct protection_domain *domain)
1354{
1355 unsigned long flags;
1356
1357 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1358 list_del(&domain->list);
1359 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1360}
1361
Joerg Roedelec487d12008-06-26 21:27:58 +02001362static u16 domain_id_alloc(void)
1363{
1364 unsigned long flags;
1365 int id;
1366
1367 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1368 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1369 BUG_ON(id == 0);
1370 if (id > 0 && id < MAX_DOMAIN_ID)
1371 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1372 else
1373 id = 0;
1374 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1375
1376 return id;
1377}
1378
Joerg Roedela2acfb72008-12-02 18:28:53 +01001379static void domain_id_free(int id)
1380{
1381 unsigned long flags;
1382
1383 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1384 if (id > 0 && id < MAX_DOMAIN_ID)
1385 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1386 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1387}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001388
Joerg Roedel86db2e52008-12-02 18:20:21 +01001389static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001390{
1391 int i, j;
1392 u64 *p1, *p2, *p3;
1393
Joerg Roedel86db2e52008-12-02 18:20:21 +01001394 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001395
1396 if (!p1)
1397 return;
1398
1399 for (i = 0; i < 512; ++i) {
1400 if (!IOMMU_PTE_PRESENT(p1[i]))
1401 continue;
1402
1403 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001404 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001405 if (!IOMMU_PTE_PRESENT(p2[j]))
1406 continue;
1407 p3 = IOMMU_PTE_PAGE(p2[j]);
1408 free_page((unsigned long)p3);
1409 }
1410
1411 free_page((unsigned long)p2);
1412 }
1413
1414 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001415
1416 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001417}
1418
Joerg Roedel431b2a22008-07-11 17:14:22 +02001419/*
1420 * Free a domain, only used if something went wrong in the
1421 * allocation path and we need to free an already allocated page table
1422 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001423static void dma_ops_domain_free(struct dma_ops_domain *dom)
1424{
Joerg Roedel384de722009-05-15 12:30:05 +02001425 int i;
1426
Joerg Roedelec487d12008-06-26 21:27:58 +02001427 if (!dom)
1428 return;
1429
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001430 del_domain_from_list(&dom->domain);
1431
Joerg Roedel86db2e52008-12-02 18:20:21 +01001432 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001433
Joerg Roedel384de722009-05-15 12:30:05 +02001434 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1435 if (!dom->aperture[i])
1436 continue;
1437 free_page((unsigned long)dom->aperture[i]->bitmap);
1438 kfree(dom->aperture[i]);
1439 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001440
1441 kfree(dom);
1442}
1443
Joerg Roedel431b2a22008-07-11 17:14:22 +02001444/*
1445 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001446 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001447 * structures required for the dma_ops interface
1448 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001449static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001450{
1451 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001452
1453 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1454 if (!dma_dom)
1455 return NULL;
1456
1457 spin_lock_init(&dma_dom->domain.lock);
1458
1459 dma_dom->domain.id = domain_id_alloc();
1460 if (dma_dom->domain.id == 0)
1461 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001462 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001463 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001464 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001465 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001466 dma_dom->domain.priv = dma_dom;
1467 if (!dma_dom->domain.pt_root)
1468 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001469
Joerg Roedel1c655772008-09-04 18:40:05 +02001470 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001471 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001472
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001473 add_domain_to_list(&dma_dom->domain);
1474
Joerg Roedel576175c2009-11-23 19:08:46 +01001475 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001476 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001477
Joerg Roedel431b2a22008-07-11 17:14:22 +02001478 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001479 * mark the first page as allocated so we never return 0 as
1480 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001481 */
Joerg Roedel384de722009-05-15 12:30:05 +02001482 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001483 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001484
Joerg Roedelec487d12008-06-26 21:27:58 +02001485
1486 return dma_dom;
1487
1488free_dma_dom:
1489 dma_ops_domain_free(dma_dom);
1490
1491 return NULL;
1492}
1493
Joerg Roedel431b2a22008-07-11 17:14:22 +02001494/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001495 * little helper function to check whether a given protection domain is a
1496 * dma_ops domain
1497 */
1498static bool dma_ops_domain(struct protection_domain *domain)
1499{
1500 return domain->flags & PD_DMA_OPS_MASK;
1501}
1502
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001503static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001504{
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001505 u64 pte_root = virt_to_phys(domain->pt_root);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001506 u32 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001507
Joerg Roedel38ddf412008-09-11 10:38:32 +02001508 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1509 << DEV_ENTRY_MODE_SHIFT;
1510 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001511
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001512 if (ats)
1513 flags |= DTE_FLAG_IOTLB;
1514
1515 amd_iommu_dev_table[devid].data[3] |= flags;
1516 amd_iommu_dev_table[devid].data[2] = domain->id;
1517 amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
1518 amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001519}
1520
Joerg Roedel15898bb2009-11-24 15:39:42 +01001521static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001522{
Joerg Roedel355bf552008-12-08 12:02:41 +01001523 /* remove entry from the device table seen by the hardware */
1524 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1525 amd_iommu_dev_table[devid].data[1] = 0;
1526 amd_iommu_dev_table[devid].data[2] = 0;
1527
Joerg Roedelc5cca142009-10-09 18:31:20 +02001528 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001529}
1530
1531static void do_attach(struct device *dev, struct protection_domain *domain)
1532{
1533 struct iommu_dev_data *dev_data;
1534 struct amd_iommu *iommu;
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001535 struct pci_dev *pdev;
1536 bool ats = false;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001537 u16 devid;
1538
1539 devid = get_device_id(dev);
1540 iommu = amd_iommu_rlookup_table[devid];
1541 dev_data = get_dev_data(dev);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001542 pdev = to_pci_dev(dev);
1543
1544 if (amd_iommu_iotlb_sup)
1545 ats = pci_ats_enabled(pdev);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001546
1547 /* Update data structures */
1548 dev_data->domain = domain;
1549 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001550 set_dte_entry(devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001551
1552 /* Do reference counting */
1553 domain->dev_iommu[iommu->index] += 1;
1554 domain->dev_cnt += 1;
1555
1556 /* Flush the DTE entry */
Joerg Roedeld8c13082011-04-06 18:51:26 +02001557 device_flush_dte(dev);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001558}
1559
1560static void do_detach(struct device *dev)
1561{
1562 struct iommu_dev_data *dev_data;
1563 struct amd_iommu *iommu;
1564 u16 devid;
1565
1566 devid = get_device_id(dev);
1567 iommu = amd_iommu_rlookup_table[devid];
1568 dev_data = get_dev_data(dev);
Joerg Roedelc5cca142009-10-09 18:31:20 +02001569
Joerg Roedelc4596112009-11-20 14:57:32 +01001570 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001571 dev_data->domain->dev_iommu[iommu->index] -= 1;
1572 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001573
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001574 /* Update data structures */
1575 dev_data->domain = NULL;
1576 list_del(&dev_data->list);
1577 clear_dte_entry(devid);
1578
1579 /* Flush the DTE entry */
Joerg Roedeld8c13082011-04-06 18:51:26 +02001580 device_flush_dte(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001581}
1582
1583/*
1584 * If a device is not yet associated with a domain, this function does
1585 * assigns it visible for the hardware
1586 */
1587static int __attach_device(struct device *dev,
1588 struct protection_domain *domain)
1589{
Joerg Roedel657cbb62009-11-23 15:26:46 +01001590 struct iommu_dev_data *dev_data, *alias_data;
Julia Lawall84fe6c12010-05-27 12:31:51 +02001591 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001592
Joerg Roedel657cbb62009-11-23 15:26:46 +01001593 dev_data = get_dev_data(dev);
1594 alias_data = get_dev_data(dev_data->alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001595
Joerg Roedel657cbb62009-11-23 15:26:46 +01001596 if (!alias_data)
1597 return -EINVAL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001598
1599 /* lock domain */
1600 spin_lock(&domain->lock);
1601
1602 /* Some sanity checks */
Julia Lawall84fe6c12010-05-27 12:31:51 +02001603 ret = -EBUSY;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001604 if (alias_data->domain != NULL &&
1605 alias_data->domain != domain)
Julia Lawall84fe6c12010-05-27 12:31:51 +02001606 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001607
Joerg Roedel657cbb62009-11-23 15:26:46 +01001608 if (dev_data->domain != NULL &&
1609 dev_data->domain != domain)
Julia Lawall84fe6c12010-05-27 12:31:51 +02001610 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001611
1612 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001613 if (dev_data->alias != dev) {
1614 alias_data = get_dev_data(dev_data->alias);
1615 if (alias_data->domain == NULL)
1616 do_attach(dev_data->alias, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01001617
1618 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001619 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01001620
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001621 if (dev_data->domain == NULL)
1622 do_attach(dev, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001623
Joerg Roedel24100052009-11-25 15:59:57 +01001624 atomic_inc(&dev_data->bind);
1625
Julia Lawall84fe6c12010-05-27 12:31:51 +02001626 ret = 0;
1627
1628out_unlock:
1629
Joerg Roedel355bf552008-12-08 12:02:41 +01001630 /* ready */
1631 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001632
Julia Lawall84fe6c12010-05-27 12:31:51 +02001633 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001634}
1635
1636/*
1637 * If a device is not yet associated with a domain, this function does
1638 * assigns it visible for the hardware
1639 */
1640static int attach_device(struct device *dev,
1641 struct protection_domain *domain)
1642{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001643 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001644 unsigned long flags;
1645 int ret;
1646
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001647 if (amd_iommu_iotlb_sup)
1648 pci_enable_ats(pdev, PAGE_SHIFT);
1649
Joerg Roedel15898bb2009-11-24 15:39:42 +01001650 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1651 ret = __attach_device(dev, domain);
1652 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1653
1654 /*
1655 * We might boot into a crash-kernel here. The crashed kernel
1656 * left the caches in the IOMMU dirty. So we have to flush
1657 * here to evict all dirty stuff.
1658 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001659 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001660
1661 return ret;
1662}
1663
1664/*
1665 * Removes a device from a protection domain (unlocked)
1666 */
1667static void __detach_device(struct device *dev)
1668{
Joerg Roedel657cbb62009-11-23 15:26:46 +01001669 struct iommu_dev_data *dev_data = get_dev_data(dev);
Joerg Roedel24100052009-11-25 15:59:57 +01001670 struct iommu_dev_data *alias_data;
Joerg Roedel2ca76272010-01-22 16:45:31 +01001671 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001672 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001673
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001674 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001675
Joerg Roedel2ca76272010-01-22 16:45:31 +01001676 domain = dev_data->domain;
1677
1678 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01001679
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001680 if (dev_data->alias != dev) {
Joerg Roedel24100052009-11-25 15:59:57 +01001681 alias_data = get_dev_data(dev_data->alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001682 if (atomic_dec_and_test(&alias_data->bind))
1683 do_detach(dev_data->alias);
Joerg Roedel24100052009-11-25 15:59:57 +01001684 }
1685
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001686 if (atomic_dec_and_test(&dev_data->bind))
1687 do_detach(dev);
1688
Joerg Roedel2ca76272010-01-22 16:45:31 +01001689 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001690
Joerg Roedel21129f72009-09-01 11:59:42 +02001691 /*
1692 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001693 * passthrough domain if it is detached from any other domain.
1694 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02001695 */
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001696 if (iommu_pass_through &&
1697 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedel15898bb2009-11-24 15:39:42 +01001698 __attach_device(dev, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01001699}
1700
1701/*
1702 * Removes a device from a protection domain (with devtable_lock held)
1703 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01001704static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01001705{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001706 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedel355bf552008-12-08 12:02:41 +01001707 unsigned long flags;
1708
1709 /* lock device table */
1710 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001711 __detach_device(dev);
Joerg Roedel355bf552008-12-08 12:02:41 +01001712 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001713
1714 if (amd_iommu_iotlb_sup && pci_ats_enabled(pdev))
1715 pci_disable_ats(pdev);
Joerg Roedel355bf552008-12-08 12:02:41 +01001716}
Joerg Roedele275a2a2008-12-10 18:27:25 +01001717
Joerg Roedel15898bb2009-11-24 15:39:42 +01001718/*
1719 * Find out the protection domain structure for a given PCI device. This
1720 * will give us the pointer to the page table root for example.
1721 */
1722static struct protection_domain *domain_for_device(struct device *dev)
1723{
1724 struct protection_domain *dom;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001725 struct iommu_dev_data *dev_data, *alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001726 unsigned long flags;
Gustavo F. Padovan6ec5ff42011-05-20 16:13:00 -03001727 u16 devid;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001728
Joerg Roedel657cbb62009-11-23 15:26:46 +01001729 devid = get_device_id(dev);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001730 dev_data = get_dev_data(dev);
1731 alias_data = get_dev_data(dev_data->alias);
1732 if (!alias_data)
1733 return NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001734
1735 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001736 dom = dev_data->domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001737 if (dom == NULL &&
Joerg Roedel657cbb62009-11-23 15:26:46 +01001738 alias_data->domain != NULL) {
1739 __attach_device(dev, alias_data->domain);
1740 dom = alias_data->domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001741 }
1742
1743 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1744
1745 return dom;
1746}
1747
Joerg Roedele275a2a2008-12-10 18:27:25 +01001748static int device_change_notifier(struct notifier_block *nb,
1749 unsigned long action, void *data)
1750{
1751 struct device *dev = data;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001752 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001753 struct protection_domain *domain;
1754 struct dma_ops_domain *dma_domain;
1755 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001756 unsigned long flags;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001757
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001758 if (!check_device(dev))
1759 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001760
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001761 devid = get_device_id(dev);
1762 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01001763
1764 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07001765 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001766
1767 domain = domain_for_device(dev);
1768
Joerg Roedele275a2a2008-12-10 18:27:25 +01001769 if (!domain)
1770 goto out;
Joerg Roedela1ca3312009-09-01 12:22:22 +02001771 if (iommu_pass_through)
1772 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001773 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001774 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001775 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001776
1777 iommu_init_device(dev);
1778
1779 domain = domain_for_device(dev);
1780
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001781 /* allocate a protection domain if a device is added */
1782 dma_domain = find_protection_domain(devid);
1783 if (dma_domain)
1784 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01001785 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001786 if (!dma_domain)
1787 goto out;
1788 dma_domain->target_dev = devid;
1789
1790 spin_lock_irqsave(&iommu_pd_list_lock, flags);
1791 list_add_tail(&dma_domain->list, &iommu_pd_list);
1792 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
1793
1794 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001795 case BUS_NOTIFY_DEL_DEVICE:
1796
1797 iommu_uninit_device(dev);
1798
Joerg Roedele275a2a2008-12-10 18:27:25 +01001799 default:
1800 goto out;
1801 }
1802
Joerg Roedeld8c13082011-04-06 18:51:26 +02001803 device_flush_dte(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001804 iommu_completion_wait(iommu);
1805
1806out:
1807 return 0;
1808}
1809
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05301810static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01001811 .notifier_call = device_change_notifier,
1812};
Joerg Roedel355bf552008-12-08 12:02:41 +01001813
Joerg Roedel8638c492009-12-10 11:12:25 +01001814void amd_iommu_init_notifier(void)
1815{
1816 bus_register_notifier(&pci_bus_type, &device_nb);
1817}
1818
Joerg Roedel431b2a22008-07-11 17:14:22 +02001819/*****************************************************************************
1820 *
1821 * The next functions belong to the dma_ops mapping/unmapping code.
1822 *
1823 *****************************************************************************/
1824
1825/*
1826 * In the dma_ops path we only have the struct device. This function
1827 * finds the corresponding IOMMU, the protection domain and the
1828 * requestor id for a given device.
1829 * If the device is not yet associated with a domain this is also done
1830 * in this function.
1831 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001832static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001833{
Joerg Roedel94f6d192009-11-24 16:40:02 +01001834 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001835 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01001836 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001837
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001838 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01001839 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001840
Joerg Roedel94f6d192009-11-24 16:40:02 +01001841 domain = domain_for_device(dev);
1842 if (domain != NULL && !dma_ops_domain(domain))
1843 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001844
Joerg Roedel94f6d192009-11-24 16:40:02 +01001845 if (domain != NULL)
1846 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001847
Joerg Roedel15898bb2009-11-24 15:39:42 +01001848 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001849 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001850 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01001851 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
1852 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001853 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01001854 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01001855
Joerg Roedel94f6d192009-11-24 16:40:02 +01001856 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001857}
1858
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001859static void update_device_table(struct protection_domain *domain)
1860{
Joerg Roedel492667d2009-11-27 13:25:47 +01001861 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001862
Joerg Roedel492667d2009-11-27 13:25:47 +01001863 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001864 struct pci_dev *pdev = to_pci_dev(dev_data->dev);
Joerg Roedel492667d2009-11-27 13:25:47 +01001865 u16 devid = get_device_id(dev_data->dev);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001866 set_dte_entry(devid, domain, pci_ats_enabled(pdev));
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001867 }
1868}
1869
1870static void update_domain(struct protection_domain *domain)
1871{
1872 if (!domain->updated)
1873 return;
1874
1875 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02001876
1877 domain_flush_devices(domain);
1878 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001879
1880 domain->updated = false;
1881}
1882
Joerg Roedel431b2a22008-07-11 17:14:22 +02001883/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02001884 * This function fetches the PTE for a given address in the aperture
1885 */
1886static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
1887 unsigned long address)
1888{
Joerg Roedel384de722009-05-15 12:30:05 +02001889 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02001890 u64 *pte, *pte_page;
1891
Joerg Roedel384de722009-05-15 12:30:05 +02001892 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1893 if (!aperture)
1894 return NULL;
1895
1896 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02001897 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001898 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001899 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02001900 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
1901 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001902 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001903
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001904 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001905
1906 return pte;
1907}
1908
1909/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001910 * This is the generic map function. It maps one 4kb page at paddr to
1911 * the given address in the DMA address space for the domain.
1912 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001913static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001914 unsigned long address,
1915 phys_addr_t paddr,
1916 int direction)
1917{
1918 u64 *pte, __pte;
1919
1920 WARN_ON(address > dom->aperture_size);
1921
1922 paddr &= PAGE_MASK;
1923
Joerg Roedel8bda3092009-05-12 12:02:46 +02001924 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02001925 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001926 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001927
1928 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
1929
1930 if (direction == DMA_TO_DEVICE)
1931 __pte |= IOMMU_PTE_IR;
1932 else if (direction == DMA_FROM_DEVICE)
1933 __pte |= IOMMU_PTE_IW;
1934 else if (direction == DMA_BIDIRECTIONAL)
1935 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
1936
1937 WARN_ON(*pte);
1938
1939 *pte = __pte;
1940
1941 return (dma_addr_t)address;
1942}
1943
Joerg Roedel431b2a22008-07-11 17:14:22 +02001944/*
1945 * The generic unmapping function for on page in the DMA address space.
1946 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001947static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001948 unsigned long address)
1949{
Joerg Roedel384de722009-05-15 12:30:05 +02001950 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001951 u64 *pte;
1952
1953 if (address >= dom->aperture_size)
1954 return;
1955
Joerg Roedel384de722009-05-15 12:30:05 +02001956 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1957 if (!aperture)
1958 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001959
Joerg Roedel384de722009-05-15 12:30:05 +02001960 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
1961 if (!pte)
1962 return;
1963
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001964 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001965
1966 WARN_ON(!*pte);
1967
1968 *pte = 0ULL;
1969}
1970
Joerg Roedel431b2a22008-07-11 17:14:22 +02001971/*
1972 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01001973 * contiguous memory region into DMA address space. It is used by all
1974 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001975 * Must be called with the domain lock held.
1976 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02001977static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001978 struct dma_ops_domain *dma_dom,
1979 phys_addr_t paddr,
1980 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001981 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001982 bool align,
1983 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02001984{
1985 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02001986 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001987 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001988 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001989 int i;
1990
Joerg Roedele3c449f2008-10-15 22:02:11 -07001991 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001992 paddr &= PAGE_MASK;
1993
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01001994 INC_STATS_COUNTER(total_map_requests);
1995
Joerg Roedelc1858972008-12-12 15:42:39 +01001996 if (pages > 1)
1997 INC_STATS_COUNTER(cross_page);
1998
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001999 if (align)
2000 align_mask = (1UL << get_order(size)) - 1;
2001
Joerg Roedel11b83882009-05-19 10:23:15 +02002002retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002003 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2004 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002005 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002006 /*
2007 * setting next_address here will let the address
2008 * allocator only scan the new allocated range in the
2009 * first run. This is a small optimization.
2010 */
2011 dma_dom->next_address = dma_dom->aperture_size;
2012
Joerg Roedel576175c2009-11-23 19:08:46 +01002013 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002014 goto out;
2015
2016 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002017 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002018 * allocation again
2019 */
2020 goto retry;
2021 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002022
2023 start = address;
2024 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002025 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002026 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002027 goto out_unmap;
2028
Joerg Roedelcb76c322008-06-26 21:28:00 +02002029 paddr += PAGE_SIZE;
2030 start += PAGE_SIZE;
2031 }
2032 address += offset;
2033
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002034 ADD_STATS_COUNTER(alloced_io_mem, size);
2035
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002036 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002037 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002038 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002039 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002040 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002041
Joerg Roedelcb76c322008-06-26 21:28:00 +02002042out:
2043 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002044
2045out_unmap:
2046
2047 for (--i; i >= 0; --i) {
2048 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002049 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002050 }
2051
2052 dma_ops_free_addresses(dma_dom, address, pages);
2053
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002054 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002055}
2056
Joerg Roedel431b2a22008-07-11 17:14:22 +02002057/*
2058 * Does the reverse of the __map_single function. Must be called with
2059 * the domain lock held too
2060 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002061static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002062 dma_addr_t dma_addr,
2063 size_t size,
2064 int dir)
2065{
Joerg Roedel04e04632010-09-23 16:12:48 +02002066 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002067 dma_addr_t i, start;
2068 unsigned int pages;
2069
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002070 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002071 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002072 return;
2073
Joerg Roedel04e04632010-09-23 16:12:48 +02002074 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002075 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002076 dma_addr &= PAGE_MASK;
2077 start = dma_addr;
2078
2079 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002080 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002081 start += PAGE_SIZE;
2082 }
2083
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002084 SUB_STATS_COUNTER(alloced_io_mem, size);
2085
Joerg Roedelcb76c322008-06-26 21:28:00 +02002086 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002087
Joerg Roedel80be3082008-11-06 14:59:05 +01002088 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002089 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002090 dma_dom->need_flush = false;
2091 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002092}
2093
Joerg Roedel431b2a22008-07-11 17:14:22 +02002094/*
2095 * The exported map_single function for dma_ops.
2096 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002097static dma_addr_t map_page(struct device *dev, struct page *page,
2098 unsigned long offset, size_t size,
2099 enum dma_data_direction dir,
2100 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002101{
2102 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002103 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002104 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002105 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002106 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002107
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002108 INC_STATS_COUNTER(cnt_map_single);
2109
Joerg Roedel94f6d192009-11-24 16:40:02 +01002110 domain = get_domain(dev);
2111 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002112 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002113 else if (IS_ERR(domain))
2114 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002115
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002116 dma_mask = *dev->dma_mask;
2117
Joerg Roedel4da70b92008-06-26 21:28:01 +02002118 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002119
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002120 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002121 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002122 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002123 goto out;
2124
Joerg Roedel17b124b2011-04-06 18:01:35 +02002125 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002126
2127out:
2128 spin_unlock_irqrestore(&domain->lock, flags);
2129
2130 return addr;
2131}
2132
Joerg Roedel431b2a22008-07-11 17:14:22 +02002133/*
2134 * The exported unmap_single function for dma_ops.
2135 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002136static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2137 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002138{
2139 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002140 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002141
Joerg Roedel146a6912008-12-12 15:07:12 +01002142 INC_STATS_COUNTER(cnt_unmap_single);
2143
Joerg Roedel94f6d192009-11-24 16:40:02 +01002144 domain = get_domain(dev);
2145 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002146 return;
2147
Joerg Roedel4da70b92008-06-26 21:28:01 +02002148 spin_lock_irqsave(&domain->lock, flags);
2149
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002150 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002151
Joerg Roedel17b124b2011-04-06 18:01:35 +02002152 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002153
2154 spin_unlock_irqrestore(&domain->lock, flags);
2155}
2156
Joerg Roedel431b2a22008-07-11 17:14:22 +02002157/*
2158 * This is a special map_sg function which is used if we should map a
2159 * device which is not handled by an AMD IOMMU in the system.
2160 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002161static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2162 int nelems, int dir)
2163{
2164 struct scatterlist *s;
2165 int i;
2166
2167 for_each_sg(sglist, s, nelems, i) {
2168 s->dma_address = (dma_addr_t)sg_phys(s);
2169 s->dma_length = s->length;
2170 }
2171
2172 return nelems;
2173}
2174
Joerg Roedel431b2a22008-07-11 17:14:22 +02002175/*
2176 * The exported map_sg function for dma_ops (handles scatter-gather
2177 * lists).
2178 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002179static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002180 int nelems, enum dma_data_direction dir,
2181 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002182{
2183 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002184 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002185 int i;
2186 struct scatterlist *s;
2187 phys_addr_t paddr;
2188 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002189 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002190
Joerg Roedeld03f0672008-12-12 15:09:48 +01002191 INC_STATS_COUNTER(cnt_map_sg);
2192
Joerg Roedel94f6d192009-11-24 16:40:02 +01002193 domain = get_domain(dev);
2194 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002195 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002196 else if (IS_ERR(domain))
2197 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002198
Joerg Roedel832a90c2008-09-18 15:54:23 +02002199 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002200
Joerg Roedel65b050a2008-06-26 21:28:02 +02002201 spin_lock_irqsave(&domain->lock, flags);
2202
2203 for_each_sg(sglist, s, nelems, i) {
2204 paddr = sg_phys(s);
2205
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002206 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002207 paddr, s->length, dir, false,
2208 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002209
2210 if (s->dma_address) {
2211 s->dma_length = s->length;
2212 mapped_elems++;
2213 } else
2214 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002215 }
2216
Joerg Roedel17b124b2011-04-06 18:01:35 +02002217 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002218
2219out:
2220 spin_unlock_irqrestore(&domain->lock, flags);
2221
2222 return mapped_elems;
2223unmap:
2224 for_each_sg(sglist, s, mapped_elems, i) {
2225 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002226 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002227 s->dma_length, dir);
2228 s->dma_address = s->dma_length = 0;
2229 }
2230
2231 mapped_elems = 0;
2232
2233 goto out;
2234}
2235
Joerg Roedel431b2a22008-07-11 17:14:22 +02002236/*
2237 * The exported map_sg function for dma_ops (handles scatter-gather
2238 * lists).
2239 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002240static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002241 int nelems, enum dma_data_direction dir,
2242 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002243{
2244 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002245 struct protection_domain *domain;
2246 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002247 int i;
2248
Joerg Roedel55877a62008-12-12 15:12:14 +01002249 INC_STATS_COUNTER(cnt_unmap_sg);
2250
Joerg Roedel94f6d192009-11-24 16:40:02 +01002251 domain = get_domain(dev);
2252 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002253 return;
2254
Joerg Roedel65b050a2008-06-26 21:28:02 +02002255 spin_lock_irqsave(&domain->lock, flags);
2256
2257 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002258 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002259 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002260 s->dma_address = s->dma_length = 0;
2261 }
2262
Joerg Roedel17b124b2011-04-06 18:01:35 +02002263 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002264
2265 spin_unlock_irqrestore(&domain->lock, flags);
2266}
2267
Joerg Roedel431b2a22008-07-11 17:14:22 +02002268/*
2269 * The exported alloc_coherent function for dma_ops.
2270 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002271static void *alloc_coherent(struct device *dev, size_t size,
2272 dma_addr_t *dma_addr, gfp_t flag)
2273{
2274 unsigned long flags;
2275 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002276 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002277 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002278 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002279
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002280 INC_STATS_COUNTER(cnt_alloc_coherent);
2281
Joerg Roedel94f6d192009-11-24 16:40:02 +01002282 domain = get_domain(dev);
2283 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002284 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2285 *dma_addr = __pa(virt_addr);
2286 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002287 } else if (IS_ERR(domain))
2288 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002289
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002290 dma_mask = dev->coherent_dma_mask;
2291 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2292 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002293
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002294 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2295 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302296 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002297
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002298 paddr = virt_to_phys(virt_addr);
2299
Joerg Roedel832a90c2008-09-18 15:54:23 +02002300 if (!dma_mask)
2301 dma_mask = *dev->dma_mask;
2302
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002303 spin_lock_irqsave(&domain->lock, flags);
2304
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002305 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002306 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002307
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002308 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002309 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002310 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002311 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002312
Joerg Roedel17b124b2011-04-06 18:01:35 +02002313 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002314
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002315 spin_unlock_irqrestore(&domain->lock, flags);
2316
2317 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002318
2319out_free:
2320
2321 free_pages((unsigned long)virt_addr, get_order(size));
2322
2323 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002324}
2325
Joerg Roedel431b2a22008-07-11 17:14:22 +02002326/*
2327 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002328 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002329static void free_coherent(struct device *dev, size_t size,
2330 void *virt_addr, dma_addr_t dma_addr)
2331{
2332 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002333 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002334
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002335 INC_STATS_COUNTER(cnt_free_coherent);
2336
Joerg Roedel94f6d192009-11-24 16:40:02 +01002337 domain = get_domain(dev);
2338 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002339 goto free_mem;
2340
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002341 spin_lock_irqsave(&domain->lock, flags);
2342
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002343 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002344
Joerg Roedel17b124b2011-04-06 18:01:35 +02002345 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002346
2347 spin_unlock_irqrestore(&domain->lock, flags);
2348
2349free_mem:
2350 free_pages((unsigned long)virt_addr, get_order(size));
2351}
2352
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002353/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002354 * This function is called by the DMA layer to find out if we can handle a
2355 * particular device. It is part of the dma_ops.
2356 */
2357static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2358{
Joerg Roedel420aef82009-11-23 16:14:57 +01002359 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002360}
2361
2362/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002363 * The function for pre-allocating protection domains.
2364 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002365 * If the driver core informs the DMA layer if a driver grabs a device
2366 * we don't need to preallocate the protection domains anymore.
2367 * For now we have to.
2368 */
Jaswinder Singh Rajput0e93dd82008-12-29 21:45:22 +05302369static void prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002370{
2371 struct pci_dev *dev = NULL;
2372 struct dma_ops_domain *dma_dom;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002373 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002374
Chris Wrightd18c69d2010-04-02 18:27:55 -07002375 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002376
2377 /* Do we handle this device? */
2378 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002379 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002380
2381 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002382 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002383 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002384
2385 devid = get_device_id(&dev->dev);
2386
Joerg Roedel87a64d52009-11-24 17:26:43 +01002387 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002388 if (!dma_dom)
2389 continue;
2390 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002391 dma_dom->target_dev = devid;
2392
Joerg Roedel15898bb2009-11-24 15:39:42 +01002393 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01002394
Joerg Roedelbd60b732008-09-11 10:24:48 +02002395 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002396 }
2397}
2398
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002399static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002400 .alloc_coherent = alloc_coherent,
2401 .free_coherent = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002402 .map_page = map_page,
2403 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002404 .map_sg = map_sg,
2405 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002406 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002407};
2408
Joerg Roedel27c21272011-05-30 15:56:24 +02002409static unsigned device_dma_ops_init(void)
2410{
2411 struct pci_dev *pdev = NULL;
2412 unsigned unhandled = 0;
2413
2414 for_each_pci_dev(pdev) {
2415 if (!check_device(&pdev->dev)) {
2416 unhandled += 1;
2417 continue;
2418 }
2419
2420 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
2421 }
2422
2423 return unhandled;
2424}
2425
Joerg Roedel431b2a22008-07-11 17:14:22 +02002426/*
2427 * The function which clues the AMD IOMMU driver into dma_ops.
2428 */
Joerg Roedelf5325092010-01-22 17:44:35 +01002429
2430void __init amd_iommu_init_api(void)
2431{
2432 register_iommu(&amd_iommu_ops);
2433}
2434
Joerg Roedel6631ee92008-06-26 21:28:05 +02002435int __init amd_iommu_init_dma_ops(void)
2436{
2437 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02002438 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002439
Joerg Roedel431b2a22008-07-11 17:14:22 +02002440 /*
2441 * first allocate a default protection domain for every IOMMU we
2442 * found in the system. Devices not assigned to any other
2443 * protection domain will be assigned to the default one.
2444 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02002445 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01002446 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002447 if (iommu->default_dom == NULL)
2448 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01002449 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002450 ret = iommu_init_unity_mappings(iommu);
2451 if (ret)
2452 goto free_domains;
2453 }
2454
Joerg Roedel431b2a22008-07-11 17:14:22 +02002455 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01002456 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002457 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01002458 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002459
2460 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002461 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002462
Joerg Roedel431b2a22008-07-11 17:14:22 +02002463 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02002464 unhandled = device_dma_ops_init();
2465 if (unhandled && max_pfn > MAX_DMA32_PFN) {
2466 /* There are unhandled devices - initialize swiotlb for them */
2467 swiotlb = 1;
2468 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02002469
Joerg Roedel7f265082008-12-12 13:50:21 +01002470 amd_iommu_stats_init();
2471
Joerg Roedel6631ee92008-06-26 21:28:05 +02002472 return 0;
2473
2474free_domains:
2475
Joerg Roedel3bd22172009-05-04 15:06:20 +02002476 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002477 if (iommu->default_dom)
2478 dma_ops_domain_free(iommu->default_dom);
2479 }
2480
2481 return ret;
2482}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002483
2484/*****************************************************************************
2485 *
2486 * The following functions belong to the exported interface of AMD IOMMU
2487 *
2488 * This interface allows access to lower level functions of the IOMMU
2489 * like protection domain handling and assignement of devices to domains
2490 * which is not possible with the dma_ops interface.
2491 *
2492 *****************************************************************************/
2493
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002494static void cleanup_domain(struct protection_domain *domain)
2495{
Joerg Roedel492667d2009-11-27 13:25:47 +01002496 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002497 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002498
2499 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2500
Joerg Roedel492667d2009-11-27 13:25:47 +01002501 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
2502 struct device *dev = dev_data->dev;
2503
Chris Wright04e856c2010-02-17 08:51:20 -08002504 __detach_device(dev);
Joerg Roedel492667d2009-11-27 13:25:47 +01002505 atomic_set(&dev_data->bind, 0);
2506 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002507
2508 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2509}
2510
Joerg Roedel26508152009-08-26 16:52:40 +02002511static void protection_domain_free(struct protection_domain *domain)
2512{
2513 if (!domain)
2514 return;
2515
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002516 del_domain_from_list(domain);
2517
Joerg Roedel26508152009-08-26 16:52:40 +02002518 if (domain->id)
2519 domain_id_free(domain->id);
2520
2521 kfree(domain);
2522}
2523
2524static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002525{
2526 struct protection_domain *domain;
2527
2528 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2529 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002530 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002531
2532 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002533 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01002534 domain->id = domain_id_alloc();
2535 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02002536 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002537 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02002538
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002539 add_domain_to_list(domain);
2540
Joerg Roedel26508152009-08-26 16:52:40 +02002541 return domain;
2542
2543out_err:
2544 kfree(domain);
2545
2546 return NULL;
2547}
2548
2549static int amd_iommu_domain_init(struct iommu_domain *dom)
2550{
2551 struct protection_domain *domain;
2552
2553 domain = protection_domain_alloc();
2554 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01002555 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02002556
2557 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002558 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2559 if (!domain->pt_root)
2560 goto out_free;
2561
2562 dom->priv = domain;
2563
2564 return 0;
2565
2566out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02002567 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01002568
2569 return -ENOMEM;
2570}
2571
Joerg Roedel98383fc2008-12-02 18:34:12 +01002572static void amd_iommu_domain_destroy(struct iommu_domain *dom)
2573{
2574 struct protection_domain *domain = dom->priv;
2575
2576 if (!domain)
2577 return;
2578
2579 if (domain->dev_cnt > 0)
2580 cleanup_domain(domain);
2581
2582 BUG_ON(domain->dev_cnt != 0);
2583
2584 free_pagetable(domain);
2585
Joerg Roedel8b408fe2010-03-08 14:20:07 +01002586 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002587
2588 dom->priv = NULL;
2589}
2590
Joerg Roedel684f2882008-12-08 12:07:44 +01002591static void amd_iommu_detach_device(struct iommu_domain *dom,
2592 struct device *dev)
2593{
Joerg Roedel657cbb62009-11-23 15:26:46 +01002594 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002595 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002596 u16 devid;
2597
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002598 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01002599 return;
2600
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002601 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002602
Joerg Roedel657cbb62009-11-23 15:26:46 +01002603 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002604 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002605
2606 iommu = amd_iommu_rlookup_table[devid];
2607 if (!iommu)
2608 return;
2609
Joerg Roedeld8c13082011-04-06 18:51:26 +02002610 device_flush_dte(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002611 iommu_completion_wait(iommu);
2612}
2613
Joerg Roedel01106062008-12-02 19:34:11 +01002614static int amd_iommu_attach_device(struct iommu_domain *dom,
2615 struct device *dev)
2616{
2617 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002618 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01002619 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002620 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002621 u16 devid;
2622
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002623 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01002624 return -EINVAL;
2625
Joerg Roedel657cbb62009-11-23 15:26:46 +01002626 dev_data = dev->archdata.iommu;
2627
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002628 devid = get_device_id(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002629
2630 iommu = amd_iommu_rlookup_table[devid];
2631 if (!iommu)
2632 return -EINVAL;
2633
Joerg Roedel657cbb62009-11-23 15:26:46 +01002634 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002635 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002636
Joerg Roedel15898bb2009-11-24 15:39:42 +01002637 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01002638
2639 iommu_completion_wait(iommu);
2640
Joerg Roedel15898bb2009-11-24 15:39:42 +01002641 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002642}
2643
Joerg Roedel468e2362010-01-21 16:37:36 +01002644static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
2645 phys_addr_t paddr, int gfp_order, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002646{
Joerg Roedel468e2362010-01-21 16:37:36 +01002647 unsigned long page_size = 0x1000UL << gfp_order;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002648 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002649 int prot = 0;
2650 int ret;
2651
2652 if (iommu_prot & IOMMU_READ)
2653 prot |= IOMMU_PROT_IR;
2654 if (iommu_prot & IOMMU_WRITE)
2655 prot |= IOMMU_PROT_IW;
2656
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002657 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f2010-05-11 17:40:57 +02002658 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002659 mutex_unlock(&domain->api_lock);
2660
Joerg Roedel795e74f2010-05-11 17:40:57 +02002661 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002662}
2663
Joerg Roedel468e2362010-01-21 16:37:36 +01002664static int amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2665 int gfp_order)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002666{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002667 struct protection_domain *domain = dom->priv;
Joerg Roedel468e2362010-01-21 16:37:36 +01002668 unsigned long page_size, unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002669
Joerg Roedel468e2362010-01-21 16:37:36 +01002670 page_size = 0x1000UL << gfp_order;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002671
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002672 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01002673 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f2010-05-11 17:40:57 +02002674 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002675
Joerg Roedel17b124b2011-04-06 18:01:35 +02002676 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002677
Joerg Roedel468e2362010-01-21 16:37:36 +01002678 return get_order(unmap_size);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002679}
2680
Joerg Roedel645c4c82008-12-02 20:05:50 +01002681static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2682 unsigned long iova)
2683{
2684 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002685 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002686 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002687 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002688
Joerg Roedel24cd7722010-01-19 17:27:39 +01002689 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002690
Joerg Roedela6d41a42009-09-02 17:08:55 +02002691 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01002692 return 0;
2693
Joerg Roedelf03152b2010-01-21 16:15:24 +01002694 if (PM_PTE_LEVEL(*pte) == 0)
2695 offset_mask = PAGE_SIZE - 1;
2696 else
2697 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
2698
2699 __pte = *pte & PM_ADDR_MASK;
2700 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002701
2702 return paddr;
2703}
2704
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002705static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
2706 unsigned long cap)
2707{
Joerg Roedel80a506b2010-07-27 17:14:24 +02002708 switch (cap) {
2709 case IOMMU_CAP_CACHE_COHERENCY:
2710 return 1;
2711 }
2712
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002713 return 0;
2714}
2715
Joerg Roedel26961ef2008-12-03 17:00:17 +01002716static struct iommu_ops amd_iommu_ops = {
2717 .domain_init = amd_iommu_domain_init,
2718 .domain_destroy = amd_iommu_domain_destroy,
2719 .attach_dev = amd_iommu_attach_device,
2720 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01002721 .map = amd_iommu_map,
2722 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002723 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002724 .domain_has_cap = amd_iommu_domain_has_cap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002725};
2726
Joerg Roedel0feae532009-08-26 15:26:30 +02002727/*****************************************************************************
2728 *
2729 * The next functions do a basic initialization of IOMMU for pass through
2730 * mode
2731 *
2732 * In passthrough mode the IOMMU is initialized and enabled but not used for
2733 * DMA-API translation.
2734 *
2735 *****************************************************************************/
2736
2737int __init amd_iommu_init_passthrough(void)
2738{
Joerg Roedel15898bb2009-11-24 15:39:42 +01002739 struct amd_iommu *iommu;
Joerg Roedel0feae532009-08-26 15:26:30 +02002740 struct pci_dev *dev = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002741 u16 devid;
Joerg Roedel0feae532009-08-26 15:26:30 +02002742
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002743 /* allocate passthrough domain */
Joerg Roedel0feae532009-08-26 15:26:30 +02002744 pt_domain = protection_domain_alloc();
2745 if (!pt_domain)
2746 return -ENOMEM;
2747
2748 pt_domain->mode |= PAGE_MODE_NONE;
2749
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04002750 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002751 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02002752 continue;
2753
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002754 devid = get_device_id(&dev->dev);
2755
Joerg Roedel15898bb2009-11-24 15:39:42 +01002756 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02002757 if (!iommu)
2758 continue;
2759
Joerg Roedel15898bb2009-11-24 15:39:42 +01002760 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02002761 }
2762
2763 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
2764
2765 return 0;
2766}