blob: 71773d0fb7698c34f1796084f63a5a3d897ef112 [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
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010020#include <linux/ratelimit.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020021#include <linux/pci.h>
Joerg Roedelcb41ed82011-04-05 11:00:53 +020022#include <linux/pci-ats.h>
Akinobu Mitaa66022c2009-12-15 16:48:28 -080023#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Joerg Roedel7f265082008-12-12 13:50:21 +010025#include <linux/debugfs.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020026#include <linux/scatterlist.h>
FUJITA Tomonori51491362009-01-05 23:47:25 +090027#include <linux/dma-mapping.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020028#include <linux/iommu-helper.h>
Joerg Roedelc156e342008-12-02 18:13:27 +010029#include <linux/iommu.h>
Joerg Roedel815b33f2011-04-06 17:26:49 +020030#include <linux/delay.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020031#include <linux/amd-iommu.h>
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010032#include <linux/notifier.h>
33#include <linux/export.h>
Joerg Roedel17f5b562011-07-06 17:14:44 +020034#include <asm/msidef.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020035#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090036#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010037#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020038#include <asm/dma.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020039
40#include "amd_iommu_proto.h"
41#include "amd_iommu_types.h"
Joerg Roedelb6c02712008-06-26 21:27:53 +020042
43#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
44
Joerg Roedel815b33f2011-04-06 17:26:49 +020045#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020046
Joerg Roedelb6c02712008-06-26 21:27:53 +020047static DEFINE_RWLOCK(amd_iommu_devtable_lock);
48
Joerg Roedelbd60b732008-09-11 10:24:48 +020049/* A list of preallocated protection domains */
50static LIST_HEAD(iommu_pd_list);
51static DEFINE_SPINLOCK(iommu_pd_list_lock);
52
Joerg Roedel8fa5f802011-06-09 12:24:45 +020053/* List of all available dev_data structures */
54static LIST_HEAD(dev_data_list);
55static DEFINE_SPINLOCK(dev_data_list_lock);
56
Joerg Roedel0feae532009-08-26 15:26:30 +020057/*
58 * Domain for untranslated devices - only allocated
59 * if iommu=pt passed on kernel cmd line.
60 */
61static struct protection_domain *pt_domain;
62
Joerg Roedel26961ef2008-12-03 17:00:17 +010063static struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010064
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010065static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +010066int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010067
Joerg Roedel431b2a22008-07-11 17:14:22 +020068/*
69 * general struct to manage commands send to an IOMMU
70 */
Joerg Roedeld6449532008-07-11 17:14:28 +020071struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +020072 u32 data[4];
73};
74
Joerg Roedel04bfdd82009-09-02 16:00:23 +020075static void update_domain(struct protection_domain *domain);
Joerg Roedel5abcdba2011-12-01 15:49:45 +010076static int __init alloc_passthrough_domain(void);
Chris Wrightc1eee672009-05-21 00:56:58 -070077
Joerg Roedel15898bb2009-11-24 15:39:42 +010078/****************************************************************************
79 *
80 * Helper functions
81 *
82 ****************************************************************************/
83
Joerg Roedelf62dda62011-06-09 12:55:35 +020084static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +020085{
86 struct iommu_dev_data *dev_data;
87 unsigned long flags;
88
89 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
90 if (!dev_data)
91 return NULL;
92
Joerg Roedelf62dda62011-06-09 12:55:35 +020093 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +020094 atomic_set(&dev_data->bind, 0);
95
96 spin_lock_irqsave(&dev_data_list_lock, flags);
97 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
98 spin_unlock_irqrestore(&dev_data_list_lock, flags);
99
100 return dev_data;
101}
102
103static void free_dev_data(struct iommu_dev_data *dev_data)
104{
105 unsigned long flags;
106
107 spin_lock_irqsave(&dev_data_list_lock, flags);
108 list_del(&dev_data->dev_data_list);
109 spin_unlock_irqrestore(&dev_data_list_lock, flags);
110
111 kfree(dev_data);
112}
113
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200114static struct iommu_dev_data *search_dev_data(u16 devid)
115{
116 struct iommu_dev_data *dev_data;
117 unsigned long flags;
118
119 spin_lock_irqsave(&dev_data_list_lock, flags);
120 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
121 if (dev_data->devid == devid)
122 goto out_unlock;
123 }
124
125 dev_data = NULL;
126
127out_unlock:
128 spin_unlock_irqrestore(&dev_data_list_lock, flags);
129
130 return dev_data;
131}
132
133static struct iommu_dev_data *find_dev_data(u16 devid)
134{
135 struct iommu_dev_data *dev_data;
136
137 dev_data = search_dev_data(devid);
138
139 if (dev_data == NULL)
140 dev_data = alloc_dev_data(devid);
141
142 return dev_data;
143}
144
Joerg Roedel15898bb2009-11-24 15:39:42 +0100145static inline u16 get_device_id(struct device *dev)
146{
147 struct pci_dev *pdev = to_pci_dev(dev);
148
149 return calc_devid(pdev->bus->number, pdev->devfn);
150}
151
Joerg Roedel657cbb62009-11-23 15:26:46 +0100152static struct iommu_dev_data *get_dev_data(struct device *dev)
153{
154 return dev->archdata.iommu;
155}
156
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100157static bool pci_iommuv2_capable(struct pci_dev *pdev)
158{
159 static const int caps[] = {
160 PCI_EXT_CAP_ID_ATS,
161 PCI_PRI_CAP,
162 PCI_PASID_CAP,
163 };
164 int i, pos;
165
166 for (i = 0; i < 3; ++i) {
167 pos = pci_find_ext_capability(pdev, caps[i]);
168 if (pos == 0)
169 return false;
170 }
171
172 return true;
173}
174
Joerg Roedel71c70982009-11-24 16:43:06 +0100175/*
176 * In this function the list of preallocated protection domains is traversed to
177 * find the domain for a specific device
178 */
179static struct dma_ops_domain *find_protection_domain(u16 devid)
180{
181 struct dma_ops_domain *entry, *ret = NULL;
182 unsigned long flags;
183 u16 alias = amd_iommu_alias_table[devid];
184
185 if (list_empty(&iommu_pd_list))
186 return NULL;
187
188 spin_lock_irqsave(&iommu_pd_list_lock, flags);
189
190 list_for_each_entry(entry, &iommu_pd_list, list) {
191 if (entry->target_dev == devid ||
192 entry->target_dev == alias) {
193 ret = entry;
194 break;
195 }
196 }
197
198 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
199
200 return ret;
201}
202
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100203/*
204 * This function checks if the driver got a valid device from the caller to
205 * avoid dereferencing invalid pointers.
206 */
207static bool check_device(struct device *dev)
208{
209 u16 devid;
210
211 if (!dev || !dev->dma_mask)
212 return false;
213
214 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100215 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100216 return false;
217
218 devid = get_device_id(dev);
219
220 /* Out of our scope? */
221 if (devid > amd_iommu_last_bdf)
222 return false;
223
224 if (amd_iommu_rlookup_table[devid] == NULL)
225 return false;
226
227 return true;
228}
229
Joerg Roedel657cbb62009-11-23 15:26:46 +0100230static int iommu_init_device(struct device *dev)
231{
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100232 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedel657cbb62009-11-23 15:26:46 +0100233 struct iommu_dev_data *dev_data;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200234 u16 alias;
Joerg Roedel657cbb62009-11-23 15:26:46 +0100235
236 if (dev->archdata.iommu)
237 return 0;
238
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200239 dev_data = find_dev_data(get_device_id(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +0100240 if (!dev_data)
241 return -ENOMEM;
242
Joerg Roedelf62dda62011-06-09 12:55:35 +0200243 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedel2b02b092011-06-09 17:48:39 +0200244 if (alias != dev_data->devid) {
Joerg Roedel71f77582011-06-09 19:03:15 +0200245 struct iommu_dev_data *alias_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100246
Joerg Roedel71f77582011-06-09 19:03:15 +0200247 alias_data = find_dev_data(alias);
248 if (alias_data == NULL) {
249 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
250 dev_name(dev));
Joerg Roedel2b02b092011-06-09 17:48:39 +0200251 free_dev_data(dev_data);
252 return -ENOTSUPP;
253 }
Joerg Roedel71f77582011-06-09 19:03:15 +0200254 dev_data->alias_data = alias_data;
Joerg Roedel26018872011-06-06 16:50:14 +0200255 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100256
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100257 if (pci_iommuv2_capable(pdev)) {
258 struct amd_iommu *iommu;
259
260 iommu = amd_iommu_rlookup_table[dev_data->devid];
261 dev_data->iommu_v2 = iommu->is_iommu_v2;
262 }
263
Joerg Roedel657cbb62009-11-23 15:26:46 +0100264 dev->archdata.iommu = dev_data;
265
Joerg Roedel657cbb62009-11-23 15:26:46 +0100266 return 0;
267}
268
Joerg Roedel26018872011-06-06 16:50:14 +0200269static void iommu_ignore_device(struct device *dev)
270{
271 u16 devid, alias;
272
273 devid = get_device_id(dev);
274 alias = amd_iommu_alias_table[devid];
275
276 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
277 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
278
279 amd_iommu_rlookup_table[devid] = NULL;
280 amd_iommu_rlookup_table[alias] = NULL;
281}
282
Joerg Roedel657cbb62009-11-23 15:26:46 +0100283static void iommu_uninit_device(struct device *dev)
284{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200285 /*
286 * Nothing to do here - we keep dev_data around for unplugged devices
287 * and reuse it when the device is re-plugged - not doing so would
288 * introduce a ton of races.
289 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100290}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100291
292void __init amd_iommu_uninit_devices(void)
293{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200294 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100295 struct pci_dev *pdev = NULL;
296
297 for_each_pci_dev(pdev) {
298
299 if (!check_device(&pdev->dev))
300 continue;
301
302 iommu_uninit_device(&pdev->dev);
303 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200304
305 /* Free all of our dev_data structures */
306 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
307 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100308}
309
310int __init amd_iommu_init_devices(void)
311{
312 struct pci_dev *pdev = NULL;
313 int ret = 0;
314
315 for_each_pci_dev(pdev) {
316
317 if (!check_device(&pdev->dev))
318 continue;
319
320 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200321 if (ret == -ENOTSUPP)
322 iommu_ignore_device(&pdev->dev);
323 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100324 goto out_free;
325 }
326
327 return 0;
328
329out_free:
330
331 amd_iommu_uninit_devices();
332
333 return ret;
334}
Joerg Roedel7f265082008-12-12 13:50:21 +0100335#ifdef CONFIG_AMD_IOMMU_STATS
336
337/*
338 * Initialization code for statistics collection
339 */
340
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100341DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100342DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100343DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100344DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100345DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100346DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100347DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100348DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100349DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100350DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100351DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100352DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100353
Joerg Roedel7f265082008-12-12 13:50:21 +0100354static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100355static struct dentry *de_fflush;
356
357static void amd_iommu_stats_add(struct __iommu_counter *cnt)
358{
359 if (stats_dir == NULL)
360 return;
361
362 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
363 &cnt->value);
364}
365
366static void amd_iommu_stats_init(void)
367{
368 stats_dir = debugfs_create_dir("amd-iommu", NULL);
369 if (stats_dir == NULL)
370 return;
371
Joerg Roedel7f265082008-12-12 13:50:21 +0100372 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
373 (u32 *)&amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100374
375 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100376 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100377 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100378 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100379 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100380 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100381 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100382 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100383 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100384 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100385 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100386 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100387}
388
389#endif
390
Joerg Roedel431b2a22008-07-11 17:14:22 +0200391/****************************************************************************
392 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200393 * Interrupt handling functions
394 *
395 ****************************************************************************/
396
Joerg Roedele3e59872009-09-03 14:02:10 +0200397static void dump_dte_entry(u16 devid)
398{
399 int i;
400
Joerg Roedelee6c2862011-11-09 12:06:03 +0100401 for (i = 0; i < 4; ++i)
402 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200403 amd_iommu_dev_table[devid].data[i]);
404}
405
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200406static void dump_command(unsigned long phys_addr)
407{
408 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
409 int i;
410
411 for (i = 0; i < 4; ++i)
412 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
413}
414
Joerg Roedela345b232009-09-03 15:01:43 +0200415static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200416{
417 u32 *event = __evt;
418 int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
419 int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
420 int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
421 int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
422 u64 address = (u64)(((u64)event[3]) << 32) | event[2];
423
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200424 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200425
426 switch (type) {
427 case EVENT_TYPE_ILL_DEV:
428 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
429 "address=0x%016llx flags=0x%04x]\n",
430 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
431 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200432 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200433 break;
434 case EVENT_TYPE_IO_FAULT:
435 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
436 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
437 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
438 domid, address, flags);
439 break;
440 case EVENT_TYPE_DEV_TAB_ERR:
441 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
442 "address=0x%016llx flags=0x%04x]\n",
443 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
444 address, flags);
445 break;
446 case EVENT_TYPE_PAGE_TAB_ERR:
447 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
448 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
449 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
450 domid, address, flags);
451 break;
452 case EVENT_TYPE_ILL_CMD:
453 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200454 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200455 break;
456 case EVENT_TYPE_CMD_HARD_ERR:
457 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
458 "flags=0x%04x]\n", address, flags);
459 break;
460 case EVENT_TYPE_IOTLB_INV_TO:
461 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
462 "address=0x%016llx]\n",
463 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
464 address);
465 break;
466 case EVENT_TYPE_INV_DEV_REQ:
467 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
468 "address=0x%016llx flags=0x%04x]\n",
469 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
470 address, flags);
471 break;
472 default:
473 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
474 }
475}
476
477static void iommu_poll_events(struct amd_iommu *iommu)
478{
479 u32 head, tail;
480 unsigned long flags;
481
482 spin_lock_irqsave(&iommu->lock, flags);
483
484 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
485 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
486
487 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200488 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200489 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
490 }
491
492 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
493
494 spin_unlock_irqrestore(&iommu->lock, flags);
495}
496
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100497static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u32 head)
498{
499 struct amd_iommu_fault fault;
500 volatile u64 *raw;
501 int i;
502
503 raw = (u64 *)(iommu->ppr_log + head);
504
505 /*
506 * Hardware bug: Interrupt may arrive before the entry is written to
507 * memory. If this happens we need to wait for the entry to arrive.
508 */
509 for (i = 0; i < LOOP_TIMEOUT; ++i) {
510 if (PPR_REQ_TYPE(raw[0]) != 0)
511 break;
512 udelay(1);
513 }
514
515 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
516 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
517 return;
518 }
519
520 fault.address = raw[1];
521 fault.pasid = PPR_PASID(raw[0]);
522 fault.device_id = PPR_DEVID(raw[0]);
523 fault.tag = PPR_TAG(raw[0]);
524 fault.flags = PPR_FLAGS(raw[0]);
525
526 /*
527 * To detect the hardware bug we need to clear the entry
528 * to back to zero.
529 */
530 raw[0] = raw[1] = 0;
531
532 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
533}
534
535static void iommu_poll_ppr_log(struct amd_iommu *iommu)
536{
537 unsigned long flags;
538 u32 head, tail;
539
540 if (iommu->ppr_log == NULL)
541 return;
542
543 spin_lock_irqsave(&iommu->lock, flags);
544
545 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
546 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
547
548 while (head != tail) {
549
550 /* Handle PPR entry */
551 iommu_handle_ppr_entry(iommu, head);
552
553 /* Update and refresh ring-buffer state*/
554 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
555 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
556 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
557 }
558
559 /* enable ppr interrupts again */
560 writel(MMIO_STATUS_PPR_INT_MASK, iommu->mmio_base + MMIO_STATUS_OFFSET);
561
562 spin_unlock_irqrestore(&iommu->lock, flags);
563}
564
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200565irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200566{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200567 struct amd_iommu *iommu;
568
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100569 for_each_iommu(iommu) {
Joerg Roedel90008ee2008-09-09 16:41:05 +0200570 iommu_poll_events(iommu);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100571 iommu_poll_ppr_log(iommu);
572 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200573
574 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200575}
576
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200577irqreturn_t amd_iommu_int_handler(int irq, void *data)
578{
579 return IRQ_WAKE_THREAD;
580}
581
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200582/****************************************************************************
583 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200584 * IOMMU command queuing functions
585 *
586 ****************************************************************************/
587
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200588static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200589{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200590 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200591
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200592 while (*sem == 0 && i < LOOP_TIMEOUT) {
593 udelay(1);
594 i += 1;
595 }
596
597 if (i == LOOP_TIMEOUT) {
598 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
599 return -EIO;
600 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200601
602 return 0;
603}
604
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200605static void copy_cmd_to_buffer(struct amd_iommu *iommu,
606 struct iommu_cmd *cmd,
607 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200608{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200609 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200610
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200611 target = iommu->cmd_buf + tail;
612 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200613
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200614 /* Copy command to buffer */
615 memcpy(target, cmd, sizeof(*cmd));
616
617 /* Tell the IOMMU about it */
618 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
619}
620
Joerg Roedel815b33f2011-04-06 17:26:49 +0200621static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200622{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200623 WARN_ON(address & 0x7ULL);
624
Joerg Roedelded46732011-04-06 10:53:48 +0200625 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200626 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
627 cmd->data[1] = upper_32_bits(__pa(address));
628 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200629 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
630}
631
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200632static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
633{
634 memset(cmd, 0, sizeof(*cmd));
635 cmd->data[0] = devid;
636 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
637}
638
Joerg Roedel11b64022011-04-06 11:49:28 +0200639static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
640 size_t size, u16 domid, int pde)
641{
642 u64 pages;
643 int s;
644
645 pages = iommu_num_pages(address, size, PAGE_SIZE);
646 s = 0;
647
648 if (pages > 1) {
649 /*
650 * If we have to flush more than one page, flush all
651 * TLB entries for this domain
652 */
653 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
654 s = 1;
655 }
656
657 address &= PAGE_MASK;
658
659 memset(cmd, 0, sizeof(*cmd));
660 cmd->data[1] |= domid;
661 cmd->data[2] = lower_32_bits(address);
662 cmd->data[3] = upper_32_bits(address);
663 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
664 if (s) /* size bit - we flush more than one 4kb page */
665 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
666 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
667 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
668}
669
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200670static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
671 u64 address, size_t size)
672{
673 u64 pages;
674 int s;
675
676 pages = iommu_num_pages(address, size, PAGE_SIZE);
677 s = 0;
678
679 if (pages > 1) {
680 /*
681 * If we have to flush more than one page, flush all
682 * TLB entries for this domain
683 */
684 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
685 s = 1;
686 }
687
688 address &= PAGE_MASK;
689
690 memset(cmd, 0, sizeof(*cmd));
691 cmd->data[0] = devid;
692 cmd->data[0] |= (qdep & 0xff) << 24;
693 cmd->data[1] = devid;
694 cmd->data[2] = lower_32_bits(address);
695 cmd->data[3] = upper_32_bits(address);
696 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
697 if (s)
698 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
699}
700
Joerg Roedel22e266c2011-11-21 15:59:08 +0100701static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
702 u64 address, bool size)
703{
704 memset(cmd, 0, sizeof(*cmd));
705
706 address &= ~(0xfffULL);
707
708 cmd->data[0] = pasid & PASID_MASK;
709 cmd->data[1] = domid;
710 cmd->data[2] = lower_32_bits(address);
711 cmd->data[3] = upper_32_bits(address);
712 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
713 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
714 if (size)
715 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
716 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
717}
718
719static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
720 int qdep, u64 address, bool size)
721{
722 memset(cmd, 0, sizeof(*cmd));
723
724 address &= ~(0xfffULL);
725
726 cmd->data[0] = devid;
727 cmd->data[0] |= (pasid & 0xff) << 16;
728 cmd->data[0] |= (qdep & 0xff) << 24;
729 cmd->data[1] = devid;
730 cmd->data[1] |= ((pasid >> 8) & 0xfff) << 16;
731 cmd->data[2] = lower_32_bits(address);
732 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
733 cmd->data[3] = upper_32_bits(address);
734 if (size)
735 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
736 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
737}
738
Joerg Roedelc99afa22011-11-21 18:19:25 +0100739static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
740 int status, int tag, bool gn)
741{
742 memset(cmd, 0, sizeof(*cmd));
743
744 cmd->data[0] = devid;
745 if (gn) {
746 cmd->data[1] = pasid & PASID_MASK;
747 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
748 }
749 cmd->data[3] = tag & 0x1ff;
750 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
751
752 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
753}
754
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200755static void build_inv_all(struct iommu_cmd *cmd)
756{
757 memset(cmd, 0, sizeof(*cmd));
758 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200759}
760
Joerg Roedel431b2a22008-07-11 17:14:22 +0200761/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200762 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200763 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200764 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200765static int iommu_queue_command_sync(struct amd_iommu *iommu,
766 struct iommu_cmd *cmd,
767 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200768{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200769 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200770 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200771
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200772 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100773
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200774again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200775 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200776
777 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
778 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
779 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
780 left = (head - next_tail) % iommu->cmd_buf_size;
781
782 if (left <= 2) {
783 struct iommu_cmd sync_cmd;
784 volatile u64 sem = 0;
785 int ret;
786
787 build_completion_wait(&sync_cmd, (u64)&sem);
788 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
789
790 spin_unlock_irqrestore(&iommu->lock, flags);
791
792 if ((ret = wait_on_sem(&sem)) != 0)
793 return ret;
794
795 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200796 }
797
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200798 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200799
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200800 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200801 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200802
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200803 spin_unlock_irqrestore(&iommu->lock, flags);
804
Joerg Roedel815b33f2011-04-06 17:26:49 +0200805 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100806}
807
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200808static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
809{
810 return iommu_queue_command_sync(iommu, cmd, true);
811}
812
Joerg Roedel8d201962008-12-02 20:34:41 +0100813/*
814 * This function queues a completion wait command into the command
815 * buffer of an IOMMU
816 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100817static int iommu_completion_wait(struct amd_iommu *iommu)
818{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200819 struct iommu_cmd cmd;
820 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200821 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100822
823 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200824 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100825
Joerg Roedel815b33f2011-04-06 17:26:49 +0200826 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100827
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200828 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +0100829 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200830 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100831
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200832 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200833}
834
Joerg Roedeld8c13082011-04-06 18:51:26 +0200835static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200836{
837 struct iommu_cmd cmd;
838
Joerg Roedeld8c13082011-04-06 18:51:26 +0200839 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200840
Joerg Roedeld8c13082011-04-06 18:51:26 +0200841 return iommu_queue_command(iommu, &cmd);
842}
843
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200844static void iommu_flush_dte_all(struct amd_iommu *iommu)
845{
846 u32 devid;
847
848 for (devid = 0; devid <= 0xffff; ++devid)
849 iommu_flush_dte(iommu, devid);
850
851 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200852}
853
854/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200855 * This function uses heavy locking and may disable irqs for some time. But
856 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200857 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200858static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200859{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200860 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200861
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200862 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
863 struct iommu_cmd cmd;
864 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
865 dom_id, 1);
866 iommu_queue_command(iommu, &cmd);
867 }
Joerg Roedel431b2a22008-07-11 17:14:22 +0200868
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200869 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200870}
871
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200872static void iommu_flush_all(struct amd_iommu *iommu)
873{
874 struct iommu_cmd cmd;
875
876 build_inv_all(&cmd);
877
878 iommu_queue_command(iommu, &cmd);
879 iommu_completion_wait(iommu);
880}
881
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200882void iommu_flush_all_caches(struct amd_iommu *iommu)
883{
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200884 if (iommu_feature(iommu, FEATURE_IA)) {
885 iommu_flush_all(iommu);
886 } else {
887 iommu_flush_dte_all(iommu);
888 iommu_flush_tlb_all(iommu);
889 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200890}
891
Joerg Roedel431b2a22008-07-11 17:14:22 +0200892/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200893 * Command send function for flushing on-device TLB
894 */
Joerg Roedel6c542042011-06-09 17:07:31 +0200895static int device_flush_iotlb(struct iommu_dev_data *dev_data,
896 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200897{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200898 struct amd_iommu *iommu;
899 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200900 int qdep;
901
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200902 qdep = dev_data->ats.qdep;
903 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200904
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200905 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200906
907 return iommu_queue_command(iommu, &cmd);
908}
909
910/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200911 * Command send function for invalidating a device table entry
912 */
Joerg Roedel6c542042011-06-09 17:07:31 +0200913static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +0100914{
915 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200916 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100917
Joerg Roedel6c542042011-06-09 17:07:31 +0200918 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +0100919
Joerg Roedelf62dda62011-06-09 12:55:35 +0200920 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200921 if (ret)
922 return ret;
923
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200924 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +0200925 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200926
927 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100928}
929
Joerg Roedel431b2a22008-07-11 17:14:22 +0200930/*
931 * TLB invalidation function which is called from the mapping functions.
932 * It invalidates a single PTE if the range to flush is within a single
933 * page. Otherwise it flushes the whole TLB of the IOMMU.
934 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200935static void __domain_flush_pages(struct protection_domain *domain,
936 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200937{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200938 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +0200939 struct iommu_cmd cmd;
940 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200941
Joerg Roedel11b64022011-04-06 11:49:28 +0200942 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +0200943
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100944 for (i = 0; i < amd_iommus_present; ++i) {
945 if (!domain->dev_iommu[i])
946 continue;
947
948 /*
949 * Devices of this domain are behind this IOMMU
950 * We need a TLB flush
951 */
Joerg Roedel11b64022011-04-06 11:49:28 +0200952 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100953 }
954
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200955 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200956
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200957 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200958 continue;
959
Joerg Roedel6c542042011-06-09 17:07:31 +0200960 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200961 }
962
Joerg Roedel11b64022011-04-06 11:49:28 +0200963 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100964}
965
Joerg Roedel17b124b2011-04-06 18:01:35 +0200966static void domain_flush_pages(struct protection_domain *domain,
967 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100968{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200969 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200970}
Joerg Roedelb6c02712008-06-26 21:27:53 +0200971
Joerg Roedel1c655772008-09-04 18:40:05 +0200972/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200973static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +0200974{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200975 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +0200976}
977
Chris Wright42a49f92009-06-15 15:42:00 +0200978/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200979static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +0200980{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200981 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
982}
983
984static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +0200985{
986 int i;
987
988 for (i = 0; i < amd_iommus_present; ++i) {
989 if (!domain->dev_iommu[i])
990 continue;
991
992 /*
993 * Devices of this domain are behind this IOMMU
994 * We need to wait for completion of all commands.
995 */
996 iommu_completion_wait(amd_iommus[i]);
997 }
998}
999
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001000
Joerg Roedel43f49602008-12-02 21:01:12 +01001001/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001002 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001003 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001004static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001005{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001006 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001007
1008 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001009 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001010}
1011
Joerg Roedel431b2a22008-07-11 17:14:22 +02001012/****************************************************************************
1013 *
1014 * The functions below are used the create the page table mappings for
1015 * unity mapped regions.
1016 *
1017 ****************************************************************************/
1018
1019/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001020 * This function is used to add another level to an IO page table. Adding
1021 * another level increases the size of the address space by 9 bits to a size up
1022 * to 64 bits.
1023 */
1024static bool increase_address_space(struct protection_domain *domain,
1025 gfp_t gfp)
1026{
1027 u64 *pte;
1028
1029 if (domain->mode == PAGE_MODE_6_LEVEL)
1030 /* address space already 64 bit large */
1031 return false;
1032
1033 pte = (void *)get_zeroed_page(gfp);
1034 if (!pte)
1035 return false;
1036
1037 *pte = PM_LEVEL_PDE(domain->mode,
1038 virt_to_phys(domain->pt_root));
1039 domain->pt_root = pte;
1040 domain->mode += 1;
1041 domain->updated = true;
1042
1043 return true;
1044}
1045
1046static u64 *alloc_pte(struct protection_domain *domain,
1047 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001048 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001049 u64 **pte_page,
1050 gfp_t gfp)
1051{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001052 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001053 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001054
1055 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001056
1057 while (address > PM_LEVEL_SIZE(domain->mode))
1058 increase_address_space(domain, gfp);
1059
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001060 level = domain->mode - 1;
1061 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1062 address = PAGE_SIZE_ALIGN(address, page_size);
1063 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001064
1065 while (level > end_lvl) {
1066 if (!IOMMU_PTE_PRESENT(*pte)) {
1067 page = (u64 *)get_zeroed_page(gfp);
1068 if (!page)
1069 return NULL;
1070 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1071 }
1072
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001073 /* No level skipping support yet */
1074 if (PM_PTE_LEVEL(*pte) != level)
1075 return NULL;
1076
Joerg Roedel308973d2009-11-24 17:43:32 +01001077 level -= 1;
1078
1079 pte = IOMMU_PTE_PAGE(*pte);
1080
1081 if (pte_page && level == end_lvl)
1082 *pte_page = pte;
1083
1084 pte = &pte[PM_LEVEL_INDEX(level, address)];
1085 }
1086
1087 return pte;
1088}
1089
1090/*
1091 * This function checks if there is a PTE for a given dma address. If
1092 * there is one, it returns the pointer to it.
1093 */
Joerg Roedel24cd7722010-01-19 17:27:39 +01001094static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +01001095{
1096 int level;
1097 u64 *pte;
1098
Joerg Roedel24cd7722010-01-19 17:27:39 +01001099 if (address > PM_LEVEL_SIZE(domain->mode))
1100 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001101
Joerg Roedel24cd7722010-01-19 17:27:39 +01001102 level = domain->mode - 1;
1103 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1104
1105 while (level > 0) {
1106
1107 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001108 if (!IOMMU_PTE_PRESENT(*pte))
1109 return NULL;
1110
Joerg Roedel24cd7722010-01-19 17:27:39 +01001111 /* Large PTE */
1112 if (PM_PTE_LEVEL(*pte) == 0x07) {
1113 unsigned long pte_mask, __pte;
1114
1115 /*
1116 * If we have a series of large PTEs, make
1117 * sure to return a pointer to the first one.
1118 */
1119 pte_mask = PTE_PAGE_SIZE(*pte);
1120 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1121 __pte = ((unsigned long)pte) & pte_mask;
1122
1123 return (u64 *)__pte;
1124 }
1125
1126 /* No level skipping support yet */
1127 if (PM_PTE_LEVEL(*pte) != level)
1128 return NULL;
1129
Joerg Roedel308973d2009-11-24 17:43:32 +01001130 level -= 1;
1131
Joerg Roedel24cd7722010-01-19 17:27:39 +01001132 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +01001133 pte = IOMMU_PTE_PAGE(*pte);
1134 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +01001135 }
1136
1137 return pte;
1138}
1139
1140/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001141 * Generic mapping functions. It maps a physical address into a DMA
1142 * address space. It allocates the page table pages if necessary.
1143 * In the future it can be extended to a generic mapping function
1144 * supporting all features of AMD IOMMU page tables like level skipping
1145 * and full 64 bit address spaces.
1146 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001147static int iommu_map_page(struct protection_domain *dom,
1148 unsigned long bus_addr,
1149 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001150 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001151 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001152{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001153 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001154 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001155
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001156 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001157 return -EINVAL;
1158
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001159 bus_addr = PAGE_ALIGN(bus_addr);
1160 phys_addr = PAGE_ALIGN(phys_addr);
1161 count = PAGE_SIZE_PTE_COUNT(page_size);
1162 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001163
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001164 for (i = 0; i < count; ++i)
1165 if (IOMMU_PTE_PRESENT(pte[i]))
1166 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001167
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001168 if (page_size > PAGE_SIZE) {
1169 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1170 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1171 } else
1172 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1173
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001174 if (prot & IOMMU_PROT_IR)
1175 __pte |= IOMMU_PTE_IR;
1176 if (prot & IOMMU_PROT_IW)
1177 __pte |= IOMMU_PTE_IW;
1178
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001179 for (i = 0; i < count; ++i)
1180 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001181
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001182 update_domain(dom);
1183
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001184 return 0;
1185}
1186
Joerg Roedel24cd7722010-01-19 17:27:39 +01001187static unsigned long iommu_unmap_page(struct protection_domain *dom,
1188 unsigned long bus_addr,
1189 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001190{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001191 unsigned long long unmap_size, unmapped;
1192 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001193
Joerg Roedel24cd7722010-01-19 17:27:39 +01001194 BUG_ON(!is_power_of_2(page_size));
1195
1196 unmapped = 0;
1197
1198 while (unmapped < page_size) {
1199
1200 pte = fetch_pte(dom, bus_addr);
1201
1202 if (!pte) {
1203 /*
1204 * No PTE for this address
1205 * move forward in 4kb steps
1206 */
1207 unmap_size = PAGE_SIZE;
1208 } else if (PM_PTE_LEVEL(*pte) == 0) {
1209 /* 4kb PTE found for this address */
1210 unmap_size = PAGE_SIZE;
1211 *pte = 0ULL;
1212 } else {
1213 int count, i;
1214
1215 /* Large PTE found which maps this address */
1216 unmap_size = PTE_PAGE_SIZE(*pte);
1217 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1218 for (i = 0; i < count; i++)
1219 pte[i] = 0ULL;
1220 }
1221
1222 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1223 unmapped += unmap_size;
1224 }
1225
1226 BUG_ON(!is_power_of_2(unmapped));
1227
1228 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001229}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001230
Joerg Roedel431b2a22008-07-11 17:14:22 +02001231/*
1232 * This function checks if a specific unity mapping entry is needed for
1233 * this specific IOMMU.
1234 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001235static int iommu_for_unity_map(struct amd_iommu *iommu,
1236 struct unity_map_entry *entry)
1237{
1238 u16 bdf, i;
1239
1240 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1241 bdf = amd_iommu_alias_table[i];
1242 if (amd_iommu_rlookup_table[bdf] == iommu)
1243 return 1;
1244 }
1245
1246 return 0;
1247}
1248
Joerg Roedel431b2a22008-07-11 17:14:22 +02001249/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001250 * This function actually applies the mapping to the page table of the
1251 * dma_ops domain.
1252 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001253static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1254 struct unity_map_entry *e)
1255{
1256 u64 addr;
1257 int ret;
1258
1259 for (addr = e->address_start; addr < e->address_end;
1260 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001261 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001262 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001263 if (ret)
1264 return ret;
1265 /*
1266 * if unity mapping is in aperture range mark the page
1267 * as allocated in the aperture
1268 */
1269 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001270 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001271 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001272 }
1273
1274 return 0;
1275}
1276
Joerg Roedel431b2a22008-07-11 17:14:22 +02001277/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001278 * Init the unity mappings for a specific IOMMU in the system
1279 *
1280 * Basically iterates over all unity mapping entries and applies them to
1281 * the default domain DMA of that IOMMU if necessary.
1282 */
1283static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1284{
1285 struct unity_map_entry *entry;
1286 int ret;
1287
1288 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1289 if (!iommu_for_unity_map(iommu, entry))
1290 continue;
1291 ret = dma_ops_unity_map(iommu->default_dom, entry);
1292 if (ret)
1293 return ret;
1294 }
1295
1296 return 0;
1297}
1298
1299/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001300 * Inits the unity mappings required for a specific device
1301 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001302static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1303 u16 devid)
1304{
1305 struct unity_map_entry *e;
1306 int ret;
1307
1308 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1309 if (!(devid >= e->devid_start && devid <= e->devid_end))
1310 continue;
1311 ret = dma_ops_unity_map(dma_dom, e);
1312 if (ret)
1313 return ret;
1314 }
1315
1316 return 0;
1317}
1318
Joerg Roedel431b2a22008-07-11 17:14:22 +02001319/****************************************************************************
1320 *
1321 * The next functions belong to the address allocator for the dma_ops
1322 * interface functions. They work like the allocators in the other IOMMU
1323 * drivers. Its basically a bitmap which marks the allocated pages in
1324 * the aperture. Maybe it could be enhanced in the future to a more
1325 * efficient allocator.
1326 *
1327 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001328
Joerg Roedel431b2a22008-07-11 17:14:22 +02001329/*
Joerg Roedel384de722009-05-15 12:30:05 +02001330 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001331 *
1332 * called with domain->lock held
1333 */
Joerg Roedel384de722009-05-15 12:30:05 +02001334
Joerg Roedel9cabe892009-05-18 16:38:55 +02001335/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001336 * Used to reserve address ranges in the aperture (e.g. for exclusion
1337 * ranges.
1338 */
1339static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1340 unsigned long start_page,
1341 unsigned int pages)
1342{
1343 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1344
1345 if (start_page + pages > last_page)
1346 pages = last_page - start_page;
1347
1348 for (i = start_page; i < start_page + pages; ++i) {
1349 int index = i / APERTURE_RANGE_PAGES;
1350 int page = i % APERTURE_RANGE_PAGES;
1351 __set_bit(page, dom->aperture[index]->bitmap);
1352 }
1353}
1354
1355/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001356 * This function is used to add a new aperture range to an existing
1357 * aperture in case of dma_ops domain allocation or address allocation
1358 * failure.
1359 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001360static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001361 bool populate, gfp_t gfp)
1362{
1363 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001364 struct amd_iommu *iommu;
Joerg Roedel17f5b562011-07-06 17:14:44 +02001365 unsigned long i, old_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001366
Joerg Roedelf5e97052009-05-22 12:31:53 +02001367#ifdef CONFIG_IOMMU_STRESS
1368 populate = false;
1369#endif
1370
Joerg Roedel9cabe892009-05-18 16:38:55 +02001371 if (index >= APERTURE_MAX_RANGES)
1372 return -ENOMEM;
1373
1374 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1375 if (!dma_dom->aperture[index])
1376 return -ENOMEM;
1377
1378 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1379 if (!dma_dom->aperture[index]->bitmap)
1380 goto out_free;
1381
1382 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1383
1384 if (populate) {
1385 unsigned long address = dma_dom->aperture_size;
1386 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1387 u64 *pte, *pte_page;
1388
1389 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001390 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001391 &pte_page, gfp);
1392 if (!pte)
1393 goto out_free;
1394
1395 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1396
1397 address += APERTURE_RANGE_SIZE / 64;
1398 }
1399 }
1400
Joerg Roedel17f5b562011-07-06 17:14:44 +02001401 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001402 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1403
Joerg Roedel17f5b562011-07-06 17:14:44 +02001404 /* Reserve address range used for MSI messages */
1405 if (old_size < MSI_ADDR_BASE_LO &&
1406 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1407 unsigned long spage;
1408 int pages;
1409
1410 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1411 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1412
1413 dma_ops_reserve_addresses(dma_dom, spage, pages);
1414 }
1415
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001416 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001417 for_each_iommu(iommu) {
1418 if (iommu->exclusion_start &&
1419 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1420 && iommu->exclusion_start < dma_dom->aperture_size) {
1421 unsigned long startpage;
1422 int pages = iommu_num_pages(iommu->exclusion_start,
1423 iommu->exclusion_length,
1424 PAGE_SIZE);
1425 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1426 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1427 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001428 }
1429
1430 /*
1431 * Check for areas already mapped as present in the new aperture
1432 * range and mark those pages as reserved in the allocator. Such
1433 * mappings may already exist as a result of requested unity
1434 * mappings for devices.
1435 */
1436 for (i = dma_dom->aperture[index]->offset;
1437 i < dma_dom->aperture_size;
1438 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001439 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001440 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1441 continue;
1442
Joerg Roedelfcd08612011-10-11 17:41:32 +02001443 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001444 }
1445
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001446 update_domain(&dma_dom->domain);
1447
Joerg Roedel9cabe892009-05-18 16:38:55 +02001448 return 0;
1449
1450out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001451 update_domain(&dma_dom->domain);
1452
Joerg Roedel9cabe892009-05-18 16:38:55 +02001453 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1454
1455 kfree(dma_dom->aperture[index]);
1456 dma_dom->aperture[index] = NULL;
1457
1458 return -ENOMEM;
1459}
1460
Joerg Roedel384de722009-05-15 12:30:05 +02001461static unsigned long dma_ops_area_alloc(struct device *dev,
1462 struct dma_ops_domain *dom,
1463 unsigned int pages,
1464 unsigned long align_mask,
1465 u64 dma_mask,
1466 unsigned long start)
1467{
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001468 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001469 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1470 int i = start >> APERTURE_RANGE_SHIFT;
1471 unsigned long boundary_size;
1472 unsigned long address = -1;
1473 unsigned long limit;
1474
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001475 next_bit >>= PAGE_SHIFT;
1476
Joerg Roedel384de722009-05-15 12:30:05 +02001477 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1478 PAGE_SIZE) >> PAGE_SHIFT;
1479
1480 for (;i < max_index; ++i) {
1481 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1482
1483 if (dom->aperture[i]->offset >= dma_mask)
1484 break;
1485
1486 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1487 dma_mask >> PAGE_SHIFT);
1488
1489 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1490 limit, next_bit, pages, 0,
1491 boundary_size, align_mask);
1492 if (address != -1) {
1493 address = dom->aperture[i]->offset +
1494 (address << PAGE_SHIFT);
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001495 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001496 break;
1497 }
1498
1499 next_bit = 0;
1500 }
1501
1502 return address;
1503}
1504
Joerg Roedeld3086442008-06-26 21:27:57 +02001505static unsigned long dma_ops_alloc_addresses(struct device *dev,
1506 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001507 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001508 unsigned long align_mask,
1509 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001510{
Joerg Roedeld3086442008-06-26 21:27:57 +02001511 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001512
Joerg Roedelfe16f082009-05-22 12:27:53 +02001513#ifdef CONFIG_IOMMU_STRESS
1514 dom->next_address = 0;
1515 dom->need_flush = true;
1516#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001517
Joerg Roedel384de722009-05-15 12:30:05 +02001518 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001519 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001520
Joerg Roedel1c655772008-09-04 18:40:05 +02001521 if (address == -1) {
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001522 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001523 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1524 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001525 dom->need_flush = true;
1526 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001527
Joerg Roedel384de722009-05-15 12:30:05 +02001528 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001529 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001530
1531 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1532
1533 return address;
1534}
1535
Joerg Roedel431b2a22008-07-11 17:14:22 +02001536/*
1537 * The address free function.
1538 *
1539 * called with domain->lock held
1540 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001541static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1542 unsigned long address,
1543 unsigned int pages)
1544{
Joerg Roedel384de722009-05-15 12:30:05 +02001545 unsigned i = address >> APERTURE_RANGE_SHIFT;
1546 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001547
Joerg Roedel384de722009-05-15 12:30:05 +02001548 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1549
Joerg Roedel47bccd62009-05-22 12:40:54 +02001550#ifdef CONFIG_IOMMU_STRESS
1551 if (i < 4)
1552 return;
1553#endif
1554
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001555 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001556 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001557
1558 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001559
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001560 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001561
Joerg Roedeld3086442008-06-26 21:27:57 +02001562}
1563
Joerg Roedel431b2a22008-07-11 17:14:22 +02001564/****************************************************************************
1565 *
1566 * The next functions belong to the domain allocation. A domain is
1567 * allocated for every IOMMU as the default domain. If device isolation
1568 * is enabled, every device get its own domain. The most important thing
1569 * about domains is the page table mapping the DMA address space they
1570 * contain.
1571 *
1572 ****************************************************************************/
1573
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001574/*
1575 * This function adds a protection domain to the global protection domain list
1576 */
1577static void add_domain_to_list(struct protection_domain *domain)
1578{
1579 unsigned long flags;
1580
1581 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1582 list_add(&domain->list, &amd_iommu_pd_list);
1583 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1584}
1585
1586/*
1587 * This function removes a protection domain to the global
1588 * protection domain list
1589 */
1590static void del_domain_from_list(struct protection_domain *domain)
1591{
1592 unsigned long flags;
1593
1594 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1595 list_del(&domain->list);
1596 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1597}
1598
Joerg Roedelec487d12008-06-26 21:27:58 +02001599static u16 domain_id_alloc(void)
1600{
1601 unsigned long flags;
1602 int id;
1603
1604 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1605 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1606 BUG_ON(id == 0);
1607 if (id > 0 && id < MAX_DOMAIN_ID)
1608 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1609 else
1610 id = 0;
1611 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1612
1613 return id;
1614}
1615
Joerg Roedela2acfb72008-12-02 18:28:53 +01001616static void domain_id_free(int id)
1617{
1618 unsigned long flags;
1619
1620 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1621 if (id > 0 && id < MAX_DOMAIN_ID)
1622 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1623 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1624}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001625
Joerg Roedel86db2e52008-12-02 18:20:21 +01001626static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001627{
1628 int i, j;
1629 u64 *p1, *p2, *p3;
1630
Joerg Roedel86db2e52008-12-02 18:20:21 +01001631 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001632
1633 if (!p1)
1634 return;
1635
1636 for (i = 0; i < 512; ++i) {
1637 if (!IOMMU_PTE_PRESENT(p1[i]))
1638 continue;
1639
1640 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001641 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001642 if (!IOMMU_PTE_PRESENT(p2[j]))
1643 continue;
1644 p3 = IOMMU_PTE_PAGE(p2[j]);
1645 free_page((unsigned long)p3);
1646 }
1647
1648 free_page((unsigned long)p2);
1649 }
1650
1651 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001652
1653 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001654}
1655
Joerg Roedelb16137b2011-11-21 16:50:23 +01001656static void free_gcr3_tbl_level1(u64 *tbl)
1657{
1658 u64 *ptr;
1659 int i;
1660
1661 for (i = 0; i < 512; ++i) {
1662 if (!(tbl[i] & GCR3_VALID))
1663 continue;
1664
1665 ptr = __va(tbl[i] & PAGE_MASK);
1666
1667 free_page((unsigned long)ptr);
1668 }
1669}
1670
1671static void free_gcr3_tbl_level2(u64 *tbl)
1672{
1673 u64 *ptr;
1674 int i;
1675
1676 for (i = 0; i < 512; ++i) {
1677 if (!(tbl[i] & GCR3_VALID))
1678 continue;
1679
1680 ptr = __va(tbl[i] & PAGE_MASK);
1681
1682 free_gcr3_tbl_level1(ptr);
1683 }
1684}
1685
Joerg Roedel52815b72011-11-17 17:24:28 +01001686static void free_gcr3_table(struct protection_domain *domain)
1687{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001688 if (domain->glx == 2)
1689 free_gcr3_tbl_level2(domain->gcr3_tbl);
1690 else if (domain->glx == 1)
1691 free_gcr3_tbl_level1(domain->gcr3_tbl);
1692 else if (domain->glx != 0)
1693 BUG();
1694
Joerg Roedel52815b72011-11-17 17:24:28 +01001695 free_page((unsigned long)domain->gcr3_tbl);
1696}
1697
Joerg Roedel431b2a22008-07-11 17:14:22 +02001698/*
1699 * Free a domain, only used if something went wrong in the
1700 * allocation path and we need to free an already allocated page table
1701 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001702static void dma_ops_domain_free(struct dma_ops_domain *dom)
1703{
Joerg Roedel384de722009-05-15 12:30:05 +02001704 int i;
1705
Joerg Roedelec487d12008-06-26 21:27:58 +02001706 if (!dom)
1707 return;
1708
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001709 del_domain_from_list(&dom->domain);
1710
Joerg Roedel86db2e52008-12-02 18:20:21 +01001711 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001712
Joerg Roedel384de722009-05-15 12:30:05 +02001713 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1714 if (!dom->aperture[i])
1715 continue;
1716 free_page((unsigned long)dom->aperture[i]->bitmap);
1717 kfree(dom->aperture[i]);
1718 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001719
1720 kfree(dom);
1721}
1722
Joerg Roedel431b2a22008-07-11 17:14:22 +02001723/*
1724 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001725 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001726 * structures required for the dma_ops interface
1727 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001728static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001729{
1730 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001731
1732 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1733 if (!dma_dom)
1734 return NULL;
1735
1736 spin_lock_init(&dma_dom->domain.lock);
1737
1738 dma_dom->domain.id = domain_id_alloc();
1739 if (dma_dom->domain.id == 0)
1740 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001741 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001742 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001743 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001744 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001745 dma_dom->domain.priv = dma_dom;
1746 if (!dma_dom->domain.pt_root)
1747 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001748
Joerg Roedel1c655772008-09-04 18:40:05 +02001749 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001750 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001751
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001752 add_domain_to_list(&dma_dom->domain);
1753
Joerg Roedel576175c2009-11-23 19:08:46 +01001754 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001755 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001756
Joerg Roedel431b2a22008-07-11 17:14:22 +02001757 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001758 * mark the first page as allocated so we never return 0 as
1759 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001760 */
Joerg Roedel384de722009-05-15 12:30:05 +02001761 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001762 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001763
Joerg Roedelec487d12008-06-26 21:27:58 +02001764
1765 return dma_dom;
1766
1767free_dma_dom:
1768 dma_ops_domain_free(dma_dom);
1769
1770 return NULL;
1771}
1772
Joerg Roedel431b2a22008-07-11 17:14:22 +02001773/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001774 * little helper function to check whether a given protection domain is a
1775 * dma_ops domain
1776 */
1777static bool dma_ops_domain(struct protection_domain *domain)
1778{
1779 return domain->flags & PD_DMA_OPS_MASK;
1780}
1781
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001782static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001783{
Joerg Roedel132bd682011-11-17 14:18:46 +01001784 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001785 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001786
Joerg Roedel132bd682011-11-17 14:18:46 +01001787 if (domain->mode != PAGE_MODE_NONE)
1788 pte_root = virt_to_phys(domain->pt_root);
1789
Joerg Roedel38ddf412008-09-11 10:38:32 +02001790 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1791 << DEV_ENTRY_MODE_SHIFT;
1792 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001793
Joerg Roedelee6c2862011-11-09 12:06:03 +01001794 flags = amd_iommu_dev_table[devid].data[1];
1795
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001796 if (ats)
1797 flags |= DTE_FLAG_IOTLB;
1798
Joerg Roedel52815b72011-11-17 17:24:28 +01001799 if (domain->flags & PD_IOMMUV2_MASK) {
1800 u64 gcr3 = __pa(domain->gcr3_tbl);
1801 u64 glx = domain->glx;
1802 u64 tmp;
1803
1804 pte_root |= DTE_FLAG_GV;
1805 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1806
1807 /* First mask out possible old values for GCR3 table */
1808 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1809 flags &= ~tmp;
1810
1811 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1812 flags &= ~tmp;
1813
1814 /* Encode GCR3 table into DTE */
1815 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1816 pte_root |= tmp;
1817
1818 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1819 flags |= tmp;
1820
1821 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1822 flags |= tmp;
1823 }
1824
Joerg Roedelee6c2862011-11-09 12:06:03 +01001825 flags &= ~(0xffffUL);
1826 flags |= domain->id;
1827
1828 amd_iommu_dev_table[devid].data[1] = flags;
1829 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001830}
1831
Joerg Roedel15898bb2009-11-24 15:39:42 +01001832static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001833{
Joerg Roedel355bf552008-12-08 12:02:41 +01001834 /* remove entry from the device table seen by the hardware */
1835 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1836 amd_iommu_dev_table[devid].data[1] = 0;
Joerg Roedel355bf552008-12-08 12:02:41 +01001837
Joerg Roedelc5cca142009-10-09 18:31:20 +02001838 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001839}
1840
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001841static void do_attach(struct iommu_dev_data *dev_data,
1842 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001843{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001844 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001845 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001846
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001847 iommu = amd_iommu_rlookup_table[dev_data->devid];
1848 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001849
1850 /* Update data structures */
1851 dev_data->domain = domain;
1852 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001853 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001854
1855 /* Do reference counting */
1856 domain->dev_iommu[iommu->index] += 1;
1857 domain->dev_cnt += 1;
1858
1859 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001860 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001861}
1862
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001863static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001864{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001865 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001866
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001867 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02001868
Joerg Roedelc4596112009-11-20 14:57:32 +01001869 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001870 dev_data->domain->dev_iommu[iommu->index] -= 1;
1871 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001872
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001873 /* Update data structures */
1874 dev_data->domain = NULL;
1875 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001876 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001877
1878 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001879 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001880}
1881
1882/*
1883 * If a device is not yet associated with a domain, this function does
1884 * assigns it visible for the hardware
1885 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001886static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01001887 struct protection_domain *domain)
1888{
Julia Lawall84fe6c12010-05-27 12:31:51 +02001889 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001890
Joerg Roedel15898bb2009-11-24 15:39:42 +01001891 /* lock domain */
1892 spin_lock(&domain->lock);
1893
Joerg Roedel71f77582011-06-09 19:03:15 +02001894 if (dev_data->alias_data != NULL) {
1895 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001896
Joerg Roedel2b02b092011-06-09 17:48:39 +02001897 /* Some sanity checks */
1898 ret = -EBUSY;
1899 if (alias_data->domain != NULL &&
1900 alias_data->domain != domain)
1901 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001902
Joerg Roedel2b02b092011-06-09 17:48:39 +02001903 if (dev_data->domain != NULL &&
1904 dev_data->domain != domain)
1905 goto out_unlock;
1906
1907 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001908 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001909 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01001910
1911 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001912 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01001913
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001914 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001915 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001916
Joerg Roedel24100052009-11-25 15:59:57 +01001917 atomic_inc(&dev_data->bind);
1918
Julia Lawall84fe6c12010-05-27 12:31:51 +02001919 ret = 0;
1920
1921out_unlock:
1922
Joerg Roedel355bf552008-12-08 12:02:41 +01001923 /* ready */
1924 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001925
Julia Lawall84fe6c12010-05-27 12:31:51 +02001926 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001927}
1928
Joerg Roedel52815b72011-11-17 17:24:28 +01001929
1930static void pdev_iommuv2_disable(struct pci_dev *pdev)
1931{
1932 pci_disable_ats(pdev);
1933 pci_disable_pri(pdev);
1934 pci_disable_pasid(pdev);
1935}
1936
1937static int pdev_iommuv2_enable(struct pci_dev *pdev)
1938{
1939 int ret;
1940
1941 /* Only allow access to user-accessible pages */
1942 ret = pci_enable_pasid(pdev, 0);
1943 if (ret)
1944 goto out_err;
1945
1946 /* First reset the PRI state of the device */
1947 ret = pci_reset_pri(pdev);
1948 if (ret)
1949 goto out_err;
1950
1951 /* FIXME: Hardcode number of outstanding requests for now */
1952 ret = pci_enable_pri(pdev, 32);
1953 if (ret)
1954 goto out_err;
1955
1956 ret = pci_enable_ats(pdev, PAGE_SHIFT);
1957 if (ret)
1958 goto out_err;
1959
1960 return 0;
1961
1962out_err:
1963 pci_disable_pri(pdev);
1964 pci_disable_pasid(pdev);
1965
1966 return ret;
1967}
1968
Joerg Roedelc99afa22011-11-21 18:19:25 +01001969/* FIXME: Move this to PCI code */
1970#define PCI_PRI_TLP_OFF (1 << 2)
1971
1972bool pci_pri_tlp_required(struct pci_dev *pdev)
1973{
1974 u16 control;
1975 int pos;
1976
1977 pos = pci_find_ext_capability(pdev, PCI_PRI_CAP);
1978 if (!pos)
1979 return false;
1980
1981 pci_read_config_word(pdev, pos + PCI_PRI_CONTROL_OFF, &control);
1982
1983 return (control & PCI_PRI_TLP_OFF) ? true : false;
1984}
1985
Joerg Roedel15898bb2009-11-24 15:39:42 +01001986/*
1987 * If a device is not yet associated with a domain, this function does
1988 * assigns it visible for the hardware
1989 */
1990static int attach_device(struct device *dev,
1991 struct protection_domain *domain)
1992{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001993 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001994 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001995 unsigned long flags;
1996 int ret;
1997
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001998 dev_data = get_dev_data(dev);
1999
Joerg Roedel52815b72011-11-17 17:24:28 +01002000 if (domain->flags & PD_IOMMUV2_MASK) {
2001 if (!dev_data->iommu_v2 || !dev_data->passthrough)
2002 return -EINVAL;
2003
2004 if (pdev_iommuv2_enable(pdev) != 0)
2005 return -EINVAL;
2006
2007 dev_data->ats.enabled = true;
2008 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002009 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002010 } else if (amd_iommu_iotlb_sup &&
2011 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002012 dev_data->ats.enabled = true;
2013 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2014 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002015
Joerg Roedel15898bb2009-11-24 15:39:42 +01002016 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002017 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002018 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2019
2020 /*
2021 * We might boot into a crash-kernel here. The crashed kernel
2022 * left the caches in the IOMMU dirty. So we have to flush
2023 * here to evict all dirty stuff.
2024 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002025 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002026
2027 return ret;
2028}
2029
2030/*
2031 * Removes a device from a protection domain (unlocked)
2032 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002033static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002034{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002035 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002036 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002037
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002038 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002039
Joerg Roedel2ca76272010-01-22 16:45:31 +01002040 domain = dev_data->domain;
2041
2042 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01002043
Joerg Roedel71f77582011-06-09 19:03:15 +02002044 if (dev_data->alias_data != NULL) {
2045 struct iommu_dev_data *alias_data = dev_data->alias_data;
2046
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002047 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002048 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01002049 }
2050
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002051 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002052 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002053
Joerg Roedel2ca76272010-01-22 16:45:31 +01002054 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002055
Joerg Roedel21129f72009-09-01 11:59:42 +02002056 /*
2057 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002058 * passthrough domain if it is detached from any other domain.
2059 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02002060 */
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002061 if (dev_data->passthrough &&
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002062 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002063 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01002064}
2065
2066/*
2067 * Removes a device from a protection domain (with devtable_lock held)
2068 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002069static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002070{
Joerg Roedel52815b72011-11-17 17:24:28 +01002071 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002072 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002073 unsigned long flags;
2074
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002075 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002076 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002077
Joerg Roedel355bf552008-12-08 12:02:41 +01002078 /* lock device table */
2079 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002080 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002081 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002082
Joerg Roedel52815b72011-11-17 17:24:28 +01002083 if (domain->flags & PD_IOMMUV2_MASK)
2084 pdev_iommuv2_disable(to_pci_dev(dev));
2085 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002086 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002087
2088 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002089}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002090
Joerg Roedel15898bb2009-11-24 15:39:42 +01002091/*
2092 * Find out the protection domain structure for a given PCI device. This
2093 * will give us the pointer to the page table root for example.
2094 */
2095static struct protection_domain *domain_for_device(struct device *dev)
2096{
Joerg Roedel71f77582011-06-09 19:03:15 +02002097 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002098 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002099 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002100
Joerg Roedel657cbb62009-11-23 15:26:46 +01002101 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002102
Joerg Roedel2b02b092011-06-09 17:48:39 +02002103 if (dev_data->domain)
2104 return dev_data->domain;
2105
Joerg Roedel71f77582011-06-09 19:03:15 +02002106 if (dev_data->alias_data != NULL) {
2107 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002108
2109 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
2110 if (alias_data->domain != NULL) {
2111 __attach_device(dev_data, alias_data->domain);
2112 dom = alias_data->domain;
2113 }
2114 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002115 }
2116
Joerg Roedel15898bb2009-11-24 15:39:42 +01002117 return dom;
2118}
2119
Joerg Roedele275a2a2008-12-10 18:27:25 +01002120static int device_change_notifier(struct notifier_block *nb,
2121 unsigned long action, void *data)
2122{
Joerg Roedele275a2a2008-12-10 18:27:25 +01002123 struct dma_ops_domain *dma_domain;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002124 struct protection_domain *domain;
2125 struct iommu_dev_data *dev_data;
2126 struct device *dev = data;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002127 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002128 unsigned long flags;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002129 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002130
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002131 if (!check_device(dev))
2132 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002133
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002134 devid = get_device_id(dev);
2135 iommu = amd_iommu_rlookup_table[devid];
2136 dev_data = get_dev_data(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002137
2138 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07002139 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002140
2141 domain = domain_for_device(dev);
2142
Joerg Roedele275a2a2008-12-10 18:27:25 +01002143 if (!domain)
2144 goto out;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002145 if (dev_data->passthrough)
Joerg Roedela1ca3312009-09-01 12:22:22 +02002146 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002147 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002148 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002149 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002150
2151 iommu_init_device(dev);
2152
2153 domain = domain_for_device(dev);
2154
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002155 /* allocate a protection domain if a device is added */
2156 dma_domain = find_protection_domain(devid);
2157 if (dma_domain)
2158 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01002159 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002160 if (!dma_domain)
2161 goto out;
2162 dma_domain->target_dev = devid;
2163
2164 spin_lock_irqsave(&iommu_pd_list_lock, flags);
2165 list_add_tail(&dma_domain->list, &iommu_pd_list);
2166 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
2167
2168 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002169 case BUS_NOTIFY_DEL_DEVICE:
2170
2171 iommu_uninit_device(dev);
2172
Joerg Roedele275a2a2008-12-10 18:27:25 +01002173 default:
2174 goto out;
2175 }
2176
Joerg Roedele275a2a2008-12-10 18:27:25 +01002177 iommu_completion_wait(iommu);
2178
2179out:
2180 return 0;
2181}
2182
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302183static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01002184 .notifier_call = device_change_notifier,
2185};
Joerg Roedel355bf552008-12-08 12:02:41 +01002186
Joerg Roedel8638c492009-12-10 11:12:25 +01002187void amd_iommu_init_notifier(void)
2188{
2189 bus_register_notifier(&pci_bus_type, &device_nb);
2190}
2191
Joerg Roedel431b2a22008-07-11 17:14:22 +02002192/*****************************************************************************
2193 *
2194 * The next functions belong to the dma_ops mapping/unmapping code.
2195 *
2196 *****************************************************************************/
2197
2198/*
2199 * In the dma_ops path we only have the struct device. This function
2200 * finds the corresponding IOMMU, the protection domain and the
2201 * requestor id for a given device.
2202 * If the device is not yet associated with a domain this is also done
2203 * in this function.
2204 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002205static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002206{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002207 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002208 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002209 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002210
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002211 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002212 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002213
Joerg Roedel94f6d192009-11-24 16:40:02 +01002214 domain = domain_for_device(dev);
2215 if (domain != NULL && !dma_ops_domain(domain))
2216 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002217
Joerg Roedel94f6d192009-11-24 16:40:02 +01002218 if (domain != NULL)
2219 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002220
Joerg Roedel15898bb2009-11-24 15:39:42 +01002221 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002222 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002223 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01002224 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2225 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002226 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01002227 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01002228
Joerg Roedel94f6d192009-11-24 16:40:02 +01002229 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002230}
2231
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002232static void update_device_table(struct protection_domain *domain)
2233{
Joerg Roedel492667d2009-11-27 13:25:47 +01002234 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002235
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002236 list_for_each_entry(dev_data, &domain->dev_list, list)
2237 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002238}
2239
2240static void update_domain(struct protection_domain *domain)
2241{
2242 if (!domain->updated)
2243 return;
2244
2245 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002246
2247 domain_flush_devices(domain);
2248 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002249
2250 domain->updated = false;
2251}
2252
Joerg Roedel431b2a22008-07-11 17:14:22 +02002253/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002254 * This function fetches the PTE for a given address in the aperture
2255 */
2256static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2257 unsigned long address)
2258{
Joerg Roedel384de722009-05-15 12:30:05 +02002259 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002260 u64 *pte, *pte_page;
2261
Joerg Roedel384de722009-05-15 12:30:05 +02002262 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2263 if (!aperture)
2264 return NULL;
2265
2266 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002267 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002268 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002269 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002270 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2271 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002272 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002273
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002274 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002275
2276 return pte;
2277}
2278
2279/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002280 * This is the generic map function. It maps one 4kb page at paddr to
2281 * the given address in the DMA address space for the domain.
2282 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002283static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002284 unsigned long address,
2285 phys_addr_t paddr,
2286 int direction)
2287{
2288 u64 *pte, __pte;
2289
2290 WARN_ON(address > dom->aperture_size);
2291
2292 paddr &= PAGE_MASK;
2293
Joerg Roedel8bda3092009-05-12 12:02:46 +02002294 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002295 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002296 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002297
2298 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2299
2300 if (direction == DMA_TO_DEVICE)
2301 __pte |= IOMMU_PTE_IR;
2302 else if (direction == DMA_FROM_DEVICE)
2303 __pte |= IOMMU_PTE_IW;
2304 else if (direction == DMA_BIDIRECTIONAL)
2305 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2306
2307 WARN_ON(*pte);
2308
2309 *pte = __pte;
2310
2311 return (dma_addr_t)address;
2312}
2313
Joerg Roedel431b2a22008-07-11 17:14:22 +02002314/*
2315 * The generic unmapping function for on page in the DMA address space.
2316 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002317static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002318 unsigned long address)
2319{
Joerg Roedel384de722009-05-15 12:30:05 +02002320 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002321 u64 *pte;
2322
2323 if (address >= dom->aperture_size)
2324 return;
2325
Joerg Roedel384de722009-05-15 12:30:05 +02002326 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2327 if (!aperture)
2328 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002329
Joerg Roedel384de722009-05-15 12:30:05 +02002330 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2331 if (!pte)
2332 return;
2333
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002334 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002335
2336 WARN_ON(!*pte);
2337
2338 *pte = 0ULL;
2339}
2340
Joerg Roedel431b2a22008-07-11 17:14:22 +02002341/*
2342 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002343 * contiguous memory region into DMA address space. It is used by all
2344 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002345 * Must be called with the domain lock held.
2346 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002347static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002348 struct dma_ops_domain *dma_dom,
2349 phys_addr_t paddr,
2350 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002351 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002352 bool align,
2353 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002354{
2355 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002356 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002357 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002358 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002359 int i;
2360
Joerg Roedele3c449f2008-10-15 22:02:11 -07002361 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002362 paddr &= PAGE_MASK;
2363
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002364 INC_STATS_COUNTER(total_map_requests);
2365
Joerg Roedelc1858972008-12-12 15:42:39 +01002366 if (pages > 1)
2367 INC_STATS_COUNTER(cross_page);
2368
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002369 if (align)
2370 align_mask = (1UL << get_order(size)) - 1;
2371
Joerg Roedel11b83882009-05-19 10:23:15 +02002372retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002373 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2374 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002375 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002376 /*
2377 * setting next_address here will let the address
2378 * allocator only scan the new allocated range in the
2379 * first run. This is a small optimization.
2380 */
2381 dma_dom->next_address = dma_dom->aperture_size;
2382
Joerg Roedel576175c2009-11-23 19:08:46 +01002383 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002384 goto out;
2385
2386 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002387 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002388 * allocation again
2389 */
2390 goto retry;
2391 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002392
2393 start = address;
2394 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002395 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002396 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002397 goto out_unmap;
2398
Joerg Roedelcb76c322008-06-26 21:28:00 +02002399 paddr += PAGE_SIZE;
2400 start += PAGE_SIZE;
2401 }
2402 address += offset;
2403
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002404 ADD_STATS_COUNTER(alloced_io_mem, size);
2405
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002406 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002407 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002408 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002409 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002410 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002411
Joerg Roedelcb76c322008-06-26 21:28:00 +02002412out:
2413 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002414
2415out_unmap:
2416
2417 for (--i; i >= 0; --i) {
2418 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002419 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002420 }
2421
2422 dma_ops_free_addresses(dma_dom, address, pages);
2423
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002424 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002425}
2426
Joerg Roedel431b2a22008-07-11 17:14:22 +02002427/*
2428 * Does the reverse of the __map_single function. Must be called with
2429 * the domain lock held too
2430 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002431static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002432 dma_addr_t dma_addr,
2433 size_t size,
2434 int dir)
2435{
Joerg Roedel04e04632010-09-23 16:12:48 +02002436 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002437 dma_addr_t i, start;
2438 unsigned int pages;
2439
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002440 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002441 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002442 return;
2443
Joerg Roedel04e04632010-09-23 16:12:48 +02002444 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002445 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002446 dma_addr &= PAGE_MASK;
2447 start = dma_addr;
2448
2449 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002450 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002451 start += PAGE_SIZE;
2452 }
2453
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002454 SUB_STATS_COUNTER(alloced_io_mem, size);
2455
Joerg Roedelcb76c322008-06-26 21:28:00 +02002456 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002457
Joerg Roedel80be3082008-11-06 14:59:05 +01002458 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002459 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002460 dma_dom->need_flush = false;
2461 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002462}
2463
Joerg Roedel431b2a22008-07-11 17:14:22 +02002464/*
2465 * The exported map_single function for dma_ops.
2466 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002467static dma_addr_t map_page(struct device *dev, struct page *page,
2468 unsigned long offset, size_t size,
2469 enum dma_data_direction dir,
2470 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002471{
2472 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002473 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002474 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002475 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002476 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002477
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002478 INC_STATS_COUNTER(cnt_map_single);
2479
Joerg Roedel94f6d192009-11-24 16:40:02 +01002480 domain = get_domain(dev);
2481 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002482 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002483 else if (IS_ERR(domain))
2484 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002485
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002486 dma_mask = *dev->dma_mask;
2487
Joerg Roedel4da70b92008-06-26 21:28:01 +02002488 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002489
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002490 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002491 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002492 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002493 goto out;
2494
Joerg Roedel17b124b2011-04-06 18:01:35 +02002495 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002496
2497out:
2498 spin_unlock_irqrestore(&domain->lock, flags);
2499
2500 return addr;
2501}
2502
Joerg Roedel431b2a22008-07-11 17:14:22 +02002503/*
2504 * The exported unmap_single function for dma_ops.
2505 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002506static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2507 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002508{
2509 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002510 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002511
Joerg Roedel146a6912008-12-12 15:07:12 +01002512 INC_STATS_COUNTER(cnt_unmap_single);
2513
Joerg Roedel94f6d192009-11-24 16:40:02 +01002514 domain = get_domain(dev);
2515 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002516 return;
2517
Joerg Roedel4da70b92008-06-26 21:28:01 +02002518 spin_lock_irqsave(&domain->lock, flags);
2519
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002520 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002521
Joerg Roedel17b124b2011-04-06 18:01:35 +02002522 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002523
2524 spin_unlock_irqrestore(&domain->lock, flags);
2525}
2526
Joerg Roedel431b2a22008-07-11 17:14:22 +02002527/*
2528 * This is a special map_sg function which is used if we should map a
2529 * device which is not handled by an AMD IOMMU in the system.
2530 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002531static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2532 int nelems, int dir)
2533{
2534 struct scatterlist *s;
2535 int i;
2536
2537 for_each_sg(sglist, s, nelems, i) {
2538 s->dma_address = (dma_addr_t)sg_phys(s);
2539 s->dma_length = s->length;
2540 }
2541
2542 return nelems;
2543}
2544
Joerg Roedel431b2a22008-07-11 17:14:22 +02002545/*
2546 * The exported map_sg function for dma_ops (handles scatter-gather
2547 * lists).
2548 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002549static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002550 int nelems, enum dma_data_direction dir,
2551 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002552{
2553 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002554 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002555 int i;
2556 struct scatterlist *s;
2557 phys_addr_t paddr;
2558 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002559 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002560
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002561 INC_STATS_COUNTER(cnt_map_sg);
2562
Joerg Roedel94f6d192009-11-24 16:40:02 +01002563 domain = get_domain(dev);
2564 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002565 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002566 else if (IS_ERR(domain))
2567 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002568
Joerg Roedel832a90c2008-09-18 15:54:23 +02002569 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002570
Joerg Roedel65b050a2008-06-26 21:28:02 +02002571 spin_lock_irqsave(&domain->lock, flags);
2572
2573 for_each_sg(sglist, s, nelems, i) {
2574 paddr = sg_phys(s);
2575
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002576 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002577 paddr, s->length, dir, false,
2578 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002579
2580 if (s->dma_address) {
2581 s->dma_length = s->length;
2582 mapped_elems++;
2583 } else
2584 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002585 }
2586
Joerg Roedel17b124b2011-04-06 18:01:35 +02002587 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002588
2589out:
2590 spin_unlock_irqrestore(&domain->lock, flags);
2591
2592 return mapped_elems;
2593unmap:
2594 for_each_sg(sglist, s, mapped_elems, i) {
2595 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002596 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002597 s->dma_length, dir);
2598 s->dma_address = s->dma_length = 0;
2599 }
2600
2601 mapped_elems = 0;
2602
2603 goto out;
2604}
2605
Joerg Roedel431b2a22008-07-11 17:14:22 +02002606/*
2607 * The exported map_sg function for dma_ops (handles scatter-gather
2608 * lists).
2609 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002610static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002611 int nelems, enum dma_data_direction dir,
2612 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002613{
2614 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002615 struct protection_domain *domain;
2616 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002617 int i;
2618
Joerg Roedel55877a62008-12-12 15:12:14 +01002619 INC_STATS_COUNTER(cnt_unmap_sg);
2620
Joerg Roedel94f6d192009-11-24 16:40:02 +01002621 domain = get_domain(dev);
2622 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002623 return;
2624
Joerg Roedel65b050a2008-06-26 21:28:02 +02002625 spin_lock_irqsave(&domain->lock, flags);
2626
2627 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002628 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002629 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002630 s->dma_address = s->dma_length = 0;
2631 }
2632
Joerg Roedel17b124b2011-04-06 18:01:35 +02002633 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002634
2635 spin_unlock_irqrestore(&domain->lock, flags);
2636}
2637
Joerg Roedel431b2a22008-07-11 17:14:22 +02002638/*
2639 * The exported alloc_coherent function for dma_ops.
2640 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002641static void *alloc_coherent(struct device *dev, size_t size,
2642 dma_addr_t *dma_addr, gfp_t flag)
2643{
2644 unsigned long flags;
2645 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002646 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002647 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002648 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002649
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002650 INC_STATS_COUNTER(cnt_alloc_coherent);
2651
Joerg Roedel94f6d192009-11-24 16:40:02 +01002652 domain = get_domain(dev);
2653 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002654 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2655 *dma_addr = __pa(virt_addr);
2656 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002657 } else if (IS_ERR(domain))
2658 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002659
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002660 dma_mask = dev->coherent_dma_mask;
2661 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2662 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002663
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002664 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2665 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302666 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002667
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002668 paddr = virt_to_phys(virt_addr);
2669
Joerg Roedel832a90c2008-09-18 15:54:23 +02002670 if (!dma_mask)
2671 dma_mask = *dev->dma_mask;
2672
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002673 spin_lock_irqsave(&domain->lock, flags);
2674
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002675 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002676 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002677
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002678 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002679 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002680 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002681 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002682
Joerg Roedel17b124b2011-04-06 18:01:35 +02002683 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002684
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002685 spin_unlock_irqrestore(&domain->lock, flags);
2686
2687 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002688
2689out_free:
2690
2691 free_pages((unsigned long)virt_addr, get_order(size));
2692
2693 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002694}
2695
Joerg Roedel431b2a22008-07-11 17:14:22 +02002696/*
2697 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002698 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002699static void free_coherent(struct device *dev, size_t size,
2700 void *virt_addr, dma_addr_t dma_addr)
2701{
2702 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002703 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002704
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002705 INC_STATS_COUNTER(cnt_free_coherent);
2706
Joerg Roedel94f6d192009-11-24 16:40:02 +01002707 domain = get_domain(dev);
2708 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002709 goto free_mem;
2710
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002711 spin_lock_irqsave(&domain->lock, flags);
2712
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002713 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002714
Joerg Roedel17b124b2011-04-06 18:01:35 +02002715 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002716
2717 spin_unlock_irqrestore(&domain->lock, flags);
2718
2719free_mem:
2720 free_pages((unsigned long)virt_addr, get_order(size));
2721}
2722
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002723/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002724 * This function is called by the DMA layer to find out if we can handle a
2725 * particular device. It is part of the dma_ops.
2726 */
2727static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2728{
Joerg Roedel420aef82009-11-23 16:14:57 +01002729 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002730}
2731
2732/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002733 * The function for pre-allocating protection domains.
2734 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002735 * If the driver core informs the DMA layer if a driver grabs a device
2736 * we don't need to preallocate the protection domains anymore.
2737 * For now we have to.
2738 */
Jaswinder Singh Rajput0e93dd82008-12-29 21:45:22 +05302739static void prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002740{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002741 struct iommu_dev_data *dev_data;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002742 struct dma_ops_domain *dma_dom;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002743 struct pci_dev *dev = NULL;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002744 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002745
Chris Wrightd18c69d2010-04-02 18:27:55 -07002746 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002747
2748 /* Do we handle this device? */
2749 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002750 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002751
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002752 dev_data = get_dev_data(&dev->dev);
2753 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
2754 /* Make sure passthrough domain is allocated */
2755 alloc_passthrough_domain();
2756 dev_data->passthrough = true;
2757 attach_device(&dev->dev, pt_domain);
2758 pr_info("AMD-Vi: Using passthough domain for device %s\n",
2759 dev_name(&dev->dev));
2760 }
2761
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002762 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002763 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002764 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002765
2766 devid = get_device_id(&dev->dev);
2767
Joerg Roedel87a64d52009-11-24 17:26:43 +01002768 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002769 if (!dma_dom)
2770 continue;
2771 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002772 dma_dom->target_dev = devid;
2773
Joerg Roedel15898bb2009-11-24 15:39:42 +01002774 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01002775
Joerg Roedelbd60b732008-09-11 10:24:48 +02002776 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002777 }
2778}
2779
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002780static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002781 .alloc_coherent = alloc_coherent,
2782 .free_coherent = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002783 .map_page = map_page,
2784 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002785 .map_sg = map_sg,
2786 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002787 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002788};
2789
Joerg Roedel27c21272011-05-30 15:56:24 +02002790static unsigned device_dma_ops_init(void)
2791{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002792 struct iommu_dev_data *dev_data;
Joerg Roedel27c21272011-05-30 15:56:24 +02002793 struct pci_dev *pdev = NULL;
2794 unsigned unhandled = 0;
2795
2796 for_each_pci_dev(pdev) {
2797 if (!check_device(&pdev->dev)) {
2798 unhandled += 1;
2799 continue;
2800 }
2801
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002802 dev_data = get_dev_data(&pdev->dev);
2803
2804 if (!dev_data->passthrough)
2805 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
2806 else
2807 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
Joerg Roedel27c21272011-05-30 15:56:24 +02002808 }
2809
2810 return unhandled;
2811}
2812
Joerg Roedel431b2a22008-07-11 17:14:22 +02002813/*
2814 * The function which clues the AMD IOMMU driver into dma_ops.
2815 */
Joerg Roedelf5325092010-01-22 17:44:35 +01002816
2817void __init amd_iommu_init_api(void)
2818{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02002819 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01002820}
2821
Joerg Roedel6631ee92008-06-26 21:28:05 +02002822int __init amd_iommu_init_dma_ops(void)
2823{
2824 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02002825 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002826
Joerg Roedel431b2a22008-07-11 17:14:22 +02002827 /*
2828 * first allocate a default protection domain for every IOMMU we
2829 * found in the system. Devices not assigned to any other
2830 * protection domain will be assigned to the default one.
2831 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02002832 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01002833 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002834 if (iommu->default_dom == NULL)
2835 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01002836 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002837 ret = iommu_init_unity_mappings(iommu);
2838 if (ret)
2839 goto free_domains;
2840 }
2841
Joerg Roedel431b2a22008-07-11 17:14:22 +02002842 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01002843 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002844 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01002845 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002846
2847 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002848 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002849
Joerg Roedel431b2a22008-07-11 17:14:22 +02002850 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02002851 unhandled = device_dma_ops_init();
2852 if (unhandled && max_pfn > MAX_DMA32_PFN) {
2853 /* There are unhandled devices - initialize swiotlb for them */
2854 swiotlb = 1;
2855 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02002856
Joerg Roedel7f265082008-12-12 13:50:21 +01002857 amd_iommu_stats_init();
2858
Joerg Roedel6631ee92008-06-26 21:28:05 +02002859 return 0;
2860
2861free_domains:
2862
Joerg Roedel3bd22172009-05-04 15:06:20 +02002863 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002864 if (iommu->default_dom)
2865 dma_ops_domain_free(iommu->default_dom);
2866 }
2867
2868 return ret;
2869}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002870
2871/*****************************************************************************
2872 *
2873 * The following functions belong to the exported interface of AMD IOMMU
2874 *
2875 * This interface allows access to lower level functions of the IOMMU
2876 * like protection domain handling and assignement of devices to domains
2877 * which is not possible with the dma_ops interface.
2878 *
2879 *****************************************************************************/
2880
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002881static void cleanup_domain(struct protection_domain *domain)
2882{
Joerg Roedel492667d2009-11-27 13:25:47 +01002883 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002884 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002885
2886 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2887
Joerg Roedel492667d2009-11-27 13:25:47 +01002888 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002889 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01002890 atomic_set(&dev_data->bind, 0);
2891 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002892
2893 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2894}
2895
Joerg Roedel26508152009-08-26 16:52:40 +02002896static void protection_domain_free(struct protection_domain *domain)
2897{
2898 if (!domain)
2899 return;
2900
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002901 del_domain_from_list(domain);
2902
Joerg Roedel26508152009-08-26 16:52:40 +02002903 if (domain->id)
2904 domain_id_free(domain->id);
2905
2906 kfree(domain);
2907}
2908
2909static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002910{
2911 struct protection_domain *domain;
2912
2913 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2914 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002915 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002916
2917 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002918 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01002919 domain->id = domain_id_alloc();
2920 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02002921 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002922 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02002923
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002924 add_domain_to_list(domain);
2925
Joerg Roedel26508152009-08-26 16:52:40 +02002926 return domain;
2927
2928out_err:
2929 kfree(domain);
2930
2931 return NULL;
2932}
2933
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002934static int __init alloc_passthrough_domain(void)
2935{
2936 if (pt_domain != NULL)
2937 return 0;
2938
2939 /* allocate passthrough domain */
2940 pt_domain = protection_domain_alloc();
2941 if (!pt_domain)
2942 return -ENOMEM;
2943
2944 pt_domain->mode = PAGE_MODE_NONE;
2945
2946 return 0;
2947}
Joerg Roedel26508152009-08-26 16:52:40 +02002948static int amd_iommu_domain_init(struct iommu_domain *dom)
2949{
2950 struct protection_domain *domain;
2951
2952 domain = protection_domain_alloc();
2953 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01002954 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02002955
2956 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002957 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2958 if (!domain->pt_root)
2959 goto out_free;
2960
Joerg Roedelf3572db2011-11-23 12:36:25 +01002961 domain->iommu_domain = dom;
2962
Joerg Roedelc156e342008-12-02 18:13:27 +01002963 dom->priv = domain;
2964
2965 return 0;
2966
2967out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02002968 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01002969
2970 return -ENOMEM;
2971}
2972
Joerg Roedel98383fc2008-12-02 18:34:12 +01002973static void amd_iommu_domain_destroy(struct iommu_domain *dom)
2974{
2975 struct protection_domain *domain = dom->priv;
2976
2977 if (!domain)
2978 return;
2979
2980 if (domain->dev_cnt > 0)
2981 cleanup_domain(domain);
2982
2983 BUG_ON(domain->dev_cnt != 0);
2984
Joerg Roedel132bd682011-11-17 14:18:46 +01002985 if (domain->mode != PAGE_MODE_NONE)
2986 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002987
Joerg Roedel52815b72011-11-17 17:24:28 +01002988 if (domain->flags & PD_IOMMUV2_MASK)
2989 free_gcr3_table(domain);
2990
Joerg Roedel8b408fe2010-03-08 14:20:07 +01002991 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002992
2993 dom->priv = NULL;
2994}
2995
Joerg Roedel684f2882008-12-08 12:07:44 +01002996static void amd_iommu_detach_device(struct iommu_domain *dom,
2997 struct device *dev)
2998{
Joerg Roedel657cbb62009-11-23 15:26:46 +01002999 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003000 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003001 u16 devid;
3002
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003003 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003004 return;
3005
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003006 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003007
Joerg Roedel657cbb62009-11-23 15:26:46 +01003008 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003009 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003010
3011 iommu = amd_iommu_rlookup_table[devid];
3012 if (!iommu)
3013 return;
3014
Joerg Roedel684f2882008-12-08 12:07:44 +01003015 iommu_completion_wait(iommu);
3016}
3017
Joerg Roedel01106062008-12-02 19:34:11 +01003018static int amd_iommu_attach_device(struct iommu_domain *dom,
3019 struct device *dev)
3020{
3021 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01003022 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003023 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003024 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003025
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003026 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003027 return -EINVAL;
3028
Joerg Roedel657cbb62009-11-23 15:26:46 +01003029 dev_data = dev->archdata.iommu;
3030
Joerg Roedelf62dda62011-06-09 12:55:35 +02003031 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003032 if (!iommu)
3033 return -EINVAL;
3034
Joerg Roedel657cbb62009-11-23 15:26:46 +01003035 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003036 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003037
Joerg Roedel15898bb2009-11-24 15:39:42 +01003038 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003039
3040 iommu_completion_wait(iommu);
3041
Joerg Roedel15898bb2009-11-24 15:39:42 +01003042 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003043}
3044
Joerg Roedel468e2362010-01-21 16:37:36 +01003045static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
3046 phys_addr_t paddr, int gfp_order, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003047{
Joerg Roedel468e2362010-01-21 16:37:36 +01003048 unsigned long page_size = 0x1000UL << gfp_order;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003049 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003050 int prot = 0;
3051 int ret;
3052
Joerg Roedel132bd682011-11-17 14:18:46 +01003053 if (domain->mode == PAGE_MODE_NONE)
3054 return -EINVAL;
3055
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003056 if (iommu_prot & IOMMU_READ)
3057 prot |= IOMMU_PROT_IR;
3058 if (iommu_prot & IOMMU_WRITE)
3059 prot |= IOMMU_PROT_IW;
3060
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003061 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003062 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003063 mutex_unlock(&domain->api_lock);
3064
Joerg Roedel795e74f72010-05-11 17:40:57 +02003065 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003066}
3067
Joerg Roedel468e2362010-01-21 16:37:36 +01003068static int amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3069 int gfp_order)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003070{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003071 struct protection_domain *domain = dom->priv;
Joerg Roedel468e2362010-01-21 16:37:36 +01003072 unsigned long page_size, unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003073
Joerg Roedel132bd682011-11-17 14:18:46 +01003074 if (domain->mode == PAGE_MODE_NONE)
3075 return -EINVAL;
3076
Joerg Roedel468e2362010-01-21 16:37:36 +01003077 page_size = 0x1000UL << gfp_order;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003078
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003079 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003080 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003081 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003082
Joerg Roedel17b124b2011-04-06 18:01:35 +02003083 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003084
Joerg Roedel468e2362010-01-21 16:37:36 +01003085 return get_order(unmap_size);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003086}
3087
Joerg Roedel645c4c82008-12-02 20:05:50 +01003088static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
3089 unsigned long iova)
3090{
3091 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003092 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003093 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003094 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003095
Joerg Roedel132bd682011-11-17 14:18:46 +01003096 if (domain->mode == PAGE_MODE_NONE)
3097 return iova;
3098
Joerg Roedel24cd7722010-01-19 17:27:39 +01003099 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003100
Joerg Roedela6d41a42009-09-02 17:08:55 +02003101 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003102 return 0;
3103
Joerg Roedelf03152b2010-01-21 16:15:24 +01003104 if (PM_PTE_LEVEL(*pte) == 0)
3105 offset_mask = PAGE_SIZE - 1;
3106 else
3107 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
3108
3109 __pte = *pte & PM_ADDR_MASK;
3110 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003111
3112 return paddr;
3113}
3114
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003115static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
3116 unsigned long cap)
3117{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003118 switch (cap) {
3119 case IOMMU_CAP_CACHE_COHERENCY:
3120 return 1;
3121 }
3122
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003123 return 0;
3124}
3125
Joerg Roedel26961ef2008-12-03 17:00:17 +01003126static struct iommu_ops amd_iommu_ops = {
3127 .domain_init = amd_iommu_domain_init,
3128 .domain_destroy = amd_iommu_domain_destroy,
3129 .attach_dev = amd_iommu_attach_device,
3130 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003131 .map = amd_iommu_map,
3132 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003133 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003134 .domain_has_cap = amd_iommu_domain_has_cap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003135};
3136
Joerg Roedel0feae532009-08-26 15:26:30 +02003137/*****************************************************************************
3138 *
3139 * The next functions do a basic initialization of IOMMU for pass through
3140 * mode
3141 *
3142 * In passthrough mode the IOMMU is initialized and enabled but not used for
3143 * DMA-API translation.
3144 *
3145 *****************************************************************************/
3146
3147int __init amd_iommu_init_passthrough(void)
3148{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003149 struct iommu_dev_data *dev_data;
Joerg Roedel0feae532009-08-26 15:26:30 +02003150 struct pci_dev *dev = NULL;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003151 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003152 u16 devid;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003153 int ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003154
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003155 ret = alloc_passthrough_domain();
3156 if (ret)
3157 return ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003158
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04003159 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003160 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02003161 continue;
3162
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003163 dev_data = get_dev_data(&dev->dev);
3164 dev_data->passthrough = true;
3165
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003166 devid = get_device_id(&dev->dev);
3167
Joerg Roedel15898bb2009-11-24 15:39:42 +01003168 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02003169 if (!iommu)
3170 continue;
3171
Joerg Roedel15898bb2009-11-24 15:39:42 +01003172 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02003173 }
3174
3175 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
3176
3177 return 0;
3178}
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003179
3180/* IOMMUv2 specific functions */
3181int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3182{
3183 return atomic_notifier_chain_register(&ppr_notifier, nb);
3184}
3185EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3186
3187int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3188{
3189 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3190}
3191EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003192
3193void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3194{
3195 struct protection_domain *domain = dom->priv;
3196 unsigned long flags;
3197
3198 spin_lock_irqsave(&domain->lock, flags);
3199
3200 /* Update data structure */
3201 domain->mode = PAGE_MODE_NONE;
3202 domain->updated = true;
3203
3204 /* Make changes visible to IOMMUs */
3205 update_domain(domain);
3206
3207 /* Page-table is not visible to IOMMU anymore, so free it */
3208 free_pagetable(domain);
3209
3210 spin_unlock_irqrestore(&domain->lock, flags);
3211}
3212EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003213
3214int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3215{
3216 struct protection_domain *domain = dom->priv;
3217 unsigned long flags;
3218 int levels, ret;
3219
3220 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3221 return -EINVAL;
3222
3223 /* Number of GCR3 table levels required */
3224 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3225 levels += 1;
3226
3227 if (levels > amd_iommu_max_glx_val)
3228 return -EINVAL;
3229
3230 spin_lock_irqsave(&domain->lock, flags);
3231
3232 /*
3233 * Save us all sanity checks whether devices already in the
3234 * domain support IOMMUv2. Just force that the domain has no
3235 * devices attached when it is switched into IOMMUv2 mode.
3236 */
3237 ret = -EBUSY;
3238 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3239 goto out;
3240
3241 ret = -ENOMEM;
3242 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3243 if (domain->gcr3_tbl == NULL)
3244 goto out;
3245
3246 domain->glx = levels;
3247 domain->flags |= PD_IOMMUV2_MASK;
3248 domain->updated = true;
3249
3250 update_domain(domain);
3251
3252 ret = 0;
3253
3254out:
3255 spin_unlock_irqrestore(&domain->lock, flags);
3256
3257 return ret;
3258}
3259EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003260
3261static int __flush_pasid(struct protection_domain *domain, int pasid,
3262 u64 address, bool size)
3263{
3264 struct iommu_dev_data *dev_data;
3265 struct iommu_cmd cmd;
3266 int i, ret;
3267
3268 if (!(domain->flags & PD_IOMMUV2_MASK))
3269 return -EINVAL;
3270
3271 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3272
3273 /*
3274 * IOMMU TLB needs to be flushed before Device TLB to
3275 * prevent device TLB refill from IOMMU TLB
3276 */
3277 for (i = 0; i < amd_iommus_present; ++i) {
3278 if (domain->dev_iommu[i] == 0)
3279 continue;
3280
3281 ret = iommu_queue_command(amd_iommus[i], &cmd);
3282 if (ret != 0)
3283 goto out;
3284 }
3285
3286 /* Wait until IOMMU TLB flushes are complete */
3287 domain_flush_complete(domain);
3288
3289 /* Now flush device TLBs */
3290 list_for_each_entry(dev_data, &domain->dev_list, list) {
3291 struct amd_iommu *iommu;
3292 int qdep;
3293
3294 BUG_ON(!dev_data->ats.enabled);
3295
3296 qdep = dev_data->ats.qdep;
3297 iommu = amd_iommu_rlookup_table[dev_data->devid];
3298
3299 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3300 qdep, address, size);
3301
3302 ret = iommu_queue_command(iommu, &cmd);
3303 if (ret != 0)
3304 goto out;
3305 }
3306
3307 /* Wait until all device TLBs are flushed */
3308 domain_flush_complete(domain);
3309
3310 ret = 0;
3311
3312out:
3313
3314 return ret;
3315}
3316
3317static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3318 u64 address)
3319{
3320 return __flush_pasid(domain, pasid, address, false);
3321}
3322
3323int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3324 u64 address)
3325{
3326 struct protection_domain *domain = dom->priv;
3327 unsigned long flags;
3328 int ret;
3329
3330 spin_lock_irqsave(&domain->lock, flags);
3331 ret = __amd_iommu_flush_page(domain, pasid, address);
3332 spin_unlock_irqrestore(&domain->lock, flags);
3333
3334 return ret;
3335}
3336EXPORT_SYMBOL(amd_iommu_flush_page);
3337
3338static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3339{
3340 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3341 true);
3342}
3343
3344int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3345{
3346 struct protection_domain *domain = dom->priv;
3347 unsigned long flags;
3348 int ret;
3349
3350 spin_lock_irqsave(&domain->lock, flags);
3351 ret = __amd_iommu_flush_tlb(domain, pasid);
3352 spin_unlock_irqrestore(&domain->lock, flags);
3353
3354 return ret;
3355}
3356EXPORT_SYMBOL(amd_iommu_flush_tlb);
3357
Joerg Roedelb16137b2011-11-21 16:50:23 +01003358static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3359{
3360 int index;
3361 u64 *pte;
3362
3363 while (true) {
3364
3365 index = (pasid >> (9 * level)) & 0x1ff;
3366 pte = &root[index];
3367
3368 if (level == 0)
3369 break;
3370
3371 if (!(*pte & GCR3_VALID)) {
3372 if (!alloc)
3373 return NULL;
3374
3375 root = (void *)get_zeroed_page(GFP_ATOMIC);
3376 if (root == NULL)
3377 return NULL;
3378
3379 *pte = __pa(root) | GCR3_VALID;
3380 }
3381
3382 root = __va(*pte & PAGE_MASK);
3383
3384 level -= 1;
3385 }
3386
3387 return pte;
3388}
3389
3390static int __set_gcr3(struct protection_domain *domain, int pasid,
3391 unsigned long cr3)
3392{
3393 u64 *pte;
3394
3395 if (domain->mode != PAGE_MODE_NONE)
3396 return -EINVAL;
3397
3398 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3399 if (pte == NULL)
3400 return -ENOMEM;
3401
3402 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3403
3404 return __amd_iommu_flush_tlb(domain, pasid);
3405}
3406
3407static int __clear_gcr3(struct protection_domain *domain, int pasid)
3408{
3409 u64 *pte;
3410
3411 if (domain->mode != PAGE_MODE_NONE)
3412 return -EINVAL;
3413
3414 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3415 if (pte == NULL)
3416 return 0;
3417
3418 *pte = 0;
3419
3420 return __amd_iommu_flush_tlb(domain, pasid);
3421}
3422
3423int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3424 unsigned long cr3)
3425{
3426 struct protection_domain *domain = dom->priv;
3427 unsigned long flags;
3428 int ret;
3429
3430 spin_lock_irqsave(&domain->lock, flags);
3431 ret = __set_gcr3(domain, pasid, cr3);
3432 spin_unlock_irqrestore(&domain->lock, flags);
3433
3434 return ret;
3435}
3436EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3437
3438int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3439{
3440 struct protection_domain *domain = dom->priv;
3441 unsigned long flags;
3442 int ret;
3443
3444 spin_lock_irqsave(&domain->lock, flags);
3445 ret = __clear_gcr3(domain, pasid);
3446 spin_unlock_irqrestore(&domain->lock, flags);
3447
3448 return ret;
3449}
3450EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003451
3452int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3453 int status, int tag)
3454{
3455 struct iommu_dev_data *dev_data;
3456 struct amd_iommu *iommu;
3457 struct iommu_cmd cmd;
3458
3459 dev_data = get_dev_data(&pdev->dev);
3460 iommu = amd_iommu_rlookup_table[dev_data->devid];
3461
3462 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3463 tag, dev_data->pri_tlp);
3464
3465 return iommu_queue_command(iommu, &cmd);
3466}
3467EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003468
3469struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3470{
3471 struct protection_domain *domain;
3472
3473 domain = get_domain(&pdev->dev);
3474 if (IS_ERR(domain))
3475 return NULL;
3476
3477 /* Only return IOMMUv2 domains */
3478 if (!(domain->flags & PD_IOMMUV2_MASK))
3479 return NULL;
3480
3481 return domain->iommu_domain;
3482}
3483EXPORT_SYMBOL(amd_iommu_get_v2_domain);