blob: 7ccfc80ceb7a1687b0e07b58d620f5e79fdedaef [file] [log] [blame]
Joerg Roedelb6c02712008-06-26 21:27:53 +02001/*
Joerg Roedel5d0d7152010-10-13 11:13:21 +02002 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
Joerg Roedelb6c02712008-06-26 21:27:53 +02003 * Author: Joerg Roedel <joerg.roedel@amd.com>
4 * Leo Duran <leo.duran@amd.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/pci.h>
Joerg Roedelcb41ed82011-04-05 11:00:53 +020021#include <linux/pci-ats.h>
Akinobu Mitaa66022c2009-12-15 16:48:28 -080022#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Joerg Roedel7f265082008-12-12 13:50:21 +010024#include <linux/debugfs.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020025#include <linux/scatterlist.h>
FUJITA Tomonori51491362009-01-05 23:47:25 +090026#include <linux/dma-mapping.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020027#include <linux/iommu-helper.h>
Joerg Roedelc156e342008-12-02 18:13:27 +010028#include <linux/iommu.h>
Joerg Roedel815b33f2011-04-06 17:26:49 +020029#include <linux/delay.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020030#include <linux/amd-iommu.h>
Joerg Roedel17f5b562011-07-06 17:14:44 +020031#include <asm/msidef.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020032#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090033#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010034#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020035#include <asm/dma.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020036
37#include "amd_iommu_proto.h"
38#include "amd_iommu_types.h"
Joerg Roedelb6c02712008-06-26 21:27:53 +020039
40#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
41
Joerg Roedel815b33f2011-04-06 17:26:49 +020042#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020043
Joerg Roedelb6c02712008-06-26 21:27:53 +020044static DEFINE_RWLOCK(amd_iommu_devtable_lock);
45
Joerg Roedelbd60b732008-09-11 10:24:48 +020046/* A list of preallocated protection domains */
47static LIST_HEAD(iommu_pd_list);
48static DEFINE_SPINLOCK(iommu_pd_list_lock);
49
Joerg Roedel8fa5f802011-06-09 12:24:45 +020050/* List of all available dev_data structures */
51static LIST_HEAD(dev_data_list);
52static DEFINE_SPINLOCK(dev_data_list_lock);
53
Joerg Roedel0feae532009-08-26 15:26:30 +020054/*
55 * Domain for untranslated devices - only allocated
56 * if iommu=pt passed on kernel cmd line.
57 */
58static struct protection_domain *pt_domain;
59
Joerg Roedel26961ef2008-12-03 17:00:17 +010060static struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010061
Joerg Roedel431b2a22008-07-11 17:14:22 +020062/*
63 * general struct to manage commands send to an IOMMU
64 */
Joerg Roedeld6449532008-07-11 17:14:28 +020065struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +020066 u32 data[4];
67};
68
Joerg Roedel04bfdd82009-09-02 16:00:23 +020069static void update_domain(struct protection_domain *domain);
Joerg Roedel5abcdba2011-12-01 15:49:45 +010070static int __init alloc_passthrough_domain(void);
Chris Wrightc1eee672009-05-21 00:56:58 -070071
Joerg Roedel15898bb2009-11-24 15:39:42 +010072/****************************************************************************
73 *
74 * Helper functions
75 *
76 ****************************************************************************/
77
Joerg Roedelf62dda62011-06-09 12:55:35 +020078static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +020079{
80 struct iommu_dev_data *dev_data;
81 unsigned long flags;
82
83 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
84 if (!dev_data)
85 return NULL;
86
Joerg Roedelf62dda62011-06-09 12:55:35 +020087 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +020088 atomic_set(&dev_data->bind, 0);
89
90 spin_lock_irqsave(&dev_data_list_lock, flags);
91 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
92 spin_unlock_irqrestore(&dev_data_list_lock, flags);
93
94 return dev_data;
95}
96
97static void free_dev_data(struct iommu_dev_data *dev_data)
98{
99 unsigned long flags;
100
101 spin_lock_irqsave(&dev_data_list_lock, flags);
102 list_del(&dev_data->dev_data_list);
103 spin_unlock_irqrestore(&dev_data_list_lock, flags);
104
105 kfree(dev_data);
106}
107
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200108static struct iommu_dev_data *search_dev_data(u16 devid)
109{
110 struct iommu_dev_data *dev_data;
111 unsigned long flags;
112
113 spin_lock_irqsave(&dev_data_list_lock, flags);
114 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
115 if (dev_data->devid == devid)
116 goto out_unlock;
117 }
118
119 dev_data = NULL;
120
121out_unlock:
122 spin_unlock_irqrestore(&dev_data_list_lock, flags);
123
124 return dev_data;
125}
126
127static struct iommu_dev_data *find_dev_data(u16 devid)
128{
129 struct iommu_dev_data *dev_data;
130
131 dev_data = search_dev_data(devid);
132
133 if (dev_data == NULL)
134 dev_data = alloc_dev_data(devid);
135
136 return dev_data;
137}
138
Joerg Roedel15898bb2009-11-24 15:39:42 +0100139static inline u16 get_device_id(struct device *dev)
140{
141 struct pci_dev *pdev = to_pci_dev(dev);
142
143 return calc_devid(pdev->bus->number, pdev->devfn);
144}
145
Joerg Roedel657cbb62009-11-23 15:26:46 +0100146static struct iommu_dev_data *get_dev_data(struct device *dev)
147{
148 return dev->archdata.iommu;
149}
150
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100151static bool pci_iommuv2_capable(struct pci_dev *pdev)
152{
153 static const int caps[] = {
154 PCI_EXT_CAP_ID_ATS,
155 PCI_PRI_CAP,
156 PCI_PASID_CAP,
157 };
158 int i, pos;
159
160 for (i = 0; i < 3; ++i) {
161 pos = pci_find_ext_capability(pdev, caps[i]);
162 if (pos == 0)
163 return false;
164 }
165
166 return true;
167}
168
Joerg Roedel71c70982009-11-24 16:43:06 +0100169/*
170 * In this function the list of preallocated protection domains is traversed to
171 * find the domain for a specific device
172 */
173static struct dma_ops_domain *find_protection_domain(u16 devid)
174{
175 struct dma_ops_domain *entry, *ret = NULL;
176 unsigned long flags;
177 u16 alias = amd_iommu_alias_table[devid];
178
179 if (list_empty(&iommu_pd_list))
180 return NULL;
181
182 spin_lock_irqsave(&iommu_pd_list_lock, flags);
183
184 list_for_each_entry(entry, &iommu_pd_list, list) {
185 if (entry->target_dev == devid ||
186 entry->target_dev == alias) {
187 ret = entry;
188 break;
189 }
190 }
191
192 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
193
194 return ret;
195}
196
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100197/*
198 * This function checks if the driver got a valid device from the caller to
199 * avoid dereferencing invalid pointers.
200 */
201static bool check_device(struct device *dev)
202{
203 u16 devid;
204
205 if (!dev || !dev->dma_mask)
206 return false;
207
208 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100209 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100210 return false;
211
212 devid = get_device_id(dev);
213
214 /* Out of our scope? */
215 if (devid > amd_iommu_last_bdf)
216 return false;
217
218 if (amd_iommu_rlookup_table[devid] == NULL)
219 return false;
220
221 return true;
222}
223
Joerg Roedel657cbb62009-11-23 15:26:46 +0100224static int iommu_init_device(struct device *dev)
225{
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100226 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedel657cbb62009-11-23 15:26:46 +0100227 struct iommu_dev_data *dev_data;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200228 u16 alias;
Joerg Roedel657cbb62009-11-23 15:26:46 +0100229
230 if (dev->archdata.iommu)
231 return 0;
232
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200233 dev_data = find_dev_data(get_device_id(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +0100234 if (!dev_data)
235 return -ENOMEM;
236
Joerg Roedelf62dda62011-06-09 12:55:35 +0200237 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedel2b02b092011-06-09 17:48:39 +0200238 if (alias != dev_data->devid) {
Joerg Roedel71f77582011-06-09 19:03:15 +0200239 struct iommu_dev_data *alias_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100240
Joerg Roedel71f77582011-06-09 19:03:15 +0200241 alias_data = find_dev_data(alias);
242 if (alias_data == NULL) {
243 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
244 dev_name(dev));
Joerg Roedel2b02b092011-06-09 17:48:39 +0200245 free_dev_data(dev_data);
246 return -ENOTSUPP;
247 }
Joerg Roedel71f77582011-06-09 19:03:15 +0200248 dev_data->alias_data = alias_data;
Joerg Roedel26018872011-06-06 16:50:14 +0200249 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100250
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100251 if (pci_iommuv2_capable(pdev)) {
252 struct amd_iommu *iommu;
253
254 iommu = amd_iommu_rlookup_table[dev_data->devid];
255 dev_data->iommu_v2 = iommu->is_iommu_v2;
256 }
257
Joerg Roedel657cbb62009-11-23 15:26:46 +0100258 dev->archdata.iommu = dev_data;
259
Joerg Roedel657cbb62009-11-23 15:26:46 +0100260 return 0;
261}
262
Joerg Roedel26018872011-06-06 16:50:14 +0200263static void iommu_ignore_device(struct device *dev)
264{
265 u16 devid, alias;
266
267 devid = get_device_id(dev);
268 alias = amd_iommu_alias_table[devid];
269
270 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
271 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
272
273 amd_iommu_rlookup_table[devid] = NULL;
274 amd_iommu_rlookup_table[alias] = NULL;
275}
276
Joerg Roedel657cbb62009-11-23 15:26:46 +0100277static void iommu_uninit_device(struct device *dev)
278{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200279 /*
280 * Nothing to do here - we keep dev_data around for unplugged devices
281 * and reuse it when the device is re-plugged - not doing so would
282 * introduce a ton of races.
283 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100284}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100285
286void __init amd_iommu_uninit_devices(void)
287{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200288 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100289 struct pci_dev *pdev = NULL;
290
291 for_each_pci_dev(pdev) {
292
293 if (!check_device(&pdev->dev))
294 continue;
295
296 iommu_uninit_device(&pdev->dev);
297 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200298
299 /* Free all of our dev_data structures */
300 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
301 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100302}
303
304int __init amd_iommu_init_devices(void)
305{
306 struct pci_dev *pdev = NULL;
307 int ret = 0;
308
309 for_each_pci_dev(pdev) {
310
311 if (!check_device(&pdev->dev))
312 continue;
313
314 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200315 if (ret == -ENOTSUPP)
316 iommu_ignore_device(&pdev->dev);
317 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100318 goto out_free;
319 }
320
321 return 0;
322
323out_free:
324
325 amd_iommu_uninit_devices();
326
327 return ret;
328}
Joerg Roedel7f265082008-12-12 13:50:21 +0100329#ifdef CONFIG_AMD_IOMMU_STATS
330
331/*
332 * Initialization code for statistics collection
333 */
334
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100335DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100336DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100337DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100338DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100339DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100340DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100341DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100342DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100343DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100344DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100345DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100346DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100347
Joerg Roedel7f265082008-12-12 13:50:21 +0100348static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100349static struct dentry *de_fflush;
350
351static void amd_iommu_stats_add(struct __iommu_counter *cnt)
352{
353 if (stats_dir == NULL)
354 return;
355
356 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
357 &cnt->value);
358}
359
360static void amd_iommu_stats_init(void)
361{
362 stats_dir = debugfs_create_dir("amd-iommu", NULL);
363 if (stats_dir == NULL)
364 return;
365
Joerg Roedel7f265082008-12-12 13:50:21 +0100366 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
367 (u32 *)&amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100368
369 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100370 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100371 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100372 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100373 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100374 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100375 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100376 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100377 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100378 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100379 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100380 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100381}
382
383#endif
384
Joerg Roedel431b2a22008-07-11 17:14:22 +0200385/****************************************************************************
386 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200387 * Interrupt handling functions
388 *
389 ****************************************************************************/
390
Joerg Roedele3e59872009-09-03 14:02:10 +0200391static void dump_dte_entry(u16 devid)
392{
393 int i;
394
Joerg Roedelee6c2862011-11-09 12:06:03 +0100395 for (i = 0; i < 4; ++i)
396 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200397 amd_iommu_dev_table[devid].data[i]);
398}
399
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200400static void dump_command(unsigned long phys_addr)
401{
402 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
403 int i;
404
405 for (i = 0; i < 4; ++i)
406 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
407}
408
Joerg Roedela345b232009-09-03 15:01:43 +0200409static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200410{
411 u32 *event = __evt;
412 int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
413 int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
414 int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
415 int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
416 u64 address = (u64)(((u64)event[3]) << 32) | event[2];
417
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200418 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200419
420 switch (type) {
421 case EVENT_TYPE_ILL_DEV:
422 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
423 "address=0x%016llx flags=0x%04x]\n",
424 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
425 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200426 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200427 break;
428 case EVENT_TYPE_IO_FAULT:
429 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
430 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
431 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
432 domid, address, flags);
433 break;
434 case EVENT_TYPE_DEV_TAB_ERR:
435 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
436 "address=0x%016llx flags=0x%04x]\n",
437 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
438 address, flags);
439 break;
440 case EVENT_TYPE_PAGE_TAB_ERR:
441 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
442 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
443 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
444 domid, address, flags);
445 break;
446 case EVENT_TYPE_ILL_CMD:
447 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200448 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200449 break;
450 case EVENT_TYPE_CMD_HARD_ERR:
451 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
452 "flags=0x%04x]\n", address, flags);
453 break;
454 case EVENT_TYPE_IOTLB_INV_TO:
455 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
456 "address=0x%016llx]\n",
457 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
458 address);
459 break;
460 case EVENT_TYPE_INV_DEV_REQ:
461 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
462 "address=0x%016llx flags=0x%04x]\n",
463 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
464 address, flags);
465 break;
466 default:
467 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
468 }
469}
470
471static void iommu_poll_events(struct amd_iommu *iommu)
472{
473 u32 head, tail;
474 unsigned long flags;
475
476 spin_lock_irqsave(&iommu->lock, flags);
477
478 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
479 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
480
481 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200482 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200483 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
484 }
485
486 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
487
488 spin_unlock_irqrestore(&iommu->lock, flags);
489}
490
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200491irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200492{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200493 struct amd_iommu *iommu;
494
Joerg Roedel3bd22172009-05-04 15:06:20 +0200495 for_each_iommu(iommu)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200496 iommu_poll_events(iommu);
497
498 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200499}
500
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200501irqreturn_t amd_iommu_int_handler(int irq, void *data)
502{
503 return IRQ_WAKE_THREAD;
504}
505
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200506/****************************************************************************
507 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200508 * IOMMU command queuing functions
509 *
510 ****************************************************************************/
511
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200512static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200513{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200514 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200515
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200516 while (*sem == 0 && i < LOOP_TIMEOUT) {
517 udelay(1);
518 i += 1;
519 }
520
521 if (i == LOOP_TIMEOUT) {
522 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
523 return -EIO;
524 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200525
526 return 0;
527}
528
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200529static void copy_cmd_to_buffer(struct amd_iommu *iommu,
530 struct iommu_cmd *cmd,
531 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200532{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200533 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200534
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200535 target = iommu->cmd_buf + tail;
536 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200537
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200538 /* Copy command to buffer */
539 memcpy(target, cmd, sizeof(*cmd));
540
541 /* Tell the IOMMU about it */
542 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
543}
544
Joerg Roedel815b33f2011-04-06 17:26:49 +0200545static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200546{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200547 WARN_ON(address & 0x7ULL);
548
Joerg Roedelded46732011-04-06 10:53:48 +0200549 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200550 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
551 cmd->data[1] = upper_32_bits(__pa(address));
552 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200553 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
554}
555
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200556static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
557{
558 memset(cmd, 0, sizeof(*cmd));
559 cmd->data[0] = devid;
560 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
561}
562
Joerg Roedel11b64022011-04-06 11:49:28 +0200563static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
564 size_t size, u16 domid, int pde)
565{
566 u64 pages;
567 int s;
568
569 pages = iommu_num_pages(address, size, PAGE_SIZE);
570 s = 0;
571
572 if (pages > 1) {
573 /*
574 * If we have to flush more than one page, flush all
575 * TLB entries for this domain
576 */
577 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
578 s = 1;
579 }
580
581 address &= PAGE_MASK;
582
583 memset(cmd, 0, sizeof(*cmd));
584 cmd->data[1] |= domid;
585 cmd->data[2] = lower_32_bits(address);
586 cmd->data[3] = upper_32_bits(address);
587 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
588 if (s) /* size bit - we flush more than one 4kb page */
589 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
590 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
591 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
592}
593
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200594static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
595 u64 address, size_t size)
596{
597 u64 pages;
598 int s;
599
600 pages = iommu_num_pages(address, size, PAGE_SIZE);
601 s = 0;
602
603 if (pages > 1) {
604 /*
605 * If we have to flush more than one page, flush all
606 * TLB entries for this domain
607 */
608 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
609 s = 1;
610 }
611
612 address &= PAGE_MASK;
613
614 memset(cmd, 0, sizeof(*cmd));
615 cmd->data[0] = devid;
616 cmd->data[0] |= (qdep & 0xff) << 24;
617 cmd->data[1] = devid;
618 cmd->data[2] = lower_32_bits(address);
619 cmd->data[3] = upper_32_bits(address);
620 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
621 if (s)
622 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
623}
624
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200625static void build_inv_all(struct iommu_cmd *cmd)
626{
627 memset(cmd, 0, sizeof(*cmd));
628 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200629}
630
Joerg Roedel431b2a22008-07-11 17:14:22 +0200631/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200632 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200633 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200634 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200635static int iommu_queue_command_sync(struct amd_iommu *iommu,
636 struct iommu_cmd *cmd,
637 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200638{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200639 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200640 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200641
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200642 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100643
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200644again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200645 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200646
647 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
648 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
649 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
650 left = (head - next_tail) % iommu->cmd_buf_size;
651
652 if (left <= 2) {
653 struct iommu_cmd sync_cmd;
654 volatile u64 sem = 0;
655 int ret;
656
657 build_completion_wait(&sync_cmd, (u64)&sem);
658 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
659
660 spin_unlock_irqrestore(&iommu->lock, flags);
661
662 if ((ret = wait_on_sem(&sem)) != 0)
663 return ret;
664
665 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200666 }
667
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200668 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200669
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200670 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200671 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200672
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200673 spin_unlock_irqrestore(&iommu->lock, flags);
674
Joerg Roedel815b33f2011-04-06 17:26:49 +0200675 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100676}
677
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200678static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
679{
680 return iommu_queue_command_sync(iommu, cmd, true);
681}
682
Joerg Roedel8d201962008-12-02 20:34:41 +0100683/*
684 * This function queues a completion wait command into the command
685 * buffer of an IOMMU
686 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100687static int iommu_completion_wait(struct amd_iommu *iommu)
688{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200689 struct iommu_cmd cmd;
690 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200691 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100692
693 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200694 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100695
Joerg Roedel815b33f2011-04-06 17:26:49 +0200696 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100697
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200698 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +0100699 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200700 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100701
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200702 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200703}
704
Joerg Roedeld8c13082011-04-06 18:51:26 +0200705static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200706{
707 struct iommu_cmd cmd;
708
Joerg Roedeld8c13082011-04-06 18:51:26 +0200709 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200710
Joerg Roedeld8c13082011-04-06 18:51:26 +0200711 return iommu_queue_command(iommu, &cmd);
712}
713
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200714static void iommu_flush_dte_all(struct amd_iommu *iommu)
715{
716 u32 devid;
717
718 for (devid = 0; devid <= 0xffff; ++devid)
719 iommu_flush_dte(iommu, devid);
720
721 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200722}
723
724/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200725 * This function uses heavy locking and may disable irqs for some time. But
726 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200727 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200728static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200729{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200730 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200731
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200732 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
733 struct iommu_cmd cmd;
734 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
735 dom_id, 1);
736 iommu_queue_command(iommu, &cmd);
737 }
Joerg Roedel431b2a22008-07-11 17:14:22 +0200738
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200739 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200740}
741
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200742static void iommu_flush_all(struct amd_iommu *iommu)
743{
744 struct iommu_cmd cmd;
745
746 build_inv_all(&cmd);
747
748 iommu_queue_command(iommu, &cmd);
749 iommu_completion_wait(iommu);
750}
751
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200752void iommu_flush_all_caches(struct amd_iommu *iommu)
753{
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200754 if (iommu_feature(iommu, FEATURE_IA)) {
755 iommu_flush_all(iommu);
756 } else {
757 iommu_flush_dte_all(iommu);
758 iommu_flush_tlb_all(iommu);
759 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200760}
761
Joerg Roedel431b2a22008-07-11 17:14:22 +0200762/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200763 * Command send function for flushing on-device TLB
764 */
Joerg Roedel6c542042011-06-09 17:07:31 +0200765static int device_flush_iotlb(struct iommu_dev_data *dev_data,
766 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200767{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200768 struct amd_iommu *iommu;
769 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200770 int qdep;
771
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200772 qdep = dev_data->ats.qdep;
773 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200774
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200775 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200776
777 return iommu_queue_command(iommu, &cmd);
778}
779
780/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200781 * Command send function for invalidating a device table entry
782 */
Joerg Roedel6c542042011-06-09 17:07:31 +0200783static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +0100784{
785 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200786 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100787
Joerg Roedel6c542042011-06-09 17:07:31 +0200788 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +0100789
Joerg Roedelf62dda62011-06-09 12:55:35 +0200790 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200791 if (ret)
792 return ret;
793
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200794 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +0200795 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200796
797 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100798}
799
Joerg Roedel431b2a22008-07-11 17:14:22 +0200800/*
801 * TLB invalidation function which is called from the mapping functions.
802 * It invalidates a single PTE if the range to flush is within a single
803 * page. Otherwise it flushes the whole TLB of the IOMMU.
804 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200805static void __domain_flush_pages(struct protection_domain *domain,
806 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200807{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200808 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +0200809 struct iommu_cmd cmd;
810 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200811
Joerg Roedel11b64022011-04-06 11:49:28 +0200812 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +0200813
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100814 for (i = 0; i < amd_iommus_present; ++i) {
815 if (!domain->dev_iommu[i])
816 continue;
817
818 /*
819 * Devices of this domain are behind this IOMMU
820 * We need a TLB flush
821 */
Joerg Roedel11b64022011-04-06 11:49:28 +0200822 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100823 }
824
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200825 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200826
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200827 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200828 continue;
829
Joerg Roedel6c542042011-06-09 17:07:31 +0200830 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200831 }
832
Joerg Roedel11b64022011-04-06 11:49:28 +0200833 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100834}
835
Joerg Roedel17b124b2011-04-06 18:01:35 +0200836static void domain_flush_pages(struct protection_domain *domain,
837 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100838{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200839 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200840}
Joerg Roedelb6c02712008-06-26 21:27:53 +0200841
Joerg Roedel1c655772008-09-04 18:40:05 +0200842/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200843static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +0200844{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200845 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +0200846}
847
Chris Wright42a49f92009-06-15 15:42:00 +0200848/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200849static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +0200850{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200851 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
852}
853
854static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +0200855{
856 int i;
857
858 for (i = 0; i < amd_iommus_present; ++i) {
859 if (!domain->dev_iommu[i])
860 continue;
861
862 /*
863 * Devices of this domain are behind this IOMMU
864 * We need to wait for completion of all commands.
865 */
866 iommu_completion_wait(amd_iommus[i]);
867 }
868}
869
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100870
Joerg Roedel43f49602008-12-02 21:01:12 +0100871/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100872 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +0100873 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200874static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200875{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100876 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100877
878 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +0200879 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100880}
881
Joerg Roedel431b2a22008-07-11 17:14:22 +0200882/****************************************************************************
883 *
884 * The functions below are used the create the page table mappings for
885 * unity mapped regions.
886 *
887 ****************************************************************************/
888
889/*
Joerg Roedel308973d2009-11-24 17:43:32 +0100890 * This function is used to add another level to an IO page table. Adding
891 * another level increases the size of the address space by 9 bits to a size up
892 * to 64 bits.
893 */
894static bool increase_address_space(struct protection_domain *domain,
895 gfp_t gfp)
896{
897 u64 *pte;
898
899 if (domain->mode == PAGE_MODE_6_LEVEL)
900 /* address space already 64 bit large */
901 return false;
902
903 pte = (void *)get_zeroed_page(gfp);
904 if (!pte)
905 return false;
906
907 *pte = PM_LEVEL_PDE(domain->mode,
908 virt_to_phys(domain->pt_root));
909 domain->pt_root = pte;
910 domain->mode += 1;
911 domain->updated = true;
912
913 return true;
914}
915
916static u64 *alloc_pte(struct protection_domain *domain,
917 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100918 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +0100919 u64 **pte_page,
920 gfp_t gfp)
921{
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100922 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +0100923 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100924
925 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +0100926
927 while (address > PM_LEVEL_SIZE(domain->mode))
928 increase_address_space(domain, gfp);
929
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100930 level = domain->mode - 1;
931 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
932 address = PAGE_SIZE_ALIGN(address, page_size);
933 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +0100934
935 while (level > end_lvl) {
936 if (!IOMMU_PTE_PRESENT(*pte)) {
937 page = (u64 *)get_zeroed_page(gfp);
938 if (!page)
939 return NULL;
940 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
941 }
942
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100943 /* No level skipping support yet */
944 if (PM_PTE_LEVEL(*pte) != level)
945 return NULL;
946
Joerg Roedel308973d2009-11-24 17:43:32 +0100947 level -= 1;
948
949 pte = IOMMU_PTE_PAGE(*pte);
950
951 if (pte_page && level == end_lvl)
952 *pte_page = pte;
953
954 pte = &pte[PM_LEVEL_INDEX(level, address)];
955 }
956
957 return pte;
958}
959
960/*
961 * This function checks if there is a PTE for a given dma address. If
962 * there is one, it returns the pointer to it.
963 */
Joerg Roedel24cd7722010-01-19 17:27:39 +0100964static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +0100965{
966 int level;
967 u64 *pte;
968
Joerg Roedel24cd7722010-01-19 17:27:39 +0100969 if (address > PM_LEVEL_SIZE(domain->mode))
970 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +0100971
Joerg Roedel24cd7722010-01-19 17:27:39 +0100972 level = domain->mode - 1;
973 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
974
975 while (level > 0) {
976
977 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +0100978 if (!IOMMU_PTE_PRESENT(*pte))
979 return NULL;
980
Joerg Roedel24cd7722010-01-19 17:27:39 +0100981 /* Large PTE */
982 if (PM_PTE_LEVEL(*pte) == 0x07) {
983 unsigned long pte_mask, __pte;
984
985 /*
986 * If we have a series of large PTEs, make
987 * sure to return a pointer to the first one.
988 */
989 pte_mask = PTE_PAGE_SIZE(*pte);
990 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
991 __pte = ((unsigned long)pte) & pte_mask;
992
993 return (u64 *)__pte;
994 }
995
996 /* No level skipping support yet */
997 if (PM_PTE_LEVEL(*pte) != level)
998 return NULL;
999
Joerg Roedel308973d2009-11-24 17:43:32 +01001000 level -= 1;
1001
Joerg Roedel24cd7722010-01-19 17:27:39 +01001002 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +01001003 pte = IOMMU_PTE_PAGE(*pte);
1004 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +01001005 }
1006
1007 return pte;
1008}
1009
1010/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001011 * Generic mapping functions. It maps a physical address into a DMA
1012 * address space. It allocates the page table pages if necessary.
1013 * In the future it can be extended to a generic mapping function
1014 * supporting all features of AMD IOMMU page tables like level skipping
1015 * and full 64 bit address spaces.
1016 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001017static int iommu_map_page(struct protection_domain *dom,
1018 unsigned long bus_addr,
1019 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001020 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001021 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001022{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001023 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001024 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001025
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001026 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001027 return -EINVAL;
1028
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001029 bus_addr = PAGE_ALIGN(bus_addr);
1030 phys_addr = PAGE_ALIGN(phys_addr);
1031 count = PAGE_SIZE_PTE_COUNT(page_size);
1032 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001033
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001034 for (i = 0; i < count; ++i)
1035 if (IOMMU_PTE_PRESENT(pte[i]))
1036 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001037
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001038 if (page_size > PAGE_SIZE) {
1039 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1040 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1041 } else
1042 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1043
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001044 if (prot & IOMMU_PROT_IR)
1045 __pte |= IOMMU_PTE_IR;
1046 if (prot & IOMMU_PROT_IW)
1047 __pte |= IOMMU_PTE_IW;
1048
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001049 for (i = 0; i < count; ++i)
1050 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001051
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001052 update_domain(dom);
1053
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001054 return 0;
1055}
1056
Joerg Roedel24cd7722010-01-19 17:27:39 +01001057static unsigned long iommu_unmap_page(struct protection_domain *dom,
1058 unsigned long bus_addr,
1059 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001060{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001061 unsigned long long unmap_size, unmapped;
1062 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001063
Joerg Roedel24cd7722010-01-19 17:27:39 +01001064 BUG_ON(!is_power_of_2(page_size));
1065
1066 unmapped = 0;
1067
1068 while (unmapped < page_size) {
1069
1070 pte = fetch_pte(dom, bus_addr);
1071
1072 if (!pte) {
1073 /*
1074 * No PTE for this address
1075 * move forward in 4kb steps
1076 */
1077 unmap_size = PAGE_SIZE;
1078 } else if (PM_PTE_LEVEL(*pte) == 0) {
1079 /* 4kb PTE found for this address */
1080 unmap_size = PAGE_SIZE;
1081 *pte = 0ULL;
1082 } else {
1083 int count, i;
1084
1085 /* Large PTE found which maps this address */
1086 unmap_size = PTE_PAGE_SIZE(*pte);
1087 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1088 for (i = 0; i < count; i++)
1089 pte[i] = 0ULL;
1090 }
1091
1092 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1093 unmapped += unmap_size;
1094 }
1095
1096 BUG_ON(!is_power_of_2(unmapped));
1097
1098 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001099}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001100
Joerg Roedel431b2a22008-07-11 17:14:22 +02001101/*
1102 * This function checks if a specific unity mapping entry is needed for
1103 * this specific IOMMU.
1104 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001105static int iommu_for_unity_map(struct amd_iommu *iommu,
1106 struct unity_map_entry *entry)
1107{
1108 u16 bdf, i;
1109
1110 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1111 bdf = amd_iommu_alias_table[i];
1112 if (amd_iommu_rlookup_table[bdf] == iommu)
1113 return 1;
1114 }
1115
1116 return 0;
1117}
1118
Joerg Roedel431b2a22008-07-11 17:14:22 +02001119/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001120 * This function actually applies the mapping to the page table of the
1121 * dma_ops domain.
1122 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001123static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1124 struct unity_map_entry *e)
1125{
1126 u64 addr;
1127 int ret;
1128
1129 for (addr = e->address_start; addr < e->address_end;
1130 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001131 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001132 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001133 if (ret)
1134 return ret;
1135 /*
1136 * if unity mapping is in aperture range mark the page
1137 * as allocated in the aperture
1138 */
1139 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001140 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001141 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001142 }
1143
1144 return 0;
1145}
1146
Joerg Roedel431b2a22008-07-11 17:14:22 +02001147/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001148 * Init the unity mappings for a specific IOMMU in the system
1149 *
1150 * Basically iterates over all unity mapping entries and applies them to
1151 * the default domain DMA of that IOMMU if necessary.
1152 */
1153static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1154{
1155 struct unity_map_entry *entry;
1156 int ret;
1157
1158 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1159 if (!iommu_for_unity_map(iommu, entry))
1160 continue;
1161 ret = dma_ops_unity_map(iommu->default_dom, entry);
1162 if (ret)
1163 return ret;
1164 }
1165
1166 return 0;
1167}
1168
1169/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001170 * Inits the unity mappings required for a specific device
1171 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001172static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1173 u16 devid)
1174{
1175 struct unity_map_entry *e;
1176 int ret;
1177
1178 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1179 if (!(devid >= e->devid_start && devid <= e->devid_end))
1180 continue;
1181 ret = dma_ops_unity_map(dma_dom, e);
1182 if (ret)
1183 return ret;
1184 }
1185
1186 return 0;
1187}
1188
Joerg Roedel431b2a22008-07-11 17:14:22 +02001189/****************************************************************************
1190 *
1191 * The next functions belong to the address allocator for the dma_ops
1192 * interface functions. They work like the allocators in the other IOMMU
1193 * drivers. Its basically a bitmap which marks the allocated pages in
1194 * the aperture. Maybe it could be enhanced in the future to a more
1195 * efficient allocator.
1196 *
1197 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001198
Joerg Roedel431b2a22008-07-11 17:14:22 +02001199/*
Joerg Roedel384de722009-05-15 12:30:05 +02001200 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001201 *
1202 * called with domain->lock held
1203 */
Joerg Roedel384de722009-05-15 12:30:05 +02001204
Joerg Roedel9cabe892009-05-18 16:38:55 +02001205/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001206 * Used to reserve address ranges in the aperture (e.g. for exclusion
1207 * ranges.
1208 */
1209static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1210 unsigned long start_page,
1211 unsigned int pages)
1212{
1213 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1214
1215 if (start_page + pages > last_page)
1216 pages = last_page - start_page;
1217
1218 for (i = start_page; i < start_page + pages; ++i) {
1219 int index = i / APERTURE_RANGE_PAGES;
1220 int page = i % APERTURE_RANGE_PAGES;
1221 __set_bit(page, dom->aperture[index]->bitmap);
1222 }
1223}
1224
1225/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001226 * This function is used to add a new aperture range to an existing
1227 * aperture in case of dma_ops domain allocation or address allocation
1228 * failure.
1229 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001230static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001231 bool populate, gfp_t gfp)
1232{
1233 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001234 struct amd_iommu *iommu;
Joerg Roedel17f5b562011-07-06 17:14:44 +02001235 unsigned long i, old_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001236
Joerg Roedelf5e97052009-05-22 12:31:53 +02001237#ifdef CONFIG_IOMMU_STRESS
1238 populate = false;
1239#endif
1240
Joerg Roedel9cabe892009-05-18 16:38:55 +02001241 if (index >= APERTURE_MAX_RANGES)
1242 return -ENOMEM;
1243
1244 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1245 if (!dma_dom->aperture[index])
1246 return -ENOMEM;
1247
1248 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1249 if (!dma_dom->aperture[index]->bitmap)
1250 goto out_free;
1251
1252 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1253
1254 if (populate) {
1255 unsigned long address = dma_dom->aperture_size;
1256 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1257 u64 *pte, *pte_page;
1258
1259 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001260 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001261 &pte_page, gfp);
1262 if (!pte)
1263 goto out_free;
1264
1265 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1266
1267 address += APERTURE_RANGE_SIZE / 64;
1268 }
1269 }
1270
Joerg Roedel17f5b562011-07-06 17:14:44 +02001271 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001272 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1273
Joerg Roedel17f5b562011-07-06 17:14:44 +02001274 /* Reserve address range used for MSI messages */
1275 if (old_size < MSI_ADDR_BASE_LO &&
1276 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1277 unsigned long spage;
1278 int pages;
1279
1280 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1281 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1282
1283 dma_ops_reserve_addresses(dma_dom, spage, pages);
1284 }
1285
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001286 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001287 for_each_iommu(iommu) {
1288 if (iommu->exclusion_start &&
1289 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1290 && iommu->exclusion_start < dma_dom->aperture_size) {
1291 unsigned long startpage;
1292 int pages = iommu_num_pages(iommu->exclusion_start,
1293 iommu->exclusion_length,
1294 PAGE_SIZE);
1295 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1296 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1297 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001298 }
1299
1300 /*
1301 * Check for areas already mapped as present in the new aperture
1302 * range and mark those pages as reserved in the allocator. Such
1303 * mappings may already exist as a result of requested unity
1304 * mappings for devices.
1305 */
1306 for (i = dma_dom->aperture[index]->offset;
1307 i < dma_dom->aperture_size;
1308 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001309 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001310 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1311 continue;
1312
Joerg Roedelfcd08612011-10-11 17:41:32 +02001313 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001314 }
1315
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001316 update_domain(&dma_dom->domain);
1317
Joerg Roedel9cabe892009-05-18 16:38:55 +02001318 return 0;
1319
1320out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001321 update_domain(&dma_dom->domain);
1322
Joerg Roedel9cabe892009-05-18 16:38:55 +02001323 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1324
1325 kfree(dma_dom->aperture[index]);
1326 dma_dom->aperture[index] = NULL;
1327
1328 return -ENOMEM;
1329}
1330
Joerg Roedel384de722009-05-15 12:30:05 +02001331static unsigned long dma_ops_area_alloc(struct device *dev,
1332 struct dma_ops_domain *dom,
1333 unsigned int pages,
1334 unsigned long align_mask,
1335 u64 dma_mask,
1336 unsigned long start)
1337{
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001338 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001339 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1340 int i = start >> APERTURE_RANGE_SHIFT;
1341 unsigned long boundary_size;
1342 unsigned long address = -1;
1343 unsigned long limit;
1344
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001345 next_bit >>= PAGE_SHIFT;
1346
Joerg Roedel384de722009-05-15 12:30:05 +02001347 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1348 PAGE_SIZE) >> PAGE_SHIFT;
1349
1350 for (;i < max_index; ++i) {
1351 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1352
1353 if (dom->aperture[i]->offset >= dma_mask)
1354 break;
1355
1356 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1357 dma_mask >> PAGE_SHIFT);
1358
1359 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1360 limit, next_bit, pages, 0,
1361 boundary_size, align_mask);
1362 if (address != -1) {
1363 address = dom->aperture[i]->offset +
1364 (address << PAGE_SHIFT);
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001365 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001366 break;
1367 }
1368
1369 next_bit = 0;
1370 }
1371
1372 return address;
1373}
1374
Joerg Roedeld3086442008-06-26 21:27:57 +02001375static unsigned long dma_ops_alloc_addresses(struct device *dev,
1376 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001377 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001378 unsigned long align_mask,
1379 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001380{
Joerg Roedeld3086442008-06-26 21:27:57 +02001381 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001382
Joerg Roedelfe16f082009-05-22 12:27:53 +02001383#ifdef CONFIG_IOMMU_STRESS
1384 dom->next_address = 0;
1385 dom->need_flush = true;
1386#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001387
Joerg Roedel384de722009-05-15 12:30:05 +02001388 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001389 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001390
Joerg Roedel1c655772008-09-04 18:40:05 +02001391 if (address == -1) {
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001392 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001393 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1394 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001395 dom->need_flush = true;
1396 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001397
Joerg Roedel384de722009-05-15 12:30:05 +02001398 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001399 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001400
1401 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1402
1403 return address;
1404}
1405
Joerg Roedel431b2a22008-07-11 17:14:22 +02001406/*
1407 * The address free function.
1408 *
1409 * called with domain->lock held
1410 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001411static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1412 unsigned long address,
1413 unsigned int pages)
1414{
Joerg Roedel384de722009-05-15 12:30:05 +02001415 unsigned i = address >> APERTURE_RANGE_SHIFT;
1416 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001417
Joerg Roedel384de722009-05-15 12:30:05 +02001418 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1419
Joerg Roedel47bccd62009-05-22 12:40:54 +02001420#ifdef CONFIG_IOMMU_STRESS
1421 if (i < 4)
1422 return;
1423#endif
1424
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001425 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001426 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001427
1428 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001429
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001430 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001431
Joerg Roedeld3086442008-06-26 21:27:57 +02001432}
1433
Joerg Roedel431b2a22008-07-11 17:14:22 +02001434/****************************************************************************
1435 *
1436 * The next functions belong to the domain allocation. A domain is
1437 * allocated for every IOMMU as the default domain. If device isolation
1438 * is enabled, every device get its own domain. The most important thing
1439 * about domains is the page table mapping the DMA address space they
1440 * contain.
1441 *
1442 ****************************************************************************/
1443
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001444/*
1445 * This function adds a protection domain to the global protection domain list
1446 */
1447static void add_domain_to_list(struct protection_domain *domain)
1448{
1449 unsigned long flags;
1450
1451 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1452 list_add(&domain->list, &amd_iommu_pd_list);
1453 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1454}
1455
1456/*
1457 * This function removes a protection domain to the global
1458 * protection domain list
1459 */
1460static void del_domain_from_list(struct protection_domain *domain)
1461{
1462 unsigned long flags;
1463
1464 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1465 list_del(&domain->list);
1466 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1467}
1468
Joerg Roedelec487d12008-06-26 21:27:58 +02001469static u16 domain_id_alloc(void)
1470{
1471 unsigned long flags;
1472 int id;
1473
1474 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1475 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1476 BUG_ON(id == 0);
1477 if (id > 0 && id < MAX_DOMAIN_ID)
1478 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1479 else
1480 id = 0;
1481 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1482
1483 return id;
1484}
1485
Joerg Roedela2acfb72008-12-02 18:28:53 +01001486static void domain_id_free(int id)
1487{
1488 unsigned long flags;
1489
1490 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1491 if (id > 0 && id < MAX_DOMAIN_ID)
1492 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1493 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1494}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001495
Joerg Roedel86db2e52008-12-02 18:20:21 +01001496static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001497{
1498 int i, j;
1499 u64 *p1, *p2, *p3;
1500
Joerg Roedel86db2e52008-12-02 18:20:21 +01001501 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001502
1503 if (!p1)
1504 return;
1505
1506 for (i = 0; i < 512; ++i) {
1507 if (!IOMMU_PTE_PRESENT(p1[i]))
1508 continue;
1509
1510 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001511 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001512 if (!IOMMU_PTE_PRESENT(p2[j]))
1513 continue;
1514 p3 = IOMMU_PTE_PAGE(p2[j]);
1515 free_page((unsigned long)p3);
1516 }
1517
1518 free_page((unsigned long)p2);
1519 }
1520
1521 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001522
1523 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001524}
1525
Joerg Roedel431b2a22008-07-11 17:14:22 +02001526/*
1527 * Free a domain, only used if something went wrong in the
1528 * allocation path and we need to free an already allocated page table
1529 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001530static void dma_ops_domain_free(struct dma_ops_domain *dom)
1531{
Joerg Roedel384de722009-05-15 12:30:05 +02001532 int i;
1533
Joerg Roedelec487d12008-06-26 21:27:58 +02001534 if (!dom)
1535 return;
1536
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001537 del_domain_from_list(&dom->domain);
1538
Joerg Roedel86db2e52008-12-02 18:20:21 +01001539 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001540
Joerg Roedel384de722009-05-15 12:30:05 +02001541 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1542 if (!dom->aperture[i])
1543 continue;
1544 free_page((unsigned long)dom->aperture[i]->bitmap);
1545 kfree(dom->aperture[i]);
1546 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001547
1548 kfree(dom);
1549}
1550
Joerg Roedel431b2a22008-07-11 17:14:22 +02001551/*
1552 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001553 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001554 * structures required for the dma_ops interface
1555 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001556static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001557{
1558 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001559
1560 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1561 if (!dma_dom)
1562 return NULL;
1563
1564 spin_lock_init(&dma_dom->domain.lock);
1565
1566 dma_dom->domain.id = domain_id_alloc();
1567 if (dma_dom->domain.id == 0)
1568 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001569 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001570 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001571 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001572 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001573 dma_dom->domain.priv = dma_dom;
1574 if (!dma_dom->domain.pt_root)
1575 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001576
Joerg Roedel1c655772008-09-04 18:40:05 +02001577 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001578 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001579
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001580 add_domain_to_list(&dma_dom->domain);
1581
Joerg Roedel576175c2009-11-23 19:08:46 +01001582 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001583 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001584
Joerg Roedel431b2a22008-07-11 17:14:22 +02001585 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001586 * mark the first page as allocated so we never return 0 as
1587 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001588 */
Joerg Roedel384de722009-05-15 12:30:05 +02001589 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001590 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001591
Joerg Roedelec487d12008-06-26 21:27:58 +02001592
1593 return dma_dom;
1594
1595free_dma_dom:
1596 dma_ops_domain_free(dma_dom);
1597
1598 return NULL;
1599}
1600
Joerg Roedel431b2a22008-07-11 17:14:22 +02001601/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001602 * little helper function to check whether a given protection domain is a
1603 * dma_ops domain
1604 */
1605static bool dma_ops_domain(struct protection_domain *domain)
1606{
1607 return domain->flags & PD_DMA_OPS_MASK;
1608}
1609
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001610static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001611{
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001612 u64 pte_root = virt_to_phys(domain->pt_root);
Joerg Roedelee6c2862011-11-09 12:06:03 +01001613 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001614
Joerg Roedel38ddf412008-09-11 10:38:32 +02001615 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1616 << DEV_ENTRY_MODE_SHIFT;
1617 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001618
Joerg Roedelee6c2862011-11-09 12:06:03 +01001619 flags = amd_iommu_dev_table[devid].data[1];
1620
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001621 if (ats)
1622 flags |= DTE_FLAG_IOTLB;
1623
Joerg Roedelee6c2862011-11-09 12:06:03 +01001624 flags &= ~(0xffffUL);
1625 flags |= domain->id;
1626
1627 amd_iommu_dev_table[devid].data[1] = flags;
1628 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001629}
1630
Joerg Roedel15898bb2009-11-24 15:39:42 +01001631static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001632{
Joerg Roedel355bf552008-12-08 12:02:41 +01001633 /* remove entry from the device table seen by the hardware */
1634 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1635 amd_iommu_dev_table[devid].data[1] = 0;
Joerg Roedel355bf552008-12-08 12:02:41 +01001636
Joerg Roedelc5cca142009-10-09 18:31:20 +02001637 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001638}
1639
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001640static void do_attach(struct iommu_dev_data *dev_data,
1641 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001642{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001643 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001644 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001645
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001646 iommu = amd_iommu_rlookup_table[dev_data->devid];
1647 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001648
1649 /* Update data structures */
1650 dev_data->domain = domain;
1651 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001652 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001653
1654 /* Do reference counting */
1655 domain->dev_iommu[iommu->index] += 1;
1656 domain->dev_cnt += 1;
1657
1658 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001659 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001660}
1661
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001662static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001663{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001664 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001665
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001666 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02001667
Joerg Roedelc4596112009-11-20 14:57:32 +01001668 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001669 dev_data->domain->dev_iommu[iommu->index] -= 1;
1670 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001671
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001672 /* Update data structures */
1673 dev_data->domain = NULL;
1674 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001675 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001676
1677 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001678 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001679}
1680
1681/*
1682 * If a device is not yet associated with a domain, this function does
1683 * assigns it visible for the hardware
1684 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001685static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01001686 struct protection_domain *domain)
1687{
Julia Lawall84fe6c12010-05-27 12:31:51 +02001688 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001689
Joerg Roedel15898bb2009-11-24 15:39:42 +01001690 /* lock domain */
1691 spin_lock(&domain->lock);
1692
Joerg Roedel71f77582011-06-09 19:03:15 +02001693 if (dev_data->alias_data != NULL) {
1694 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001695
Joerg Roedel2b02b092011-06-09 17:48:39 +02001696 /* Some sanity checks */
1697 ret = -EBUSY;
1698 if (alias_data->domain != NULL &&
1699 alias_data->domain != domain)
1700 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001701
Joerg Roedel2b02b092011-06-09 17:48:39 +02001702 if (dev_data->domain != NULL &&
1703 dev_data->domain != domain)
1704 goto out_unlock;
1705
1706 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001707 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001708 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01001709
1710 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001711 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01001712
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001713 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001714 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001715
Joerg Roedel24100052009-11-25 15:59:57 +01001716 atomic_inc(&dev_data->bind);
1717
Julia Lawall84fe6c12010-05-27 12:31:51 +02001718 ret = 0;
1719
1720out_unlock:
1721
Joerg Roedel355bf552008-12-08 12:02:41 +01001722 /* ready */
1723 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001724
Julia Lawall84fe6c12010-05-27 12:31:51 +02001725 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001726}
1727
1728/*
1729 * If a device is not yet associated with a domain, this function does
1730 * assigns it visible for the hardware
1731 */
1732static int attach_device(struct device *dev,
1733 struct protection_domain *domain)
1734{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001735 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001736 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001737 unsigned long flags;
1738 int ret;
1739
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001740 dev_data = get_dev_data(dev);
1741
1742 if (amd_iommu_iotlb_sup && pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
1743 dev_data->ats.enabled = true;
1744 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1745 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001746
Joerg Roedel15898bb2009-11-24 15:39:42 +01001747 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001748 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001749 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1750
1751 /*
1752 * We might boot into a crash-kernel here. The crashed kernel
1753 * left the caches in the IOMMU dirty. So we have to flush
1754 * here to evict all dirty stuff.
1755 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001756 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001757
1758 return ret;
1759}
1760
1761/*
1762 * Removes a device from a protection domain (unlocked)
1763 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001764static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01001765{
Joerg Roedel2ca76272010-01-22 16:45:31 +01001766 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001767 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001768
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001769 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001770
Joerg Roedel2ca76272010-01-22 16:45:31 +01001771 domain = dev_data->domain;
1772
1773 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01001774
Joerg Roedel71f77582011-06-09 19:03:15 +02001775 if (dev_data->alias_data != NULL) {
1776 struct iommu_dev_data *alias_data = dev_data->alias_data;
1777
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001778 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001779 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01001780 }
1781
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001782 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001783 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001784
Joerg Roedel2ca76272010-01-22 16:45:31 +01001785 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001786
Joerg Roedel21129f72009-09-01 11:59:42 +02001787 /*
1788 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001789 * passthrough domain if it is detached from any other domain.
1790 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02001791 */
Joerg Roedel5abcdba2011-12-01 15:49:45 +01001792 if (dev_data->passthrough &&
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001793 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001794 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01001795}
1796
1797/*
1798 * Removes a device from a protection domain (with devtable_lock held)
1799 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01001800static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01001801{
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001802 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01001803 unsigned long flags;
1804
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001805 dev_data = get_dev_data(dev);
1806
Joerg Roedel355bf552008-12-08 12:02:41 +01001807 /* lock device table */
1808 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001809 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01001810 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001811
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001812 if (dev_data->ats.enabled) {
1813 pci_disable_ats(to_pci_dev(dev));
1814 dev_data->ats.enabled = false;
1815 }
Joerg Roedel355bf552008-12-08 12:02:41 +01001816}
Joerg Roedele275a2a2008-12-10 18:27:25 +01001817
Joerg Roedel15898bb2009-11-24 15:39:42 +01001818/*
1819 * Find out the protection domain structure for a given PCI device. This
1820 * will give us the pointer to the page table root for example.
1821 */
1822static struct protection_domain *domain_for_device(struct device *dev)
1823{
Joerg Roedel71f77582011-06-09 19:03:15 +02001824 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02001825 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001826 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001827
Joerg Roedel657cbb62009-11-23 15:26:46 +01001828 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001829
Joerg Roedel2b02b092011-06-09 17:48:39 +02001830 if (dev_data->domain)
1831 return dev_data->domain;
1832
Joerg Roedel71f77582011-06-09 19:03:15 +02001833 if (dev_data->alias_data != NULL) {
1834 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02001835
1836 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
1837 if (alias_data->domain != NULL) {
1838 __attach_device(dev_data, alias_data->domain);
1839 dom = alias_data->domain;
1840 }
1841 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001842 }
1843
Joerg Roedel15898bb2009-11-24 15:39:42 +01001844 return dom;
1845}
1846
Joerg Roedele275a2a2008-12-10 18:27:25 +01001847static int device_change_notifier(struct notifier_block *nb,
1848 unsigned long action, void *data)
1849{
Joerg Roedele275a2a2008-12-10 18:27:25 +01001850 struct dma_ops_domain *dma_domain;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01001851 struct protection_domain *domain;
1852 struct iommu_dev_data *dev_data;
1853 struct device *dev = data;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001854 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001855 unsigned long flags;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01001856 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001857
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001858 if (!check_device(dev))
1859 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001860
Joerg Roedel5abcdba2011-12-01 15:49:45 +01001861 devid = get_device_id(dev);
1862 iommu = amd_iommu_rlookup_table[devid];
1863 dev_data = get_dev_data(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001864
1865 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07001866 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001867
1868 domain = domain_for_device(dev);
1869
Joerg Roedele275a2a2008-12-10 18:27:25 +01001870 if (!domain)
1871 goto out;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01001872 if (dev_data->passthrough)
Joerg Roedela1ca3312009-09-01 12:22:22 +02001873 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001874 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001875 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001876 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001877
1878 iommu_init_device(dev);
1879
1880 domain = domain_for_device(dev);
1881
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001882 /* allocate a protection domain if a device is added */
1883 dma_domain = find_protection_domain(devid);
1884 if (dma_domain)
1885 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01001886 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001887 if (!dma_domain)
1888 goto out;
1889 dma_domain->target_dev = devid;
1890
1891 spin_lock_irqsave(&iommu_pd_list_lock, flags);
1892 list_add_tail(&dma_domain->list, &iommu_pd_list);
1893 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
1894
1895 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001896 case BUS_NOTIFY_DEL_DEVICE:
1897
1898 iommu_uninit_device(dev);
1899
Joerg Roedele275a2a2008-12-10 18:27:25 +01001900 default:
1901 goto out;
1902 }
1903
Joerg Roedele275a2a2008-12-10 18:27:25 +01001904 iommu_completion_wait(iommu);
1905
1906out:
1907 return 0;
1908}
1909
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05301910static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01001911 .notifier_call = device_change_notifier,
1912};
Joerg Roedel355bf552008-12-08 12:02:41 +01001913
Joerg Roedel8638c492009-12-10 11:12:25 +01001914void amd_iommu_init_notifier(void)
1915{
1916 bus_register_notifier(&pci_bus_type, &device_nb);
1917}
1918
Joerg Roedel431b2a22008-07-11 17:14:22 +02001919/*****************************************************************************
1920 *
1921 * The next functions belong to the dma_ops mapping/unmapping code.
1922 *
1923 *****************************************************************************/
1924
1925/*
1926 * In the dma_ops path we only have the struct device. This function
1927 * finds the corresponding IOMMU, the protection domain and the
1928 * requestor id for a given device.
1929 * If the device is not yet associated with a domain this is also done
1930 * in this function.
1931 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001932static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001933{
Joerg Roedel94f6d192009-11-24 16:40:02 +01001934 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001935 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01001936 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001937
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001938 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01001939 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001940
Joerg Roedel94f6d192009-11-24 16:40:02 +01001941 domain = domain_for_device(dev);
1942 if (domain != NULL && !dma_ops_domain(domain))
1943 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001944
Joerg Roedel94f6d192009-11-24 16:40:02 +01001945 if (domain != NULL)
1946 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001947
Joerg Roedel15898bb2009-11-24 15:39:42 +01001948 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001949 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001950 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01001951 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
1952 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001953 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01001954 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01001955
Joerg Roedel94f6d192009-11-24 16:40:02 +01001956 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001957}
1958
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001959static void update_device_table(struct protection_domain *domain)
1960{
Joerg Roedel492667d2009-11-27 13:25:47 +01001961 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001962
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001963 list_for_each_entry(dev_data, &domain->dev_list, list)
1964 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001965}
1966
1967static void update_domain(struct protection_domain *domain)
1968{
1969 if (!domain->updated)
1970 return;
1971
1972 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02001973
1974 domain_flush_devices(domain);
1975 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001976
1977 domain->updated = false;
1978}
1979
Joerg Roedel431b2a22008-07-11 17:14:22 +02001980/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02001981 * This function fetches the PTE for a given address in the aperture
1982 */
1983static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
1984 unsigned long address)
1985{
Joerg Roedel384de722009-05-15 12:30:05 +02001986 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02001987 u64 *pte, *pte_page;
1988
Joerg Roedel384de722009-05-15 12:30:05 +02001989 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1990 if (!aperture)
1991 return NULL;
1992
1993 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02001994 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001995 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001996 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02001997 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
1998 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001999 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002000
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002001 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002002
2003 return pte;
2004}
2005
2006/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002007 * This is the generic map function. It maps one 4kb page at paddr to
2008 * the given address in the DMA address space for the domain.
2009 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002010static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002011 unsigned long address,
2012 phys_addr_t paddr,
2013 int direction)
2014{
2015 u64 *pte, __pte;
2016
2017 WARN_ON(address > dom->aperture_size);
2018
2019 paddr &= PAGE_MASK;
2020
Joerg Roedel8bda3092009-05-12 12:02:46 +02002021 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002022 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002023 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002024
2025 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2026
2027 if (direction == DMA_TO_DEVICE)
2028 __pte |= IOMMU_PTE_IR;
2029 else if (direction == DMA_FROM_DEVICE)
2030 __pte |= IOMMU_PTE_IW;
2031 else if (direction == DMA_BIDIRECTIONAL)
2032 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2033
2034 WARN_ON(*pte);
2035
2036 *pte = __pte;
2037
2038 return (dma_addr_t)address;
2039}
2040
Joerg Roedel431b2a22008-07-11 17:14:22 +02002041/*
2042 * The generic unmapping function for on page in the DMA address space.
2043 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002044static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002045 unsigned long address)
2046{
Joerg Roedel384de722009-05-15 12:30:05 +02002047 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002048 u64 *pte;
2049
2050 if (address >= dom->aperture_size)
2051 return;
2052
Joerg Roedel384de722009-05-15 12:30:05 +02002053 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2054 if (!aperture)
2055 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002056
Joerg Roedel384de722009-05-15 12:30:05 +02002057 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2058 if (!pte)
2059 return;
2060
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002061 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002062
2063 WARN_ON(!*pte);
2064
2065 *pte = 0ULL;
2066}
2067
Joerg Roedel431b2a22008-07-11 17:14:22 +02002068/*
2069 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002070 * contiguous memory region into DMA address space. It is used by all
2071 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002072 * Must be called with the domain lock held.
2073 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002074static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002075 struct dma_ops_domain *dma_dom,
2076 phys_addr_t paddr,
2077 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002078 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002079 bool align,
2080 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002081{
2082 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002083 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002084 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002085 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002086 int i;
2087
Joerg Roedele3c449f2008-10-15 22:02:11 -07002088 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002089 paddr &= PAGE_MASK;
2090
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002091 INC_STATS_COUNTER(total_map_requests);
2092
Joerg Roedelc1858972008-12-12 15:42:39 +01002093 if (pages > 1)
2094 INC_STATS_COUNTER(cross_page);
2095
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002096 if (align)
2097 align_mask = (1UL << get_order(size)) - 1;
2098
Joerg Roedel11b83882009-05-19 10:23:15 +02002099retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002100 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2101 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002102 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002103 /*
2104 * setting next_address here will let the address
2105 * allocator only scan the new allocated range in the
2106 * first run. This is a small optimization.
2107 */
2108 dma_dom->next_address = dma_dom->aperture_size;
2109
Joerg Roedel576175c2009-11-23 19:08:46 +01002110 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002111 goto out;
2112
2113 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002114 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002115 * allocation again
2116 */
2117 goto retry;
2118 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002119
2120 start = address;
2121 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002122 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002123 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002124 goto out_unmap;
2125
Joerg Roedelcb76c322008-06-26 21:28:00 +02002126 paddr += PAGE_SIZE;
2127 start += PAGE_SIZE;
2128 }
2129 address += offset;
2130
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002131 ADD_STATS_COUNTER(alloced_io_mem, size);
2132
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002133 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002134 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002135 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002136 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002137 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002138
Joerg Roedelcb76c322008-06-26 21:28:00 +02002139out:
2140 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002141
2142out_unmap:
2143
2144 for (--i; i >= 0; --i) {
2145 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002146 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002147 }
2148
2149 dma_ops_free_addresses(dma_dom, address, pages);
2150
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002151 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002152}
2153
Joerg Roedel431b2a22008-07-11 17:14:22 +02002154/*
2155 * Does the reverse of the __map_single function. Must be called with
2156 * the domain lock held too
2157 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002158static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002159 dma_addr_t dma_addr,
2160 size_t size,
2161 int dir)
2162{
Joerg Roedel04e04632010-09-23 16:12:48 +02002163 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002164 dma_addr_t i, start;
2165 unsigned int pages;
2166
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002167 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002168 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002169 return;
2170
Joerg Roedel04e04632010-09-23 16:12:48 +02002171 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002172 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002173 dma_addr &= PAGE_MASK;
2174 start = dma_addr;
2175
2176 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002177 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002178 start += PAGE_SIZE;
2179 }
2180
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002181 SUB_STATS_COUNTER(alloced_io_mem, size);
2182
Joerg Roedelcb76c322008-06-26 21:28:00 +02002183 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002184
Joerg Roedel80be3082008-11-06 14:59:05 +01002185 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002186 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002187 dma_dom->need_flush = false;
2188 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002189}
2190
Joerg Roedel431b2a22008-07-11 17:14:22 +02002191/*
2192 * The exported map_single function for dma_ops.
2193 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002194static dma_addr_t map_page(struct device *dev, struct page *page,
2195 unsigned long offset, size_t size,
2196 enum dma_data_direction dir,
2197 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002198{
2199 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002200 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002201 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002202 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002203 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002204
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002205 INC_STATS_COUNTER(cnt_map_single);
2206
Joerg Roedel94f6d192009-11-24 16:40:02 +01002207 domain = get_domain(dev);
2208 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002209 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002210 else if (IS_ERR(domain))
2211 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002212
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002213 dma_mask = *dev->dma_mask;
2214
Joerg Roedel4da70b92008-06-26 21:28:01 +02002215 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002216
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002217 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002218 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002219 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002220 goto out;
2221
Joerg Roedel17b124b2011-04-06 18:01:35 +02002222 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002223
2224out:
2225 spin_unlock_irqrestore(&domain->lock, flags);
2226
2227 return addr;
2228}
2229
Joerg Roedel431b2a22008-07-11 17:14:22 +02002230/*
2231 * The exported unmap_single function for dma_ops.
2232 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002233static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2234 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002235{
2236 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002237 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002238
Joerg Roedel146a6912008-12-12 15:07:12 +01002239 INC_STATS_COUNTER(cnt_unmap_single);
2240
Joerg Roedel94f6d192009-11-24 16:40:02 +01002241 domain = get_domain(dev);
2242 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002243 return;
2244
Joerg Roedel4da70b92008-06-26 21:28:01 +02002245 spin_lock_irqsave(&domain->lock, flags);
2246
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002247 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002248
Joerg Roedel17b124b2011-04-06 18:01:35 +02002249 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002250
2251 spin_unlock_irqrestore(&domain->lock, flags);
2252}
2253
Joerg Roedel431b2a22008-07-11 17:14:22 +02002254/*
2255 * This is a special map_sg function which is used if we should map a
2256 * device which is not handled by an AMD IOMMU in the system.
2257 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002258static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2259 int nelems, int dir)
2260{
2261 struct scatterlist *s;
2262 int i;
2263
2264 for_each_sg(sglist, s, nelems, i) {
2265 s->dma_address = (dma_addr_t)sg_phys(s);
2266 s->dma_length = s->length;
2267 }
2268
2269 return nelems;
2270}
2271
Joerg Roedel431b2a22008-07-11 17:14:22 +02002272/*
2273 * The exported map_sg function for dma_ops (handles scatter-gather
2274 * lists).
2275 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002276static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002277 int nelems, enum dma_data_direction dir,
2278 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002279{
2280 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002281 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002282 int i;
2283 struct scatterlist *s;
2284 phys_addr_t paddr;
2285 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002286 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002287
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002288 INC_STATS_COUNTER(cnt_map_sg);
2289
Joerg Roedel94f6d192009-11-24 16:40:02 +01002290 domain = get_domain(dev);
2291 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002292 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002293 else if (IS_ERR(domain))
2294 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002295
Joerg Roedel832a90c2008-09-18 15:54:23 +02002296 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002297
Joerg Roedel65b050a2008-06-26 21:28:02 +02002298 spin_lock_irqsave(&domain->lock, flags);
2299
2300 for_each_sg(sglist, s, nelems, i) {
2301 paddr = sg_phys(s);
2302
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002303 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002304 paddr, s->length, dir, false,
2305 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002306
2307 if (s->dma_address) {
2308 s->dma_length = s->length;
2309 mapped_elems++;
2310 } else
2311 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002312 }
2313
Joerg Roedel17b124b2011-04-06 18:01:35 +02002314 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002315
2316out:
2317 spin_unlock_irqrestore(&domain->lock, flags);
2318
2319 return mapped_elems;
2320unmap:
2321 for_each_sg(sglist, s, mapped_elems, i) {
2322 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002323 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002324 s->dma_length, dir);
2325 s->dma_address = s->dma_length = 0;
2326 }
2327
2328 mapped_elems = 0;
2329
2330 goto out;
2331}
2332
Joerg Roedel431b2a22008-07-11 17:14:22 +02002333/*
2334 * The exported map_sg function for dma_ops (handles scatter-gather
2335 * lists).
2336 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002337static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002338 int nelems, enum dma_data_direction dir,
2339 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002340{
2341 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002342 struct protection_domain *domain;
2343 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002344 int i;
2345
Joerg Roedel55877a62008-12-12 15:12:14 +01002346 INC_STATS_COUNTER(cnt_unmap_sg);
2347
Joerg Roedel94f6d192009-11-24 16:40:02 +01002348 domain = get_domain(dev);
2349 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002350 return;
2351
Joerg Roedel65b050a2008-06-26 21:28:02 +02002352 spin_lock_irqsave(&domain->lock, flags);
2353
2354 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002355 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002356 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002357 s->dma_address = s->dma_length = 0;
2358 }
2359
Joerg Roedel17b124b2011-04-06 18:01:35 +02002360 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002361
2362 spin_unlock_irqrestore(&domain->lock, flags);
2363}
2364
Joerg Roedel431b2a22008-07-11 17:14:22 +02002365/*
2366 * The exported alloc_coherent function for dma_ops.
2367 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002368static void *alloc_coherent(struct device *dev, size_t size,
2369 dma_addr_t *dma_addr, gfp_t flag)
2370{
2371 unsigned long flags;
2372 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002373 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002374 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002375 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002376
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002377 INC_STATS_COUNTER(cnt_alloc_coherent);
2378
Joerg Roedel94f6d192009-11-24 16:40:02 +01002379 domain = get_domain(dev);
2380 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002381 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2382 *dma_addr = __pa(virt_addr);
2383 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002384 } else if (IS_ERR(domain))
2385 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002386
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002387 dma_mask = dev->coherent_dma_mask;
2388 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2389 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002390
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002391 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2392 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302393 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002394
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002395 paddr = virt_to_phys(virt_addr);
2396
Joerg Roedel832a90c2008-09-18 15:54:23 +02002397 if (!dma_mask)
2398 dma_mask = *dev->dma_mask;
2399
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002400 spin_lock_irqsave(&domain->lock, flags);
2401
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002402 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002403 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002404
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002405 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002406 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002407 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002408 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002409
Joerg Roedel17b124b2011-04-06 18:01:35 +02002410 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002411
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002412 spin_unlock_irqrestore(&domain->lock, flags);
2413
2414 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002415
2416out_free:
2417
2418 free_pages((unsigned long)virt_addr, get_order(size));
2419
2420 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002421}
2422
Joerg Roedel431b2a22008-07-11 17:14:22 +02002423/*
2424 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002425 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002426static void free_coherent(struct device *dev, size_t size,
2427 void *virt_addr, dma_addr_t dma_addr)
2428{
2429 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002430 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002431
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002432 INC_STATS_COUNTER(cnt_free_coherent);
2433
Joerg Roedel94f6d192009-11-24 16:40:02 +01002434 domain = get_domain(dev);
2435 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002436 goto free_mem;
2437
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002438 spin_lock_irqsave(&domain->lock, flags);
2439
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002440 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002441
Joerg Roedel17b124b2011-04-06 18:01:35 +02002442 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002443
2444 spin_unlock_irqrestore(&domain->lock, flags);
2445
2446free_mem:
2447 free_pages((unsigned long)virt_addr, get_order(size));
2448}
2449
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002450/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002451 * This function is called by the DMA layer to find out if we can handle a
2452 * particular device. It is part of the dma_ops.
2453 */
2454static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2455{
Joerg Roedel420aef82009-11-23 16:14:57 +01002456 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002457}
2458
2459/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002460 * The function for pre-allocating protection domains.
2461 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002462 * If the driver core informs the DMA layer if a driver grabs a device
2463 * we don't need to preallocate the protection domains anymore.
2464 * For now we have to.
2465 */
Jaswinder Singh Rajput0e93dd82008-12-29 21:45:22 +05302466static void prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002467{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002468 struct iommu_dev_data *dev_data;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002469 struct dma_ops_domain *dma_dom;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002470 struct pci_dev *dev = NULL;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002471 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002472
Chris Wrightd18c69d2010-04-02 18:27:55 -07002473 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002474
2475 /* Do we handle this device? */
2476 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002477 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002478
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002479 dev_data = get_dev_data(&dev->dev);
2480 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
2481 /* Make sure passthrough domain is allocated */
2482 alloc_passthrough_domain();
2483 dev_data->passthrough = true;
2484 attach_device(&dev->dev, pt_domain);
2485 pr_info("AMD-Vi: Using passthough domain for device %s\n",
2486 dev_name(&dev->dev));
2487 }
2488
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002489 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002490 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002491 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002492
2493 devid = get_device_id(&dev->dev);
2494
Joerg Roedel87a64d52009-11-24 17:26:43 +01002495 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002496 if (!dma_dom)
2497 continue;
2498 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002499 dma_dom->target_dev = devid;
2500
Joerg Roedel15898bb2009-11-24 15:39:42 +01002501 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01002502
Joerg Roedelbd60b732008-09-11 10:24:48 +02002503 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002504 }
2505}
2506
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002507static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002508 .alloc_coherent = alloc_coherent,
2509 .free_coherent = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002510 .map_page = map_page,
2511 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002512 .map_sg = map_sg,
2513 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002514 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002515};
2516
Joerg Roedel27c21272011-05-30 15:56:24 +02002517static unsigned device_dma_ops_init(void)
2518{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002519 struct iommu_dev_data *dev_data;
Joerg Roedel27c21272011-05-30 15:56:24 +02002520 struct pci_dev *pdev = NULL;
2521 unsigned unhandled = 0;
2522
2523 for_each_pci_dev(pdev) {
2524 if (!check_device(&pdev->dev)) {
2525 unhandled += 1;
2526 continue;
2527 }
2528
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002529 dev_data = get_dev_data(&pdev->dev);
2530
2531 if (!dev_data->passthrough)
2532 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
2533 else
2534 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
Joerg Roedel27c21272011-05-30 15:56:24 +02002535 }
2536
2537 return unhandled;
2538}
2539
Joerg Roedel431b2a22008-07-11 17:14:22 +02002540/*
2541 * The function which clues the AMD IOMMU driver into dma_ops.
2542 */
Joerg Roedelf5325092010-01-22 17:44:35 +01002543
2544void __init amd_iommu_init_api(void)
2545{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02002546 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01002547}
2548
Joerg Roedel6631ee92008-06-26 21:28:05 +02002549int __init amd_iommu_init_dma_ops(void)
2550{
2551 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02002552 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002553
Joerg Roedel431b2a22008-07-11 17:14:22 +02002554 /*
2555 * first allocate a default protection domain for every IOMMU we
2556 * found in the system. Devices not assigned to any other
2557 * protection domain will be assigned to the default one.
2558 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02002559 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01002560 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002561 if (iommu->default_dom == NULL)
2562 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01002563 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002564 ret = iommu_init_unity_mappings(iommu);
2565 if (ret)
2566 goto free_domains;
2567 }
2568
Joerg Roedel431b2a22008-07-11 17:14:22 +02002569 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01002570 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002571 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01002572 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002573
2574 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002575 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002576
Joerg Roedel431b2a22008-07-11 17:14:22 +02002577 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02002578 unhandled = device_dma_ops_init();
2579 if (unhandled && max_pfn > MAX_DMA32_PFN) {
2580 /* There are unhandled devices - initialize swiotlb for them */
2581 swiotlb = 1;
2582 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02002583
Joerg Roedel7f265082008-12-12 13:50:21 +01002584 amd_iommu_stats_init();
2585
Joerg Roedel6631ee92008-06-26 21:28:05 +02002586 return 0;
2587
2588free_domains:
2589
Joerg Roedel3bd22172009-05-04 15:06:20 +02002590 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002591 if (iommu->default_dom)
2592 dma_ops_domain_free(iommu->default_dom);
2593 }
2594
2595 return ret;
2596}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002597
2598/*****************************************************************************
2599 *
2600 * The following functions belong to the exported interface of AMD IOMMU
2601 *
2602 * This interface allows access to lower level functions of the IOMMU
2603 * like protection domain handling and assignement of devices to domains
2604 * which is not possible with the dma_ops interface.
2605 *
2606 *****************************************************************************/
2607
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002608static void cleanup_domain(struct protection_domain *domain)
2609{
Joerg Roedel492667d2009-11-27 13:25:47 +01002610 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002611 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002612
2613 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2614
Joerg Roedel492667d2009-11-27 13:25:47 +01002615 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002616 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01002617 atomic_set(&dev_data->bind, 0);
2618 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002619
2620 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2621}
2622
Joerg Roedel26508152009-08-26 16:52:40 +02002623static void protection_domain_free(struct protection_domain *domain)
2624{
2625 if (!domain)
2626 return;
2627
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002628 del_domain_from_list(domain);
2629
Joerg Roedel26508152009-08-26 16:52:40 +02002630 if (domain->id)
2631 domain_id_free(domain->id);
2632
2633 kfree(domain);
2634}
2635
2636static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002637{
2638 struct protection_domain *domain;
2639
2640 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2641 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002642 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002643
2644 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002645 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01002646 domain->id = domain_id_alloc();
2647 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02002648 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002649 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02002650
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002651 add_domain_to_list(domain);
2652
Joerg Roedel26508152009-08-26 16:52:40 +02002653 return domain;
2654
2655out_err:
2656 kfree(domain);
2657
2658 return NULL;
2659}
2660
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002661static int __init alloc_passthrough_domain(void)
2662{
2663 if (pt_domain != NULL)
2664 return 0;
2665
2666 /* allocate passthrough domain */
2667 pt_domain = protection_domain_alloc();
2668 if (!pt_domain)
2669 return -ENOMEM;
2670
2671 pt_domain->mode = PAGE_MODE_NONE;
2672
2673 return 0;
2674}
Joerg Roedel26508152009-08-26 16:52:40 +02002675static int amd_iommu_domain_init(struct iommu_domain *dom)
2676{
2677 struct protection_domain *domain;
2678
2679 domain = protection_domain_alloc();
2680 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01002681 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02002682
2683 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002684 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2685 if (!domain->pt_root)
2686 goto out_free;
2687
2688 dom->priv = domain;
2689
2690 return 0;
2691
2692out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02002693 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01002694
2695 return -ENOMEM;
2696}
2697
Joerg Roedel98383fc2008-12-02 18:34:12 +01002698static void amd_iommu_domain_destroy(struct iommu_domain *dom)
2699{
2700 struct protection_domain *domain = dom->priv;
2701
2702 if (!domain)
2703 return;
2704
2705 if (domain->dev_cnt > 0)
2706 cleanup_domain(domain);
2707
2708 BUG_ON(domain->dev_cnt != 0);
2709
2710 free_pagetable(domain);
2711
Joerg Roedel8b408fe2010-03-08 14:20:07 +01002712 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002713
2714 dom->priv = NULL;
2715}
2716
Joerg Roedel684f2882008-12-08 12:07:44 +01002717static void amd_iommu_detach_device(struct iommu_domain *dom,
2718 struct device *dev)
2719{
Joerg Roedel657cbb62009-11-23 15:26:46 +01002720 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002721 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002722 u16 devid;
2723
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002724 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01002725 return;
2726
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002727 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002728
Joerg Roedel657cbb62009-11-23 15:26:46 +01002729 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002730 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002731
2732 iommu = amd_iommu_rlookup_table[devid];
2733 if (!iommu)
2734 return;
2735
Joerg Roedel684f2882008-12-08 12:07:44 +01002736 iommu_completion_wait(iommu);
2737}
2738
Joerg Roedel01106062008-12-02 19:34:11 +01002739static int amd_iommu_attach_device(struct iommu_domain *dom,
2740 struct device *dev)
2741{
2742 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002743 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01002744 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002745 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002746
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002747 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01002748 return -EINVAL;
2749
Joerg Roedel657cbb62009-11-23 15:26:46 +01002750 dev_data = dev->archdata.iommu;
2751
Joerg Roedelf62dda62011-06-09 12:55:35 +02002752 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01002753 if (!iommu)
2754 return -EINVAL;
2755
Joerg Roedel657cbb62009-11-23 15:26:46 +01002756 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002757 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002758
Joerg Roedel15898bb2009-11-24 15:39:42 +01002759 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01002760
2761 iommu_completion_wait(iommu);
2762
Joerg Roedel15898bb2009-11-24 15:39:42 +01002763 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002764}
2765
Joerg Roedel468e2362010-01-21 16:37:36 +01002766static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
2767 phys_addr_t paddr, int gfp_order, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002768{
Joerg Roedel468e2362010-01-21 16:37:36 +01002769 unsigned long page_size = 0x1000UL << gfp_order;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002770 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002771 int prot = 0;
2772 int ret;
2773
2774 if (iommu_prot & IOMMU_READ)
2775 prot |= IOMMU_PROT_IR;
2776 if (iommu_prot & IOMMU_WRITE)
2777 prot |= IOMMU_PROT_IW;
2778
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002779 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002780 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002781 mutex_unlock(&domain->api_lock);
2782
Joerg Roedel795e74f72010-05-11 17:40:57 +02002783 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002784}
2785
Joerg Roedel468e2362010-01-21 16:37:36 +01002786static int amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2787 int gfp_order)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002788{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002789 struct protection_domain *domain = dom->priv;
Joerg Roedel468e2362010-01-21 16:37:36 +01002790 unsigned long page_size, unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002791
Joerg Roedel468e2362010-01-21 16:37:36 +01002792 page_size = 0x1000UL << gfp_order;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002793
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002794 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01002795 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002796 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002797
Joerg Roedel17b124b2011-04-06 18:01:35 +02002798 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002799
Joerg Roedel468e2362010-01-21 16:37:36 +01002800 return get_order(unmap_size);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002801}
2802
Joerg Roedel645c4c82008-12-02 20:05:50 +01002803static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2804 unsigned long iova)
2805{
2806 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002807 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002808 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002809 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002810
Joerg Roedel24cd7722010-01-19 17:27:39 +01002811 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002812
Joerg Roedela6d41a42009-09-02 17:08:55 +02002813 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01002814 return 0;
2815
Joerg Roedelf03152b2010-01-21 16:15:24 +01002816 if (PM_PTE_LEVEL(*pte) == 0)
2817 offset_mask = PAGE_SIZE - 1;
2818 else
2819 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
2820
2821 __pte = *pte & PM_ADDR_MASK;
2822 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002823
2824 return paddr;
2825}
2826
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002827static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
2828 unsigned long cap)
2829{
Joerg Roedel80a506b2010-07-27 17:14:24 +02002830 switch (cap) {
2831 case IOMMU_CAP_CACHE_COHERENCY:
2832 return 1;
2833 }
2834
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002835 return 0;
2836}
2837
Joerg Roedel26961ef2008-12-03 17:00:17 +01002838static struct iommu_ops amd_iommu_ops = {
2839 .domain_init = amd_iommu_domain_init,
2840 .domain_destroy = amd_iommu_domain_destroy,
2841 .attach_dev = amd_iommu_attach_device,
2842 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01002843 .map = amd_iommu_map,
2844 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002845 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002846 .domain_has_cap = amd_iommu_domain_has_cap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002847};
2848
Joerg Roedel0feae532009-08-26 15:26:30 +02002849/*****************************************************************************
2850 *
2851 * The next functions do a basic initialization of IOMMU for pass through
2852 * mode
2853 *
2854 * In passthrough mode the IOMMU is initialized and enabled but not used for
2855 * DMA-API translation.
2856 *
2857 *****************************************************************************/
2858
2859int __init amd_iommu_init_passthrough(void)
2860{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002861 struct iommu_dev_data *dev_data;
Joerg Roedel0feae532009-08-26 15:26:30 +02002862 struct pci_dev *dev = NULL;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002863 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002864 u16 devid;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002865 int ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02002866
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002867 ret = alloc_passthrough_domain();
2868 if (ret)
2869 return ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02002870
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04002871 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002872 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02002873 continue;
2874
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002875 dev_data = get_dev_data(&dev->dev);
2876 dev_data->passthrough = true;
2877
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002878 devid = get_device_id(&dev->dev);
2879
Joerg Roedel15898bb2009-11-24 15:39:42 +01002880 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02002881 if (!iommu)
2882 continue;
2883
Joerg Roedel15898bb2009-11-24 15:39:42 +01002884 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02002885 }
2886
2887 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
2888
2889 return 0;
2890}