blob: f64799878d45d7e1140bf5ec75058aa75becee67 [file] [log] [blame]
Joerg Roedelb6c02712008-06-26 21:27:53 +02001/*
Joerg Roedel5d0d7152010-10-13 11:13:21 +02002 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
Joerg Roedelb6c02712008-06-26 21:27:53 +02003 * Author: Joerg Roedel <joerg.roedel@amd.com>
4 * Leo Duran <leo.duran@amd.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/pci.h>
Joerg Roedelcb41ed82011-04-05 11:00:53 +020021#include <linux/pci-ats.h>
Akinobu Mitaa66022c2009-12-15 16:48:28 -080022#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Joerg Roedel7f265082008-12-12 13:50:21 +010024#include <linux/debugfs.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020025#include <linux/scatterlist.h>
FUJITA Tomonori51491362009-01-05 23:47:25 +090026#include <linux/dma-mapping.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020027#include <linux/iommu-helper.h>
Joerg Roedelc156e342008-12-02 18:13:27 +010028#include <linux/iommu.h>
Joerg Roedel815b33f2011-04-06 17:26:49 +020029#include <linux/delay.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020030#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090031#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010032#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020033#include <asm/dma.h>
Joerg Roedel6a9401a2009-11-20 13:22:21 +010034#include <asm/amd_iommu_proto.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020035#include <asm/amd_iommu_types.h>
Joerg Roedelc6da9922008-06-26 21:28:06 +020036#include <asm/amd_iommu.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020037
38#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
39
Joerg Roedel815b33f2011-04-06 17:26:49 +020040#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020041
Joerg Roedelb6c02712008-06-26 21:27:53 +020042static DEFINE_RWLOCK(amd_iommu_devtable_lock);
43
Joerg Roedelbd60b732008-09-11 10:24:48 +020044/* A list of preallocated protection domains */
45static LIST_HEAD(iommu_pd_list);
46static DEFINE_SPINLOCK(iommu_pd_list_lock);
47
Joerg Roedel8fa5f802011-06-09 12:24:45 +020048/* List of all available dev_data structures */
49static LIST_HEAD(dev_data_list);
50static DEFINE_SPINLOCK(dev_data_list_lock);
51
Joerg Roedel0feae532009-08-26 15:26:30 +020052/*
53 * Domain for untranslated devices - only allocated
54 * if iommu=pt passed on kernel cmd line.
55 */
56static struct protection_domain *pt_domain;
57
Joerg Roedel26961ef2008-12-03 17:00:17 +010058static struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010059
Joerg Roedel431b2a22008-07-11 17:14:22 +020060/*
61 * general struct to manage commands send to an IOMMU
62 */
Joerg Roedeld6449532008-07-11 17:14:28 +020063struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +020064 u32 data[4];
65};
66
Joerg Roedel04bfdd82009-09-02 16:00:23 +020067static void update_domain(struct protection_domain *domain);
Chris Wrightc1eee672009-05-21 00:56:58 -070068
Joerg Roedel15898bb2009-11-24 15:39:42 +010069/****************************************************************************
70 *
71 * Helper functions
72 *
73 ****************************************************************************/
74
Joerg Roedelf62dda62011-06-09 12:55:35 +020075static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +020076{
77 struct iommu_dev_data *dev_data;
78 unsigned long flags;
79
80 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
81 if (!dev_data)
82 return NULL;
83
Joerg Roedelf62dda62011-06-09 12:55:35 +020084 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +020085 atomic_set(&dev_data->bind, 0);
86
87 spin_lock_irqsave(&dev_data_list_lock, flags);
88 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
89 spin_unlock_irqrestore(&dev_data_list_lock, flags);
90
91 return dev_data;
92}
93
94static void free_dev_data(struct iommu_dev_data *dev_data)
95{
96 unsigned long flags;
97
98 spin_lock_irqsave(&dev_data_list_lock, flags);
99 list_del(&dev_data->dev_data_list);
100 spin_unlock_irqrestore(&dev_data_list_lock, flags);
101
102 kfree(dev_data);
103}
104
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200105static struct iommu_dev_data *search_dev_data(u16 devid)
106{
107 struct iommu_dev_data *dev_data;
108 unsigned long flags;
109
110 spin_lock_irqsave(&dev_data_list_lock, flags);
111 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
112 if (dev_data->devid == devid)
113 goto out_unlock;
114 }
115
116 dev_data = NULL;
117
118out_unlock:
119 spin_unlock_irqrestore(&dev_data_list_lock, flags);
120
121 return dev_data;
122}
123
124static struct iommu_dev_data *find_dev_data(u16 devid)
125{
126 struct iommu_dev_data *dev_data;
127
128 dev_data = search_dev_data(devid);
129
130 if (dev_data == NULL)
131 dev_data = alloc_dev_data(devid);
132
133 return dev_data;
134}
135
Joerg Roedel15898bb2009-11-24 15:39:42 +0100136static inline u16 get_device_id(struct device *dev)
137{
138 struct pci_dev *pdev = to_pci_dev(dev);
139
140 return calc_devid(pdev->bus->number, pdev->devfn);
141}
142
Joerg Roedel657cbb62009-11-23 15:26:46 +0100143static struct iommu_dev_data *get_dev_data(struct device *dev)
144{
145 return dev->archdata.iommu;
146}
147
Joerg Roedel71c70982009-11-24 16:43:06 +0100148/*
149 * In this function the list of preallocated protection domains is traversed to
150 * find the domain for a specific device
151 */
152static struct dma_ops_domain *find_protection_domain(u16 devid)
153{
154 struct dma_ops_domain *entry, *ret = NULL;
155 unsigned long flags;
156 u16 alias = amd_iommu_alias_table[devid];
157
158 if (list_empty(&iommu_pd_list))
159 return NULL;
160
161 spin_lock_irqsave(&iommu_pd_list_lock, flags);
162
163 list_for_each_entry(entry, &iommu_pd_list, list) {
164 if (entry->target_dev == devid ||
165 entry->target_dev == alias) {
166 ret = entry;
167 break;
168 }
169 }
170
171 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
172
173 return ret;
174}
175
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100176/*
177 * This function checks if the driver got a valid device from the caller to
178 * avoid dereferencing invalid pointers.
179 */
180static bool check_device(struct device *dev)
181{
182 u16 devid;
183
184 if (!dev || !dev->dma_mask)
185 return false;
186
187 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100188 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100189 return false;
190
191 devid = get_device_id(dev);
192
193 /* Out of our scope? */
194 if (devid > amd_iommu_last_bdf)
195 return false;
196
197 if (amd_iommu_rlookup_table[devid] == NULL)
198 return false;
199
200 return true;
201}
202
Joerg Roedel657cbb62009-11-23 15:26:46 +0100203static int iommu_init_device(struct device *dev)
204{
205 struct iommu_dev_data *dev_data;
206 struct pci_dev *pdev;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200207 u16 alias;
Joerg Roedel657cbb62009-11-23 15:26:46 +0100208
209 if (dev->archdata.iommu)
210 return 0;
211
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200212 dev_data = find_dev_data(get_device_id(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +0100213 if (!dev_data)
214 return -ENOMEM;
215
Joerg Roedelf62dda62011-06-09 12:55:35 +0200216 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedel2b02b092011-06-09 17:48:39 +0200217 if (alias != dev_data->devid) {
218 pdev = pci_get_bus_and_slot(PCI_BUS(alias), alias & 0xff);
219 if (pdev)
220 dev_data->alias = &pdev->dev;
221 else {
222 free_dev_data(dev_data);
223 return -ENOTSUPP;
224 }
Joerg Roedel26018872011-06-06 16:50:14 +0200225 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100226
227 dev->archdata.iommu = dev_data;
228
Joerg Roedel657cbb62009-11-23 15:26:46 +0100229 return 0;
230}
231
Joerg Roedel26018872011-06-06 16:50:14 +0200232static void iommu_ignore_device(struct device *dev)
233{
234 u16 devid, alias;
235
236 devid = get_device_id(dev);
237 alias = amd_iommu_alias_table[devid];
238
239 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
240 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
241
242 amd_iommu_rlookup_table[devid] = NULL;
243 amd_iommu_rlookup_table[alias] = NULL;
244}
245
Joerg Roedel657cbb62009-11-23 15:26:46 +0100246static void iommu_uninit_device(struct device *dev)
247{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200248 /*
249 * Nothing to do here - we keep dev_data around for unplugged devices
250 * and reuse it when the device is re-plugged - not doing so would
251 * introduce a ton of races.
252 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100253}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100254
255void __init amd_iommu_uninit_devices(void)
256{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200257 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100258 struct pci_dev *pdev = NULL;
259
260 for_each_pci_dev(pdev) {
261
262 if (!check_device(&pdev->dev))
263 continue;
264
265 iommu_uninit_device(&pdev->dev);
266 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200267
268 /* Free all of our dev_data structures */
269 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
270 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100271}
272
273int __init amd_iommu_init_devices(void)
274{
275 struct pci_dev *pdev = NULL;
276 int ret = 0;
277
278 for_each_pci_dev(pdev) {
279
280 if (!check_device(&pdev->dev))
281 continue;
282
283 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200284 if (ret == -ENOTSUPP)
285 iommu_ignore_device(&pdev->dev);
286 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100287 goto out_free;
288 }
289
290 return 0;
291
292out_free:
293
294 amd_iommu_uninit_devices();
295
296 return ret;
297}
Joerg Roedel7f265082008-12-12 13:50:21 +0100298#ifdef CONFIG_AMD_IOMMU_STATS
299
300/*
301 * Initialization code for statistics collection
302 */
303
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100304DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100305DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100306DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100307DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100308DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100309DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100310DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100311DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100312DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100313DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100314DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100315DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100316
Joerg Roedel7f265082008-12-12 13:50:21 +0100317static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100318static struct dentry *de_fflush;
319
320static void amd_iommu_stats_add(struct __iommu_counter *cnt)
321{
322 if (stats_dir == NULL)
323 return;
324
325 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
326 &cnt->value);
327}
328
329static void amd_iommu_stats_init(void)
330{
331 stats_dir = debugfs_create_dir("amd-iommu", NULL);
332 if (stats_dir == NULL)
333 return;
334
Joerg Roedel7f265082008-12-12 13:50:21 +0100335 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
336 (u32 *)&amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100337
338 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100339 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100340 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100341 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100342 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100343 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100344 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100345 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100346 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100347 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100348 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100349 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100350}
351
352#endif
353
Joerg Roedel431b2a22008-07-11 17:14:22 +0200354/****************************************************************************
355 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200356 * Interrupt handling functions
357 *
358 ****************************************************************************/
359
Joerg Roedele3e59872009-09-03 14:02:10 +0200360static void dump_dte_entry(u16 devid)
361{
362 int i;
363
364 for (i = 0; i < 8; ++i)
365 pr_err("AMD-Vi: DTE[%d]: %08x\n", i,
366 amd_iommu_dev_table[devid].data[i]);
367}
368
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200369static void dump_command(unsigned long phys_addr)
370{
371 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
372 int i;
373
374 for (i = 0; i < 4; ++i)
375 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
376}
377
Joerg Roedela345b232009-09-03 15:01:43 +0200378static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200379{
380 u32 *event = __evt;
381 int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
382 int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
383 int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
384 int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
385 u64 address = (u64)(((u64)event[3]) << 32) | event[2];
386
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200387 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200388
389 switch (type) {
390 case EVENT_TYPE_ILL_DEV:
391 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
392 "address=0x%016llx flags=0x%04x]\n",
393 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
394 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200395 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200396 break;
397 case EVENT_TYPE_IO_FAULT:
398 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
399 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
400 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
401 domid, address, flags);
402 break;
403 case EVENT_TYPE_DEV_TAB_ERR:
404 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
405 "address=0x%016llx flags=0x%04x]\n",
406 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
407 address, flags);
408 break;
409 case EVENT_TYPE_PAGE_TAB_ERR:
410 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
411 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
412 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
413 domid, address, flags);
414 break;
415 case EVENT_TYPE_ILL_CMD:
416 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200417 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200418 break;
419 case EVENT_TYPE_CMD_HARD_ERR:
420 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
421 "flags=0x%04x]\n", address, flags);
422 break;
423 case EVENT_TYPE_IOTLB_INV_TO:
424 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
425 "address=0x%016llx]\n",
426 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
427 address);
428 break;
429 case EVENT_TYPE_INV_DEV_REQ:
430 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
431 "address=0x%016llx flags=0x%04x]\n",
432 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
433 address, flags);
434 break;
435 default:
436 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
437 }
438}
439
440static void iommu_poll_events(struct amd_iommu *iommu)
441{
442 u32 head, tail;
443 unsigned long flags;
444
445 spin_lock_irqsave(&iommu->lock, flags);
446
447 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
448 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
449
450 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200451 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200452 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
453 }
454
455 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
456
457 spin_unlock_irqrestore(&iommu->lock, flags);
458}
459
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200460irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200461{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200462 struct amd_iommu *iommu;
463
Joerg Roedel3bd22172009-05-04 15:06:20 +0200464 for_each_iommu(iommu)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200465 iommu_poll_events(iommu);
466
467 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200468}
469
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200470irqreturn_t amd_iommu_int_handler(int irq, void *data)
471{
472 return IRQ_WAKE_THREAD;
473}
474
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200475/****************************************************************************
476 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200477 * IOMMU command queuing functions
478 *
479 ****************************************************************************/
480
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200481static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200482{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200483 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200484
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200485 while (*sem == 0 && i < LOOP_TIMEOUT) {
486 udelay(1);
487 i += 1;
488 }
489
490 if (i == LOOP_TIMEOUT) {
491 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
492 return -EIO;
493 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200494
495 return 0;
496}
497
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200498static void copy_cmd_to_buffer(struct amd_iommu *iommu,
499 struct iommu_cmd *cmd,
500 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200501{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200502 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200503
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200504 target = iommu->cmd_buf + tail;
505 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200506
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200507 /* Copy command to buffer */
508 memcpy(target, cmd, sizeof(*cmd));
509
510 /* Tell the IOMMU about it */
511 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
512}
513
Joerg Roedel815b33f2011-04-06 17:26:49 +0200514static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200515{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200516 WARN_ON(address & 0x7ULL);
517
Joerg Roedelded46732011-04-06 10:53:48 +0200518 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200519 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
520 cmd->data[1] = upper_32_bits(__pa(address));
521 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200522 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
523}
524
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200525static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
526{
527 memset(cmd, 0, sizeof(*cmd));
528 cmd->data[0] = devid;
529 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
530}
531
Joerg Roedel11b64022011-04-06 11:49:28 +0200532static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
533 size_t size, u16 domid, int pde)
534{
535 u64 pages;
536 int s;
537
538 pages = iommu_num_pages(address, size, PAGE_SIZE);
539 s = 0;
540
541 if (pages > 1) {
542 /*
543 * If we have to flush more than one page, flush all
544 * TLB entries for this domain
545 */
546 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
547 s = 1;
548 }
549
550 address &= PAGE_MASK;
551
552 memset(cmd, 0, sizeof(*cmd));
553 cmd->data[1] |= domid;
554 cmd->data[2] = lower_32_bits(address);
555 cmd->data[3] = upper_32_bits(address);
556 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
557 if (s) /* size bit - we flush more than one 4kb page */
558 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
559 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
560 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
561}
562
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200563static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
564 u64 address, size_t size)
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[0] = devid;
585 cmd->data[0] |= (qdep & 0xff) << 24;
586 cmd->data[1] = devid;
587 cmd->data[2] = lower_32_bits(address);
588 cmd->data[3] = upper_32_bits(address);
589 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
590 if (s)
591 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
592}
593
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200594static void build_inv_all(struct iommu_cmd *cmd)
595{
596 memset(cmd, 0, sizeof(*cmd));
597 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200598}
599
Joerg Roedel431b2a22008-07-11 17:14:22 +0200600/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200601 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200602 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200603 */
Joerg Roedel815b33f2011-04-06 17:26:49 +0200604static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200605{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200606 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200607 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200608
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200609 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100610
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200611again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200612 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200613
614 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
615 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
616 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
617 left = (head - next_tail) % iommu->cmd_buf_size;
618
619 if (left <= 2) {
620 struct iommu_cmd sync_cmd;
621 volatile u64 sem = 0;
622 int ret;
623
624 build_completion_wait(&sync_cmd, (u64)&sem);
625 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
626
627 spin_unlock_irqrestore(&iommu->lock, flags);
628
629 if ((ret = wait_on_sem(&sem)) != 0)
630 return ret;
631
632 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200633 }
634
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200635 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200636
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200637 /* We need to sync now to make sure all commands are processed */
Joerg Roedel815b33f2011-04-06 17:26:49 +0200638 iommu->need_sync = true;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200639
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200640 spin_unlock_irqrestore(&iommu->lock, flags);
641
Joerg Roedel815b33f2011-04-06 17:26:49 +0200642 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100643}
644
645/*
646 * This function queues a completion wait command into the command
647 * buffer of an IOMMU
648 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100649static int iommu_completion_wait(struct amd_iommu *iommu)
650{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200651 struct iommu_cmd cmd;
652 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200653 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100654
655 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200656 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100657
Joerg Roedel815b33f2011-04-06 17:26:49 +0200658 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100659
Joerg Roedel815b33f2011-04-06 17:26:49 +0200660 ret = iommu_queue_command(iommu, &cmd);
Joerg Roedel8d201962008-12-02 20:34:41 +0100661 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200662 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100663
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200664 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200665}
666
Joerg Roedeld8c13082011-04-06 18:51:26 +0200667static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200668{
669 struct iommu_cmd cmd;
670
Joerg Roedeld8c13082011-04-06 18:51:26 +0200671 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200672
Joerg Roedeld8c13082011-04-06 18:51:26 +0200673 return iommu_queue_command(iommu, &cmd);
674}
675
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200676static void iommu_flush_dte_all(struct amd_iommu *iommu)
677{
678 u32 devid;
679
680 for (devid = 0; devid <= 0xffff; ++devid)
681 iommu_flush_dte(iommu, devid);
682
683 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200684}
685
686/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200687 * This function uses heavy locking and may disable irqs for some time. But
688 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200689 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200690static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200691{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200692 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200693
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200694 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
695 struct iommu_cmd cmd;
696 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
697 dom_id, 1);
698 iommu_queue_command(iommu, &cmd);
699 }
Joerg Roedel431b2a22008-07-11 17:14:22 +0200700
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200701 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200702}
703
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200704static void iommu_flush_all(struct amd_iommu *iommu)
705{
706 struct iommu_cmd cmd;
707
708 build_inv_all(&cmd);
709
710 iommu_queue_command(iommu, &cmd);
711 iommu_completion_wait(iommu);
712}
713
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200714void iommu_flush_all_caches(struct amd_iommu *iommu)
715{
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200716 if (iommu_feature(iommu, FEATURE_IA)) {
717 iommu_flush_all(iommu);
718 } else {
719 iommu_flush_dte_all(iommu);
720 iommu_flush_tlb_all(iommu);
721 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200722}
723
Joerg Roedel431b2a22008-07-11 17:14:22 +0200724/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200725 * Command send function for flushing on-device TLB
726 */
Joerg Roedel6c542042011-06-09 17:07:31 +0200727static int device_flush_iotlb(struct iommu_dev_data *dev_data,
728 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200729{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200730 struct amd_iommu *iommu;
731 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200732 int qdep;
733
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200734 qdep = dev_data->ats.qdep;
735 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200736
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200737 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200738
739 return iommu_queue_command(iommu, &cmd);
740}
741
742/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200743 * Command send function for invalidating a device table entry
744 */
Joerg Roedel6c542042011-06-09 17:07:31 +0200745static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +0100746{
747 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200748 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100749
Joerg Roedel6c542042011-06-09 17:07:31 +0200750 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +0100751
Joerg Roedelf62dda62011-06-09 12:55:35 +0200752 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200753 if (ret)
754 return ret;
755
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200756 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +0200757 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200758
759 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100760}
761
Joerg Roedel431b2a22008-07-11 17:14:22 +0200762/*
763 * TLB invalidation function which is called from the mapping functions.
764 * It invalidates a single PTE if the range to flush is within a single
765 * page. Otherwise it flushes the whole TLB of the IOMMU.
766 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200767static void __domain_flush_pages(struct protection_domain *domain,
768 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200769{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200770 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +0200771 struct iommu_cmd cmd;
772 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200773
Joerg Roedel11b64022011-04-06 11:49:28 +0200774 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +0200775
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100776 for (i = 0; i < amd_iommus_present; ++i) {
777 if (!domain->dev_iommu[i])
778 continue;
779
780 /*
781 * Devices of this domain are behind this IOMMU
782 * We need a TLB flush
783 */
Joerg Roedel11b64022011-04-06 11:49:28 +0200784 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100785 }
786
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200787 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200788
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200789 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200790 continue;
791
Joerg Roedel6c542042011-06-09 17:07:31 +0200792 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200793 }
794
Joerg Roedel11b64022011-04-06 11:49:28 +0200795 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100796}
797
Joerg Roedel17b124b2011-04-06 18:01:35 +0200798static void domain_flush_pages(struct protection_domain *domain,
799 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100800{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200801 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200802}
Joerg Roedelb6c02712008-06-26 21:27:53 +0200803
Joerg Roedel1c655772008-09-04 18:40:05 +0200804/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200805static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +0200806{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200807 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +0200808}
809
Chris Wright42a49f92009-06-15 15:42:00 +0200810/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200811static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +0200812{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200813 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
814}
815
816static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +0200817{
818 int i;
819
820 for (i = 0; i < amd_iommus_present; ++i) {
821 if (!domain->dev_iommu[i])
822 continue;
823
824 /*
825 * Devices of this domain are behind this IOMMU
826 * We need to wait for completion of all commands.
827 */
828 iommu_completion_wait(amd_iommus[i]);
829 }
830}
831
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100832
Joerg Roedel43f49602008-12-02 21:01:12 +0100833/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100834 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +0100835 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200836static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200837{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100838 struct iommu_dev_data *dev_data;
839 unsigned long flags;
840
841 spin_lock_irqsave(&domain->lock, flags);
842
843 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +0200844 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100845
846 spin_unlock_irqrestore(&domain->lock, flags);
847}
848
Joerg Roedel431b2a22008-07-11 17:14:22 +0200849/****************************************************************************
850 *
851 * The functions below are used the create the page table mappings for
852 * unity mapped regions.
853 *
854 ****************************************************************************/
855
856/*
Joerg Roedel308973d2009-11-24 17:43:32 +0100857 * This function is used to add another level to an IO page table. Adding
858 * another level increases the size of the address space by 9 bits to a size up
859 * to 64 bits.
860 */
861static bool increase_address_space(struct protection_domain *domain,
862 gfp_t gfp)
863{
864 u64 *pte;
865
866 if (domain->mode == PAGE_MODE_6_LEVEL)
867 /* address space already 64 bit large */
868 return false;
869
870 pte = (void *)get_zeroed_page(gfp);
871 if (!pte)
872 return false;
873
874 *pte = PM_LEVEL_PDE(domain->mode,
875 virt_to_phys(domain->pt_root));
876 domain->pt_root = pte;
877 domain->mode += 1;
878 domain->updated = true;
879
880 return true;
881}
882
883static u64 *alloc_pte(struct protection_domain *domain,
884 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100885 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +0100886 u64 **pte_page,
887 gfp_t gfp)
888{
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100889 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +0100890 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100891
892 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +0100893
894 while (address > PM_LEVEL_SIZE(domain->mode))
895 increase_address_space(domain, gfp);
896
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100897 level = domain->mode - 1;
898 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
899 address = PAGE_SIZE_ALIGN(address, page_size);
900 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +0100901
902 while (level > end_lvl) {
903 if (!IOMMU_PTE_PRESENT(*pte)) {
904 page = (u64 *)get_zeroed_page(gfp);
905 if (!page)
906 return NULL;
907 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
908 }
909
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100910 /* No level skipping support yet */
911 if (PM_PTE_LEVEL(*pte) != level)
912 return NULL;
913
Joerg Roedel308973d2009-11-24 17:43:32 +0100914 level -= 1;
915
916 pte = IOMMU_PTE_PAGE(*pte);
917
918 if (pte_page && level == end_lvl)
919 *pte_page = pte;
920
921 pte = &pte[PM_LEVEL_INDEX(level, address)];
922 }
923
924 return pte;
925}
926
927/*
928 * This function checks if there is a PTE for a given dma address. If
929 * there is one, it returns the pointer to it.
930 */
Joerg Roedel24cd7722010-01-19 17:27:39 +0100931static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +0100932{
933 int level;
934 u64 *pte;
935
Joerg Roedel24cd7722010-01-19 17:27:39 +0100936 if (address > PM_LEVEL_SIZE(domain->mode))
937 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +0100938
Joerg Roedel24cd7722010-01-19 17:27:39 +0100939 level = domain->mode - 1;
940 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
941
942 while (level > 0) {
943
944 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +0100945 if (!IOMMU_PTE_PRESENT(*pte))
946 return NULL;
947
Joerg Roedel24cd7722010-01-19 17:27:39 +0100948 /* Large PTE */
949 if (PM_PTE_LEVEL(*pte) == 0x07) {
950 unsigned long pte_mask, __pte;
951
952 /*
953 * If we have a series of large PTEs, make
954 * sure to return a pointer to the first one.
955 */
956 pte_mask = PTE_PAGE_SIZE(*pte);
957 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
958 __pte = ((unsigned long)pte) & pte_mask;
959
960 return (u64 *)__pte;
961 }
962
963 /* No level skipping support yet */
964 if (PM_PTE_LEVEL(*pte) != level)
965 return NULL;
966
Joerg Roedel308973d2009-11-24 17:43:32 +0100967 level -= 1;
968
Joerg Roedel24cd7722010-01-19 17:27:39 +0100969 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +0100970 pte = IOMMU_PTE_PAGE(*pte);
971 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +0100972 }
973
974 return pte;
975}
976
977/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200978 * Generic mapping functions. It maps a physical address into a DMA
979 * address space. It allocates the page table pages if necessary.
980 * In the future it can be extended to a generic mapping function
981 * supporting all features of AMD IOMMU page tables like level skipping
982 * and full 64 bit address spaces.
983 */
Joerg Roedel38e817f2008-12-02 17:27:52 +0100984static int iommu_map_page(struct protection_domain *dom,
985 unsigned long bus_addr,
986 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200987 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100988 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200989{
Joerg Roedel8bda3092009-05-12 12:02:46 +0200990 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100991 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200992
Joerg Roedelbad1cac2009-09-02 16:52:23 +0200993 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200994 return -EINVAL;
995
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100996 bus_addr = PAGE_ALIGN(bus_addr);
997 phys_addr = PAGE_ALIGN(phys_addr);
998 count = PAGE_SIZE_PTE_COUNT(page_size);
999 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001000
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001001 for (i = 0; i < count; ++i)
1002 if (IOMMU_PTE_PRESENT(pte[i]))
1003 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001004
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001005 if (page_size > PAGE_SIZE) {
1006 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1007 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1008 } else
1009 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1010
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001011 if (prot & IOMMU_PROT_IR)
1012 __pte |= IOMMU_PTE_IR;
1013 if (prot & IOMMU_PROT_IW)
1014 __pte |= IOMMU_PTE_IW;
1015
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001016 for (i = 0; i < count; ++i)
1017 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001018
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001019 update_domain(dom);
1020
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001021 return 0;
1022}
1023
Joerg Roedel24cd7722010-01-19 17:27:39 +01001024static unsigned long iommu_unmap_page(struct protection_domain *dom,
1025 unsigned long bus_addr,
1026 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001027{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001028 unsigned long long unmap_size, unmapped;
1029 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001030
Joerg Roedel24cd7722010-01-19 17:27:39 +01001031 BUG_ON(!is_power_of_2(page_size));
1032
1033 unmapped = 0;
1034
1035 while (unmapped < page_size) {
1036
1037 pte = fetch_pte(dom, bus_addr);
1038
1039 if (!pte) {
1040 /*
1041 * No PTE for this address
1042 * move forward in 4kb steps
1043 */
1044 unmap_size = PAGE_SIZE;
1045 } else if (PM_PTE_LEVEL(*pte) == 0) {
1046 /* 4kb PTE found for this address */
1047 unmap_size = PAGE_SIZE;
1048 *pte = 0ULL;
1049 } else {
1050 int count, i;
1051
1052 /* Large PTE found which maps this address */
1053 unmap_size = PTE_PAGE_SIZE(*pte);
1054 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1055 for (i = 0; i < count; i++)
1056 pte[i] = 0ULL;
1057 }
1058
1059 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1060 unmapped += unmap_size;
1061 }
1062
1063 BUG_ON(!is_power_of_2(unmapped));
1064
1065 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001066}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001067
Joerg Roedel431b2a22008-07-11 17:14:22 +02001068/*
1069 * This function checks if a specific unity mapping entry is needed for
1070 * this specific IOMMU.
1071 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001072static int iommu_for_unity_map(struct amd_iommu *iommu,
1073 struct unity_map_entry *entry)
1074{
1075 u16 bdf, i;
1076
1077 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1078 bdf = amd_iommu_alias_table[i];
1079 if (amd_iommu_rlookup_table[bdf] == iommu)
1080 return 1;
1081 }
1082
1083 return 0;
1084}
1085
Joerg Roedel431b2a22008-07-11 17:14:22 +02001086/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001087 * This function actually applies the mapping to the page table of the
1088 * dma_ops domain.
1089 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001090static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1091 struct unity_map_entry *e)
1092{
1093 u64 addr;
1094 int ret;
1095
1096 for (addr = e->address_start; addr < e->address_end;
1097 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001098 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001099 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001100 if (ret)
1101 return ret;
1102 /*
1103 * if unity mapping is in aperture range mark the page
1104 * as allocated in the aperture
1105 */
1106 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001107 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001108 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001109 }
1110
1111 return 0;
1112}
1113
Joerg Roedel431b2a22008-07-11 17:14:22 +02001114/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001115 * Init the unity mappings for a specific IOMMU in the system
1116 *
1117 * Basically iterates over all unity mapping entries and applies them to
1118 * the default domain DMA of that IOMMU if necessary.
1119 */
1120static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1121{
1122 struct unity_map_entry *entry;
1123 int ret;
1124
1125 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1126 if (!iommu_for_unity_map(iommu, entry))
1127 continue;
1128 ret = dma_ops_unity_map(iommu->default_dom, entry);
1129 if (ret)
1130 return ret;
1131 }
1132
1133 return 0;
1134}
1135
1136/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001137 * Inits the unity mappings required for a specific device
1138 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001139static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1140 u16 devid)
1141{
1142 struct unity_map_entry *e;
1143 int ret;
1144
1145 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1146 if (!(devid >= e->devid_start && devid <= e->devid_end))
1147 continue;
1148 ret = dma_ops_unity_map(dma_dom, e);
1149 if (ret)
1150 return ret;
1151 }
1152
1153 return 0;
1154}
1155
Joerg Roedel431b2a22008-07-11 17:14:22 +02001156/****************************************************************************
1157 *
1158 * The next functions belong to the address allocator for the dma_ops
1159 * interface functions. They work like the allocators in the other IOMMU
1160 * drivers. Its basically a bitmap which marks the allocated pages in
1161 * the aperture. Maybe it could be enhanced in the future to a more
1162 * efficient allocator.
1163 *
1164 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001165
Joerg Roedel431b2a22008-07-11 17:14:22 +02001166/*
Joerg Roedel384de722009-05-15 12:30:05 +02001167 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001168 *
1169 * called with domain->lock held
1170 */
Joerg Roedel384de722009-05-15 12:30:05 +02001171
Joerg Roedel9cabe892009-05-18 16:38:55 +02001172/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001173 * Used to reserve address ranges in the aperture (e.g. for exclusion
1174 * ranges.
1175 */
1176static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1177 unsigned long start_page,
1178 unsigned int pages)
1179{
1180 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1181
1182 if (start_page + pages > last_page)
1183 pages = last_page - start_page;
1184
1185 for (i = start_page; i < start_page + pages; ++i) {
1186 int index = i / APERTURE_RANGE_PAGES;
1187 int page = i % APERTURE_RANGE_PAGES;
1188 __set_bit(page, dom->aperture[index]->bitmap);
1189 }
1190}
1191
1192/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001193 * This function is used to add a new aperture range to an existing
1194 * aperture in case of dma_ops domain allocation or address allocation
1195 * failure.
1196 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001197static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001198 bool populate, gfp_t gfp)
1199{
1200 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001201 struct amd_iommu *iommu;
Joerg Roedeld91afd12010-01-22 16:40:20 +01001202 unsigned long i;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001203
Joerg Roedelf5e97052009-05-22 12:31:53 +02001204#ifdef CONFIG_IOMMU_STRESS
1205 populate = false;
1206#endif
1207
Joerg Roedel9cabe892009-05-18 16:38:55 +02001208 if (index >= APERTURE_MAX_RANGES)
1209 return -ENOMEM;
1210
1211 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1212 if (!dma_dom->aperture[index])
1213 return -ENOMEM;
1214
1215 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1216 if (!dma_dom->aperture[index]->bitmap)
1217 goto out_free;
1218
1219 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1220
1221 if (populate) {
1222 unsigned long address = dma_dom->aperture_size;
1223 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1224 u64 *pte, *pte_page;
1225
1226 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001227 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001228 &pte_page, gfp);
1229 if (!pte)
1230 goto out_free;
1231
1232 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1233
1234 address += APERTURE_RANGE_SIZE / 64;
1235 }
1236 }
1237
1238 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1239
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001240 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001241 for_each_iommu(iommu) {
1242 if (iommu->exclusion_start &&
1243 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1244 && iommu->exclusion_start < dma_dom->aperture_size) {
1245 unsigned long startpage;
1246 int pages = iommu_num_pages(iommu->exclusion_start,
1247 iommu->exclusion_length,
1248 PAGE_SIZE);
1249 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1250 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1251 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001252 }
1253
1254 /*
1255 * Check for areas already mapped as present in the new aperture
1256 * range and mark those pages as reserved in the allocator. Such
1257 * mappings may already exist as a result of requested unity
1258 * mappings for devices.
1259 */
1260 for (i = dma_dom->aperture[index]->offset;
1261 i < dma_dom->aperture_size;
1262 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001263 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001264 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1265 continue;
1266
1267 dma_ops_reserve_addresses(dma_dom, i << PAGE_SHIFT, 1);
1268 }
1269
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001270 update_domain(&dma_dom->domain);
1271
Joerg Roedel9cabe892009-05-18 16:38:55 +02001272 return 0;
1273
1274out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001275 update_domain(&dma_dom->domain);
1276
Joerg Roedel9cabe892009-05-18 16:38:55 +02001277 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1278
1279 kfree(dma_dom->aperture[index]);
1280 dma_dom->aperture[index] = NULL;
1281
1282 return -ENOMEM;
1283}
1284
Joerg Roedel384de722009-05-15 12:30:05 +02001285static unsigned long dma_ops_area_alloc(struct device *dev,
1286 struct dma_ops_domain *dom,
1287 unsigned int pages,
1288 unsigned long align_mask,
1289 u64 dma_mask,
1290 unsigned long start)
1291{
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001292 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001293 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1294 int i = start >> APERTURE_RANGE_SHIFT;
1295 unsigned long boundary_size;
1296 unsigned long address = -1;
1297 unsigned long limit;
1298
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001299 next_bit >>= PAGE_SHIFT;
1300
Joerg Roedel384de722009-05-15 12:30:05 +02001301 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1302 PAGE_SIZE) >> PAGE_SHIFT;
1303
1304 for (;i < max_index; ++i) {
1305 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1306
1307 if (dom->aperture[i]->offset >= dma_mask)
1308 break;
1309
1310 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1311 dma_mask >> PAGE_SHIFT);
1312
1313 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1314 limit, next_bit, pages, 0,
1315 boundary_size, align_mask);
1316 if (address != -1) {
1317 address = dom->aperture[i]->offset +
1318 (address << PAGE_SHIFT);
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001319 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001320 break;
1321 }
1322
1323 next_bit = 0;
1324 }
1325
1326 return address;
1327}
1328
Joerg Roedeld3086442008-06-26 21:27:57 +02001329static unsigned long dma_ops_alloc_addresses(struct device *dev,
1330 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001331 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001332 unsigned long align_mask,
1333 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001334{
Joerg Roedeld3086442008-06-26 21:27:57 +02001335 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001336
Joerg Roedelfe16f082009-05-22 12:27:53 +02001337#ifdef CONFIG_IOMMU_STRESS
1338 dom->next_address = 0;
1339 dom->need_flush = true;
1340#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001341
Joerg Roedel384de722009-05-15 12:30:05 +02001342 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001343 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001344
Joerg Roedel1c655772008-09-04 18:40:05 +02001345 if (address == -1) {
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001346 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001347 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1348 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001349 dom->need_flush = true;
1350 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001351
Joerg Roedel384de722009-05-15 12:30:05 +02001352 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001353 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001354
1355 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1356
1357 return address;
1358}
1359
Joerg Roedel431b2a22008-07-11 17:14:22 +02001360/*
1361 * The address free function.
1362 *
1363 * called with domain->lock held
1364 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001365static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1366 unsigned long address,
1367 unsigned int pages)
1368{
Joerg Roedel384de722009-05-15 12:30:05 +02001369 unsigned i = address >> APERTURE_RANGE_SHIFT;
1370 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001371
Joerg Roedel384de722009-05-15 12:30:05 +02001372 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1373
Joerg Roedel47bccd62009-05-22 12:40:54 +02001374#ifdef CONFIG_IOMMU_STRESS
1375 if (i < 4)
1376 return;
1377#endif
1378
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001379 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001380 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001381
1382 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001383
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001384 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001385
Joerg Roedeld3086442008-06-26 21:27:57 +02001386}
1387
Joerg Roedel431b2a22008-07-11 17:14:22 +02001388/****************************************************************************
1389 *
1390 * The next functions belong to the domain allocation. A domain is
1391 * allocated for every IOMMU as the default domain. If device isolation
1392 * is enabled, every device get its own domain. The most important thing
1393 * about domains is the page table mapping the DMA address space they
1394 * contain.
1395 *
1396 ****************************************************************************/
1397
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001398/*
1399 * This function adds a protection domain to the global protection domain list
1400 */
1401static void add_domain_to_list(struct protection_domain *domain)
1402{
1403 unsigned long flags;
1404
1405 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1406 list_add(&domain->list, &amd_iommu_pd_list);
1407 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1408}
1409
1410/*
1411 * This function removes a protection domain to the global
1412 * protection domain list
1413 */
1414static void del_domain_from_list(struct protection_domain *domain)
1415{
1416 unsigned long flags;
1417
1418 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1419 list_del(&domain->list);
1420 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1421}
1422
Joerg Roedelec487d12008-06-26 21:27:58 +02001423static u16 domain_id_alloc(void)
1424{
1425 unsigned long flags;
1426 int id;
1427
1428 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1429 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1430 BUG_ON(id == 0);
1431 if (id > 0 && id < MAX_DOMAIN_ID)
1432 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1433 else
1434 id = 0;
1435 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1436
1437 return id;
1438}
1439
Joerg Roedela2acfb72008-12-02 18:28:53 +01001440static void domain_id_free(int id)
1441{
1442 unsigned long flags;
1443
1444 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1445 if (id > 0 && id < MAX_DOMAIN_ID)
1446 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1447 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1448}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001449
Joerg Roedel86db2e52008-12-02 18:20:21 +01001450static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001451{
1452 int i, j;
1453 u64 *p1, *p2, *p3;
1454
Joerg Roedel86db2e52008-12-02 18:20:21 +01001455 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001456
1457 if (!p1)
1458 return;
1459
1460 for (i = 0; i < 512; ++i) {
1461 if (!IOMMU_PTE_PRESENT(p1[i]))
1462 continue;
1463
1464 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001465 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001466 if (!IOMMU_PTE_PRESENT(p2[j]))
1467 continue;
1468 p3 = IOMMU_PTE_PAGE(p2[j]);
1469 free_page((unsigned long)p3);
1470 }
1471
1472 free_page((unsigned long)p2);
1473 }
1474
1475 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001476
1477 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001478}
1479
Joerg Roedel431b2a22008-07-11 17:14:22 +02001480/*
1481 * Free a domain, only used if something went wrong in the
1482 * allocation path and we need to free an already allocated page table
1483 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001484static void dma_ops_domain_free(struct dma_ops_domain *dom)
1485{
Joerg Roedel384de722009-05-15 12:30:05 +02001486 int i;
1487
Joerg Roedelec487d12008-06-26 21:27:58 +02001488 if (!dom)
1489 return;
1490
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001491 del_domain_from_list(&dom->domain);
1492
Joerg Roedel86db2e52008-12-02 18:20:21 +01001493 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001494
Joerg Roedel384de722009-05-15 12:30:05 +02001495 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1496 if (!dom->aperture[i])
1497 continue;
1498 free_page((unsigned long)dom->aperture[i]->bitmap);
1499 kfree(dom->aperture[i]);
1500 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001501
1502 kfree(dom);
1503}
1504
Joerg Roedel431b2a22008-07-11 17:14:22 +02001505/*
1506 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001507 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001508 * structures required for the dma_ops interface
1509 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001510static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001511{
1512 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001513
1514 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1515 if (!dma_dom)
1516 return NULL;
1517
1518 spin_lock_init(&dma_dom->domain.lock);
1519
1520 dma_dom->domain.id = domain_id_alloc();
1521 if (dma_dom->domain.id == 0)
1522 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001523 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001524 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001525 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001526 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001527 dma_dom->domain.priv = dma_dom;
1528 if (!dma_dom->domain.pt_root)
1529 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001530
Joerg Roedel1c655772008-09-04 18:40:05 +02001531 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001532 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001533
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001534 add_domain_to_list(&dma_dom->domain);
1535
Joerg Roedel576175c2009-11-23 19:08:46 +01001536 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001537 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001538
Joerg Roedel431b2a22008-07-11 17:14:22 +02001539 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001540 * mark the first page as allocated so we never return 0 as
1541 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001542 */
Joerg Roedel384de722009-05-15 12:30:05 +02001543 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001544 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001545
Joerg Roedelec487d12008-06-26 21:27:58 +02001546
1547 return dma_dom;
1548
1549free_dma_dom:
1550 dma_ops_domain_free(dma_dom);
1551
1552 return NULL;
1553}
1554
Joerg Roedel431b2a22008-07-11 17:14:22 +02001555/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001556 * little helper function to check whether a given protection domain is a
1557 * dma_ops domain
1558 */
1559static bool dma_ops_domain(struct protection_domain *domain)
1560{
1561 return domain->flags & PD_DMA_OPS_MASK;
1562}
1563
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001564static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001565{
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001566 u64 pte_root = virt_to_phys(domain->pt_root);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001567 u32 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001568
Joerg Roedel38ddf412008-09-11 10:38:32 +02001569 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1570 << DEV_ENTRY_MODE_SHIFT;
1571 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001572
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001573 if (ats)
1574 flags |= DTE_FLAG_IOTLB;
1575
1576 amd_iommu_dev_table[devid].data[3] |= flags;
1577 amd_iommu_dev_table[devid].data[2] = domain->id;
1578 amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
1579 amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001580}
1581
Joerg Roedel15898bb2009-11-24 15:39:42 +01001582static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001583{
Joerg Roedel355bf552008-12-08 12:02:41 +01001584 /* remove entry from the device table seen by the hardware */
1585 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1586 amd_iommu_dev_table[devid].data[1] = 0;
1587 amd_iommu_dev_table[devid].data[2] = 0;
1588
Joerg Roedelc5cca142009-10-09 18:31:20 +02001589 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001590}
1591
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001592static void do_attach(struct iommu_dev_data *dev_data,
1593 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001594{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001595 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001596 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001597
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001598 iommu = amd_iommu_rlookup_table[dev_data->devid];
1599 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001600
1601 /* Update data structures */
1602 dev_data->domain = domain;
1603 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001604 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001605
1606 /* Do reference counting */
1607 domain->dev_iommu[iommu->index] += 1;
1608 domain->dev_cnt += 1;
1609
1610 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001611 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001612}
1613
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001614static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001615{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001616 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001617
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001618 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02001619
Joerg Roedelc4596112009-11-20 14:57:32 +01001620 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001621 dev_data->domain->dev_iommu[iommu->index] -= 1;
1622 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001623
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001624 /* Update data structures */
1625 dev_data->domain = NULL;
1626 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001627 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001628
1629 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001630 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001631}
1632
1633/*
1634 * If a device is not yet associated with a domain, this function does
1635 * assigns it visible for the hardware
1636 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001637static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01001638 struct protection_domain *domain)
1639{
Julia Lawall84fe6c12010-05-27 12:31:51 +02001640 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001641
Joerg Roedel15898bb2009-11-24 15:39:42 +01001642 /* lock domain */
1643 spin_lock(&domain->lock);
1644
Joerg Roedel2b02b092011-06-09 17:48:39 +02001645 if (dev_data->alias != NULL) {
1646 struct iommu_dev_data *alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001647
Joerg Roedel2b02b092011-06-09 17:48:39 +02001648 alias_data = get_dev_data(dev_data->alias);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001649
Joerg Roedel2b02b092011-06-09 17:48:39 +02001650 ret = -EINVAL;
1651 if (!alias_data)
1652 goto out_unlock;
1653
1654 /* Some sanity checks */
1655 ret = -EBUSY;
1656 if (alias_data->domain != NULL &&
1657 alias_data->domain != domain)
1658 goto out_unlock;
1659
1660 if (dev_data->domain != NULL &&
1661 dev_data->domain != domain)
1662 goto out_unlock;
1663
1664 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001665 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001666 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01001667
1668 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001669 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01001670
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001671 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001672 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001673
Joerg Roedel24100052009-11-25 15:59:57 +01001674 atomic_inc(&dev_data->bind);
1675
Julia Lawall84fe6c12010-05-27 12:31:51 +02001676 ret = 0;
1677
1678out_unlock:
1679
Joerg Roedel355bf552008-12-08 12:02:41 +01001680 /* ready */
1681 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001682
Julia Lawall84fe6c12010-05-27 12:31:51 +02001683 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001684}
1685
1686/*
1687 * If a device is not yet associated with a domain, this function does
1688 * assigns it visible for the hardware
1689 */
1690static int attach_device(struct device *dev,
1691 struct protection_domain *domain)
1692{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001693 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001694 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001695 unsigned long flags;
1696 int ret;
1697
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001698 dev_data = get_dev_data(dev);
1699
1700 if (amd_iommu_iotlb_sup && pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
1701 dev_data->ats.enabled = true;
1702 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1703 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001704
Joerg Roedel15898bb2009-11-24 15:39:42 +01001705 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001706 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001707 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1708
1709 /*
1710 * We might boot into a crash-kernel here. The crashed kernel
1711 * left the caches in the IOMMU dirty. So we have to flush
1712 * here to evict all dirty stuff.
1713 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001714 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001715
1716 return ret;
1717}
1718
1719/*
1720 * Removes a device from a protection domain (unlocked)
1721 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001722static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01001723{
Joerg Roedel24100052009-11-25 15:59:57 +01001724 struct iommu_dev_data *alias_data;
Joerg Roedel2ca76272010-01-22 16:45:31 +01001725 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001726 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001727
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001728 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001729
Joerg Roedel2ca76272010-01-22 16:45:31 +01001730 domain = dev_data->domain;
1731
1732 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01001733
Joerg Roedel2b02b092011-06-09 17:48:39 +02001734 if (dev_data->alias) {
1735 alias_data = get_dev_data(dev_data->alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001736 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001737 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01001738 }
1739
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001740 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001741 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001742
Joerg Roedel2ca76272010-01-22 16:45:31 +01001743 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001744
Joerg Roedel21129f72009-09-01 11:59:42 +02001745 /*
1746 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001747 * passthrough domain if it is detached from any other domain.
1748 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02001749 */
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001750 if (iommu_pass_through &&
1751 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001752 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01001753}
1754
1755/*
1756 * Removes a device from a protection domain (with devtable_lock held)
1757 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01001758static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01001759{
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001760 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01001761 unsigned long flags;
1762
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001763 dev_data = get_dev_data(dev);
1764
Joerg Roedel355bf552008-12-08 12:02:41 +01001765 /* lock device table */
1766 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001767 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01001768 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001769
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001770 if (dev_data->ats.enabled) {
1771 pci_disable_ats(to_pci_dev(dev));
1772 dev_data->ats.enabled = false;
1773 }
Joerg Roedel355bf552008-12-08 12:02:41 +01001774}
Joerg Roedele275a2a2008-12-10 18:27:25 +01001775
Joerg Roedel15898bb2009-11-24 15:39:42 +01001776/*
1777 * Find out the protection domain structure for a given PCI device. This
1778 * will give us the pointer to the page table root for example.
1779 */
1780static struct protection_domain *domain_for_device(struct device *dev)
1781{
Joerg Roedel657cbb62009-11-23 15:26:46 +01001782 struct iommu_dev_data *dev_data, *alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02001783 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001784 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001785
Joerg Roedel657cbb62009-11-23 15:26:46 +01001786 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001787
Joerg Roedel2b02b092011-06-09 17:48:39 +02001788 if (dev_data->domain)
1789 return dev_data->domain;
1790
1791 if (dev_data->alias != NULL) {
1792 alias_data = get_dev_data(dev_data->alias);
1793
1794 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
1795 if (alias_data->domain != NULL) {
1796 __attach_device(dev_data, alias_data->domain);
1797 dom = alias_data->domain;
1798 }
1799 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001800 }
1801
Joerg Roedel15898bb2009-11-24 15:39:42 +01001802 return dom;
1803}
1804
Joerg Roedele275a2a2008-12-10 18:27:25 +01001805static int device_change_notifier(struct notifier_block *nb,
1806 unsigned long action, void *data)
1807{
1808 struct device *dev = data;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001809 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001810 struct protection_domain *domain;
1811 struct dma_ops_domain *dma_domain;
1812 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001813 unsigned long flags;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001814
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001815 if (!check_device(dev))
1816 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001817
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001818 devid = get_device_id(dev);
1819 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01001820
1821 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07001822 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001823
1824 domain = domain_for_device(dev);
1825
Joerg Roedele275a2a2008-12-10 18:27:25 +01001826 if (!domain)
1827 goto out;
Joerg Roedela1ca3312009-09-01 12:22:22 +02001828 if (iommu_pass_through)
1829 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001830 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001831 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001832 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001833
1834 iommu_init_device(dev);
1835
1836 domain = domain_for_device(dev);
1837
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001838 /* allocate a protection domain if a device is added */
1839 dma_domain = find_protection_domain(devid);
1840 if (dma_domain)
1841 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01001842 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001843 if (!dma_domain)
1844 goto out;
1845 dma_domain->target_dev = devid;
1846
1847 spin_lock_irqsave(&iommu_pd_list_lock, flags);
1848 list_add_tail(&dma_domain->list, &iommu_pd_list);
1849 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
1850
1851 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001852 case BUS_NOTIFY_DEL_DEVICE:
1853
1854 iommu_uninit_device(dev);
1855
Joerg Roedele275a2a2008-12-10 18:27:25 +01001856 default:
1857 goto out;
1858 }
1859
Joerg Roedele275a2a2008-12-10 18:27:25 +01001860 iommu_completion_wait(iommu);
1861
1862out:
1863 return 0;
1864}
1865
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05301866static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01001867 .notifier_call = device_change_notifier,
1868};
Joerg Roedel355bf552008-12-08 12:02:41 +01001869
Joerg Roedel8638c492009-12-10 11:12:25 +01001870void amd_iommu_init_notifier(void)
1871{
1872 bus_register_notifier(&pci_bus_type, &device_nb);
1873}
1874
Joerg Roedel431b2a22008-07-11 17:14:22 +02001875/*****************************************************************************
1876 *
1877 * The next functions belong to the dma_ops mapping/unmapping code.
1878 *
1879 *****************************************************************************/
1880
1881/*
1882 * In the dma_ops path we only have the struct device. This function
1883 * finds the corresponding IOMMU, the protection domain and the
1884 * requestor id for a given device.
1885 * If the device is not yet associated with a domain this is also done
1886 * in this function.
1887 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001888static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001889{
Joerg Roedel94f6d192009-11-24 16:40:02 +01001890 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001891 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01001892 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001893
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001894 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01001895 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001896
Joerg Roedel94f6d192009-11-24 16:40:02 +01001897 domain = domain_for_device(dev);
1898 if (domain != NULL && !dma_ops_domain(domain))
1899 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001900
Joerg Roedel94f6d192009-11-24 16:40:02 +01001901 if (domain != NULL)
1902 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001903
Joerg Roedel15898bb2009-11-24 15:39:42 +01001904 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001905 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001906 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01001907 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
1908 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001909 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01001910 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01001911
Joerg Roedel94f6d192009-11-24 16:40:02 +01001912 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001913}
1914
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001915static void update_device_table(struct protection_domain *domain)
1916{
Joerg Roedel492667d2009-11-27 13:25:47 +01001917 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001918
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001919 list_for_each_entry(dev_data, &domain->dev_list, list)
1920 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001921}
1922
1923static void update_domain(struct protection_domain *domain)
1924{
1925 if (!domain->updated)
1926 return;
1927
1928 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02001929
1930 domain_flush_devices(domain);
1931 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001932
1933 domain->updated = false;
1934}
1935
Joerg Roedel431b2a22008-07-11 17:14:22 +02001936/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02001937 * This function fetches the PTE for a given address in the aperture
1938 */
1939static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
1940 unsigned long address)
1941{
Joerg Roedel384de722009-05-15 12:30:05 +02001942 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02001943 u64 *pte, *pte_page;
1944
Joerg Roedel384de722009-05-15 12:30:05 +02001945 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1946 if (!aperture)
1947 return NULL;
1948
1949 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02001950 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001951 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001952 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02001953 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
1954 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001955 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001956
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001957 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001958
1959 return pte;
1960}
1961
1962/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001963 * This is the generic map function. It maps one 4kb page at paddr to
1964 * the given address in the DMA address space for the domain.
1965 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001966static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001967 unsigned long address,
1968 phys_addr_t paddr,
1969 int direction)
1970{
1971 u64 *pte, __pte;
1972
1973 WARN_ON(address > dom->aperture_size);
1974
1975 paddr &= PAGE_MASK;
1976
Joerg Roedel8bda3092009-05-12 12:02:46 +02001977 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02001978 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001979 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001980
1981 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
1982
1983 if (direction == DMA_TO_DEVICE)
1984 __pte |= IOMMU_PTE_IR;
1985 else if (direction == DMA_FROM_DEVICE)
1986 __pte |= IOMMU_PTE_IW;
1987 else if (direction == DMA_BIDIRECTIONAL)
1988 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
1989
1990 WARN_ON(*pte);
1991
1992 *pte = __pte;
1993
1994 return (dma_addr_t)address;
1995}
1996
Joerg Roedel431b2a22008-07-11 17:14:22 +02001997/*
1998 * The generic unmapping function for on page in the DMA address space.
1999 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002000static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002001 unsigned long address)
2002{
Joerg Roedel384de722009-05-15 12:30:05 +02002003 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002004 u64 *pte;
2005
2006 if (address >= dom->aperture_size)
2007 return;
2008
Joerg Roedel384de722009-05-15 12:30:05 +02002009 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2010 if (!aperture)
2011 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002012
Joerg Roedel384de722009-05-15 12:30:05 +02002013 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2014 if (!pte)
2015 return;
2016
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002017 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002018
2019 WARN_ON(!*pte);
2020
2021 *pte = 0ULL;
2022}
2023
Joerg Roedel431b2a22008-07-11 17:14:22 +02002024/*
2025 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002026 * contiguous memory region into DMA address space. It is used by all
2027 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002028 * Must be called with the domain lock held.
2029 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002030static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002031 struct dma_ops_domain *dma_dom,
2032 phys_addr_t paddr,
2033 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002034 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002035 bool align,
2036 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002037{
2038 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002039 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002040 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002041 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002042 int i;
2043
Joerg Roedele3c449f2008-10-15 22:02:11 -07002044 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002045 paddr &= PAGE_MASK;
2046
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002047 INC_STATS_COUNTER(total_map_requests);
2048
Joerg Roedelc1858972008-12-12 15:42:39 +01002049 if (pages > 1)
2050 INC_STATS_COUNTER(cross_page);
2051
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002052 if (align)
2053 align_mask = (1UL << get_order(size)) - 1;
2054
Joerg Roedel11b83882009-05-19 10:23:15 +02002055retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002056 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2057 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002058 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002059 /*
2060 * setting next_address here will let the address
2061 * allocator only scan the new allocated range in the
2062 * first run. This is a small optimization.
2063 */
2064 dma_dom->next_address = dma_dom->aperture_size;
2065
Joerg Roedel576175c2009-11-23 19:08:46 +01002066 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002067 goto out;
2068
2069 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002070 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002071 * allocation again
2072 */
2073 goto retry;
2074 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002075
2076 start = address;
2077 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002078 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002079 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002080 goto out_unmap;
2081
Joerg Roedelcb76c322008-06-26 21:28:00 +02002082 paddr += PAGE_SIZE;
2083 start += PAGE_SIZE;
2084 }
2085 address += offset;
2086
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002087 ADD_STATS_COUNTER(alloced_io_mem, size);
2088
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002089 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002090 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002091 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002092 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002093 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002094
Joerg Roedelcb76c322008-06-26 21:28:00 +02002095out:
2096 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002097
2098out_unmap:
2099
2100 for (--i; i >= 0; --i) {
2101 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002102 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002103 }
2104
2105 dma_ops_free_addresses(dma_dom, address, pages);
2106
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002107 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002108}
2109
Joerg Roedel431b2a22008-07-11 17:14:22 +02002110/*
2111 * Does the reverse of the __map_single function. Must be called with
2112 * the domain lock held too
2113 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002114static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002115 dma_addr_t dma_addr,
2116 size_t size,
2117 int dir)
2118{
Joerg Roedel04e04632010-09-23 16:12:48 +02002119 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002120 dma_addr_t i, start;
2121 unsigned int pages;
2122
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002123 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002124 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002125 return;
2126
Joerg Roedel04e04632010-09-23 16:12:48 +02002127 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002128 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002129 dma_addr &= PAGE_MASK;
2130 start = dma_addr;
2131
2132 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002133 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002134 start += PAGE_SIZE;
2135 }
2136
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002137 SUB_STATS_COUNTER(alloced_io_mem, size);
2138
Joerg Roedelcb76c322008-06-26 21:28:00 +02002139 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002140
Joerg Roedel80be3082008-11-06 14:59:05 +01002141 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002142 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002143 dma_dom->need_flush = false;
2144 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002145}
2146
Joerg Roedel431b2a22008-07-11 17:14:22 +02002147/*
2148 * The exported map_single function for dma_ops.
2149 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002150static dma_addr_t map_page(struct device *dev, struct page *page,
2151 unsigned long offset, size_t size,
2152 enum dma_data_direction dir,
2153 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002154{
2155 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002156 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002157 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002158 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002159 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002160
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002161 INC_STATS_COUNTER(cnt_map_single);
2162
Joerg Roedel94f6d192009-11-24 16:40:02 +01002163 domain = get_domain(dev);
2164 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002165 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002166 else if (IS_ERR(domain))
2167 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002168
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002169 dma_mask = *dev->dma_mask;
2170
Joerg Roedel4da70b92008-06-26 21:28:01 +02002171 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002172
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002173 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002174 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002175 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002176 goto out;
2177
Joerg Roedel17b124b2011-04-06 18:01:35 +02002178 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002179
2180out:
2181 spin_unlock_irqrestore(&domain->lock, flags);
2182
2183 return addr;
2184}
2185
Joerg Roedel431b2a22008-07-11 17:14:22 +02002186/*
2187 * The exported unmap_single function for dma_ops.
2188 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002189static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2190 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002191{
2192 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002193 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002194
Joerg Roedel146a6912008-12-12 15:07:12 +01002195 INC_STATS_COUNTER(cnt_unmap_single);
2196
Joerg Roedel94f6d192009-11-24 16:40:02 +01002197 domain = get_domain(dev);
2198 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002199 return;
2200
Joerg Roedel4da70b92008-06-26 21:28:01 +02002201 spin_lock_irqsave(&domain->lock, flags);
2202
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002203 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002204
Joerg Roedel17b124b2011-04-06 18:01:35 +02002205 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002206
2207 spin_unlock_irqrestore(&domain->lock, flags);
2208}
2209
Joerg Roedel431b2a22008-07-11 17:14:22 +02002210/*
2211 * This is a special map_sg function which is used if we should map a
2212 * device which is not handled by an AMD IOMMU in the system.
2213 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002214static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2215 int nelems, int dir)
2216{
2217 struct scatterlist *s;
2218 int i;
2219
2220 for_each_sg(sglist, s, nelems, i) {
2221 s->dma_address = (dma_addr_t)sg_phys(s);
2222 s->dma_length = s->length;
2223 }
2224
2225 return nelems;
2226}
2227
Joerg Roedel431b2a22008-07-11 17:14:22 +02002228/*
2229 * The exported map_sg function for dma_ops (handles scatter-gather
2230 * lists).
2231 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002232static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002233 int nelems, enum dma_data_direction dir,
2234 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002235{
2236 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002237 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002238 int i;
2239 struct scatterlist *s;
2240 phys_addr_t paddr;
2241 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002242 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002243
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002244 INC_STATS_COUNTER(cnt_map_sg);
2245
Joerg Roedel94f6d192009-11-24 16:40:02 +01002246 domain = get_domain(dev);
2247 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002248 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002249 else if (IS_ERR(domain))
2250 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002251
Joerg Roedel832a90c2008-09-18 15:54:23 +02002252 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002253
Joerg Roedel65b050a2008-06-26 21:28:02 +02002254 spin_lock_irqsave(&domain->lock, flags);
2255
2256 for_each_sg(sglist, s, nelems, i) {
2257 paddr = sg_phys(s);
2258
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002259 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002260 paddr, s->length, dir, false,
2261 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002262
2263 if (s->dma_address) {
2264 s->dma_length = s->length;
2265 mapped_elems++;
2266 } else
2267 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002268 }
2269
Joerg Roedel17b124b2011-04-06 18:01:35 +02002270 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002271
2272out:
2273 spin_unlock_irqrestore(&domain->lock, flags);
2274
2275 return mapped_elems;
2276unmap:
2277 for_each_sg(sglist, s, mapped_elems, i) {
2278 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002279 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002280 s->dma_length, dir);
2281 s->dma_address = s->dma_length = 0;
2282 }
2283
2284 mapped_elems = 0;
2285
2286 goto out;
2287}
2288
Joerg Roedel431b2a22008-07-11 17:14:22 +02002289/*
2290 * The exported map_sg function for dma_ops (handles scatter-gather
2291 * lists).
2292 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002293static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002294 int nelems, enum dma_data_direction dir,
2295 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002296{
2297 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002298 struct protection_domain *domain;
2299 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002300 int i;
2301
Joerg Roedel55877a62008-12-12 15:12:14 +01002302 INC_STATS_COUNTER(cnt_unmap_sg);
2303
Joerg Roedel94f6d192009-11-24 16:40:02 +01002304 domain = get_domain(dev);
2305 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002306 return;
2307
Joerg Roedel65b050a2008-06-26 21:28:02 +02002308 spin_lock_irqsave(&domain->lock, flags);
2309
2310 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002311 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002312 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002313 s->dma_address = s->dma_length = 0;
2314 }
2315
Joerg Roedel17b124b2011-04-06 18:01:35 +02002316 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002317
2318 spin_unlock_irqrestore(&domain->lock, flags);
2319}
2320
Joerg Roedel431b2a22008-07-11 17:14:22 +02002321/*
2322 * The exported alloc_coherent function for dma_ops.
2323 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002324static void *alloc_coherent(struct device *dev, size_t size,
2325 dma_addr_t *dma_addr, gfp_t flag)
2326{
2327 unsigned long flags;
2328 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002329 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002330 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002331 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002332
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002333 INC_STATS_COUNTER(cnt_alloc_coherent);
2334
Joerg Roedel94f6d192009-11-24 16:40:02 +01002335 domain = get_domain(dev);
2336 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002337 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2338 *dma_addr = __pa(virt_addr);
2339 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002340 } else if (IS_ERR(domain))
2341 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002342
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002343 dma_mask = dev->coherent_dma_mask;
2344 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2345 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002346
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002347 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2348 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302349 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002350
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002351 paddr = virt_to_phys(virt_addr);
2352
Joerg Roedel832a90c2008-09-18 15:54:23 +02002353 if (!dma_mask)
2354 dma_mask = *dev->dma_mask;
2355
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002356 spin_lock_irqsave(&domain->lock, flags);
2357
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002358 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002359 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002360
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002361 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002362 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002363 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002364 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002365
Joerg Roedel17b124b2011-04-06 18:01:35 +02002366 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002367
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002368 spin_unlock_irqrestore(&domain->lock, flags);
2369
2370 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002371
2372out_free:
2373
2374 free_pages((unsigned long)virt_addr, get_order(size));
2375
2376 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002377}
2378
Joerg Roedel431b2a22008-07-11 17:14:22 +02002379/*
2380 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002381 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002382static void free_coherent(struct device *dev, size_t size,
2383 void *virt_addr, dma_addr_t dma_addr)
2384{
2385 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002386 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002387
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002388 INC_STATS_COUNTER(cnt_free_coherent);
2389
Joerg Roedel94f6d192009-11-24 16:40:02 +01002390 domain = get_domain(dev);
2391 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002392 goto free_mem;
2393
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002394 spin_lock_irqsave(&domain->lock, flags);
2395
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002396 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002397
Joerg Roedel17b124b2011-04-06 18:01:35 +02002398 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002399
2400 spin_unlock_irqrestore(&domain->lock, flags);
2401
2402free_mem:
2403 free_pages((unsigned long)virt_addr, get_order(size));
2404}
2405
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002406/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002407 * This function is called by the DMA layer to find out if we can handle a
2408 * particular device. It is part of the dma_ops.
2409 */
2410static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2411{
Joerg Roedel420aef82009-11-23 16:14:57 +01002412 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002413}
2414
2415/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002416 * The function for pre-allocating protection domains.
2417 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002418 * If the driver core informs the DMA layer if a driver grabs a device
2419 * we don't need to preallocate the protection domains anymore.
2420 * For now we have to.
2421 */
Jaswinder Singh Rajput0e93dd82008-12-29 21:45:22 +05302422static void prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002423{
2424 struct pci_dev *dev = NULL;
2425 struct dma_ops_domain *dma_dom;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002426 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002427
Chris Wrightd18c69d2010-04-02 18:27:55 -07002428 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002429
2430 /* Do we handle this device? */
2431 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002432 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002433
2434 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002435 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002436 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002437
2438 devid = get_device_id(&dev->dev);
2439
Joerg Roedel87a64d52009-11-24 17:26:43 +01002440 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002441 if (!dma_dom)
2442 continue;
2443 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002444 dma_dom->target_dev = devid;
2445
Joerg Roedel15898bb2009-11-24 15:39:42 +01002446 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01002447
Joerg Roedelbd60b732008-09-11 10:24:48 +02002448 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002449 }
2450}
2451
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002452static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002453 .alloc_coherent = alloc_coherent,
2454 .free_coherent = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002455 .map_page = map_page,
2456 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002457 .map_sg = map_sg,
2458 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002459 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002460};
2461
Joerg Roedel27c21272011-05-30 15:56:24 +02002462static unsigned device_dma_ops_init(void)
2463{
2464 struct pci_dev *pdev = NULL;
2465 unsigned unhandled = 0;
2466
2467 for_each_pci_dev(pdev) {
2468 if (!check_device(&pdev->dev)) {
2469 unhandled += 1;
2470 continue;
2471 }
2472
2473 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
2474 }
2475
2476 return unhandled;
2477}
2478
Joerg Roedel431b2a22008-07-11 17:14:22 +02002479/*
2480 * The function which clues the AMD IOMMU driver into dma_ops.
2481 */
Joerg Roedelf5325092010-01-22 17:44:35 +01002482
2483void __init amd_iommu_init_api(void)
2484{
2485 register_iommu(&amd_iommu_ops);
2486}
2487
Joerg Roedel6631ee92008-06-26 21:28:05 +02002488int __init amd_iommu_init_dma_ops(void)
2489{
2490 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02002491 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002492
Joerg Roedel431b2a22008-07-11 17:14:22 +02002493 /*
2494 * first allocate a default protection domain for every IOMMU we
2495 * found in the system. Devices not assigned to any other
2496 * protection domain will be assigned to the default one.
2497 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02002498 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01002499 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002500 if (iommu->default_dom == NULL)
2501 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01002502 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002503 ret = iommu_init_unity_mappings(iommu);
2504 if (ret)
2505 goto free_domains;
2506 }
2507
Joerg Roedel431b2a22008-07-11 17:14:22 +02002508 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01002509 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002510 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01002511 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002512
2513 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002514 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002515
Joerg Roedel431b2a22008-07-11 17:14:22 +02002516 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02002517 unhandled = device_dma_ops_init();
2518 if (unhandled && max_pfn > MAX_DMA32_PFN) {
2519 /* There are unhandled devices - initialize swiotlb for them */
2520 swiotlb = 1;
2521 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02002522
Joerg Roedel7f265082008-12-12 13:50:21 +01002523 amd_iommu_stats_init();
2524
Joerg Roedel6631ee92008-06-26 21:28:05 +02002525 return 0;
2526
2527free_domains:
2528
Joerg Roedel3bd22172009-05-04 15:06:20 +02002529 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002530 if (iommu->default_dom)
2531 dma_ops_domain_free(iommu->default_dom);
2532 }
2533
2534 return ret;
2535}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002536
2537/*****************************************************************************
2538 *
2539 * The following functions belong to the exported interface of AMD IOMMU
2540 *
2541 * This interface allows access to lower level functions of the IOMMU
2542 * like protection domain handling and assignement of devices to domains
2543 * which is not possible with the dma_ops interface.
2544 *
2545 *****************************************************************************/
2546
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002547static void cleanup_domain(struct protection_domain *domain)
2548{
Joerg Roedel492667d2009-11-27 13:25:47 +01002549 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002550 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002551
2552 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2553
Joerg Roedel492667d2009-11-27 13:25:47 +01002554 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002555 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01002556 atomic_set(&dev_data->bind, 0);
2557 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002558
2559 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2560}
2561
Joerg Roedel26508152009-08-26 16:52:40 +02002562static void protection_domain_free(struct protection_domain *domain)
2563{
2564 if (!domain)
2565 return;
2566
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002567 del_domain_from_list(domain);
2568
Joerg Roedel26508152009-08-26 16:52:40 +02002569 if (domain->id)
2570 domain_id_free(domain->id);
2571
2572 kfree(domain);
2573}
2574
2575static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002576{
2577 struct protection_domain *domain;
2578
2579 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2580 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002581 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002582
2583 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002584 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01002585 domain->id = domain_id_alloc();
2586 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02002587 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002588 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02002589
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002590 add_domain_to_list(domain);
2591
Joerg Roedel26508152009-08-26 16:52:40 +02002592 return domain;
2593
2594out_err:
2595 kfree(domain);
2596
2597 return NULL;
2598}
2599
2600static int amd_iommu_domain_init(struct iommu_domain *dom)
2601{
2602 struct protection_domain *domain;
2603
2604 domain = protection_domain_alloc();
2605 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01002606 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02002607
2608 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002609 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2610 if (!domain->pt_root)
2611 goto out_free;
2612
2613 dom->priv = domain;
2614
2615 return 0;
2616
2617out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02002618 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01002619
2620 return -ENOMEM;
2621}
2622
Joerg Roedel98383fc2008-12-02 18:34:12 +01002623static void amd_iommu_domain_destroy(struct iommu_domain *dom)
2624{
2625 struct protection_domain *domain = dom->priv;
2626
2627 if (!domain)
2628 return;
2629
2630 if (domain->dev_cnt > 0)
2631 cleanup_domain(domain);
2632
2633 BUG_ON(domain->dev_cnt != 0);
2634
2635 free_pagetable(domain);
2636
Joerg Roedel8b408fe2010-03-08 14:20:07 +01002637 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002638
2639 dom->priv = NULL;
2640}
2641
Joerg Roedel684f2882008-12-08 12:07:44 +01002642static void amd_iommu_detach_device(struct iommu_domain *dom,
2643 struct device *dev)
2644{
Joerg Roedel657cbb62009-11-23 15:26:46 +01002645 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002646 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002647 u16 devid;
2648
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002649 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01002650 return;
2651
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002652 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002653
Joerg Roedel657cbb62009-11-23 15:26:46 +01002654 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002655 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002656
2657 iommu = amd_iommu_rlookup_table[devid];
2658 if (!iommu)
2659 return;
2660
Joerg Roedel684f2882008-12-08 12:07:44 +01002661 iommu_completion_wait(iommu);
2662}
2663
Joerg Roedel01106062008-12-02 19:34:11 +01002664static int amd_iommu_attach_device(struct iommu_domain *dom,
2665 struct device *dev)
2666{
2667 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002668 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01002669 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002670 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002671
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002672 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01002673 return -EINVAL;
2674
Joerg Roedel657cbb62009-11-23 15:26:46 +01002675 dev_data = dev->archdata.iommu;
2676
Joerg Roedelf62dda62011-06-09 12:55:35 +02002677 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01002678 if (!iommu)
2679 return -EINVAL;
2680
Joerg Roedel657cbb62009-11-23 15:26:46 +01002681 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002682 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002683
Joerg Roedel15898bb2009-11-24 15:39:42 +01002684 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01002685
2686 iommu_completion_wait(iommu);
2687
Joerg Roedel15898bb2009-11-24 15:39:42 +01002688 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002689}
2690
Joerg Roedel468e2362010-01-21 16:37:36 +01002691static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
2692 phys_addr_t paddr, int gfp_order, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002693{
Joerg Roedel468e2362010-01-21 16:37:36 +01002694 unsigned long page_size = 0x1000UL << gfp_order;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002695 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002696 int prot = 0;
2697 int ret;
2698
2699 if (iommu_prot & IOMMU_READ)
2700 prot |= IOMMU_PROT_IR;
2701 if (iommu_prot & IOMMU_WRITE)
2702 prot |= IOMMU_PROT_IW;
2703
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002704 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002705 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002706 mutex_unlock(&domain->api_lock);
2707
Joerg Roedel795e74f72010-05-11 17:40:57 +02002708 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002709}
2710
Joerg Roedel468e2362010-01-21 16:37:36 +01002711static int amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2712 int gfp_order)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002713{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002714 struct protection_domain *domain = dom->priv;
Joerg Roedel468e2362010-01-21 16:37:36 +01002715 unsigned long page_size, unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002716
Joerg Roedel468e2362010-01-21 16:37:36 +01002717 page_size = 0x1000UL << gfp_order;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002718
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002719 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01002720 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002721 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002722
Joerg Roedel17b124b2011-04-06 18:01:35 +02002723 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002724
Joerg Roedel468e2362010-01-21 16:37:36 +01002725 return get_order(unmap_size);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002726}
2727
Joerg Roedel645c4c82008-12-02 20:05:50 +01002728static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2729 unsigned long iova)
2730{
2731 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002732 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002733 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002734 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002735
Joerg Roedel24cd7722010-01-19 17:27:39 +01002736 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002737
Joerg Roedela6d41a42009-09-02 17:08:55 +02002738 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01002739 return 0;
2740
Joerg Roedelf03152b2010-01-21 16:15:24 +01002741 if (PM_PTE_LEVEL(*pte) == 0)
2742 offset_mask = PAGE_SIZE - 1;
2743 else
2744 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
2745
2746 __pte = *pte & PM_ADDR_MASK;
2747 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002748
2749 return paddr;
2750}
2751
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002752static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
2753 unsigned long cap)
2754{
Joerg Roedel80a506b2010-07-27 17:14:24 +02002755 switch (cap) {
2756 case IOMMU_CAP_CACHE_COHERENCY:
2757 return 1;
2758 }
2759
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002760 return 0;
2761}
2762
Joerg Roedel26961ef2008-12-03 17:00:17 +01002763static struct iommu_ops amd_iommu_ops = {
2764 .domain_init = amd_iommu_domain_init,
2765 .domain_destroy = amd_iommu_domain_destroy,
2766 .attach_dev = amd_iommu_attach_device,
2767 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01002768 .map = amd_iommu_map,
2769 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002770 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002771 .domain_has_cap = amd_iommu_domain_has_cap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002772};
2773
Joerg Roedel0feae532009-08-26 15:26:30 +02002774/*****************************************************************************
2775 *
2776 * The next functions do a basic initialization of IOMMU for pass through
2777 * mode
2778 *
2779 * In passthrough mode the IOMMU is initialized and enabled but not used for
2780 * DMA-API translation.
2781 *
2782 *****************************************************************************/
2783
2784int __init amd_iommu_init_passthrough(void)
2785{
Joerg Roedel15898bb2009-11-24 15:39:42 +01002786 struct amd_iommu *iommu;
Joerg Roedel0feae532009-08-26 15:26:30 +02002787 struct pci_dev *dev = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002788 u16 devid;
Joerg Roedel0feae532009-08-26 15:26:30 +02002789
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002790 /* allocate passthrough domain */
Joerg Roedel0feae532009-08-26 15:26:30 +02002791 pt_domain = protection_domain_alloc();
2792 if (!pt_domain)
2793 return -ENOMEM;
2794
2795 pt_domain->mode |= PAGE_MODE_NONE;
2796
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04002797 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002798 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02002799 continue;
2800
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002801 devid = get_device_id(&dev->dev);
2802
Joerg Roedel15898bb2009-11-24 15:39:42 +01002803 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02002804 if (!iommu)
2805 continue;
2806
Joerg Roedel15898bb2009-11-24 15:39:42 +01002807 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02002808 }
2809
2810 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
2811
2812 return 0;
2813}