blob: 6ed53676910285851195d3eaf4c3b5cd4a9485f7 [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 Roedeld03f067a2008-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 Roedeld03f067a2008-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 Roedel132bd682011-11-17 14:18:46 +01001687 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001688 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001689
Joerg Roedel132bd682011-11-17 14:18:46 +01001690 if (domain->mode != PAGE_MODE_NONE)
1691 pte_root = virt_to_phys(domain->pt_root);
1692
Joerg Roedel38ddf412008-09-11 10:38:32 +02001693 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1694 << DEV_ENTRY_MODE_SHIFT;
1695 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001696
Joerg Roedelee6c2862011-11-09 12:06:03 +01001697 flags = amd_iommu_dev_table[devid].data[1];
1698
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001699 if (ats)
1700 flags |= DTE_FLAG_IOTLB;
1701
Joerg Roedelee6c2862011-11-09 12:06:03 +01001702 flags &= ~(0xffffUL);
1703 flags |= domain->id;
1704
1705 amd_iommu_dev_table[devid].data[1] = flags;
1706 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001707}
1708
Joerg Roedel15898bb2009-11-24 15:39:42 +01001709static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001710{
Joerg Roedel355bf552008-12-08 12:02:41 +01001711 /* remove entry from the device table seen by the hardware */
1712 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1713 amd_iommu_dev_table[devid].data[1] = 0;
Joerg Roedel355bf552008-12-08 12:02:41 +01001714
Joerg Roedelc5cca142009-10-09 18:31:20 +02001715 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001716}
1717
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001718static void do_attach(struct iommu_dev_data *dev_data,
1719 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001720{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001721 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001722 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001723
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001724 iommu = amd_iommu_rlookup_table[dev_data->devid];
1725 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001726
1727 /* Update data structures */
1728 dev_data->domain = domain;
1729 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001730 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001731
1732 /* Do reference counting */
1733 domain->dev_iommu[iommu->index] += 1;
1734 domain->dev_cnt += 1;
1735
1736 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001737 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001738}
1739
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001740static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001741{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001742 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001743
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001744 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02001745
Joerg Roedelc4596112009-11-20 14:57:32 +01001746 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001747 dev_data->domain->dev_iommu[iommu->index] -= 1;
1748 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001749
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001750 /* Update data structures */
1751 dev_data->domain = NULL;
1752 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001753 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001754
1755 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001756 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001757}
1758
1759/*
1760 * If a device is not yet associated with a domain, this function does
1761 * assigns it visible for the hardware
1762 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001763static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01001764 struct protection_domain *domain)
1765{
Julia Lawall84fe6c12010-05-27 12:31:51 +02001766 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001767
Joerg Roedel15898bb2009-11-24 15:39:42 +01001768 /* lock domain */
1769 spin_lock(&domain->lock);
1770
Joerg Roedel71f77582011-06-09 19:03:15 +02001771 if (dev_data->alias_data != NULL) {
1772 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001773
Joerg Roedel2b02b092011-06-09 17:48:39 +02001774 /* Some sanity checks */
1775 ret = -EBUSY;
1776 if (alias_data->domain != NULL &&
1777 alias_data->domain != domain)
1778 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001779
Joerg Roedel2b02b092011-06-09 17:48:39 +02001780 if (dev_data->domain != NULL &&
1781 dev_data->domain != domain)
1782 goto out_unlock;
1783
1784 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001785 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001786 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01001787
1788 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001789 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01001790
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001791 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001792 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001793
Joerg Roedel24100052009-11-25 15:59:57 +01001794 atomic_inc(&dev_data->bind);
1795
Julia Lawall84fe6c12010-05-27 12:31:51 +02001796 ret = 0;
1797
1798out_unlock:
1799
Joerg Roedel355bf552008-12-08 12:02:41 +01001800 /* ready */
1801 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001802
Julia Lawall84fe6c12010-05-27 12:31:51 +02001803 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001804}
1805
1806/*
1807 * If a device is not yet associated with a domain, this function does
1808 * assigns it visible for the hardware
1809 */
1810static int attach_device(struct device *dev,
1811 struct protection_domain *domain)
1812{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001813 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001814 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001815 unsigned long flags;
1816 int ret;
1817
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001818 dev_data = get_dev_data(dev);
1819
1820 if (amd_iommu_iotlb_sup && pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
1821 dev_data->ats.enabled = true;
1822 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1823 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001824
Joerg Roedel15898bb2009-11-24 15:39:42 +01001825 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001826 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001827 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1828
1829 /*
1830 * We might boot into a crash-kernel here. The crashed kernel
1831 * left the caches in the IOMMU dirty. So we have to flush
1832 * here to evict all dirty stuff.
1833 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001834 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001835
1836 return ret;
1837}
1838
1839/*
1840 * Removes a device from a protection domain (unlocked)
1841 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001842static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01001843{
Joerg Roedel2ca76272010-01-22 16:45:31 +01001844 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001845 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001846
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001847 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001848
Joerg Roedel2ca76272010-01-22 16:45:31 +01001849 domain = dev_data->domain;
1850
1851 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01001852
Joerg Roedel71f77582011-06-09 19:03:15 +02001853 if (dev_data->alias_data != NULL) {
1854 struct iommu_dev_data *alias_data = dev_data->alias_data;
1855
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001856 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001857 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01001858 }
1859
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001860 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001861 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001862
Joerg Roedel2ca76272010-01-22 16:45:31 +01001863 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001864
Joerg Roedel21129f72009-09-01 11:59:42 +02001865 /*
1866 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001867 * passthrough domain if it is detached from any other domain.
1868 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02001869 */
Joerg Roedel5abcdba2011-12-01 15:49:45 +01001870 if (dev_data->passthrough &&
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001871 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001872 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01001873}
1874
1875/*
1876 * Removes a device from a protection domain (with devtable_lock held)
1877 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01001878static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01001879{
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001880 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01001881 unsigned long flags;
1882
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001883 dev_data = get_dev_data(dev);
1884
Joerg Roedel355bf552008-12-08 12:02:41 +01001885 /* lock device table */
1886 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001887 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01001888 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001889
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001890 if (dev_data->ats.enabled) {
1891 pci_disable_ats(to_pci_dev(dev));
1892 dev_data->ats.enabled = false;
1893 }
Joerg Roedel355bf552008-12-08 12:02:41 +01001894}
Joerg Roedele275a2a2008-12-10 18:27:25 +01001895
Joerg Roedel15898bb2009-11-24 15:39:42 +01001896/*
1897 * Find out the protection domain structure for a given PCI device. This
1898 * will give us the pointer to the page table root for example.
1899 */
1900static struct protection_domain *domain_for_device(struct device *dev)
1901{
Joerg Roedel71f77582011-06-09 19:03:15 +02001902 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02001903 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001904 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001905
Joerg Roedel657cbb62009-11-23 15:26:46 +01001906 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001907
Joerg Roedel2b02b092011-06-09 17:48:39 +02001908 if (dev_data->domain)
1909 return dev_data->domain;
1910
Joerg Roedel71f77582011-06-09 19:03:15 +02001911 if (dev_data->alias_data != NULL) {
1912 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02001913
1914 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
1915 if (alias_data->domain != NULL) {
1916 __attach_device(dev_data, alias_data->domain);
1917 dom = alias_data->domain;
1918 }
1919 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001920 }
1921
Joerg Roedel15898bb2009-11-24 15:39:42 +01001922 return dom;
1923}
1924
Joerg Roedele275a2a2008-12-10 18:27:25 +01001925static int device_change_notifier(struct notifier_block *nb,
1926 unsigned long action, void *data)
1927{
Joerg Roedele275a2a2008-12-10 18:27:25 +01001928 struct dma_ops_domain *dma_domain;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01001929 struct protection_domain *domain;
1930 struct iommu_dev_data *dev_data;
1931 struct device *dev = data;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001932 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001933 unsigned long flags;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01001934 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001935
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001936 if (!check_device(dev))
1937 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001938
Joerg Roedel5abcdba2011-12-01 15:49:45 +01001939 devid = get_device_id(dev);
1940 iommu = amd_iommu_rlookup_table[devid];
1941 dev_data = get_dev_data(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001942
1943 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07001944 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001945
1946 domain = domain_for_device(dev);
1947
Joerg Roedele275a2a2008-12-10 18:27:25 +01001948 if (!domain)
1949 goto out;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01001950 if (dev_data->passthrough)
Joerg Roedela1ca3312009-09-01 12:22:22 +02001951 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001952 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001953 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001954 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001955
1956 iommu_init_device(dev);
1957
1958 domain = domain_for_device(dev);
1959
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001960 /* allocate a protection domain if a device is added */
1961 dma_domain = find_protection_domain(devid);
1962 if (dma_domain)
1963 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01001964 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001965 if (!dma_domain)
1966 goto out;
1967 dma_domain->target_dev = devid;
1968
1969 spin_lock_irqsave(&iommu_pd_list_lock, flags);
1970 list_add_tail(&dma_domain->list, &iommu_pd_list);
1971 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
1972
1973 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001974 case BUS_NOTIFY_DEL_DEVICE:
1975
1976 iommu_uninit_device(dev);
1977
Joerg Roedele275a2a2008-12-10 18:27:25 +01001978 default:
1979 goto out;
1980 }
1981
Joerg Roedele275a2a2008-12-10 18:27:25 +01001982 iommu_completion_wait(iommu);
1983
1984out:
1985 return 0;
1986}
1987
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05301988static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01001989 .notifier_call = device_change_notifier,
1990};
Joerg Roedel355bf552008-12-08 12:02:41 +01001991
Joerg Roedel8638c492009-12-10 11:12:25 +01001992void amd_iommu_init_notifier(void)
1993{
1994 bus_register_notifier(&pci_bus_type, &device_nb);
1995}
1996
Joerg Roedel431b2a22008-07-11 17:14:22 +02001997/*****************************************************************************
1998 *
1999 * The next functions belong to the dma_ops mapping/unmapping code.
2000 *
2001 *****************************************************************************/
2002
2003/*
2004 * In the dma_ops path we only have the struct device. This function
2005 * finds the corresponding IOMMU, the protection domain and the
2006 * requestor id for a given device.
2007 * If the device is not yet associated with a domain this is also done
2008 * in this function.
2009 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002010static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002011{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002012 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002013 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002014 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002015
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002016 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002017 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002018
Joerg Roedel94f6d192009-11-24 16:40:02 +01002019 domain = domain_for_device(dev);
2020 if (domain != NULL && !dma_ops_domain(domain))
2021 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002022
Joerg Roedel94f6d192009-11-24 16:40:02 +01002023 if (domain != NULL)
2024 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002025
Joerg Roedel15898bb2009-11-24 15:39:42 +01002026 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002027 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002028 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01002029 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2030 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002031 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01002032 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01002033
Joerg Roedel94f6d192009-11-24 16:40:02 +01002034 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002035}
2036
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002037static void update_device_table(struct protection_domain *domain)
2038{
Joerg Roedel492667d2009-11-27 13:25:47 +01002039 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002040
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002041 list_for_each_entry(dev_data, &domain->dev_list, list)
2042 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002043}
2044
2045static void update_domain(struct protection_domain *domain)
2046{
2047 if (!domain->updated)
2048 return;
2049
2050 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002051
2052 domain_flush_devices(domain);
2053 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002054
2055 domain->updated = false;
2056}
2057
Joerg Roedel431b2a22008-07-11 17:14:22 +02002058/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002059 * This function fetches the PTE for a given address in the aperture
2060 */
2061static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2062 unsigned long address)
2063{
Joerg Roedel384de722009-05-15 12:30:05 +02002064 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002065 u64 *pte, *pte_page;
2066
Joerg Roedel384de722009-05-15 12:30:05 +02002067 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2068 if (!aperture)
2069 return NULL;
2070
2071 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002072 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002073 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002074 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002075 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2076 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002077 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002078
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002079 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002080
2081 return pte;
2082}
2083
2084/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002085 * This is the generic map function. It maps one 4kb page at paddr to
2086 * the given address in the DMA address space for the domain.
2087 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002088static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002089 unsigned long address,
2090 phys_addr_t paddr,
2091 int direction)
2092{
2093 u64 *pte, __pte;
2094
2095 WARN_ON(address > dom->aperture_size);
2096
2097 paddr &= PAGE_MASK;
2098
Joerg Roedel8bda3092009-05-12 12:02:46 +02002099 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002100 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002101 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002102
2103 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2104
2105 if (direction == DMA_TO_DEVICE)
2106 __pte |= IOMMU_PTE_IR;
2107 else if (direction == DMA_FROM_DEVICE)
2108 __pte |= IOMMU_PTE_IW;
2109 else if (direction == DMA_BIDIRECTIONAL)
2110 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2111
2112 WARN_ON(*pte);
2113
2114 *pte = __pte;
2115
2116 return (dma_addr_t)address;
2117}
2118
Joerg Roedel431b2a22008-07-11 17:14:22 +02002119/*
2120 * The generic unmapping function for on page in the DMA address space.
2121 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002122static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002123 unsigned long address)
2124{
Joerg Roedel384de722009-05-15 12:30:05 +02002125 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002126 u64 *pte;
2127
2128 if (address >= dom->aperture_size)
2129 return;
2130
Joerg Roedel384de722009-05-15 12:30:05 +02002131 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2132 if (!aperture)
2133 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002134
Joerg Roedel384de722009-05-15 12:30:05 +02002135 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2136 if (!pte)
2137 return;
2138
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002139 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002140
2141 WARN_ON(!*pte);
2142
2143 *pte = 0ULL;
2144}
2145
Joerg Roedel431b2a22008-07-11 17:14:22 +02002146/*
2147 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002148 * contiguous memory region into DMA address space. It is used by all
2149 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002150 * Must be called with the domain lock held.
2151 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002152static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002153 struct dma_ops_domain *dma_dom,
2154 phys_addr_t paddr,
2155 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002156 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002157 bool align,
2158 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002159{
2160 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002161 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002162 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002163 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002164 int i;
2165
Joerg Roedele3c449f2008-10-15 22:02:11 -07002166 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002167 paddr &= PAGE_MASK;
2168
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002169 INC_STATS_COUNTER(total_map_requests);
2170
Joerg Roedelc1858972008-12-12 15:42:39 +01002171 if (pages > 1)
2172 INC_STATS_COUNTER(cross_page);
2173
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002174 if (align)
2175 align_mask = (1UL << get_order(size)) - 1;
2176
Joerg Roedel11b83882009-05-19 10:23:15 +02002177retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002178 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2179 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002180 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002181 /*
2182 * setting next_address here will let the address
2183 * allocator only scan the new allocated range in the
2184 * first run. This is a small optimization.
2185 */
2186 dma_dom->next_address = dma_dom->aperture_size;
2187
Joerg Roedel576175c2009-11-23 19:08:46 +01002188 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002189 goto out;
2190
2191 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002192 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002193 * allocation again
2194 */
2195 goto retry;
2196 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002197
2198 start = address;
2199 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002200 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002201 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002202 goto out_unmap;
2203
Joerg Roedelcb76c322008-06-26 21:28:00 +02002204 paddr += PAGE_SIZE;
2205 start += PAGE_SIZE;
2206 }
2207 address += offset;
2208
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002209 ADD_STATS_COUNTER(alloced_io_mem, size);
2210
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002211 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002212 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002213 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002214 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002215 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002216
Joerg Roedelcb76c322008-06-26 21:28:00 +02002217out:
2218 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002219
2220out_unmap:
2221
2222 for (--i; i >= 0; --i) {
2223 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002224 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002225 }
2226
2227 dma_ops_free_addresses(dma_dom, address, pages);
2228
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002229 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002230}
2231
Joerg Roedel431b2a22008-07-11 17:14:22 +02002232/*
2233 * Does the reverse of the __map_single function. Must be called with
2234 * the domain lock held too
2235 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002236static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002237 dma_addr_t dma_addr,
2238 size_t size,
2239 int dir)
2240{
Joerg Roedel04e04632010-09-23 16:12:48 +02002241 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002242 dma_addr_t i, start;
2243 unsigned int pages;
2244
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002245 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002246 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002247 return;
2248
Joerg Roedel04e04632010-09-23 16:12:48 +02002249 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002250 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002251 dma_addr &= PAGE_MASK;
2252 start = dma_addr;
2253
2254 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002255 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002256 start += PAGE_SIZE;
2257 }
2258
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002259 SUB_STATS_COUNTER(alloced_io_mem, size);
2260
Joerg Roedelcb76c322008-06-26 21:28:00 +02002261 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002262
Joerg Roedel80be3082008-11-06 14:59:05 +01002263 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002264 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002265 dma_dom->need_flush = false;
2266 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002267}
2268
Joerg Roedel431b2a22008-07-11 17:14:22 +02002269/*
2270 * The exported map_single function for dma_ops.
2271 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002272static dma_addr_t map_page(struct device *dev, struct page *page,
2273 unsigned long offset, size_t size,
2274 enum dma_data_direction dir,
2275 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002276{
2277 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002278 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002279 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002280 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002281 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002282
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002283 INC_STATS_COUNTER(cnt_map_single);
2284
Joerg Roedel94f6d192009-11-24 16:40:02 +01002285 domain = get_domain(dev);
2286 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002287 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002288 else if (IS_ERR(domain))
2289 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002290
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002291 dma_mask = *dev->dma_mask;
2292
Joerg Roedel4da70b92008-06-26 21:28:01 +02002293 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002294
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002295 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002296 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002297 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002298 goto out;
2299
Joerg Roedel17b124b2011-04-06 18:01:35 +02002300 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002301
2302out:
2303 spin_unlock_irqrestore(&domain->lock, flags);
2304
2305 return addr;
2306}
2307
Joerg Roedel431b2a22008-07-11 17:14:22 +02002308/*
2309 * The exported unmap_single function for dma_ops.
2310 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002311static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2312 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002313{
2314 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002315 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002316
Joerg Roedel146a6912008-12-12 15:07:12 +01002317 INC_STATS_COUNTER(cnt_unmap_single);
2318
Joerg Roedel94f6d192009-11-24 16:40:02 +01002319 domain = get_domain(dev);
2320 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002321 return;
2322
Joerg Roedel4da70b92008-06-26 21:28:01 +02002323 spin_lock_irqsave(&domain->lock, flags);
2324
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002325 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002326
Joerg Roedel17b124b2011-04-06 18:01:35 +02002327 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002328
2329 spin_unlock_irqrestore(&domain->lock, flags);
2330}
2331
Joerg Roedel431b2a22008-07-11 17:14:22 +02002332/*
2333 * This is a special map_sg function which is used if we should map a
2334 * device which is not handled by an AMD IOMMU in the system.
2335 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002336static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2337 int nelems, int dir)
2338{
2339 struct scatterlist *s;
2340 int i;
2341
2342 for_each_sg(sglist, s, nelems, i) {
2343 s->dma_address = (dma_addr_t)sg_phys(s);
2344 s->dma_length = s->length;
2345 }
2346
2347 return nelems;
2348}
2349
Joerg Roedel431b2a22008-07-11 17:14:22 +02002350/*
2351 * The exported map_sg function for dma_ops (handles scatter-gather
2352 * lists).
2353 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002354static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002355 int nelems, enum dma_data_direction dir,
2356 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002357{
2358 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002359 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002360 int i;
2361 struct scatterlist *s;
2362 phys_addr_t paddr;
2363 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002364 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002365
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002366 INC_STATS_COUNTER(cnt_map_sg);
2367
Joerg Roedel94f6d192009-11-24 16:40:02 +01002368 domain = get_domain(dev);
2369 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002370 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002371 else if (IS_ERR(domain))
2372 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002373
Joerg Roedel832a90c2008-09-18 15:54:23 +02002374 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002375
Joerg Roedel65b050a2008-06-26 21:28:02 +02002376 spin_lock_irqsave(&domain->lock, flags);
2377
2378 for_each_sg(sglist, s, nelems, i) {
2379 paddr = sg_phys(s);
2380
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002381 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002382 paddr, s->length, dir, false,
2383 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002384
2385 if (s->dma_address) {
2386 s->dma_length = s->length;
2387 mapped_elems++;
2388 } else
2389 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002390 }
2391
Joerg Roedel17b124b2011-04-06 18:01:35 +02002392 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002393
2394out:
2395 spin_unlock_irqrestore(&domain->lock, flags);
2396
2397 return mapped_elems;
2398unmap:
2399 for_each_sg(sglist, s, mapped_elems, i) {
2400 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002401 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002402 s->dma_length, dir);
2403 s->dma_address = s->dma_length = 0;
2404 }
2405
2406 mapped_elems = 0;
2407
2408 goto out;
2409}
2410
Joerg Roedel431b2a22008-07-11 17:14:22 +02002411/*
2412 * The exported map_sg function for dma_ops (handles scatter-gather
2413 * lists).
2414 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002415static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002416 int nelems, enum dma_data_direction dir,
2417 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002418{
2419 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002420 struct protection_domain *domain;
2421 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002422 int i;
2423
Joerg Roedel55877a62008-12-12 15:12:14 +01002424 INC_STATS_COUNTER(cnt_unmap_sg);
2425
Joerg Roedel94f6d192009-11-24 16:40:02 +01002426 domain = get_domain(dev);
2427 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002428 return;
2429
Joerg Roedel65b050a2008-06-26 21:28:02 +02002430 spin_lock_irqsave(&domain->lock, flags);
2431
2432 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002433 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002434 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002435 s->dma_address = s->dma_length = 0;
2436 }
2437
Joerg Roedel17b124b2011-04-06 18:01:35 +02002438 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002439
2440 spin_unlock_irqrestore(&domain->lock, flags);
2441}
2442
Joerg Roedel431b2a22008-07-11 17:14:22 +02002443/*
2444 * The exported alloc_coherent function for dma_ops.
2445 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002446static void *alloc_coherent(struct device *dev, size_t size,
2447 dma_addr_t *dma_addr, gfp_t flag)
2448{
2449 unsigned long flags;
2450 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002451 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002452 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002453 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002454
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002455 INC_STATS_COUNTER(cnt_alloc_coherent);
2456
Joerg Roedel94f6d192009-11-24 16:40:02 +01002457 domain = get_domain(dev);
2458 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002459 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2460 *dma_addr = __pa(virt_addr);
2461 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002462 } else if (IS_ERR(domain))
2463 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002464
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002465 dma_mask = dev->coherent_dma_mask;
2466 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2467 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002468
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002469 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2470 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302471 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002472
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002473 paddr = virt_to_phys(virt_addr);
2474
Joerg Roedel832a90c2008-09-18 15:54:23 +02002475 if (!dma_mask)
2476 dma_mask = *dev->dma_mask;
2477
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002478 spin_lock_irqsave(&domain->lock, flags);
2479
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002480 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002481 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002482
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002483 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002484 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002485 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002486 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002487
Joerg Roedel17b124b2011-04-06 18:01:35 +02002488 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002489
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002490 spin_unlock_irqrestore(&domain->lock, flags);
2491
2492 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002493
2494out_free:
2495
2496 free_pages((unsigned long)virt_addr, get_order(size));
2497
2498 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002499}
2500
Joerg Roedel431b2a22008-07-11 17:14:22 +02002501/*
2502 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002503 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002504static void free_coherent(struct device *dev, size_t size,
2505 void *virt_addr, dma_addr_t dma_addr)
2506{
2507 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002508 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002509
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002510 INC_STATS_COUNTER(cnt_free_coherent);
2511
Joerg Roedel94f6d192009-11-24 16:40:02 +01002512 domain = get_domain(dev);
2513 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002514 goto free_mem;
2515
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002516 spin_lock_irqsave(&domain->lock, flags);
2517
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002518 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002519
Joerg Roedel17b124b2011-04-06 18:01:35 +02002520 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002521
2522 spin_unlock_irqrestore(&domain->lock, flags);
2523
2524free_mem:
2525 free_pages((unsigned long)virt_addr, get_order(size));
2526}
2527
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002528/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002529 * This function is called by the DMA layer to find out if we can handle a
2530 * particular device. It is part of the dma_ops.
2531 */
2532static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2533{
Joerg Roedel420aef82009-11-23 16:14:57 +01002534 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002535}
2536
2537/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002538 * The function for pre-allocating protection domains.
2539 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002540 * If the driver core informs the DMA layer if a driver grabs a device
2541 * we don't need to preallocate the protection domains anymore.
2542 * For now we have to.
2543 */
Jaswinder Singh Rajput0e93dd82008-12-29 21:45:22 +05302544static void prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002545{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002546 struct iommu_dev_data *dev_data;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002547 struct dma_ops_domain *dma_dom;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002548 struct pci_dev *dev = NULL;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002549 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002550
Chris Wrightd18c69d2010-04-02 18:27:55 -07002551 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002552
2553 /* Do we handle this device? */
2554 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002555 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002556
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002557 dev_data = get_dev_data(&dev->dev);
2558 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
2559 /* Make sure passthrough domain is allocated */
2560 alloc_passthrough_domain();
2561 dev_data->passthrough = true;
2562 attach_device(&dev->dev, pt_domain);
2563 pr_info("AMD-Vi: Using passthough domain for device %s\n",
2564 dev_name(&dev->dev));
2565 }
2566
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002567 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002568 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002569 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002570
2571 devid = get_device_id(&dev->dev);
2572
Joerg Roedel87a64d52009-11-24 17:26:43 +01002573 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002574 if (!dma_dom)
2575 continue;
2576 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002577 dma_dom->target_dev = devid;
2578
Joerg Roedel15898bb2009-11-24 15:39:42 +01002579 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01002580
Joerg Roedelbd60b732008-09-11 10:24:48 +02002581 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002582 }
2583}
2584
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002585static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002586 .alloc_coherent = alloc_coherent,
2587 .free_coherent = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002588 .map_page = map_page,
2589 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002590 .map_sg = map_sg,
2591 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002592 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002593};
2594
Joerg Roedel27c21272011-05-30 15:56:24 +02002595static unsigned device_dma_ops_init(void)
2596{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002597 struct iommu_dev_data *dev_data;
Joerg Roedel27c21272011-05-30 15:56:24 +02002598 struct pci_dev *pdev = NULL;
2599 unsigned unhandled = 0;
2600
2601 for_each_pci_dev(pdev) {
2602 if (!check_device(&pdev->dev)) {
2603 unhandled += 1;
2604 continue;
2605 }
2606
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002607 dev_data = get_dev_data(&pdev->dev);
2608
2609 if (!dev_data->passthrough)
2610 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
2611 else
2612 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
Joerg Roedel27c21272011-05-30 15:56:24 +02002613 }
2614
2615 return unhandled;
2616}
2617
Joerg Roedel431b2a22008-07-11 17:14:22 +02002618/*
2619 * The function which clues the AMD IOMMU driver into dma_ops.
2620 */
Joerg Roedelf5325092010-01-22 17:44:35 +01002621
2622void __init amd_iommu_init_api(void)
2623{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02002624 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01002625}
2626
Joerg Roedel6631ee92008-06-26 21:28:05 +02002627int __init amd_iommu_init_dma_ops(void)
2628{
2629 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02002630 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002631
Joerg Roedel431b2a22008-07-11 17:14:22 +02002632 /*
2633 * first allocate a default protection domain for every IOMMU we
2634 * found in the system. Devices not assigned to any other
2635 * protection domain will be assigned to the default one.
2636 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02002637 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01002638 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002639 if (iommu->default_dom == NULL)
2640 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01002641 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002642 ret = iommu_init_unity_mappings(iommu);
2643 if (ret)
2644 goto free_domains;
2645 }
2646
Joerg Roedel431b2a22008-07-11 17:14:22 +02002647 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01002648 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002649 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01002650 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002651
2652 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002653 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002654
Joerg Roedel431b2a22008-07-11 17:14:22 +02002655 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02002656 unhandled = device_dma_ops_init();
2657 if (unhandled && max_pfn > MAX_DMA32_PFN) {
2658 /* There are unhandled devices - initialize swiotlb for them */
2659 swiotlb = 1;
2660 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02002661
Joerg Roedel7f265082008-12-12 13:50:21 +01002662 amd_iommu_stats_init();
2663
Joerg Roedel6631ee92008-06-26 21:28:05 +02002664 return 0;
2665
2666free_domains:
2667
Joerg Roedel3bd22172009-05-04 15:06:20 +02002668 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002669 if (iommu->default_dom)
2670 dma_ops_domain_free(iommu->default_dom);
2671 }
2672
2673 return ret;
2674}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002675
2676/*****************************************************************************
2677 *
2678 * The following functions belong to the exported interface of AMD IOMMU
2679 *
2680 * This interface allows access to lower level functions of the IOMMU
2681 * like protection domain handling and assignement of devices to domains
2682 * which is not possible with the dma_ops interface.
2683 *
2684 *****************************************************************************/
2685
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002686static void cleanup_domain(struct protection_domain *domain)
2687{
Joerg Roedel492667d2009-11-27 13:25:47 +01002688 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002689 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002690
2691 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2692
Joerg Roedel492667d2009-11-27 13:25:47 +01002693 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002694 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01002695 atomic_set(&dev_data->bind, 0);
2696 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002697
2698 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2699}
2700
Joerg Roedel26508152009-08-26 16:52:40 +02002701static void protection_domain_free(struct protection_domain *domain)
2702{
2703 if (!domain)
2704 return;
2705
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002706 del_domain_from_list(domain);
2707
Joerg Roedel26508152009-08-26 16:52:40 +02002708 if (domain->id)
2709 domain_id_free(domain->id);
2710
2711 kfree(domain);
2712}
2713
2714static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002715{
2716 struct protection_domain *domain;
2717
2718 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2719 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002720 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002721
2722 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002723 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01002724 domain->id = domain_id_alloc();
2725 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02002726 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002727 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02002728
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002729 add_domain_to_list(domain);
2730
Joerg Roedel26508152009-08-26 16:52:40 +02002731 return domain;
2732
2733out_err:
2734 kfree(domain);
2735
2736 return NULL;
2737}
2738
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002739static int __init alloc_passthrough_domain(void)
2740{
2741 if (pt_domain != NULL)
2742 return 0;
2743
2744 /* allocate passthrough domain */
2745 pt_domain = protection_domain_alloc();
2746 if (!pt_domain)
2747 return -ENOMEM;
2748
2749 pt_domain->mode = PAGE_MODE_NONE;
2750
2751 return 0;
2752}
Joerg Roedel26508152009-08-26 16:52:40 +02002753static int amd_iommu_domain_init(struct iommu_domain *dom)
2754{
2755 struct protection_domain *domain;
2756
2757 domain = protection_domain_alloc();
2758 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01002759 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02002760
2761 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002762 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2763 if (!domain->pt_root)
2764 goto out_free;
2765
2766 dom->priv = domain;
2767
2768 return 0;
2769
2770out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02002771 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01002772
2773 return -ENOMEM;
2774}
2775
Joerg Roedel98383fc2008-12-02 18:34:12 +01002776static void amd_iommu_domain_destroy(struct iommu_domain *dom)
2777{
2778 struct protection_domain *domain = dom->priv;
2779
2780 if (!domain)
2781 return;
2782
2783 if (domain->dev_cnt > 0)
2784 cleanup_domain(domain);
2785
2786 BUG_ON(domain->dev_cnt != 0);
2787
Joerg Roedel132bd682011-11-17 14:18:46 +01002788 if (domain->mode != PAGE_MODE_NONE)
2789 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002790
Joerg Roedel8b408fe2010-03-08 14:20:07 +01002791 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002792
2793 dom->priv = NULL;
2794}
2795
Joerg Roedel684f2882008-12-08 12:07:44 +01002796static void amd_iommu_detach_device(struct iommu_domain *dom,
2797 struct device *dev)
2798{
Joerg Roedel657cbb62009-11-23 15:26:46 +01002799 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002800 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002801 u16 devid;
2802
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002803 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01002804 return;
2805
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002806 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002807
Joerg Roedel657cbb62009-11-23 15:26:46 +01002808 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002809 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002810
2811 iommu = amd_iommu_rlookup_table[devid];
2812 if (!iommu)
2813 return;
2814
Joerg Roedel684f2882008-12-08 12:07:44 +01002815 iommu_completion_wait(iommu);
2816}
2817
Joerg Roedel01106062008-12-02 19:34:11 +01002818static int amd_iommu_attach_device(struct iommu_domain *dom,
2819 struct device *dev)
2820{
2821 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002822 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01002823 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002824 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002825
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002826 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01002827 return -EINVAL;
2828
Joerg Roedel657cbb62009-11-23 15:26:46 +01002829 dev_data = dev->archdata.iommu;
2830
Joerg Roedelf62dda62011-06-09 12:55:35 +02002831 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01002832 if (!iommu)
2833 return -EINVAL;
2834
Joerg Roedel657cbb62009-11-23 15:26:46 +01002835 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002836 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002837
Joerg Roedel15898bb2009-11-24 15:39:42 +01002838 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01002839
2840 iommu_completion_wait(iommu);
2841
Joerg Roedel15898bb2009-11-24 15:39:42 +01002842 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002843}
2844
Joerg Roedel468e2362010-01-21 16:37:36 +01002845static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
2846 phys_addr_t paddr, int gfp_order, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002847{
Joerg Roedel468e2362010-01-21 16:37:36 +01002848 unsigned long page_size = 0x1000UL << gfp_order;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002849 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002850 int prot = 0;
2851 int ret;
2852
Joerg Roedel132bd682011-11-17 14:18:46 +01002853 if (domain->mode == PAGE_MODE_NONE)
2854 return -EINVAL;
2855
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002856 if (iommu_prot & IOMMU_READ)
2857 prot |= IOMMU_PROT_IR;
2858 if (iommu_prot & IOMMU_WRITE)
2859 prot |= IOMMU_PROT_IW;
2860
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002861 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f2010-05-11 17:40:57 +02002862 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002863 mutex_unlock(&domain->api_lock);
2864
Joerg Roedel795e74f2010-05-11 17:40:57 +02002865 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002866}
2867
Joerg Roedel468e2362010-01-21 16:37:36 +01002868static int amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2869 int gfp_order)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002870{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002871 struct protection_domain *domain = dom->priv;
Joerg Roedel468e2362010-01-21 16:37:36 +01002872 unsigned long page_size, unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002873
Joerg Roedel132bd682011-11-17 14:18:46 +01002874 if (domain->mode == PAGE_MODE_NONE)
2875 return -EINVAL;
2876
Joerg Roedel468e2362010-01-21 16:37:36 +01002877 page_size = 0x1000UL << gfp_order;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002878
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002879 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01002880 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f2010-05-11 17:40:57 +02002881 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002882
Joerg Roedel17b124b2011-04-06 18:01:35 +02002883 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002884
Joerg Roedel468e2362010-01-21 16:37:36 +01002885 return get_order(unmap_size);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002886}
2887
Joerg Roedel645c4c82008-12-02 20:05:50 +01002888static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2889 unsigned long iova)
2890{
2891 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002892 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002893 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002894 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002895
Joerg Roedel132bd682011-11-17 14:18:46 +01002896 if (domain->mode == PAGE_MODE_NONE)
2897 return iova;
2898
Joerg Roedel24cd7722010-01-19 17:27:39 +01002899 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002900
Joerg Roedela6d41a42009-09-02 17:08:55 +02002901 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01002902 return 0;
2903
Joerg Roedelf03152b2010-01-21 16:15:24 +01002904 if (PM_PTE_LEVEL(*pte) == 0)
2905 offset_mask = PAGE_SIZE - 1;
2906 else
2907 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
2908
2909 __pte = *pte & PM_ADDR_MASK;
2910 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002911
2912 return paddr;
2913}
2914
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002915static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
2916 unsigned long cap)
2917{
Joerg Roedel80a506b2010-07-27 17:14:24 +02002918 switch (cap) {
2919 case IOMMU_CAP_CACHE_COHERENCY:
2920 return 1;
2921 }
2922
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002923 return 0;
2924}
2925
Joerg Roedel26961ef2008-12-03 17:00:17 +01002926static struct iommu_ops amd_iommu_ops = {
2927 .domain_init = amd_iommu_domain_init,
2928 .domain_destroy = amd_iommu_domain_destroy,
2929 .attach_dev = amd_iommu_attach_device,
2930 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01002931 .map = amd_iommu_map,
2932 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002933 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002934 .domain_has_cap = amd_iommu_domain_has_cap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002935};
2936
Joerg Roedel0feae532009-08-26 15:26:30 +02002937/*****************************************************************************
2938 *
2939 * The next functions do a basic initialization of IOMMU for pass through
2940 * mode
2941 *
2942 * In passthrough mode the IOMMU is initialized and enabled but not used for
2943 * DMA-API translation.
2944 *
2945 *****************************************************************************/
2946
2947int __init amd_iommu_init_passthrough(void)
2948{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002949 struct iommu_dev_data *dev_data;
Joerg Roedel0feae532009-08-26 15:26:30 +02002950 struct pci_dev *dev = NULL;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002951 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002952 u16 devid;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002953 int ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02002954
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002955 ret = alloc_passthrough_domain();
2956 if (ret)
2957 return ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02002958
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04002959 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002960 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02002961 continue;
2962
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002963 dev_data = get_dev_data(&dev->dev);
2964 dev_data->passthrough = true;
2965
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002966 devid = get_device_id(&dev->dev);
2967
Joerg Roedel15898bb2009-11-24 15:39:42 +01002968 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02002969 if (!iommu)
2970 continue;
2971
Joerg Roedel15898bb2009-11-24 15:39:42 +01002972 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02002973 }
2974
2975 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
2976
2977 return 0;
2978}
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01002979
2980/* IOMMUv2 specific functions */
2981int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
2982{
2983 return atomic_notifier_chain_register(&ppr_notifier, nb);
2984}
2985EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
2986
2987int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
2988{
2989 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
2990}
2991EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01002992
2993void amd_iommu_domain_direct_map(struct iommu_domain *dom)
2994{
2995 struct protection_domain *domain = dom->priv;
2996 unsigned long flags;
2997
2998 spin_lock_irqsave(&domain->lock, flags);
2999
3000 /* Update data structure */
3001 domain->mode = PAGE_MODE_NONE;
3002 domain->updated = true;
3003
3004 /* Make changes visible to IOMMUs */
3005 update_domain(domain);
3006
3007 /* Page-table is not visible to IOMMU anymore, so free it */
3008 free_pagetable(domain);
3009
3010 spin_unlock_irqrestore(&domain->lock, flags);
3011}
3012EXPORT_SYMBOL(amd_iommu_domain_direct_map);