blob: db9b788c28ba44bebd987f90dc87c2a9c8f9e2ae [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);
66
Joerg Roedel431b2a22008-07-11 17:14:22 +020067/*
68 * general struct to manage commands send to an IOMMU
69 */
Joerg Roedeld6449532008-07-11 17:14:28 +020070struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +020071 u32 data[4];
72};
73
Joerg Roedel04bfdd82009-09-02 16:00:23 +020074static void update_domain(struct protection_domain *domain);
Joerg Roedel5abcdba2011-12-01 15:49:45 +010075static int __init alloc_passthrough_domain(void);
Chris Wrightc1eee672009-05-21 00:56:58 -070076
Joerg Roedel15898bb2009-11-24 15:39:42 +010077/****************************************************************************
78 *
79 * Helper functions
80 *
81 ****************************************************************************/
82
Joerg Roedelf62dda62011-06-09 12:55:35 +020083static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +020084{
85 struct iommu_dev_data *dev_data;
86 unsigned long flags;
87
88 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
89 if (!dev_data)
90 return NULL;
91
Joerg Roedelf62dda62011-06-09 12:55:35 +020092 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +020093 atomic_set(&dev_data->bind, 0);
94
95 spin_lock_irqsave(&dev_data_list_lock, flags);
96 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
97 spin_unlock_irqrestore(&dev_data_list_lock, flags);
98
99 return dev_data;
100}
101
102static void free_dev_data(struct iommu_dev_data *dev_data)
103{
104 unsigned long flags;
105
106 spin_lock_irqsave(&dev_data_list_lock, flags);
107 list_del(&dev_data->dev_data_list);
108 spin_unlock_irqrestore(&dev_data_list_lock, flags);
109
110 kfree(dev_data);
111}
112
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200113static struct iommu_dev_data *search_dev_data(u16 devid)
114{
115 struct iommu_dev_data *dev_data;
116 unsigned long flags;
117
118 spin_lock_irqsave(&dev_data_list_lock, flags);
119 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
120 if (dev_data->devid == devid)
121 goto out_unlock;
122 }
123
124 dev_data = NULL;
125
126out_unlock:
127 spin_unlock_irqrestore(&dev_data_list_lock, flags);
128
129 return dev_data;
130}
131
132static struct iommu_dev_data *find_dev_data(u16 devid)
133{
134 struct iommu_dev_data *dev_data;
135
136 dev_data = search_dev_data(devid);
137
138 if (dev_data == NULL)
139 dev_data = alloc_dev_data(devid);
140
141 return dev_data;
142}
143
Joerg Roedel15898bb2009-11-24 15:39:42 +0100144static inline u16 get_device_id(struct device *dev)
145{
146 struct pci_dev *pdev = to_pci_dev(dev);
147
148 return calc_devid(pdev->bus->number, pdev->devfn);
149}
150
Joerg Roedel657cbb62009-11-23 15:26:46 +0100151static struct iommu_dev_data *get_dev_data(struct device *dev)
152{
153 return dev->archdata.iommu;
154}
155
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100156static bool pci_iommuv2_capable(struct pci_dev *pdev)
157{
158 static const int caps[] = {
159 PCI_EXT_CAP_ID_ATS,
160 PCI_PRI_CAP,
161 PCI_PASID_CAP,
162 };
163 int i, pos;
164
165 for (i = 0; i < 3; ++i) {
166 pos = pci_find_ext_capability(pdev, caps[i]);
167 if (pos == 0)
168 return false;
169 }
170
171 return true;
172}
173
Joerg Roedel71c70982009-11-24 16:43:06 +0100174/*
175 * In this function the list of preallocated protection domains is traversed to
176 * find the domain for a specific device
177 */
178static struct dma_ops_domain *find_protection_domain(u16 devid)
179{
180 struct dma_ops_domain *entry, *ret = NULL;
181 unsigned long flags;
182 u16 alias = amd_iommu_alias_table[devid];
183
184 if (list_empty(&iommu_pd_list))
185 return NULL;
186
187 spin_lock_irqsave(&iommu_pd_list_lock, flags);
188
189 list_for_each_entry(entry, &iommu_pd_list, list) {
190 if (entry->target_dev == devid ||
191 entry->target_dev == alias) {
192 ret = entry;
193 break;
194 }
195 }
196
197 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
198
199 return ret;
200}
201
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100202/*
203 * This function checks if the driver got a valid device from the caller to
204 * avoid dereferencing invalid pointers.
205 */
206static bool check_device(struct device *dev)
207{
208 u16 devid;
209
210 if (!dev || !dev->dma_mask)
211 return false;
212
213 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100214 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100215 return false;
216
217 devid = get_device_id(dev);
218
219 /* Out of our scope? */
220 if (devid > amd_iommu_last_bdf)
221 return false;
222
223 if (amd_iommu_rlookup_table[devid] == NULL)
224 return false;
225
226 return true;
227}
228
Joerg Roedel657cbb62009-11-23 15:26:46 +0100229static int iommu_init_device(struct device *dev)
230{
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100231 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedel657cbb62009-11-23 15:26:46 +0100232 struct iommu_dev_data *dev_data;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200233 u16 alias;
Joerg Roedel657cbb62009-11-23 15:26:46 +0100234
235 if (dev->archdata.iommu)
236 return 0;
237
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200238 dev_data = find_dev_data(get_device_id(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +0100239 if (!dev_data)
240 return -ENOMEM;
241
Joerg Roedelf62dda62011-06-09 12:55:35 +0200242 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedel2b02b092011-06-09 17:48:39 +0200243 if (alias != dev_data->devid) {
Joerg Roedel71f77582011-06-09 19:03:15 +0200244 struct iommu_dev_data *alias_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100245
Joerg Roedel71f77582011-06-09 19:03:15 +0200246 alias_data = find_dev_data(alias);
247 if (alias_data == NULL) {
248 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
249 dev_name(dev));
Joerg Roedel2b02b092011-06-09 17:48:39 +0200250 free_dev_data(dev_data);
251 return -ENOTSUPP;
252 }
Joerg Roedel71f77582011-06-09 19:03:15 +0200253 dev_data->alias_data = alias_data;
Joerg Roedel26018872011-06-06 16:50:14 +0200254 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100255
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100256 if (pci_iommuv2_capable(pdev)) {
257 struct amd_iommu *iommu;
258
259 iommu = amd_iommu_rlookup_table[dev_data->devid];
260 dev_data->iommu_v2 = iommu->is_iommu_v2;
261 }
262
Joerg Roedel657cbb62009-11-23 15:26:46 +0100263 dev->archdata.iommu = dev_data;
264
Joerg Roedel657cbb62009-11-23 15:26:46 +0100265 return 0;
266}
267
Joerg Roedel26018872011-06-06 16:50:14 +0200268static void iommu_ignore_device(struct device *dev)
269{
270 u16 devid, alias;
271
272 devid = get_device_id(dev);
273 alias = amd_iommu_alias_table[devid];
274
275 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
276 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
277
278 amd_iommu_rlookup_table[devid] = NULL;
279 amd_iommu_rlookup_table[alias] = NULL;
280}
281
Joerg Roedel657cbb62009-11-23 15:26:46 +0100282static void iommu_uninit_device(struct device *dev)
283{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200284 /*
285 * Nothing to do here - we keep dev_data around for unplugged devices
286 * and reuse it when the device is re-plugged - not doing so would
287 * introduce a ton of races.
288 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100289}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100290
291void __init amd_iommu_uninit_devices(void)
292{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200293 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100294 struct pci_dev *pdev = NULL;
295
296 for_each_pci_dev(pdev) {
297
298 if (!check_device(&pdev->dev))
299 continue;
300
301 iommu_uninit_device(&pdev->dev);
302 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200303
304 /* Free all of our dev_data structures */
305 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
306 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100307}
308
309int __init amd_iommu_init_devices(void)
310{
311 struct pci_dev *pdev = NULL;
312 int ret = 0;
313
314 for_each_pci_dev(pdev) {
315
316 if (!check_device(&pdev->dev))
317 continue;
318
319 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200320 if (ret == -ENOTSUPP)
321 iommu_ignore_device(&pdev->dev);
322 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100323 goto out_free;
324 }
325
326 return 0;
327
328out_free:
329
330 amd_iommu_uninit_devices();
331
332 return ret;
333}
Joerg Roedel7f265082008-12-12 13:50:21 +0100334#ifdef CONFIG_AMD_IOMMU_STATS
335
336/*
337 * Initialization code for statistics collection
338 */
339
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100340DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100341DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100342DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100343DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100344DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100345DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100346DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100347DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100348DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100349DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100350DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100351DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100352
Joerg Roedel7f265082008-12-12 13:50:21 +0100353static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100354static struct dentry *de_fflush;
355
356static void amd_iommu_stats_add(struct __iommu_counter *cnt)
357{
358 if (stats_dir == NULL)
359 return;
360
361 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
362 &cnt->value);
363}
364
365static void amd_iommu_stats_init(void)
366{
367 stats_dir = debugfs_create_dir("amd-iommu", NULL);
368 if (stats_dir == NULL)
369 return;
370
Joerg Roedel7f265082008-12-12 13:50:21 +0100371 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
372 (u32 *)&amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100373
374 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100375 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100376 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100377 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100378 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100379 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100380 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100381 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100382 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100383 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100384 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100385 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100386}
387
388#endif
389
Joerg Roedel431b2a22008-07-11 17:14:22 +0200390/****************************************************************************
391 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200392 * Interrupt handling functions
393 *
394 ****************************************************************************/
395
Joerg Roedele3e59872009-09-03 14:02:10 +0200396static void dump_dte_entry(u16 devid)
397{
398 int i;
399
Joerg Roedelee6c2862011-11-09 12:06:03 +0100400 for (i = 0; i < 4; ++i)
401 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200402 amd_iommu_dev_table[devid].data[i]);
403}
404
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200405static void dump_command(unsigned long phys_addr)
406{
407 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
408 int i;
409
410 for (i = 0; i < 4; ++i)
411 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
412}
413
Joerg Roedela345b232009-09-03 15:01:43 +0200414static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200415{
416 u32 *event = __evt;
417 int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
418 int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
419 int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
420 int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
421 u64 address = (u64)(((u64)event[3]) << 32) | event[2];
422
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200423 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200424
425 switch (type) {
426 case EVENT_TYPE_ILL_DEV:
427 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
428 "address=0x%016llx flags=0x%04x]\n",
429 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
430 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200431 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200432 break;
433 case EVENT_TYPE_IO_FAULT:
434 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
435 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
436 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
437 domid, address, flags);
438 break;
439 case EVENT_TYPE_DEV_TAB_ERR:
440 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
441 "address=0x%016llx flags=0x%04x]\n",
442 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
443 address, flags);
444 break;
445 case EVENT_TYPE_PAGE_TAB_ERR:
446 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
447 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
448 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
449 domid, address, flags);
450 break;
451 case EVENT_TYPE_ILL_CMD:
452 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200453 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200454 break;
455 case EVENT_TYPE_CMD_HARD_ERR:
456 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
457 "flags=0x%04x]\n", address, flags);
458 break;
459 case EVENT_TYPE_IOTLB_INV_TO:
460 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
461 "address=0x%016llx]\n",
462 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
463 address);
464 break;
465 case EVENT_TYPE_INV_DEV_REQ:
466 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
467 "address=0x%016llx flags=0x%04x]\n",
468 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
469 address, flags);
470 break;
471 default:
472 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
473 }
474}
475
476static void iommu_poll_events(struct amd_iommu *iommu)
477{
478 u32 head, tail;
479 unsigned long flags;
480
481 spin_lock_irqsave(&iommu->lock, flags);
482
483 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
484 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
485
486 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200487 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200488 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
489 }
490
491 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
492
493 spin_unlock_irqrestore(&iommu->lock, flags);
494}
495
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100496static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u32 head)
497{
498 struct amd_iommu_fault fault;
499 volatile u64 *raw;
500 int i;
501
502 raw = (u64 *)(iommu->ppr_log + head);
503
504 /*
505 * Hardware bug: Interrupt may arrive before the entry is written to
506 * memory. If this happens we need to wait for the entry to arrive.
507 */
508 for (i = 0; i < LOOP_TIMEOUT; ++i) {
509 if (PPR_REQ_TYPE(raw[0]) != 0)
510 break;
511 udelay(1);
512 }
513
514 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
515 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
516 return;
517 }
518
519 fault.address = raw[1];
520 fault.pasid = PPR_PASID(raw[0]);
521 fault.device_id = PPR_DEVID(raw[0]);
522 fault.tag = PPR_TAG(raw[0]);
523 fault.flags = PPR_FLAGS(raw[0]);
524
525 /*
526 * To detect the hardware bug we need to clear the entry
527 * to back to zero.
528 */
529 raw[0] = raw[1] = 0;
530
531 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
532}
533
534static void iommu_poll_ppr_log(struct amd_iommu *iommu)
535{
536 unsigned long flags;
537 u32 head, tail;
538
539 if (iommu->ppr_log == NULL)
540 return;
541
542 spin_lock_irqsave(&iommu->lock, flags);
543
544 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
545 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
546
547 while (head != tail) {
548
549 /* Handle PPR entry */
550 iommu_handle_ppr_entry(iommu, head);
551
552 /* Update and refresh ring-buffer state*/
553 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
554 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
555 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
556 }
557
558 /* enable ppr interrupts again */
559 writel(MMIO_STATUS_PPR_INT_MASK, iommu->mmio_base + MMIO_STATUS_OFFSET);
560
561 spin_unlock_irqrestore(&iommu->lock, flags);
562}
563
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200564irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200565{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200566 struct amd_iommu *iommu;
567
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100568 for_each_iommu(iommu) {
Joerg Roedel90008ee2008-09-09 16:41:05 +0200569 iommu_poll_events(iommu);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100570 iommu_poll_ppr_log(iommu);
571 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200572
573 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200574}
575
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200576irqreturn_t amd_iommu_int_handler(int irq, void *data)
577{
578 return IRQ_WAKE_THREAD;
579}
580
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200581/****************************************************************************
582 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200583 * IOMMU command queuing functions
584 *
585 ****************************************************************************/
586
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200587static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200588{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200589 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200590
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200591 while (*sem == 0 && i < LOOP_TIMEOUT) {
592 udelay(1);
593 i += 1;
594 }
595
596 if (i == LOOP_TIMEOUT) {
597 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
598 return -EIO;
599 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200600
601 return 0;
602}
603
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200604static void copy_cmd_to_buffer(struct amd_iommu *iommu,
605 struct iommu_cmd *cmd,
606 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200607{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200608 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200609
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200610 target = iommu->cmd_buf + tail;
611 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200612
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200613 /* Copy command to buffer */
614 memcpy(target, cmd, sizeof(*cmd));
615
616 /* Tell the IOMMU about it */
617 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
618}
619
Joerg Roedel815b33f2011-04-06 17:26:49 +0200620static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200621{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200622 WARN_ON(address & 0x7ULL);
623
Joerg Roedelded46732011-04-06 10:53:48 +0200624 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200625 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
626 cmd->data[1] = upper_32_bits(__pa(address));
627 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200628 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
629}
630
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200631static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
632{
633 memset(cmd, 0, sizeof(*cmd));
634 cmd->data[0] = devid;
635 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
636}
637
Joerg Roedel11b64022011-04-06 11:49:28 +0200638static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
639 size_t size, u16 domid, int pde)
640{
641 u64 pages;
642 int s;
643
644 pages = iommu_num_pages(address, size, PAGE_SIZE);
645 s = 0;
646
647 if (pages > 1) {
648 /*
649 * If we have to flush more than one page, flush all
650 * TLB entries for this domain
651 */
652 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
653 s = 1;
654 }
655
656 address &= PAGE_MASK;
657
658 memset(cmd, 0, sizeof(*cmd));
659 cmd->data[1] |= domid;
660 cmd->data[2] = lower_32_bits(address);
661 cmd->data[3] = upper_32_bits(address);
662 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
663 if (s) /* size bit - we flush more than one 4kb page */
664 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
665 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
666 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
667}
668
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200669static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
670 u64 address, size_t size)
671{
672 u64 pages;
673 int s;
674
675 pages = iommu_num_pages(address, size, PAGE_SIZE);
676 s = 0;
677
678 if (pages > 1) {
679 /*
680 * If we have to flush more than one page, flush all
681 * TLB entries for this domain
682 */
683 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
684 s = 1;
685 }
686
687 address &= PAGE_MASK;
688
689 memset(cmd, 0, sizeof(*cmd));
690 cmd->data[0] = devid;
691 cmd->data[0] |= (qdep & 0xff) << 24;
692 cmd->data[1] = devid;
693 cmd->data[2] = lower_32_bits(address);
694 cmd->data[3] = upper_32_bits(address);
695 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
696 if (s)
697 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
698}
699
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200700static void build_inv_all(struct iommu_cmd *cmd)
701{
702 memset(cmd, 0, sizeof(*cmd));
703 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200704}
705
Joerg Roedel431b2a22008-07-11 17:14:22 +0200706/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200707 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200708 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200709 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200710static int iommu_queue_command_sync(struct amd_iommu *iommu,
711 struct iommu_cmd *cmd,
712 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200713{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200714 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200715 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200716
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200717 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100718
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200719again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200720 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200721
722 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
723 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
724 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
725 left = (head - next_tail) % iommu->cmd_buf_size;
726
727 if (left <= 2) {
728 struct iommu_cmd sync_cmd;
729 volatile u64 sem = 0;
730 int ret;
731
732 build_completion_wait(&sync_cmd, (u64)&sem);
733 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
734
735 spin_unlock_irqrestore(&iommu->lock, flags);
736
737 if ((ret = wait_on_sem(&sem)) != 0)
738 return ret;
739
740 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200741 }
742
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200743 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200744
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200745 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200746 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200747
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200748 spin_unlock_irqrestore(&iommu->lock, flags);
749
Joerg Roedel815b33f2011-04-06 17:26:49 +0200750 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100751}
752
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200753static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
754{
755 return iommu_queue_command_sync(iommu, cmd, true);
756}
757
Joerg Roedel8d201962008-12-02 20:34:41 +0100758/*
759 * This function queues a completion wait command into the command
760 * buffer of an IOMMU
761 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100762static int iommu_completion_wait(struct amd_iommu *iommu)
763{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200764 struct iommu_cmd cmd;
765 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200766 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100767
768 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200769 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100770
Joerg Roedel815b33f2011-04-06 17:26:49 +0200771 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100772
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200773 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +0100774 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200775 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100776
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200777 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200778}
779
Joerg Roedeld8c13082011-04-06 18:51:26 +0200780static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200781{
782 struct iommu_cmd cmd;
783
Joerg Roedeld8c13082011-04-06 18:51:26 +0200784 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200785
Joerg Roedeld8c13082011-04-06 18:51:26 +0200786 return iommu_queue_command(iommu, &cmd);
787}
788
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200789static void iommu_flush_dte_all(struct amd_iommu *iommu)
790{
791 u32 devid;
792
793 for (devid = 0; devid <= 0xffff; ++devid)
794 iommu_flush_dte(iommu, devid);
795
796 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200797}
798
799/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200800 * This function uses heavy locking and may disable irqs for some time. But
801 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200802 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200803static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200804{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200805 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200806
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200807 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
808 struct iommu_cmd cmd;
809 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
810 dom_id, 1);
811 iommu_queue_command(iommu, &cmd);
812 }
Joerg Roedel431b2a22008-07-11 17:14:22 +0200813
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200814 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200815}
816
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200817static void iommu_flush_all(struct amd_iommu *iommu)
818{
819 struct iommu_cmd cmd;
820
821 build_inv_all(&cmd);
822
823 iommu_queue_command(iommu, &cmd);
824 iommu_completion_wait(iommu);
825}
826
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200827void iommu_flush_all_caches(struct amd_iommu *iommu)
828{
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200829 if (iommu_feature(iommu, FEATURE_IA)) {
830 iommu_flush_all(iommu);
831 } else {
832 iommu_flush_dte_all(iommu);
833 iommu_flush_tlb_all(iommu);
834 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200835}
836
Joerg Roedel431b2a22008-07-11 17:14:22 +0200837/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200838 * Command send function for flushing on-device TLB
839 */
Joerg Roedel6c542042011-06-09 17:07:31 +0200840static int device_flush_iotlb(struct iommu_dev_data *dev_data,
841 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200842{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200843 struct amd_iommu *iommu;
844 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200845 int qdep;
846
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200847 qdep = dev_data->ats.qdep;
848 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200849
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200850 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200851
852 return iommu_queue_command(iommu, &cmd);
853}
854
855/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200856 * Command send function for invalidating a device table entry
857 */
Joerg Roedel6c542042011-06-09 17:07:31 +0200858static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +0100859{
860 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200861 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100862
Joerg Roedel6c542042011-06-09 17:07:31 +0200863 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +0100864
Joerg Roedelf62dda62011-06-09 12:55:35 +0200865 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200866 if (ret)
867 return ret;
868
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200869 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +0200870 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200871
872 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100873}
874
Joerg Roedel431b2a22008-07-11 17:14:22 +0200875/*
876 * TLB invalidation function which is called from the mapping functions.
877 * It invalidates a single PTE if the range to flush is within a single
878 * page. Otherwise it flushes the whole TLB of the IOMMU.
879 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200880static void __domain_flush_pages(struct protection_domain *domain,
881 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200882{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200883 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +0200884 struct iommu_cmd cmd;
885 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200886
Joerg Roedel11b64022011-04-06 11:49:28 +0200887 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +0200888
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100889 for (i = 0; i < amd_iommus_present; ++i) {
890 if (!domain->dev_iommu[i])
891 continue;
892
893 /*
894 * Devices of this domain are behind this IOMMU
895 * We need a TLB flush
896 */
Joerg Roedel11b64022011-04-06 11:49:28 +0200897 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100898 }
899
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200900 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200901
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200902 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200903 continue;
904
Joerg Roedel6c542042011-06-09 17:07:31 +0200905 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200906 }
907
Joerg Roedel11b64022011-04-06 11:49:28 +0200908 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100909}
910
Joerg Roedel17b124b2011-04-06 18:01:35 +0200911static void domain_flush_pages(struct protection_domain *domain,
912 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100913{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200914 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200915}
Joerg Roedelb6c02712008-06-26 21:27:53 +0200916
Joerg Roedel1c655772008-09-04 18:40:05 +0200917/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200918static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +0200919{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200920 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +0200921}
922
Chris Wright42a49f92009-06-15 15:42:00 +0200923/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200924static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +0200925{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200926 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
927}
928
929static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +0200930{
931 int i;
932
933 for (i = 0; i < amd_iommus_present; ++i) {
934 if (!domain->dev_iommu[i])
935 continue;
936
937 /*
938 * Devices of this domain are behind this IOMMU
939 * We need to wait for completion of all commands.
940 */
941 iommu_completion_wait(amd_iommus[i]);
942 }
943}
944
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100945
Joerg Roedel43f49602008-12-02 21:01:12 +0100946/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100947 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +0100948 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200949static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200950{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100951 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100952
953 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +0200954 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100955}
956
Joerg Roedel431b2a22008-07-11 17:14:22 +0200957/****************************************************************************
958 *
959 * The functions below are used the create the page table mappings for
960 * unity mapped regions.
961 *
962 ****************************************************************************/
963
964/*
Joerg Roedel308973d2009-11-24 17:43:32 +0100965 * This function is used to add another level to an IO page table. Adding
966 * another level increases the size of the address space by 9 bits to a size up
967 * to 64 bits.
968 */
969static bool increase_address_space(struct protection_domain *domain,
970 gfp_t gfp)
971{
972 u64 *pte;
973
974 if (domain->mode == PAGE_MODE_6_LEVEL)
975 /* address space already 64 bit large */
976 return false;
977
978 pte = (void *)get_zeroed_page(gfp);
979 if (!pte)
980 return false;
981
982 *pte = PM_LEVEL_PDE(domain->mode,
983 virt_to_phys(domain->pt_root));
984 domain->pt_root = pte;
985 domain->mode += 1;
986 domain->updated = true;
987
988 return true;
989}
990
991static u64 *alloc_pte(struct protection_domain *domain,
992 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100993 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +0100994 u64 **pte_page,
995 gfp_t gfp)
996{
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100997 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +0100998 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100999
1000 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001001
1002 while (address > PM_LEVEL_SIZE(domain->mode))
1003 increase_address_space(domain, gfp);
1004
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001005 level = domain->mode - 1;
1006 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1007 address = PAGE_SIZE_ALIGN(address, page_size);
1008 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001009
1010 while (level > end_lvl) {
1011 if (!IOMMU_PTE_PRESENT(*pte)) {
1012 page = (u64 *)get_zeroed_page(gfp);
1013 if (!page)
1014 return NULL;
1015 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1016 }
1017
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001018 /* No level skipping support yet */
1019 if (PM_PTE_LEVEL(*pte) != level)
1020 return NULL;
1021
Joerg Roedel308973d2009-11-24 17:43:32 +01001022 level -= 1;
1023
1024 pte = IOMMU_PTE_PAGE(*pte);
1025
1026 if (pte_page && level == end_lvl)
1027 *pte_page = pte;
1028
1029 pte = &pte[PM_LEVEL_INDEX(level, address)];
1030 }
1031
1032 return pte;
1033}
1034
1035/*
1036 * This function checks if there is a PTE for a given dma address. If
1037 * there is one, it returns the pointer to it.
1038 */
Joerg Roedel24cd7722010-01-19 17:27:39 +01001039static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +01001040{
1041 int level;
1042 u64 *pte;
1043
Joerg Roedel24cd7722010-01-19 17:27:39 +01001044 if (address > PM_LEVEL_SIZE(domain->mode))
1045 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001046
Joerg Roedel24cd7722010-01-19 17:27:39 +01001047 level = domain->mode - 1;
1048 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1049
1050 while (level > 0) {
1051
1052 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001053 if (!IOMMU_PTE_PRESENT(*pte))
1054 return NULL;
1055
Joerg Roedel24cd7722010-01-19 17:27:39 +01001056 /* Large PTE */
1057 if (PM_PTE_LEVEL(*pte) == 0x07) {
1058 unsigned long pte_mask, __pte;
1059
1060 /*
1061 * If we have a series of large PTEs, make
1062 * sure to return a pointer to the first one.
1063 */
1064 pte_mask = PTE_PAGE_SIZE(*pte);
1065 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1066 __pte = ((unsigned long)pte) & pte_mask;
1067
1068 return (u64 *)__pte;
1069 }
1070
1071 /* No level skipping support yet */
1072 if (PM_PTE_LEVEL(*pte) != level)
1073 return NULL;
1074
Joerg Roedel308973d2009-11-24 17:43:32 +01001075 level -= 1;
1076
Joerg Roedel24cd7722010-01-19 17:27:39 +01001077 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +01001078 pte = IOMMU_PTE_PAGE(*pte);
1079 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +01001080 }
1081
1082 return pte;
1083}
1084
1085/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001086 * Generic mapping functions. It maps a physical address into a DMA
1087 * address space. It allocates the page table pages if necessary.
1088 * In the future it can be extended to a generic mapping function
1089 * supporting all features of AMD IOMMU page tables like level skipping
1090 * and full 64 bit address spaces.
1091 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001092static int iommu_map_page(struct protection_domain *dom,
1093 unsigned long bus_addr,
1094 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001095 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001096 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001097{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001098 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001099 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001100
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001101 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001102 return -EINVAL;
1103
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001104 bus_addr = PAGE_ALIGN(bus_addr);
1105 phys_addr = PAGE_ALIGN(phys_addr);
1106 count = PAGE_SIZE_PTE_COUNT(page_size);
1107 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001108
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001109 for (i = 0; i < count; ++i)
1110 if (IOMMU_PTE_PRESENT(pte[i]))
1111 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001112
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001113 if (page_size > PAGE_SIZE) {
1114 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1115 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1116 } else
1117 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1118
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001119 if (prot & IOMMU_PROT_IR)
1120 __pte |= IOMMU_PTE_IR;
1121 if (prot & IOMMU_PROT_IW)
1122 __pte |= IOMMU_PTE_IW;
1123
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001124 for (i = 0; i < count; ++i)
1125 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001126
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001127 update_domain(dom);
1128
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001129 return 0;
1130}
1131
Joerg Roedel24cd7722010-01-19 17:27:39 +01001132static unsigned long iommu_unmap_page(struct protection_domain *dom,
1133 unsigned long bus_addr,
1134 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001135{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001136 unsigned long long unmap_size, unmapped;
1137 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001138
Joerg Roedel24cd7722010-01-19 17:27:39 +01001139 BUG_ON(!is_power_of_2(page_size));
1140
1141 unmapped = 0;
1142
1143 while (unmapped < page_size) {
1144
1145 pte = fetch_pte(dom, bus_addr);
1146
1147 if (!pte) {
1148 /*
1149 * No PTE for this address
1150 * move forward in 4kb steps
1151 */
1152 unmap_size = PAGE_SIZE;
1153 } else if (PM_PTE_LEVEL(*pte) == 0) {
1154 /* 4kb PTE found for this address */
1155 unmap_size = PAGE_SIZE;
1156 *pte = 0ULL;
1157 } else {
1158 int count, i;
1159
1160 /* Large PTE found which maps this address */
1161 unmap_size = PTE_PAGE_SIZE(*pte);
1162 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1163 for (i = 0; i < count; i++)
1164 pte[i] = 0ULL;
1165 }
1166
1167 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1168 unmapped += unmap_size;
1169 }
1170
1171 BUG_ON(!is_power_of_2(unmapped));
1172
1173 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001174}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001175
Joerg Roedel431b2a22008-07-11 17:14:22 +02001176/*
1177 * This function checks if a specific unity mapping entry is needed for
1178 * this specific IOMMU.
1179 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001180static int iommu_for_unity_map(struct amd_iommu *iommu,
1181 struct unity_map_entry *entry)
1182{
1183 u16 bdf, i;
1184
1185 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1186 bdf = amd_iommu_alias_table[i];
1187 if (amd_iommu_rlookup_table[bdf] == iommu)
1188 return 1;
1189 }
1190
1191 return 0;
1192}
1193
Joerg Roedel431b2a22008-07-11 17:14:22 +02001194/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001195 * This function actually applies the mapping to the page table of the
1196 * dma_ops domain.
1197 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001198static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1199 struct unity_map_entry *e)
1200{
1201 u64 addr;
1202 int ret;
1203
1204 for (addr = e->address_start; addr < e->address_end;
1205 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001206 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001207 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001208 if (ret)
1209 return ret;
1210 /*
1211 * if unity mapping is in aperture range mark the page
1212 * as allocated in the aperture
1213 */
1214 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001215 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001216 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001217 }
1218
1219 return 0;
1220}
1221
Joerg Roedel431b2a22008-07-11 17:14:22 +02001222/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001223 * Init the unity mappings for a specific IOMMU in the system
1224 *
1225 * Basically iterates over all unity mapping entries and applies them to
1226 * the default domain DMA of that IOMMU if necessary.
1227 */
1228static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1229{
1230 struct unity_map_entry *entry;
1231 int ret;
1232
1233 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1234 if (!iommu_for_unity_map(iommu, entry))
1235 continue;
1236 ret = dma_ops_unity_map(iommu->default_dom, entry);
1237 if (ret)
1238 return ret;
1239 }
1240
1241 return 0;
1242}
1243
1244/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001245 * Inits the unity mappings required for a specific device
1246 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001247static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1248 u16 devid)
1249{
1250 struct unity_map_entry *e;
1251 int ret;
1252
1253 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1254 if (!(devid >= e->devid_start && devid <= e->devid_end))
1255 continue;
1256 ret = dma_ops_unity_map(dma_dom, e);
1257 if (ret)
1258 return ret;
1259 }
1260
1261 return 0;
1262}
1263
Joerg Roedel431b2a22008-07-11 17:14:22 +02001264/****************************************************************************
1265 *
1266 * The next functions belong to the address allocator for the dma_ops
1267 * interface functions. They work like the allocators in the other IOMMU
1268 * drivers. Its basically a bitmap which marks the allocated pages in
1269 * the aperture. Maybe it could be enhanced in the future to a more
1270 * efficient allocator.
1271 *
1272 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001273
Joerg Roedel431b2a22008-07-11 17:14:22 +02001274/*
Joerg Roedel384de722009-05-15 12:30:05 +02001275 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001276 *
1277 * called with domain->lock held
1278 */
Joerg Roedel384de722009-05-15 12:30:05 +02001279
Joerg Roedel9cabe892009-05-18 16:38:55 +02001280/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001281 * Used to reserve address ranges in the aperture (e.g. for exclusion
1282 * ranges.
1283 */
1284static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1285 unsigned long start_page,
1286 unsigned int pages)
1287{
1288 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1289
1290 if (start_page + pages > last_page)
1291 pages = last_page - start_page;
1292
1293 for (i = start_page; i < start_page + pages; ++i) {
1294 int index = i / APERTURE_RANGE_PAGES;
1295 int page = i % APERTURE_RANGE_PAGES;
1296 __set_bit(page, dom->aperture[index]->bitmap);
1297 }
1298}
1299
1300/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001301 * This function is used to add a new aperture range to an existing
1302 * aperture in case of dma_ops domain allocation or address allocation
1303 * failure.
1304 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001305static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001306 bool populate, gfp_t gfp)
1307{
1308 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001309 struct amd_iommu *iommu;
Joerg Roedel17f5b562011-07-06 17:14:44 +02001310 unsigned long i, old_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001311
Joerg Roedelf5e97052009-05-22 12:31:53 +02001312#ifdef CONFIG_IOMMU_STRESS
1313 populate = false;
1314#endif
1315
Joerg Roedel9cabe892009-05-18 16:38:55 +02001316 if (index >= APERTURE_MAX_RANGES)
1317 return -ENOMEM;
1318
1319 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1320 if (!dma_dom->aperture[index])
1321 return -ENOMEM;
1322
1323 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1324 if (!dma_dom->aperture[index]->bitmap)
1325 goto out_free;
1326
1327 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1328
1329 if (populate) {
1330 unsigned long address = dma_dom->aperture_size;
1331 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1332 u64 *pte, *pte_page;
1333
1334 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001335 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001336 &pte_page, gfp);
1337 if (!pte)
1338 goto out_free;
1339
1340 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1341
1342 address += APERTURE_RANGE_SIZE / 64;
1343 }
1344 }
1345
Joerg Roedel17f5b562011-07-06 17:14:44 +02001346 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001347 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1348
Joerg Roedel17f5b562011-07-06 17:14:44 +02001349 /* Reserve address range used for MSI messages */
1350 if (old_size < MSI_ADDR_BASE_LO &&
1351 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1352 unsigned long spage;
1353 int pages;
1354
1355 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1356 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1357
1358 dma_ops_reserve_addresses(dma_dom, spage, pages);
1359 }
1360
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001361 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001362 for_each_iommu(iommu) {
1363 if (iommu->exclusion_start &&
1364 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1365 && iommu->exclusion_start < dma_dom->aperture_size) {
1366 unsigned long startpage;
1367 int pages = iommu_num_pages(iommu->exclusion_start,
1368 iommu->exclusion_length,
1369 PAGE_SIZE);
1370 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1371 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1372 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001373 }
1374
1375 /*
1376 * Check for areas already mapped as present in the new aperture
1377 * range and mark those pages as reserved in the allocator. Such
1378 * mappings may already exist as a result of requested unity
1379 * mappings for devices.
1380 */
1381 for (i = dma_dom->aperture[index]->offset;
1382 i < dma_dom->aperture_size;
1383 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001384 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001385 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1386 continue;
1387
Joerg Roedelfcd08612011-10-11 17:41:32 +02001388 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001389 }
1390
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001391 update_domain(&dma_dom->domain);
1392
Joerg Roedel9cabe892009-05-18 16:38:55 +02001393 return 0;
1394
1395out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001396 update_domain(&dma_dom->domain);
1397
Joerg Roedel9cabe892009-05-18 16:38:55 +02001398 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1399
1400 kfree(dma_dom->aperture[index]);
1401 dma_dom->aperture[index] = NULL;
1402
1403 return -ENOMEM;
1404}
1405
Joerg Roedel384de722009-05-15 12:30:05 +02001406static unsigned long dma_ops_area_alloc(struct device *dev,
1407 struct dma_ops_domain *dom,
1408 unsigned int pages,
1409 unsigned long align_mask,
1410 u64 dma_mask,
1411 unsigned long start)
1412{
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001413 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001414 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1415 int i = start >> APERTURE_RANGE_SHIFT;
1416 unsigned long boundary_size;
1417 unsigned long address = -1;
1418 unsigned long limit;
1419
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001420 next_bit >>= PAGE_SHIFT;
1421
Joerg Roedel384de722009-05-15 12:30:05 +02001422 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1423 PAGE_SIZE) >> PAGE_SHIFT;
1424
1425 for (;i < max_index; ++i) {
1426 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1427
1428 if (dom->aperture[i]->offset >= dma_mask)
1429 break;
1430
1431 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1432 dma_mask >> PAGE_SHIFT);
1433
1434 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1435 limit, next_bit, pages, 0,
1436 boundary_size, align_mask);
1437 if (address != -1) {
1438 address = dom->aperture[i]->offset +
1439 (address << PAGE_SHIFT);
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001440 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001441 break;
1442 }
1443
1444 next_bit = 0;
1445 }
1446
1447 return address;
1448}
1449
Joerg Roedeld3086442008-06-26 21:27:57 +02001450static unsigned long dma_ops_alloc_addresses(struct device *dev,
1451 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001452 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001453 unsigned long align_mask,
1454 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001455{
Joerg Roedeld3086442008-06-26 21:27:57 +02001456 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001457
Joerg Roedelfe16f082009-05-22 12:27:53 +02001458#ifdef CONFIG_IOMMU_STRESS
1459 dom->next_address = 0;
1460 dom->need_flush = true;
1461#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001462
Joerg Roedel384de722009-05-15 12:30:05 +02001463 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001464 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001465
Joerg Roedel1c655772008-09-04 18:40:05 +02001466 if (address == -1) {
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001467 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001468 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1469 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001470 dom->need_flush = true;
1471 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001472
Joerg Roedel384de722009-05-15 12:30:05 +02001473 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001474 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001475
1476 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1477
1478 return address;
1479}
1480
Joerg Roedel431b2a22008-07-11 17:14:22 +02001481/*
1482 * The address free function.
1483 *
1484 * called with domain->lock held
1485 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001486static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1487 unsigned long address,
1488 unsigned int pages)
1489{
Joerg Roedel384de722009-05-15 12:30:05 +02001490 unsigned i = address >> APERTURE_RANGE_SHIFT;
1491 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001492
Joerg Roedel384de722009-05-15 12:30:05 +02001493 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1494
Joerg Roedel47bccd62009-05-22 12:40:54 +02001495#ifdef CONFIG_IOMMU_STRESS
1496 if (i < 4)
1497 return;
1498#endif
1499
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001500 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001501 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001502
1503 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001504
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001505 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001506
Joerg Roedeld3086442008-06-26 21:27:57 +02001507}
1508
Joerg Roedel431b2a22008-07-11 17:14:22 +02001509/****************************************************************************
1510 *
1511 * The next functions belong to the domain allocation. A domain is
1512 * allocated for every IOMMU as the default domain. If device isolation
1513 * is enabled, every device get its own domain. The most important thing
1514 * about domains is the page table mapping the DMA address space they
1515 * contain.
1516 *
1517 ****************************************************************************/
1518
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001519/*
1520 * This function adds a protection domain to the global protection domain list
1521 */
1522static void add_domain_to_list(struct protection_domain *domain)
1523{
1524 unsigned long flags;
1525
1526 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1527 list_add(&domain->list, &amd_iommu_pd_list);
1528 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1529}
1530
1531/*
1532 * This function removes a protection domain to the global
1533 * protection domain list
1534 */
1535static void del_domain_from_list(struct protection_domain *domain)
1536{
1537 unsigned long flags;
1538
1539 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1540 list_del(&domain->list);
1541 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1542}
1543
Joerg Roedelec487d12008-06-26 21:27:58 +02001544static u16 domain_id_alloc(void)
1545{
1546 unsigned long flags;
1547 int id;
1548
1549 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1550 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1551 BUG_ON(id == 0);
1552 if (id > 0 && id < MAX_DOMAIN_ID)
1553 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1554 else
1555 id = 0;
1556 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1557
1558 return id;
1559}
1560
Joerg Roedela2acfb72008-12-02 18:28:53 +01001561static void domain_id_free(int id)
1562{
1563 unsigned long flags;
1564
1565 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1566 if (id > 0 && id < MAX_DOMAIN_ID)
1567 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1568 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1569}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001570
Joerg Roedel86db2e52008-12-02 18:20:21 +01001571static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001572{
1573 int i, j;
1574 u64 *p1, *p2, *p3;
1575
Joerg Roedel86db2e52008-12-02 18:20:21 +01001576 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001577
1578 if (!p1)
1579 return;
1580
1581 for (i = 0; i < 512; ++i) {
1582 if (!IOMMU_PTE_PRESENT(p1[i]))
1583 continue;
1584
1585 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001586 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001587 if (!IOMMU_PTE_PRESENT(p2[j]))
1588 continue;
1589 p3 = IOMMU_PTE_PAGE(p2[j]);
1590 free_page((unsigned long)p3);
1591 }
1592
1593 free_page((unsigned long)p2);
1594 }
1595
1596 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001597
1598 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001599}
1600
Joerg Roedel431b2a22008-07-11 17:14:22 +02001601/*
1602 * Free a domain, only used if something went wrong in the
1603 * allocation path and we need to free an already allocated page table
1604 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001605static void dma_ops_domain_free(struct dma_ops_domain *dom)
1606{
Joerg Roedel384de722009-05-15 12:30:05 +02001607 int i;
1608
Joerg Roedelec487d12008-06-26 21:27:58 +02001609 if (!dom)
1610 return;
1611
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001612 del_domain_from_list(&dom->domain);
1613
Joerg Roedel86db2e52008-12-02 18:20:21 +01001614 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001615
Joerg Roedel384de722009-05-15 12:30:05 +02001616 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1617 if (!dom->aperture[i])
1618 continue;
1619 free_page((unsigned long)dom->aperture[i]->bitmap);
1620 kfree(dom->aperture[i]);
1621 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001622
1623 kfree(dom);
1624}
1625
Joerg Roedel431b2a22008-07-11 17:14:22 +02001626/*
1627 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001628 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001629 * structures required for the dma_ops interface
1630 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001631static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001632{
1633 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001634
1635 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1636 if (!dma_dom)
1637 return NULL;
1638
1639 spin_lock_init(&dma_dom->domain.lock);
1640
1641 dma_dom->domain.id = domain_id_alloc();
1642 if (dma_dom->domain.id == 0)
1643 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001644 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001645 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001646 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001647 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001648 dma_dom->domain.priv = dma_dom;
1649 if (!dma_dom->domain.pt_root)
1650 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001651
Joerg Roedel1c655772008-09-04 18:40:05 +02001652 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001653 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001654
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001655 add_domain_to_list(&dma_dom->domain);
1656
Joerg Roedel576175c2009-11-23 19:08:46 +01001657 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001658 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001659
Joerg Roedel431b2a22008-07-11 17:14:22 +02001660 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001661 * mark the first page as allocated so we never return 0 as
1662 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001663 */
Joerg Roedel384de722009-05-15 12:30:05 +02001664 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001665 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001666
Joerg Roedelec487d12008-06-26 21:27:58 +02001667
1668 return dma_dom;
1669
1670free_dma_dom:
1671 dma_ops_domain_free(dma_dom);
1672
1673 return NULL;
1674}
1675
Joerg Roedel431b2a22008-07-11 17:14:22 +02001676/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001677 * little helper function to check whether a given protection domain is a
1678 * dma_ops domain
1679 */
1680static bool dma_ops_domain(struct protection_domain *domain)
1681{
1682 return domain->flags & PD_DMA_OPS_MASK;
1683}
1684
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001685static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001686{
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001687 u64 pte_root = virt_to_phys(domain->pt_root);
Joerg Roedelee6c2862011-11-09 12:06:03 +01001688 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001689
Joerg Roedel38ddf412008-09-11 10:38:32 +02001690 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1691 << DEV_ENTRY_MODE_SHIFT;
1692 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001693
Joerg Roedelee6c2862011-11-09 12:06:03 +01001694 flags = amd_iommu_dev_table[devid].data[1];
1695
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001696 if (ats)
1697 flags |= DTE_FLAG_IOTLB;
1698
Joerg Roedelee6c2862011-11-09 12:06:03 +01001699 flags &= ~(0xffffUL);
1700 flags |= domain->id;
1701
1702 amd_iommu_dev_table[devid].data[1] = flags;
1703 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001704}
1705
Joerg Roedel15898bb2009-11-24 15:39:42 +01001706static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001707{
Joerg Roedel355bf552008-12-08 12:02:41 +01001708 /* remove entry from the device table seen by the hardware */
1709 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1710 amd_iommu_dev_table[devid].data[1] = 0;
Joerg Roedel355bf552008-12-08 12:02:41 +01001711
Joerg Roedelc5cca142009-10-09 18:31:20 +02001712 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001713}
1714
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001715static void do_attach(struct iommu_dev_data *dev_data,
1716 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001717{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001718 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001719 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001720
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001721 iommu = amd_iommu_rlookup_table[dev_data->devid];
1722 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001723
1724 /* Update data structures */
1725 dev_data->domain = domain;
1726 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001727 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001728
1729 /* Do reference counting */
1730 domain->dev_iommu[iommu->index] += 1;
1731 domain->dev_cnt += 1;
1732
1733 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001734 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001735}
1736
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001737static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001738{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001739 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001740
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001741 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02001742
Joerg Roedelc4596112009-11-20 14:57:32 +01001743 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001744 dev_data->domain->dev_iommu[iommu->index] -= 1;
1745 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001746
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001747 /* Update data structures */
1748 dev_data->domain = NULL;
1749 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001750 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001751
1752 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001753 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001754}
1755
1756/*
1757 * If a device is not yet associated with a domain, this function does
1758 * assigns it visible for the hardware
1759 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001760static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01001761 struct protection_domain *domain)
1762{
Julia Lawall84fe6c12010-05-27 12:31:51 +02001763 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001764
Joerg Roedel15898bb2009-11-24 15:39:42 +01001765 /* lock domain */
1766 spin_lock(&domain->lock);
1767
Joerg Roedel71f77582011-06-09 19:03:15 +02001768 if (dev_data->alias_data != NULL) {
1769 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001770
Joerg Roedel2b02b092011-06-09 17:48:39 +02001771 /* Some sanity checks */
1772 ret = -EBUSY;
1773 if (alias_data->domain != NULL &&
1774 alias_data->domain != domain)
1775 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001776
Joerg Roedel2b02b092011-06-09 17:48:39 +02001777 if (dev_data->domain != NULL &&
1778 dev_data->domain != domain)
1779 goto out_unlock;
1780
1781 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001782 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001783 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01001784
1785 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001786 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01001787
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001788 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001789 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001790
Joerg Roedel24100052009-11-25 15:59:57 +01001791 atomic_inc(&dev_data->bind);
1792
Julia Lawall84fe6c12010-05-27 12:31:51 +02001793 ret = 0;
1794
1795out_unlock:
1796
Joerg Roedel355bf552008-12-08 12:02:41 +01001797 /* ready */
1798 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001799
Julia Lawall84fe6c12010-05-27 12:31:51 +02001800 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001801}
1802
1803/*
1804 * If a device is not yet associated with a domain, this function does
1805 * assigns it visible for the hardware
1806 */
1807static int attach_device(struct device *dev,
1808 struct protection_domain *domain)
1809{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001810 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001811 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001812 unsigned long flags;
1813 int ret;
1814
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001815 dev_data = get_dev_data(dev);
1816
1817 if (amd_iommu_iotlb_sup && pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
1818 dev_data->ats.enabled = true;
1819 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1820 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001821
Joerg Roedel15898bb2009-11-24 15:39:42 +01001822 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001823 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001824 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1825
1826 /*
1827 * We might boot into a crash-kernel here. The crashed kernel
1828 * left the caches in the IOMMU dirty. So we have to flush
1829 * here to evict all dirty stuff.
1830 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001831 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001832
1833 return ret;
1834}
1835
1836/*
1837 * Removes a device from a protection domain (unlocked)
1838 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001839static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01001840{
Joerg Roedel2ca76272010-01-22 16:45:31 +01001841 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001842 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001843
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001844 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001845
Joerg Roedel2ca76272010-01-22 16:45:31 +01001846 domain = dev_data->domain;
1847
1848 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01001849
Joerg Roedel71f77582011-06-09 19:03:15 +02001850 if (dev_data->alias_data != NULL) {
1851 struct iommu_dev_data *alias_data = dev_data->alias_data;
1852
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001853 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001854 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01001855 }
1856
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001857 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001858 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001859
Joerg Roedel2ca76272010-01-22 16:45:31 +01001860 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001861
Joerg Roedel21129f72009-09-01 11:59:42 +02001862 /*
1863 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001864 * passthrough domain if it is detached from any other domain.
1865 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02001866 */
Joerg Roedel5abcdba2011-12-01 15:49:45 +01001867 if (dev_data->passthrough &&
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001868 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001869 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01001870}
1871
1872/*
1873 * Removes a device from a protection domain (with devtable_lock held)
1874 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01001875static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01001876{
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001877 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01001878 unsigned long flags;
1879
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001880 dev_data = get_dev_data(dev);
1881
Joerg Roedel355bf552008-12-08 12:02:41 +01001882 /* lock device table */
1883 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001884 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01001885 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001886
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001887 if (dev_data->ats.enabled) {
1888 pci_disable_ats(to_pci_dev(dev));
1889 dev_data->ats.enabled = false;
1890 }
Joerg Roedel355bf552008-12-08 12:02:41 +01001891}
Joerg Roedele275a2a2008-12-10 18:27:25 +01001892
Joerg Roedel15898bb2009-11-24 15:39:42 +01001893/*
1894 * Find out the protection domain structure for a given PCI device. This
1895 * will give us the pointer to the page table root for example.
1896 */
1897static struct protection_domain *domain_for_device(struct device *dev)
1898{
Joerg Roedel71f77582011-06-09 19:03:15 +02001899 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02001900 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001901 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001902
Joerg Roedel657cbb62009-11-23 15:26:46 +01001903 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001904
Joerg Roedel2b02b092011-06-09 17:48:39 +02001905 if (dev_data->domain)
1906 return dev_data->domain;
1907
Joerg Roedel71f77582011-06-09 19:03:15 +02001908 if (dev_data->alias_data != NULL) {
1909 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02001910
1911 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
1912 if (alias_data->domain != NULL) {
1913 __attach_device(dev_data, alias_data->domain);
1914 dom = alias_data->domain;
1915 }
1916 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001917 }
1918
Joerg Roedel15898bb2009-11-24 15:39:42 +01001919 return dom;
1920}
1921
Joerg Roedele275a2a2008-12-10 18:27:25 +01001922static int device_change_notifier(struct notifier_block *nb,
1923 unsigned long action, void *data)
1924{
Joerg Roedele275a2a2008-12-10 18:27:25 +01001925 struct dma_ops_domain *dma_domain;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01001926 struct protection_domain *domain;
1927 struct iommu_dev_data *dev_data;
1928 struct device *dev = data;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001929 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001930 unsigned long flags;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01001931 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001932
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001933 if (!check_device(dev))
1934 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001935
Joerg Roedel5abcdba2011-12-01 15:49:45 +01001936 devid = get_device_id(dev);
1937 iommu = amd_iommu_rlookup_table[devid];
1938 dev_data = get_dev_data(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001939
1940 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07001941 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001942
1943 domain = domain_for_device(dev);
1944
Joerg Roedele275a2a2008-12-10 18:27:25 +01001945 if (!domain)
1946 goto out;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01001947 if (dev_data->passthrough)
Joerg Roedela1ca3312009-09-01 12:22:22 +02001948 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001949 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001950 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001951 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001952
1953 iommu_init_device(dev);
1954
1955 domain = domain_for_device(dev);
1956
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001957 /* allocate a protection domain if a device is added */
1958 dma_domain = find_protection_domain(devid);
1959 if (dma_domain)
1960 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01001961 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001962 if (!dma_domain)
1963 goto out;
1964 dma_domain->target_dev = devid;
1965
1966 spin_lock_irqsave(&iommu_pd_list_lock, flags);
1967 list_add_tail(&dma_domain->list, &iommu_pd_list);
1968 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
1969
1970 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001971 case BUS_NOTIFY_DEL_DEVICE:
1972
1973 iommu_uninit_device(dev);
1974
Joerg Roedele275a2a2008-12-10 18:27:25 +01001975 default:
1976 goto out;
1977 }
1978
Joerg Roedele275a2a2008-12-10 18:27:25 +01001979 iommu_completion_wait(iommu);
1980
1981out:
1982 return 0;
1983}
1984
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05301985static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01001986 .notifier_call = device_change_notifier,
1987};
Joerg Roedel355bf552008-12-08 12:02:41 +01001988
Joerg Roedel8638c492009-12-10 11:12:25 +01001989void amd_iommu_init_notifier(void)
1990{
1991 bus_register_notifier(&pci_bus_type, &device_nb);
1992}
1993
Joerg Roedel431b2a22008-07-11 17:14:22 +02001994/*****************************************************************************
1995 *
1996 * The next functions belong to the dma_ops mapping/unmapping code.
1997 *
1998 *****************************************************************************/
1999
2000/*
2001 * In the dma_ops path we only have the struct device. This function
2002 * finds the corresponding IOMMU, the protection domain and the
2003 * requestor id for a given device.
2004 * If the device is not yet associated with a domain this is also done
2005 * in this function.
2006 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002007static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002008{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002009 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002010 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002011 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002012
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002013 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002014 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002015
Joerg Roedel94f6d192009-11-24 16:40:02 +01002016 domain = domain_for_device(dev);
2017 if (domain != NULL && !dma_ops_domain(domain))
2018 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002019
Joerg Roedel94f6d192009-11-24 16:40:02 +01002020 if (domain != NULL)
2021 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002022
Joerg Roedel15898bb2009-11-24 15:39:42 +01002023 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002024 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002025 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01002026 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2027 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002028 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01002029 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01002030
Joerg Roedel94f6d192009-11-24 16:40:02 +01002031 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002032}
2033
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002034static void update_device_table(struct protection_domain *domain)
2035{
Joerg Roedel492667d2009-11-27 13:25:47 +01002036 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002037
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002038 list_for_each_entry(dev_data, &domain->dev_list, list)
2039 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002040}
2041
2042static void update_domain(struct protection_domain *domain)
2043{
2044 if (!domain->updated)
2045 return;
2046
2047 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002048
2049 domain_flush_devices(domain);
2050 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002051
2052 domain->updated = false;
2053}
2054
Joerg Roedel431b2a22008-07-11 17:14:22 +02002055/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002056 * This function fetches the PTE for a given address in the aperture
2057 */
2058static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2059 unsigned long address)
2060{
Joerg Roedel384de722009-05-15 12:30:05 +02002061 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002062 u64 *pte, *pte_page;
2063
Joerg Roedel384de722009-05-15 12:30:05 +02002064 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2065 if (!aperture)
2066 return NULL;
2067
2068 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002069 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002070 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002071 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002072 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2073 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002074 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002075
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002076 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002077
2078 return pte;
2079}
2080
2081/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002082 * This is the generic map function. It maps one 4kb page at paddr to
2083 * the given address in the DMA address space for the domain.
2084 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002085static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002086 unsigned long address,
2087 phys_addr_t paddr,
2088 int direction)
2089{
2090 u64 *pte, __pte;
2091
2092 WARN_ON(address > dom->aperture_size);
2093
2094 paddr &= PAGE_MASK;
2095
Joerg Roedel8bda3092009-05-12 12:02:46 +02002096 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002097 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002098 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002099
2100 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2101
2102 if (direction == DMA_TO_DEVICE)
2103 __pte |= IOMMU_PTE_IR;
2104 else if (direction == DMA_FROM_DEVICE)
2105 __pte |= IOMMU_PTE_IW;
2106 else if (direction == DMA_BIDIRECTIONAL)
2107 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2108
2109 WARN_ON(*pte);
2110
2111 *pte = __pte;
2112
2113 return (dma_addr_t)address;
2114}
2115
Joerg Roedel431b2a22008-07-11 17:14:22 +02002116/*
2117 * The generic unmapping function for on page in the DMA address space.
2118 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002119static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002120 unsigned long address)
2121{
Joerg Roedel384de722009-05-15 12:30:05 +02002122 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002123 u64 *pte;
2124
2125 if (address >= dom->aperture_size)
2126 return;
2127
Joerg Roedel384de722009-05-15 12:30:05 +02002128 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2129 if (!aperture)
2130 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002131
Joerg Roedel384de722009-05-15 12:30:05 +02002132 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2133 if (!pte)
2134 return;
2135
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002136 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002137
2138 WARN_ON(!*pte);
2139
2140 *pte = 0ULL;
2141}
2142
Joerg Roedel431b2a22008-07-11 17:14:22 +02002143/*
2144 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002145 * contiguous memory region into DMA address space. It is used by all
2146 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002147 * Must be called with the domain lock held.
2148 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002149static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002150 struct dma_ops_domain *dma_dom,
2151 phys_addr_t paddr,
2152 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002153 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002154 bool align,
2155 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002156{
2157 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002158 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002159 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002160 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002161 int i;
2162
Joerg Roedele3c449f2008-10-15 22:02:11 -07002163 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002164 paddr &= PAGE_MASK;
2165
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002166 INC_STATS_COUNTER(total_map_requests);
2167
Joerg Roedelc1858972008-12-12 15:42:39 +01002168 if (pages > 1)
2169 INC_STATS_COUNTER(cross_page);
2170
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002171 if (align)
2172 align_mask = (1UL << get_order(size)) - 1;
2173
Joerg Roedel11b83882009-05-19 10:23:15 +02002174retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002175 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2176 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002177 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002178 /*
2179 * setting next_address here will let the address
2180 * allocator only scan the new allocated range in the
2181 * first run. This is a small optimization.
2182 */
2183 dma_dom->next_address = dma_dom->aperture_size;
2184
Joerg Roedel576175c2009-11-23 19:08:46 +01002185 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002186 goto out;
2187
2188 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002189 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002190 * allocation again
2191 */
2192 goto retry;
2193 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002194
2195 start = address;
2196 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002197 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002198 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002199 goto out_unmap;
2200
Joerg Roedelcb76c322008-06-26 21:28:00 +02002201 paddr += PAGE_SIZE;
2202 start += PAGE_SIZE;
2203 }
2204 address += offset;
2205
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002206 ADD_STATS_COUNTER(alloced_io_mem, size);
2207
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002208 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002209 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002210 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002211 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002212 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002213
Joerg Roedelcb76c322008-06-26 21:28:00 +02002214out:
2215 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002216
2217out_unmap:
2218
2219 for (--i; i >= 0; --i) {
2220 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002221 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002222 }
2223
2224 dma_ops_free_addresses(dma_dom, address, pages);
2225
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002226 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002227}
2228
Joerg Roedel431b2a22008-07-11 17:14:22 +02002229/*
2230 * Does the reverse of the __map_single function. Must be called with
2231 * the domain lock held too
2232 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002233static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002234 dma_addr_t dma_addr,
2235 size_t size,
2236 int dir)
2237{
Joerg Roedel04e04632010-09-23 16:12:48 +02002238 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002239 dma_addr_t i, start;
2240 unsigned int pages;
2241
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002242 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002243 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002244 return;
2245
Joerg Roedel04e04632010-09-23 16:12:48 +02002246 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002247 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002248 dma_addr &= PAGE_MASK;
2249 start = dma_addr;
2250
2251 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002252 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002253 start += PAGE_SIZE;
2254 }
2255
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002256 SUB_STATS_COUNTER(alloced_io_mem, size);
2257
Joerg Roedelcb76c322008-06-26 21:28:00 +02002258 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002259
Joerg Roedel80be3082008-11-06 14:59:05 +01002260 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002261 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002262 dma_dom->need_flush = false;
2263 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002264}
2265
Joerg Roedel431b2a22008-07-11 17:14:22 +02002266/*
2267 * The exported map_single function for dma_ops.
2268 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002269static dma_addr_t map_page(struct device *dev, struct page *page,
2270 unsigned long offset, size_t size,
2271 enum dma_data_direction dir,
2272 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002273{
2274 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002275 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002276 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002277 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002278 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002279
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002280 INC_STATS_COUNTER(cnt_map_single);
2281
Joerg Roedel94f6d192009-11-24 16:40:02 +01002282 domain = get_domain(dev);
2283 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002284 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002285 else if (IS_ERR(domain))
2286 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002287
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002288 dma_mask = *dev->dma_mask;
2289
Joerg Roedel4da70b92008-06-26 21:28:01 +02002290 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002291
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002292 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002293 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002294 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002295 goto out;
2296
Joerg Roedel17b124b2011-04-06 18:01:35 +02002297 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002298
2299out:
2300 spin_unlock_irqrestore(&domain->lock, flags);
2301
2302 return addr;
2303}
2304
Joerg Roedel431b2a22008-07-11 17:14:22 +02002305/*
2306 * The exported unmap_single function for dma_ops.
2307 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002308static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2309 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002310{
2311 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002312 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002313
Joerg Roedel146a6912008-12-12 15:07:12 +01002314 INC_STATS_COUNTER(cnt_unmap_single);
2315
Joerg Roedel94f6d192009-11-24 16:40:02 +01002316 domain = get_domain(dev);
2317 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002318 return;
2319
Joerg Roedel4da70b92008-06-26 21:28:01 +02002320 spin_lock_irqsave(&domain->lock, flags);
2321
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002322 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002323
Joerg Roedel17b124b2011-04-06 18:01:35 +02002324 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002325
2326 spin_unlock_irqrestore(&domain->lock, flags);
2327}
2328
Joerg Roedel431b2a22008-07-11 17:14:22 +02002329/*
2330 * This is a special map_sg function which is used if we should map a
2331 * device which is not handled by an AMD IOMMU in the system.
2332 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002333static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2334 int nelems, int dir)
2335{
2336 struct scatterlist *s;
2337 int i;
2338
2339 for_each_sg(sglist, s, nelems, i) {
2340 s->dma_address = (dma_addr_t)sg_phys(s);
2341 s->dma_length = s->length;
2342 }
2343
2344 return nelems;
2345}
2346
Joerg Roedel431b2a22008-07-11 17:14:22 +02002347/*
2348 * The exported map_sg function for dma_ops (handles scatter-gather
2349 * lists).
2350 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002351static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002352 int nelems, enum dma_data_direction dir,
2353 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002354{
2355 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002356 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002357 int i;
2358 struct scatterlist *s;
2359 phys_addr_t paddr;
2360 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002361 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002362
Joerg Roedeld03f0672008-12-12 15:09:48 +01002363 INC_STATS_COUNTER(cnt_map_sg);
2364
Joerg Roedel94f6d192009-11-24 16:40:02 +01002365 domain = get_domain(dev);
2366 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002367 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002368 else if (IS_ERR(domain))
2369 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002370
Joerg Roedel832a90c2008-09-18 15:54:23 +02002371 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002372
Joerg Roedel65b050a2008-06-26 21:28:02 +02002373 spin_lock_irqsave(&domain->lock, flags);
2374
2375 for_each_sg(sglist, s, nelems, i) {
2376 paddr = sg_phys(s);
2377
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002378 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002379 paddr, s->length, dir, false,
2380 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002381
2382 if (s->dma_address) {
2383 s->dma_length = s->length;
2384 mapped_elems++;
2385 } else
2386 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002387 }
2388
Joerg Roedel17b124b2011-04-06 18:01:35 +02002389 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002390
2391out:
2392 spin_unlock_irqrestore(&domain->lock, flags);
2393
2394 return mapped_elems;
2395unmap:
2396 for_each_sg(sglist, s, mapped_elems, i) {
2397 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002398 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002399 s->dma_length, dir);
2400 s->dma_address = s->dma_length = 0;
2401 }
2402
2403 mapped_elems = 0;
2404
2405 goto out;
2406}
2407
Joerg Roedel431b2a22008-07-11 17:14:22 +02002408/*
2409 * The exported map_sg function for dma_ops (handles scatter-gather
2410 * lists).
2411 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002412static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002413 int nelems, enum dma_data_direction dir,
2414 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002415{
2416 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002417 struct protection_domain *domain;
2418 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002419 int i;
2420
Joerg Roedel55877a62008-12-12 15:12:14 +01002421 INC_STATS_COUNTER(cnt_unmap_sg);
2422
Joerg Roedel94f6d192009-11-24 16:40:02 +01002423 domain = get_domain(dev);
2424 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002425 return;
2426
Joerg Roedel65b050a2008-06-26 21:28:02 +02002427 spin_lock_irqsave(&domain->lock, flags);
2428
2429 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002430 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002431 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002432 s->dma_address = s->dma_length = 0;
2433 }
2434
Joerg Roedel17b124b2011-04-06 18:01:35 +02002435 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002436
2437 spin_unlock_irqrestore(&domain->lock, flags);
2438}
2439
Joerg Roedel431b2a22008-07-11 17:14:22 +02002440/*
2441 * The exported alloc_coherent function for dma_ops.
2442 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002443static void *alloc_coherent(struct device *dev, size_t size,
2444 dma_addr_t *dma_addr, gfp_t flag)
2445{
2446 unsigned long flags;
2447 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002448 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002449 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002450 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002451
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002452 INC_STATS_COUNTER(cnt_alloc_coherent);
2453
Joerg Roedel94f6d192009-11-24 16:40:02 +01002454 domain = get_domain(dev);
2455 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002456 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2457 *dma_addr = __pa(virt_addr);
2458 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002459 } else if (IS_ERR(domain))
2460 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002461
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002462 dma_mask = dev->coherent_dma_mask;
2463 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2464 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002465
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002466 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2467 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302468 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002469
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002470 paddr = virt_to_phys(virt_addr);
2471
Joerg Roedel832a90c2008-09-18 15:54:23 +02002472 if (!dma_mask)
2473 dma_mask = *dev->dma_mask;
2474
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002475 spin_lock_irqsave(&domain->lock, flags);
2476
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002477 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002478 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002479
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002480 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002481 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002482 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002483 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002484
Joerg Roedel17b124b2011-04-06 18:01:35 +02002485 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002486
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002487 spin_unlock_irqrestore(&domain->lock, flags);
2488
2489 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002490
2491out_free:
2492
2493 free_pages((unsigned long)virt_addr, get_order(size));
2494
2495 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002496}
2497
Joerg Roedel431b2a22008-07-11 17:14:22 +02002498/*
2499 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002500 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002501static void free_coherent(struct device *dev, size_t size,
2502 void *virt_addr, dma_addr_t dma_addr)
2503{
2504 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002505 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002506
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002507 INC_STATS_COUNTER(cnt_free_coherent);
2508
Joerg Roedel94f6d192009-11-24 16:40:02 +01002509 domain = get_domain(dev);
2510 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002511 goto free_mem;
2512
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002513 spin_lock_irqsave(&domain->lock, flags);
2514
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002515 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002516
Joerg Roedel17b124b2011-04-06 18:01:35 +02002517 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002518
2519 spin_unlock_irqrestore(&domain->lock, flags);
2520
2521free_mem:
2522 free_pages((unsigned long)virt_addr, get_order(size));
2523}
2524
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002525/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002526 * This function is called by the DMA layer to find out if we can handle a
2527 * particular device. It is part of the dma_ops.
2528 */
2529static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2530{
Joerg Roedel420aef82009-11-23 16:14:57 +01002531 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002532}
2533
2534/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002535 * The function for pre-allocating protection domains.
2536 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002537 * If the driver core informs the DMA layer if a driver grabs a device
2538 * we don't need to preallocate the protection domains anymore.
2539 * For now we have to.
2540 */
Jaswinder Singh Rajput0e93dd82008-12-29 21:45:22 +05302541static void prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002542{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002543 struct iommu_dev_data *dev_data;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002544 struct dma_ops_domain *dma_dom;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002545 struct pci_dev *dev = NULL;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002546 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002547
Chris Wrightd18c69d2010-04-02 18:27:55 -07002548 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002549
2550 /* Do we handle this device? */
2551 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002552 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002553
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002554 dev_data = get_dev_data(&dev->dev);
2555 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
2556 /* Make sure passthrough domain is allocated */
2557 alloc_passthrough_domain();
2558 dev_data->passthrough = true;
2559 attach_device(&dev->dev, pt_domain);
2560 pr_info("AMD-Vi: Using passthough domain for device %s\n",
2561 dev_name(&dev->dev));
2562 }
2563
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002564 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002565 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002566 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002567
2568 devid = get_device_id(&dev->dev);
2569
Joerg Roedel87a64d52009-11-24 17:26:43 +01002570 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002571 if (!dma_dom)
2572 continue;
2573 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002574 dma_dom->target_dev = devid;
2575
Joerg Roedel15898bb2009-11-24 15:39:42 +01002576 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01002577
Joerg Roedelbd60b732008-09-11 10:24:48 +02002578 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002579 }
2580}
2581
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002582static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002583 .alloc_coherent = alloc_coherent,
2584 .free_coherent = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002585 .map_page = map_page,
2586 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002587 .map_sg = map_sg,
2588 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002589 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002590};
2591
Joerg Roedel27c21272011-05-30 15:56:24 +02002592static unsigned device_dma_ops_init(void)
2593{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002594 struct iommu_dev_data *dev_data;
Joerg Roedel27c21272011-05-30 15:56:24 +02002595 struct pci_dev *pdev = NULL;
2596 unsigned unhandled = 0;
2597
2598 for_each_pci_dev(pdev) {
2599 if (!check_device(&pdev->dev)) {
2600 unhandled += 1;
2601 continue;
2602 }
2603
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002604 dev_data = get_dev_data(&pdev->dev);
2605
2606 if (!dev_data->passthrough)
2607 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
2608 else
2609 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
Joerg Roedel27c21272011-05-30 15:56:24 +02002610 }
2611
2612 return unhandled;
2613}
2614
Joerg Roedel431b2a22008-07-11 17:14:22 +02002615/*
2616 * The function which clues the AMD IOMMU driver into dma_ops.
2617 */
Joerg Roedelf5325092010-01-22 17:44:35 +01002618
2619void __init amd_iommu_init_api(void)
2620{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02002621 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01002622}
2623
Joerg Roedel6631ee92008-06-26 21:28:05 +02002624int __init amd_iommu_init_dma_ops(void)
2625{
2626 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02002627 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002628
Joerg Roedel431b2a22008-07-11 17:14:22 +02002629 /*
2630 * first allocate a default protection domain for every IOMMU we
2631 * found in the system. Devices not assigned to any other
2632 * protection domain will be assigned to the default one.
2633 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02002634 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01002635 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002636 if (iommu->default_dom == NULL)
2637 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01002638 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002639 ret = iommu_init_unity_mappings(iommu);
2640 if (ret)
2641 goto free_domains;
2642 }
2643
Joerg Roedel431b2a22008-07-11 17:14:22 +02002644 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01002645 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002646 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01002647 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002648
2649 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002650 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002651
Joerg Roedel431b2a22008-07-11 17:14:22 +02002652 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02002653 unhandled = device_dma_ops_init();
2654 if (unhandled && max_pfn > MAX_DMA32_PFN) {
2655 /* There are unhandled devices - initialize swiotlb for them */
2656 swiotlb = 1;
2657 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02002658
Joerg Roedel7f265082008-12-12 13:50:21 +01002659 amd_iommu_stats_init();
2660
Joerg Roedel6631ee92008-06-26 21:28:05 +02002661 return 0;
2662
2663free_domains:
2664
Joerg Roedel3bd22172009-05-04 15:06:20 +02002665 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002666 if (iommu->default_dom)
2667 dma_ops_domain_free(iommu->default_dom);
2668 }
2669
2670 return ret;
2671}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002672
2673/*****************************************************************************
2674 *
2675 * The following functions belong to the exported interface of AMD IOMMU
2676 *
2677 * This interface allows access to lower level functions of the IOMMU
2678 * like protection domain handling and assignement of devices to domains
2679 * which is not possible with the dma_ops interface.
2680 *
2681 *****************************************************************************/
2682
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002683static void cleanup_domain(struct protection_domain *domain)
2684{
Joerg Roedel492667d2009-11-27 13:25:47 +01002685 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002686 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002687
2688 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2689
Joerg Roedel492667d2009-11-27 13:25:47 +01002690 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002691 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01002692 atomic_set(&dev_data->bind, 0);
2693 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002694
2695 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2696}
2697
Joerg Roedel26508152009-08-26 16:52:40 +02002698static void protection_domain_free(struct protection_domain *domain)
2699{
2700 if (!domain)
2701 return;
2702
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002703 del_domain_from_list(domain);
2704
Joerg Roedel26508152009-08-26 16:52:40 +02002705 if (domain->id)
2706 domain_id_free(domain->id);
2707
2708 kfree(domain);
2709}
2710
2711static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002712{
2713 struct protection_domain *domain;
2714
2715 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2716 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002717 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002718
2719 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002720 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01002721 domain->id = domain_id_alloc();
2722 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02002723 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002724 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02002725
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002726 add_domain_to_list(domain);
2727
Joerg Roedel26508152009-08-26 16:52:40 +02002728 return domain;
2729
2730out_err:
2731 kfree(domain);
2732
2733 return NULL;
2734}
2735
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002736static int __init alloc_passthrough_domain(void)
2737{
2738 if (pt_domain != NULL)
2739 return 0;
2740
2741 /* allocate passthrough domain */
2742 pt_domain = protection_domain_alloc();
2743 if (!pt_domain)
2744 return -ENOMEM;
2745
2746 pt_domain->mode = PAGE_MODE_NONE;
2747
2748 return 0;
2749}
Joerg Roedel26508152009-08-26 16:52:40 +02002750static int amd_iommu_domain_init(struct iommu_domain *dom)
2751{
2752 struct protection_domain *domain;
2753
2754 domain = protection_domain_alloc();
2755 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01002756 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02002757
2758 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002759 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2760 if (!domain->pt_root)
2761 goto out_free;
2762
2763 dom->priv = domain;
2764
2765 return 0;
2766
2767out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02002768 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01002769
2770 return -ENOMEM;
2771}
2772
Joerg Roedel98383fc2008-12-02 18:34:12 +01002773static void amd_iommu_domain_destroy(struct iommu_domain *dom)
2774{
2775 struct protection_domain *domain = dom->priv;
2776
2777 if (!domain)
2778 return;
2779
2780 if (domain->dev_cnt > 0)
2781 cleanup_domain(domain);
2782
2783 BUG_ON(domain->dev_cnt != 0);
2784
2785 free_pagetable(domain);
2786
Joerg Roedel8b408fe2010-03-08 14:20:07 +01002787 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002788
2789 dom->priv = NULL;
2790}
2791
Joerg Roedel684f2882008-12-08 12:07:44 +01002792static void amd_iommu_detach_device(struct iommu_domain *dom,
2793 struct device *dev)
2794{
Joerg Roedel657cbb62009-11-23 15:26:46 +01002795 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002796 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002797 u16 devid;
2798
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002799 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01002800 return;
2801
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002802 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002803
Joerg Roedel657cbb62009-11-23 15:26:46 +01002804 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002805 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002806
2807 iommu = amd_iommu_rlookup_table[devid];
2808 if (!iommu)
2809 return;
2810
Joerg Roedel684f2882008-12-08 12:07:44 +01002811 iommu_completion_wait(iommu);
2812}
2813
Joerg Roedel01106062008-12-02 19:34:11 +01002814static int amd_iommu_attach_device(struct iommu_domain *dom,
2815 struct device *dev)
2816{
2817 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002818 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01002819 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002820 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002821
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002822 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01002823 return -EINVAL;
2824
Joerg Roedel657cbb62009-11-23 15:26:46 +01002825 dev_data = dev->archdata.iommu;
2826
Joerg Roedelf62dda62011-06-09 12:55:35 +02002827 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01002828 if (!iommu)
2829 return -EINVAL;
2830
Joerg Roedel657cbb62009-11-23 15:26:46 +01002831 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002832 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002833
Joerg Roedel15898bb2009-11-24 15:39:42 +01002834 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01002835
2836 iommu_completion_wait(iommu);
2837
Joerg Roedel15898bb2009-11-24 15:39:42 +01002838 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002839}
2840
Joerg Roedel468e2362010-01-21 16:37:36 +01002841static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
2842 phys_addr_t paddr, int gfp_order, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002843{
Joerg Roedel468e2362010-01-21 16:37:36 +01002844 unsigned long page_size = 0x1000UL << gfp_order;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002845 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002846 int prot = 0;
2847 int ret;
2848
2849 if (iommu_prot & IOMMU_READ)
2850 prot |= IOMMU_PROT_IR;
2851 if (iommu_prot & IOMMU_WRITE)
2852 prot |= IOMMU_PROT_IW;
2853
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002854 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002855 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002856 mutex_unlock(&domain->api_lock);
2857
Joerg Roedel795e74f72010-05-11 17:40:57 +02002858 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002859}
2860
Joerg Roedel468e2362010-01-21 16:37:36 +01002861static int amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2862 int gfp_order)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002863{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002864 struct protection_domain *domain = dom->priv;
Joerg Roedel468e2362010-01-21 16:37:36 +01002865 unsigned long page_size, unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002866
Joerg Roedel468e2362010-01-21 16:37:36 +01002867 page_size = 0x1000UL << gfp_order;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002868
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002869 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01002870 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002871 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002872
Joerg Roedel17b124b2011-04-06 18:01:35 +02002873 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002874
Joerg Roedel468e2362010-01-21 16:37:36 +01002875 return get_order(unmap_size);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002876}
2877
Joerg Roedel645c4c82008-12-02 20:05:50 +01002878static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2879 unsigned long iova)
2880{
2881 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002882 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002883 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002884 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002885
Joerg Roedel24cd7722010-01-19 17:27:39 +01002886 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002887
Joerg Roedela6d41a42009-09-02 17:08:55 +02002888 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01002889 return 0;
2890
Joerg Roedelf03152b2010-01-21 16:15:24 +01002891 if (PM_PTE_LEVEL(*pte) == 0)
2892 offset_mask = PAGE_SIZE - 1;
2893 else
2894 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
2895
2896 __pte = *pte & PM_ADDR_MASK;
2897 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002898
2899 return paddr;
2900}
2901
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002902static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
2903 unsigned long cap)
2904{
Joerg Roedel80a506b2010-07-27 17:14:24 +02002905 switch (cap) {
2906 case IOMMU_CAP_CACHE_COHERENCY:
2907 return 1;
2908 }
2909
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002910 return 0;
2911}
2912
Joerg Roedel26961ef2008-12-03 17:00:17 +01002913static struct iommu_ops amd_iommu_ops = {
2914 .domain_init = amd_iommu_domain_init,
2915 .domain_destroy = amd_iommu_domain_destroy,
2916 .attach_dev = amd_iommu_attach_device,
2917 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01002918 .map = amd_iommu_map,
2919 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002920 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002921 .domain_has_cap = amd_iommu_domain_has_cap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002922};
2923
Joerg Roedel0feae532009-08-26 15:26:30 +02002924/*****************************************************************************
2925 *
2926 * The next functions do a basic initialization of IOMMU for pass through
2927 * mode
2928 *
2929 * In passthrough mode the IOMMU is initialized and enabled but not used for
2930 * DMA-API translation.
2931 *
2932 *****************************************************************************/
2933
2934int __init amd_iommu_init_passthrough(void)
2935{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002936 struct iommu_dev_data *dev_data;
Joerg Roedel0feae532009-08-26 15:26:30 +02002937 struct pci_dev *dev = NULL;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002938 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002939 u16 devid;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002940 int ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02002941
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002942 ret = alloc_passthrough_domain();
2943 if (ret)
2944 return ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02002945
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04002946 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002947 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02002948 continue;
2949
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002950 dev_data = get_dev_data(&dev->dev);
2951 dev_data->passthrough = true;
2952
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002953 devid = get_device_id(&dev->dev);
2954
Joerg Roedel15898bb2009-11-24 15:39:42 +01002955 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02002956 if (!iommu)
2957 continue;
2958
Joerg Roedel15898bb2009-11-24 15:39:42 +01002959 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02002960 }
2961
2962 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
2963
2964 return 0;
2965}
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01002966
2967/* IOMMUv2 specific functions */
2968int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
2969{
2970 return atomic_notifier_chain_register(&ppr_notifier, nb);
2971}
2972EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
2973
2974int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
2975{
2976 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
2977}
2978EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);