blob: 0ed1c940d76b242251056bae437d857de11e1e70 [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;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200206 u16 alias;
Joerg Roedel657cbb62009-11-23 15:26:46 +0100207
208 if (dev->archdata.iommu)
209 return 0;
210
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200211 dev_data = find_dev_data(get_device_id(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +0100212 if (!dev_data)
213 return -ENOMEM;
214
Joerg Roedelf62dda62011-06-09 12:55:35 +0200215 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedel2b02b092011-06-09 17:48:39 +0200216 if (alias != dev_data->devid) {
Joerg Roedel71f77582011-06-09 19:03:15 +0200217 struct iommu_dev_data *alias_data;
218
219 alias_data = find_dev_data(alias);
220 if (alias_data == NULL) {
221 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
222 dev_name(dev));
Joerg Roedel2b02b092011-06-09 17:48:39 +0200223 free_dev_data(dev_data);
224 return -ENOTSUPP;
225 }
Joerg Roedel71f77582011-06-09 19:03:15 +0200226 dev_data->alias_data = alias_data;
Joerg Roedel26018872011-06-06 16:50:14 +0200227 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100228
229 dev->archdata.iommu = dev_data;
230
Joerg Roedel657cbb62009-11-23 15:26:46 +0100231 return 0;
232}
233
Joerg Roedel26018872011-06-06 16:50:14 +0200234static void iommu_ignore_device(struct device *dev)
235{
236 u16 devid, alias;
237
238 devid = get_device_id(dev);
239 alias = amd_iommu_alias_table[devid];
240
241 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
242 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
243
244 amd_iommu_rlookup_table[devid] = NULL;
245 amd_iommu_rlookup_table[alias] = NULL;
246}
247
Joerg Roedel657cbb62009-11-23 15:26:46 +0100248static void iommu_uninit_device(struct device *dev)
249{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200250 /*
251 * Nothing to do here - we keep dev_data around for unplugged devices
252 * and reuse it when the device is re-plugged - not doing so would
253 * introduce a ton of races.
254 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100255}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100256
257void __init amd_iommu_uninit_devices(void)
258{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200259 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100260 struct pci_dev *pdev = NULL;
261
262 for_each_pci_dev(pdev) {
263
264 if (!check_device(&pdev->dev))
265 continue;
266
267 iommu_uninit_device(&pdev->dev);
268 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200269
270 /* Free all of our dev_data structures */
271 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
272 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100273}
274
275int __init amd_iommu_init_devices(void)
276{
277 struct pci_dev *pdev = NULL;
278 int ret = 0;
279
280 for_each_pci_dev(pdev) {
281
282 if (!check_device(&pdev->dev))
283 continue;
284
285 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200286 if (ret == -ENOTSUPP)
287 iommu_ignore_device(&pdev->dev);
288 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100289 goto out_free;
290 }
291
292 return 0;
293
294out_free:
295
296 amd_iommu_uninit_devices();
297
298 return ret;
299}
Joerg Roedel7f265082008-12-12 13:50:21 +0100300#ifdef CONFIG_AMD_IOMMU_STATS
301
302/*
303 * Initialization code for statistics collection
304 */
305
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100306DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100307DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100308DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100309DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100310DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100311DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100312DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100313DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100314DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100315DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100316DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100317DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100318
Joerg Roedel7f265082008-12-12 13:50:21 +0100319static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100320static struct dentry *de_fflush;
321
322static void amd_iommu_stats_add(struct __iommu_counter *cnt)
323{
324 if (stats_dir == NULL)
325 return;
326
327 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
328 &cnt->value);
329}
330
331static void amd_iommu_stats_init(void)
332{
333 stats_dir = debugfs_create_dir("amd-iommu", NULL);
334 if (stats_dir == NULL)
335 return;
336
Joerg Roedel7f265082008-12-12 13:50:21 +0100337 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
338 (u32 *)&amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100339
340 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100341 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100342 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100343 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100344 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100345 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100346 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100347 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100348 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100349 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100350 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100351 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100352}
353
354#endif
355
Joerg Roedel431b2a22008-07-11 17:14:22 +0200356/****************************************************************************
357 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200358 * Interrupt handling functions
359 *
360 ****************************************************************************/
361
Joerg Roedele3e59872009-09-03 14:02:10 +0200362static void dump_dte_entry(u16 devid)
363{
364 int i;
365
366 for (i = 0; i < 8; ++i)
367 pr_err("AMD-Vi: DTE[%d]: %08x\n", i,
368 amd_iommu_dev_table[devid].data[i]);
369}
370
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200371static void dump_command(unsigned long phys_addr)
372{
373 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
374 int i;
375
376 for (i = 0; i < 4; ++i)
377 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
378}
379
Joerg Roedela345b232009-09-03 15:01:43 +0200380static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200381{
382 u32 *event = __evt;
383 int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
384 int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
385 int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
386 int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
387 u64 address = (u64)(((u64)event[3]) << 32) | event[2];
388
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200389 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200390
391 switch (type) {
392 case EVENT_TYPE_ILL_DEV:
393 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
394 "address=0x%016llx flags=0x%04x]\n",
395 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
396 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200397 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200398 break;
399 case EVENT_TYPE_IO_FAULT:
400 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
401 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
402 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
403 domid, address, flags);
404 break;
405 case EVENT_TYPE_DEV_TAB_ERR:
406 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
407 "address=0x%016llx flags=0x%04x]\n",
408 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
409 address, flags);
410 break;
411 case EVENT_TYPE_PAGE_TAB_ERR:
412 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
413 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
414 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
415 domid, address, flags);
416 break;
417 case EVENT_TYPE_ILL_CMD:
418 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200419 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200420 break;
421 case EVENT_TYPE_CMD_HARD_ERR:
422 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
423 "flags=0x%04x]\n", address, flags);
424 break;
425 case EVENT_TYPE_IOTLB_INV_TO:
426 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
427 "address=0x%016llx]\n",
428 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
429 address);
430 break;
431 case EVENT_TYPE_INV_DEV_REQ:
432 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
433 "address=0x%016llx flags=0x%04x]\n",
434 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
435 address, flags);
436 break;
437 default:
438 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
439 }
440}
441
442static void iommu_poll_events(struct amd_iommu *iommu)
443{
444 u32 head, tail;
445 unsigned long flags;
446
447 spin_lock_irqsave(&iommu->lock, flags);
448
449 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
450 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
451
452 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200453 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200454 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
455 }
456
457 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
458
459 spin_unlock_irqrestore(&iommu->lock, flags);
460}
461
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200462irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200463{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200464 struct amd_iommu *iommu;
465
Joerg Roedel3bd22172009-05-04 15:06:20 +0200466 for_each_iommu(iommu)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200467 iommu_poll_events(iommu);
468
469 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200470}
471
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200472irqreturn_t amd_iommu_int_handler(int irq, void *data)
473{
474 return IRQ_WAKE_THREAD;
475}
476
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200477/****************************************************************************
478 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200479 * IOMMU command queuing functions
480 *
481 ****************************************************************************/
482
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200483static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200484{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200485 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200486
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200487 while (*sem == 0 && i < LOOP_TIMEOUT) {
488 udelay(1);
489 i += 1;
490 }
491
492 if (i == LOOP_TIMEOUT) {
493 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
494 return -EIO;
495 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200496
497 return 0;
498}
499
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200500static void copy_cmd_to_buffer(struct amd_iommu *iommu,
501 struct iommu_cmd *cmd,
502 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200503{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200504 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200505
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200506 target = iommu->cmd_buf + tail;
507 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200508
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200509 /* Copy command to buffer */
510 memcpy(target, cmd, sizeof(*cmd));
511
512 /* Tell the IOMMU about it */
513 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
514}
515
Joerg Roedel815b33f2011-04-06 17:26:49 +0200516static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200517{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200518 WARN_ON(address & 0x7ULL);
519
Joerg Roedelded46732011-04-06 10:53:48 +0200520 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200521 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
522 cmd->data[1] = upper_32_bits(__pa(address));
523 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200524 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
525}
526
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200527static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
528{
529 memset(cmd, 0, sizeof(*cmd));
530 cmd->data[0] = devid;
531 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
532}
533
Joerg Roedel11b64022011-04-06 11:49:28 +0200534static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
535 size_t size, u16 domid, int pde)
536{
537 u64 pages;
538 int s;
539
540 pages = iommu_num_pages(address, size, PAGE_SIZE);
541 s = 0;
542
543 if (pages > 1) {
544 /*
545 * If we have to flush more than one page, flush all
546 * TLB entries for this domain
547 */
548 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
549 s = 1;
550 }
551
552 address &= PAGE_MASK;
553
554 memset(cmd, 0, sizeof(*cmd));
555 cmd->data[1] |= domid;
556 cmd->data[2] = lower_32_bits(address);
557 cmd->data[3] = upper_32_bits(address);
558 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
559 if (s) /* size bit - we flush more than one 4kb page */
560 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
561 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
562 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
563}
564
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200565static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
566 u64 address, size_t size)
567{
568 u64 pages;
569 int s;
570
571 pages = iommu_num_pages(address, size, PAGE_SIZE);
572 s = 0;
573
574 if (pages > 1) {
575 /*
576 * If we have to flush more than one page, flush all
577 * TLB entries for this domain
578 */
579 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
580 s = 1;
581 }
582
583 address &= PAGE_MASK;
584
585 memset(cmd, 0, sizeof(*cmd));
586 cmd->data[0] = devid;
587 cmd->data[0] |= (qdep & 0xff) << 24;
588 cmd->data[1] = devid;
589 cmd->data[2] = lower_32_bits(address);
590 cmd->data[3] = upper_32_bits(address);
591 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
592 if (s)
593 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
594}
595
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200596static void build_inv_all(struct iommu_cmd *cmd)
597{
598 memset(cmd, 0, sizeof(*cmd));
599 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200600}
601
Joerg Roedel431b2a22008-07-11 17:14:22 +0200602/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200603 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200604 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200605 */
Joerg Roedel815b33f2011-04-06 17:26:49 +0200606static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200607{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200608 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200609 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200610
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200611 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100612
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200613again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200614 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200615
616 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
617 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
618 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
619 left = (head - next_tail) % iommu->cmd_buf_size;
620
621 if (left <= 2) {
622 struct iommu_cmd sync_cmd;
623 volatile u64 sem = 0;
624 int ret;
625
626 build_completion_wait(&sync_cmd, (u64)&sem);
627 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
628
629 spin_unlock_irqrestore(&iommu->lock, flags);
630
631 if ((ret = wait_on_sem(&sem)) != 0)
632 return ret;
633
634 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200635 }
636
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200637 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200638
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200639 /* We need to sync now to make sure all commands are processed */
Joerg Roedel815b33f2011-04-06 17:26:49 +0200640 iommu->need_sync = true;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200641
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200642 spin_unlock_irqrestore(&iommu->lock, flags);
643
Joerg Roedel815b33f2011-04-06 17:26:49 +0200644 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100645}
646
647/*
648 * This function queues a completion wait command into the command
649 * buffer of an IOMMU
650 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100651static int iommu_completion_wait(struct amd_iommu *iommu)
652{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200653 struct iommu_cmd cmd;
654 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200655 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100656
657 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200658 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100659
Joerg Roedel815b33f2011-04-06 17:26:49 +0200660 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100661
Joerg Roedel815b33f2011-04-06 17:26:49 +0200662 ret = iommu_queue_command(iommu, &cmd);
Joerg Roedel8d201962008-12-02 20:34:41 +0100663 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200664 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100665
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200666 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200667}
668
Joerg Roedeld8c13082011-04-06 18:51:26 +0200669static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200670{
671 struct iommu_cmd cmd;
672
Joerg Roedeld8c13082011-04-06 18:51:26 +0200673 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200674
Joerg Roedeld8c13082011-04-06 18:51:26 +0200675 return iommu_queue_command(iommu, &cmd);
676}
677
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200678static void iommu_flush_dte_all(struct amd_iommu *iommu)
679{
680 u32 devid;
681
682 for (devid = 0; devid <= 0xffff; ++devid)
683 iommu_flush_dte(iommu, devid);
684
685 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200686}
687
688/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200689 * This function uses heavy locking and may disable irqs for some time. But
690 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200691 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200692static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200693{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200694 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200695
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200696 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
697 struct iommu_cmd cmd;
698 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
699 dom_id, 1);
700 iommu_queue_command(iommu, &cmd);
701 }
Joerg Roedel431b2a22008-07-11 17:14:22 +0200702
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200703 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200704}
705
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200706static void iommu_flush_all(struct amd_iommu *iommu)
707{
708 struct iommu_cmd cmd;
709
710 build_inv_all(&cmd);
711
712 iommu_queue_command(iommu, &cmd);
713 iommu_completion_wait(iommu);
714}
715
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200716void iommu_flush_all_caches(struct amd_iommu *iommu)
717{
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200718 if (iommu_feature(iommu, FEATURE_IA)) {
719 iommu_flush_all(iommu);
720 } else {
721 iommu_flush_dte_all(iommu);
722 iommu_flush_tlb_all(iommu);
723 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200724}
725
Joerg Roedel431b2a22008-07-11 17:14:22 +0200726/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200727 * Command send function for flushing on-device TLB
728 */
Joerg Roedel6c542042011-06-09 17:07:31 +0200729static int device_flush_iotlb(struct iommu_dev_data *dev_data,
730 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200731{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200732 struct amd_iommu *iommu;
733 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200734 int qdep;
735
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200736 qdep = dev_data->ats.qdep;
737 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200738
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200739 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200740
741 return iommu_queue_command(iommu, &cmd);
742}
743
744/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200745 * Command send function for invalidating a device table entry
746 */
Joerg Roedel6c542042011-06-09 17:07:31 +0200747static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +0100748{
749 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200750 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100751
Joerg Roedel6c542042011-06-09 17:07:31 +0200752 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +0100753
Joerg Roedelf62dda62011-06-09 12:55:35 +0200754 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200755 if (ret)
756 return ret;
757
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200758 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +0200759 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200760
761 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100762}
763
Joerg Roedel431b2a22008-07-11 17:14:22 +0200764/*
765 * TLB invalidation function which is called from the mapping functions.
766 * It invalidates a single PTE if the range to flush is within a single
767 * page. Otherwise it flushes the whole TLB of the IOMMU.
768 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200769static void __domain_flush_pages(struct protection_domain *domain,
770 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200771{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200772 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +0200773 struct iommu_cmd cmd;
774 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200775
Joerg Roedel11b64022011-04-06 11:49:28 +0200776 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +0200777
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100778 for (i = 0; i < amd_iommus_present; ++i) {
779 if (!domain->dev_iommu[i])
780 continue;
781
782 /*
783 * Devices of this domain are behind this IOMMU
784 * We need a TLB flush
785 */
Joerg Roedel11b64022011-04-06 11:49:28 +0200786 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100787 }
788
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200789 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200790
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200791 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200792 continue;
793
Joerg Roedel6c542042011-06-09 17:07:31 +0200794 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200795 }
796
Joerg Roedel11b64022011-04-06 11:49:28 +0200797 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100798}
799
Joerg Roedel17b124b2011-04-06 18:01:35 +0200800static void domain_flush_pages(struct protection_domain *domain,
801 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100802{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200803 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200804}
Joerg Roedelb6c02712008-06-26 21:27:53 +0200805
Joerg Roedel1c655772008-09-04 18:40:05 +0200806/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200807static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +0200808{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200809 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +0200810}
811
Chris Wright42a49f92009-06-15 15:42:00 +0200812/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200813static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +0200814{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200815 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
816}
817
818static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +0200819{
820 int i;
821
822 for (i = 0; i < amd_iommus_present; ++i) {
823 if (!domain->dev_iommu[i])
824 continue;
825
826 /*
827 * Devices of this domain are behind this IOMMU
828 * We need to wait for completion of all commands.
829 */
830 iommu_completion_wait(amd_iommus[i]);
831 }
832}
833
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100834
Joerg Roedel43f49602008-12-02 21:01:12 +0100835/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100836 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +0100837 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200838static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200839{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100840 struct iommu_dev_data *dev_data;
841 unsigned long flags;
842
843 spin_lock_irqsave(&domain->lock, flags);
844
845 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +0200846 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100847
848 spin_unlock_irqrestore(&domain->lock, flags);
849}
850
Joerg Roedel431b2a22008-07-11 17:14:22 +0200851/****************************************************************************
852 *
853 * The functions below are used the create the page table mappings for
854 * unity mapped regions.
855 *
856 ****************************************************************************/
857
858/*
Joerg Roedel308973d2009-11-24 17:43:32 +0100859 * This function is used to add another level to an IO page table. Adding
860 * another level increases the size of the address space by 9 bits to a size up
861 * to 64 bits.
862 */
863static bool increase_address_space(struct protection_domain *domain,
864 gfp_t gfp)
865{
866 u64 *pte;
867
868 if (domain->mode == PAGE_MODE_6_LEVEL)
869 /* address space already 64 bit large */
870 return false;
871
872 pte = (void *)get_zeroed_page(gfp);
873 if (!pte)
874 return false;
875
876 *pte = PM_LEVEL_PDE(domain->mode,
877 virt_to_phys(domain->pt_root));
878 domain->pt_root = pte;
879 domain->mode += 1;
880 domain->updated = true;
881
882 return true;
883}
884
885static u64 *alloc_pte(struct protection_domain *domain,
886 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100887 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +0100888 u64 **pte_page,
889 gfp_t gfp)
890{
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100891 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +0100892 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100893
894 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +0100895
896 while (address > PM_LEVEL_SIZE(domain->mode))
897 increase_address_space(domain, gfp);
898
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100899 level = domain->mode - 1;
900 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
901 address = PAGE_SIZE_ALIGN(address, page_size);
902 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +0100903
904 while (level > end_lvl) {
905 if (!IOMMU_PTE_PRESENT(*pte)) {
906 page = (u64 *)get_zeroed_page(gfp);
907 if (!page)
908 return NULL;
909 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
910 }
911
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100912 /* No level skipping support yet */
913 if (PM_PTE_LEVEL(*pte) != level)
914 return NULL;
915
Joerg Roedel308973d2009-11-24 17:43:32 +0100916 level -= 1;
917
918 pte = IOMMU_PTE_PAGE(*pte);
919
920 if (pte_page && level == end_lvl)
921 *pte_page = pte;
922
923 pte = &pte[PM_LEVEL_INDEX(level, address)];
924 }
925
926 return pte;
927}
928
929/*
930 * This function checks if there is a PTE for a given dma address. If
931 * there is one, it returns the pointer to it.
932 */
Joerg Roedel24cd7722010-01-19 17:27:39 +0100933static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +0100934{
935 int level;
936 u64 *pte;
937
Joerg Roedel24cd7722010-01-19 17:27:39 +0100938 if (address > PM_LEVEL_SIZE(domain->mode))
939 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +0100940
Joerg Roedel24cd7722010-01-19 17:27:39 +0100941 level = domain->mode - 1;
942 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
943
944 while (level > 0) {
945
946 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +0100947 if (!IOMMU_PTE_PRESENT(*pte))
948 return NULL;
949
Joerg Roedel24cd7722010-01-19 17:27:39 +0100950 /* Large PTE */
951 if (PM_PTE_LEVEL(*pte) == 0x07) {
952 unsigned long pte_mask, __pte;
953
954 /*
955 * If we have a series of large PTEs, make
956 * sure to return a pointer to the first one.
957 */
958 pte_mask = PTE_PAGE_SIZE(*pte);
959 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
960 __pte = ((unsigned long)pte) & pte_mask;
961
962 return (u64 *)__pte;
963 }
964
965 /* No level skipping support yet */
966 if (PM_PTE_LEVEL(*pte) != level)
967 return NULL;
968
Joerg Roedel308973d2009-11-24 17:43:32 +0100969 level -= 1;
970
Joerg Roedel24cd7722010-01-19 17:27:39 +0100971 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +0100972 pte = IOMMU_PTE_PAGE(*pte);
973 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +0100974 }
975
976 return pte;
977}
978
979/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200980 * Generic mapping functions. It maps a physical address into a DMA
981 * address space. It allocates the page table pages if necessary.
982 * In the future it can be extended to a generic mapping function
983 * supporting all features of AMD IOMMU page tables like level skipping
984 * and full 64 bit address spaces.
985 */
Joerg Roedel38e817f2008-12-02 17:27:52 +0100986static int iommu_map_page(struct protection_domain *dom,
987 unsigned long bus_addr,
988 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200989 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100990 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200991{
Joerg Roedel8bda3092009-05-12 12:02:46 +0200992 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100993 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200994
Joerg Roedelbad1cac2009-09-02 16:52:23 +0200995 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200996 return -EINVAL;
997
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100998 bus_addr = PAGE_ALIGN(bus_addr);
999 phys_addr = PAGE_ALIGN(phys_addr);
1000 count = PAGE_SIZE_PTE_COUNT(page_size);
1001 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001002
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001003 for (i = 0; i < count; ++i)
1004 if (IOMMU_PTE_PRESENT(pte[i]))
1005 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001006
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001007 if (page_size > PAGE_SIZE) {
1008 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1009 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1010 } else
1011 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1012
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001013 if (prot & IOMMU_PROT_IR)
1014 __pte |= IOMMU_PTE_IR;
1015 if (prot & IOMMU_PROT_IW)
1016 __pte |= IOMMU_PTE_IW;
1017
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001018 for (i = 0; i < count; ++i)
1019 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001020
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001021 update_domain(dom);
1022
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001023 return 0;
1024}
1025
Joerg Roedel24cd7722010-01-19 17:27:39 +01001026static unsigned long iommu_unmap_page(struct protection_domain *dom,
1027 unsigned long bus_addr,
1028 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001029{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001030 unsigned long long unmap_size, unmapped;
1031 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001032
Joerg Roedel24cd7722010-01-19 17:27:39 +01001033 BUG_ON(!is_power_of_2(page_size));
1034
1035 unmapped = 0;
1036
1037 while (unmapped < page_size) {
1038
1039 pte = fetch_pte(dom, bus_addr);
1040
1041 if (!pte) {
1042 /*
1043 * No PTE for this address
1044 * move forward in 4kb steps
1045 */
1046 unmap_size = PAGE_SIZE;
1047 } else if (PM_PTE_LEVEL(*pte) == 0) {
1048 /* 4kb PTE found for this address */
1049 unmap_size = PAGE_SIZE;
1050 *pte = 0ULL;
1051 } else {
1052 int count, i;
1053
1054 /* Large PTE found which maps this address */
1055 unmap_size = PTE_PAGE_SIZE(*pte);
1056 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1057 for (i = 0; i < count; i++)
1058 pte[i] = 0ULL;
1059 }
1060
1061 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1062 unmapped += unmap_size;
1063 }
1064
1065 BUG_ON(!is_power_of_2(unmapped));
1066
1067 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001068}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001069
Joerg Roedel431b2a22008-07-11 17:14:22 +02001070/*
1071 * This function checks if a specific unity mapping entry is needed for
1072 * this specific IOMMU.
1073 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001074static int iommu_for_unity_map(struct amd_iommu *iommu,
1075 struct unity_map_entry *entry)
1076{
1077 u16 bdf, i;
1078
1079 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1080 bdf = amd_iommu_alias_table[i];
1081 if (amd_iommu_rlookup_table[bdf] == iommu)
1082 return 1;
1083 }
1084
1085 return 0;
1086}
1087
Joerg Roedel431b2a22008-07-11 17:14:22 +02001088/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001089 * This function actually applies the mapping to the page table of the
1090 * dma_ops domain.
1091 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001092static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1093 struct unity_map_entry *e)
1094{
1095 u64 addr;
1096 int ret;
1097
1098 for (addr = e->address_start; addr < e->address_end;
1099 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001100 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001101 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001102 if (ret)
1103 return ret;
1104 /*
1105 * if unity mapping is in aperture range mark the page
1106 * as allocated in the aperture
1107 */
1108 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001109 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001110 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001111 }
1112
1113 return 0;
1114}
1115
Joerg Roedel431b2a22008-07-11 17:14:22 +02001116/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001117 * Init the unity mappings for a specific IOMMU in the system
1118 *
1119 * Basically iterates over all unity mapping entries and applies them to
1120 * the default domain DMA of that IOMMU if necessary.
1121 */
1122static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1123{
1124 struct unity_map_entry *entry;
1125 int ret;
1126
1127 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1128 if (!iommu_for_unity_map(iommu, entry))
1129 continue;
1130 ret = dma_ops_unity_map(iommu->default_dom, entry);
1131 if (ret)
1132 return ret;
1133 }
1134
1135 return 0;
1136}
1137
1138/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001139 * Inits the unity mappings required for a specific device
1140 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001141static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1142 u16 devid)
1143{
1144 struct unity_map_entry *e;
1145 int ret;
1146
1147 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1148 if (!(devid >= e->devid_start && devid <= e->devid_end))
1149 continue;
1150 ret = dma_ops_unity_map(dma_dom, e);
1151 if (ret)
1152 return ret;
1153 }
1154
1155 return 0;
1156}
1157
Joerg Roedel431b2a22008-07-11 17:14:22 +02001158/****************************************************************************
1159 *
1160 * The next functions belong to the address allocator for the dma_ops
1161 * interface functions. They work like the allocators in the other IOMMU
1162 * drivers. Its basically a bitmap which marks the allocated pages in
1163 * the aperture. Maybe it could be enhanced in the future to a more
1164 * efficient allocator.
1165 *
1166 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001167
Joerg Roedel431b2a22008-07-11 17:14:22 +02001168/*
Joerg Roedel384de722009-05-15 12:30:05 +02001169 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001170 *
1171 * called with domain->lock held
1172 */
Joerg Roedel384de722009-05-15 12:30:05 +02001173
Joerg Roedel9cabe892009-05-18 16:38:55 +02001174/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001175 * Used to reserve address ranges in the aperture (e.g. for exclusion
1176 * ranges.
1177 */
1178static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1179 unsigned long start_page,
1180 unsigned int pages)
1181{
1182 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1183
1184 if (start_page + pages > last_page)
1185 pages = last_page - start_page;
1186
1187 for (i = start_page; i < start_page + pages; ++i) {
1188 int index = i / APERTURE_RANGE_PAGES;
1189 int page = i % APERTURE_RANGE_PAGES;
1190 __set_bit(page, dom->aperture[index]->bitmap);
1191 }
1192}
1193
1194/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001195 * This function is used to add a new aperture range to an existing
1196 * aperture in case of dma_ops domain allocation or address allocation
1197 * failure.
1198 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001199static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001200 bool populate, gfp_t gfp)
1201{
1202 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001203 struct amd_iommu *iommu;
Joerg Roedeld91afd12010-01-22 16:40:20 +01001204 unsigned long i;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001205
Joerg Roedelf5e97052009-05-22 12:31:53 +02001206#ifdef CONFIG_IOMMU_STRESS
1207 populate = false;
1208#endif
1209
Joerg Roedel9cabe892009-05-18 16:38:55 +02001210 if (index >= APERTURE_MAX_RANGES)
1211 return -ENOMEM;
1212
1213 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1214 if (!dma_dom->aperture[index])
1215 return -ENOMEM;
1216
1217 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1218 if (!dma_dom->aperture[index]->bitmap)
1219 goto out_free;
1220
1221 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1222
1223 if (populate) {
1224 unsigned long address = dma_dom->aperture_size;
1225 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1226 u64 *pte, *pte_page;
1227
1228 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001229 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001230 &pte_page, gfp);
1231 if (!pte)
1232 goto out_free;
1233
1234 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1235
1236 address += APERTURE_RANGE_SIZE / 64;
1237 }
1238 }
1239
1240 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1241
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001242 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001243 for_each_iommu(iommu) {
1244 if (iommu->exclusion_start &&
1245 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1246 && iommu->exclusion_start < dma_dom->aperture_size) {
1247 unsigned long startpage;
1248 int pages = iommu_num_pages(iommu->exclusion_start,
1249 iommu->exclusion_length,
1250 PAGE_SIZE);
1251 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1252 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1253 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001254 }
1255
1256 /*
1257 * Check for areas already mapped as present in the new aperture
1258 * range and mark those pages as reserved in the allocator. Such
1259 * mappings may already exist as a result of requested unity
1260 * mappings for devices.
1261 */
1262 for (i = dma_dom->aperture[index]->offset;
1263 i < dma_dom->aperture_size;
1264 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001265 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001266 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1267 continue;
1268
1269 dma_ops_reserve_addresses(dma_dom, i << PAGE_SHIFT, 1);
1270 }
1271
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001272 update_domain(&dma_dom->domain);
1273
Joerg Roedel9cabe892009-05-18 16:38:55 +02001274 return 0;
1275
1276out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001277 update_domain(&dma_dom->domain);
1278
Joerg Roedel9cabe892009-05-18 16:38:55 +02001279 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1280
1281 kfree(dma_dom->aperture[index]);
1282 dma_dom->aperture[index] = NULL;
1283
1284 return -ENOMEM;
1285}
1286
Joerg Roedel384de722009-05-15 12:30:05 +02001287static unsigned long dma_ops_area_alloc(struct device *dev,
1288 struct dma_ops_domain *dom,
1289 unsigned int pages,
1290 unsigned long align_mask,
1291 u64 dma_mask,
1292 unsigned long start)
1293{
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001294 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001295 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1296 int i = start >> APERTURE_RANGE_SHIFT;
1297 unsigned long boundary_size;
1298 unsigned long address = -1;
1299 unsigned long limit;
1300
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001301 next_bit >>= PAGE_SHIFT;
1302
Joerg Roedel384de722009-05-15 12:30:05 +02001303 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1304 PAGE_SIZE) >> PAGE_SHIFT;
1305
1306 for (;i < max_index; ++i) {
1307 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1308
1309 if (dom->aperture[i]->offset >= dma_mask)
1310 break;
1311
1312 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1313 dma_mask >> PAGE_SHIFT);
1314
1315 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1316 limit, next_bit, pages, 0,
1317 boundary_size, align_mask);
1318 if (address != -1) {
1319 address = dom->aperture[i]->offset +
1320 (address << PAGE_SHIFT);
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001321 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001322 break;
1323 }
1324
1325 next_bit = 0;
1326 }
1327
1328 return address;
1329}
1330
Joerg Roedeld3086442008-06-26 21:27:57 +02001331static unsigned long dma_ops_alloc_addresses(struct device *dev,
1332 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001333 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001334 unsigned long align_mask,
1335 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001336{
Joerg Roedeld3086442008-06-26 21:27:57 +02001337 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001338
Joerg Roedelfe16f082009-05-22 12:27:53 +02001339#ifdef CONFIG_IOMMU_STRESS
1340 dom->next_address = 0;
1341 dom->need_flush = true;
1342#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001343
Joerg Roedel384de722009-05-15 12:30:05 +02001344 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001345 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001346
Joerg Roedel1c655772008-09-04 18:40:05 +02001347 if (address == -1) {
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001348 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001349 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1350 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001351 dom->need_flush = true;
1352 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001353
Joerg Roedel384de722009-05-15 12:30:05 +02001354 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001355 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001356
1357 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1358
1359 return address;
1360}
1361
Joerg Roedel431b2a22008-07-11 17:14:22 +02001362/*
1363 * The address free function.
1364 *
1365 * called with domain->lock held
1366 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001367static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1368 unsigned long address,
1369 unsigned int pages)
1370{
Joerg Roedel384de722009-05-15 12:30:05 +02001371 unsigned i = address >> APERTURE_RANGE_SHIFT;
1372 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001373
Joerg Roedel384de722009-05-15 12:30:05 +02001374 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1375
Joerg Roedel47bccd62009-05-22 12:40:54 +02001376#ifdef CONFIG_IOMMU_STRESS
1377 if (i < 4)
1378 return;
1379#endif
1380
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001381 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001382 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001383
1384 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001385
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001386 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001387
Joerg Roedeld3086442008-06-26 21:27:57 +02001388}
1389
Joerg Roedel431b2a22008-07-11 17:14:22 +02001390/****************************************************************************
1391 *
1392 * The next functions belong to the domain allocation. A domain is
1393 * allocated for every IOMMU as the default domain. If device isolation
1394 * is enabled, every device get its own domain. The most important thing
1395 * about domains is the page table mapping the DMA address space they
1396 * contain.
1397 *
1398 ****************************************************************************/
1399
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001400/*
1401 * This function adds a protection domain to the global protection domain list
1402 */
1403static void add_domain_to_list(struct protection_domain *domain)
1404{
1405 unsigned long flags;
1406
1407 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1408 list_add(&domain->list, &amd_iommu_pd_list);
1409 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1410}
1411
1412/*
1413 * This function removes a protection domain to the global
1414 * protection domain list
1415 */
1416static void del_domain_from_list(struct protection_domain *domain)
1417{
1418 unsigned long flags;
1419
1420 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1421 list_del(&domain->list);
1422 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1423}
1424
Joerg Roedelec487d12008-06-26 21:27:58 +02001425static u16 domain_id_alloc(void)
1426{
1427 unsigned long flags;
1428 int id;
1429
1430 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1431 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1432 BUG_ON(id == 0);
1433 if (id > 0 && id < MAX_DOMAIN_ID)
1434 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1435 else
1436 id = 0;
1437 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1438
1439 return id;
1440}
1441
Joerg Roedela2acfb72008-12-02 18:28:53 +01001442static void domain_id_free(int id)
1443{
1444 unsigned long flags;
1445
1446 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1447 if (id > 0 && id < MAX_DOMAIN_ID)
1448 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1449 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1450}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001451
Joerg Roedel86db2e52008-12-02 18:20:21 +01001452static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001453{
1454 int i, j;
1455 u64 *p1, *p2, *p3;
1456
Joerg Roedel86db2e52008-12-02 18:20:21 +01001457 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001458
1459 if (!p1)
1460 return;
1461
1462 for (i = 0; i < 512; ++i) {
1463 if (!IOMMU_PTE_PRESENT(p1[i]))
1464 continue;
1465
1466 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001467 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001468 if (!IOMMU_PTE_PRESENT(p2[j]))
1469 continue;
1470 p3 = IOMMU_PTE_PAGE(p2[j]);
1471 free_page((unsigned long)p3);
1472 }
1473
1474 free_page((unsigned long)p2);
1475 }
1476
1477 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001478
1479 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001480}
1481
Joerg Roedel431b2a22008-07-11 17:14:22 +02001482/*
1483 * Free a domain, only used if something went wrong in the
1484 * allocation path and we need to free an already allocated page table
1485 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001486static void dma_ops_domain_free(struct dma_ops_domain *dom)
1487{
Joerg Roedel384de722009-05-15 12:30:05 +02001488 int i;
1489
Joerg Roedelec487d12008-06-26 21:27:58 +02001490 if (!dom)
1491 return;
1492
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001493 del_domain_from_list(&dom->domain);
1494
Joerg Roedel86db2e52008-12-02 18:20:21 +01001495 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001496
Joerg Roedel384de722009-05-15 12:30:05 +02001497 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1498 if (!dom->aperture[i])
1499 continue;
1500 free_page((unsigned long)dom->aperture[i]->bitmap);
1501 kfree(dom->aperture[i]);
1502 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001503
1504 kfree(dom);
1505}
1506
Joerg Roedel431b2a22008-07-11 17:14:22 +02001507/*
1508 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001509 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001510 * structures required for the dma_ops interface
1511 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001512static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001513{
1514 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001515
1516 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1517 if (!dma_dom)
1518 return NULL;
1519
1520 spin_lock_init(&dma_dom->domain.lock);
1521
1522 dma_dom->domain.id = domain_id_alloc();
1523 if (dma_dom->domain.id == 0)
1524 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001525 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001526 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001527 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001528 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001529 dma_dom->domain.priv = dma_dom;
1530 if (!dma_dom->domain.pt_root)
1531 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001532
Joerg Roedel1c655772008-09-04 18:40:05 +02001533 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001534 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001535
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001536 add_domain_to_list(&dma_dom->domain);
1537
Joerg Roedel576175c2009-11-23 19:08:46 +01001538 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001539 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001540
Joerg Roedel431b2a22008-07-11 17:14:22 +02001541 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001542 * mark the first page as allocated so we never return 0 as
1543 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001544 */
Joerg Roedel384de722009-05-15 12:30:05 +02001545 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001546 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001547
Joerg Roedelec487d12008-06-26 21:27:58 +02001548
1549 return dma_dom;
1550
1551free_dma_dom:
1552 dma_ops_domain_free(dma_dom);
1553
1554 return NULL;
1555}
1556
Joerg Roedel431b2a22008-07-11 17:14:22 +02001557/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001558 * little helper function to check whether a given protection domain is a
1559 * dma_ops domain
1560 */
1561static bool dma_ops_domain(struct protection_domain *domain)
1562{
1563 return domain->flags & PD_DMA_OPS_MASK;
1564}
1565
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001566static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001567{
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001568 u64 pte_root = virt_to_phys(domain->pt_root);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001569 u32 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001570
Joerg Roedel38ddf412008-09-11 10:38:32 +02001571 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1572 << DEV_ENTRY_MODE_SHIFT;
1573 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001574
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001575 if (ats)
1576 flags |= DTE_FLAG_IOTLB;
1577
1578 amd_iommu_dev_table[devid].data[3] |= flags;
1579 amd_iommu_dev_table[devid].data[2] = domain->id;
1580 amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
1581 amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001582}
1583
Joerg Roedel15898bb2009-11-24 15:39:42 +01001584static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001585{
Joerg Roedel355bf552008-12-08 12:02:41 +01001586 /* remove entry from the device table seen by the hardware */
1587 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1588 amd_iommu_dev_table[devid].data[1] = 0;
1589 amd_iommu_dev_table[devid].data[2] = 0;
1590
Joerg Roedelc5cca142009-10-09 18:31:20 +02001591 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001592}
1593
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001594static void do_attach(struct iommu_dev_data *dev_data,
1595 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001596{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001597 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001598 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001599
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001600 iommu = amd_iommu_rlookup_table[dev_data->devid];
1601 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001602
1603 /* Update data structures */
1604 dev_data->domain = domain;
1605 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001606 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001607
1608 /* Do reference counting */
1609 domain->dev_iommu[iommu->index] += 1;
1610 domain->dev_cnt += 1;
1611
1612 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001613 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001614}
1615
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001616static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001617{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001618 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001619
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001620 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02001621
Joerg Roedelc4596112009-11-20 14:57:32 +01001622 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001623 dev_data->domain->dev_iommu[iommu->index] -= 1;
1624 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001625
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001626 /* Update data structures */
1627 dev_data->domain = NULL;
1628 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001629 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001630
1631 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001632 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001633}
1634
1635/*
1636 * If a device is not yet associated with a domain, this function does
1637 * assigns it visible for the hardware
1638 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001639static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01001640 struct protection_domain *domain)
1641{
Julia Lawall84fe6c12010-05-27 12:31:51 +02001642 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001643
Joerg Roedel15898bb2009-11-24 15:39:42 +01001644 /* lock domain */
1645 spin_lock(&domain->lock);
1646
Joerg Roedel71f77582011-06-09 19:03:15 +02001647 if (dev_data->alias_data != NULL) {
1648 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02001649
1650 /* Some sanity checks */
1651 ret = -EBUSY;
1652 if (alias_data->domain != NULL &&
1653 alias_data->domain != domain)
1654 goto out_unlock;
1655
1656 if (dev_data->domain != NULL &&
1657 dev_data->domain != domain)
1658 goto out_unlock;
1659
1660 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001661 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001662 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01001663
1664 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001665 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01001666
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001667 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001668 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001669
Joerg Roedel24100052009-11-25 15:59:57 +01001670 atomic_inc(&dev_data->bind);
1671
Julia Lawall84fe6c12010-05-27 12:31:51 +02001672 ret = 0;
1673
1674out_unlock:
1675
Joerg Roedel355bf552008-12-08 12:02:41 +01001676 /* ready */
1677 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001678
Julia Lawall84fe6c12010-05-27 12:31:51 +02001679 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001680}
1681
1682/*
1683 * If a device is not yet associated with a domain, this function does
1684 * assigns it visible for the hardware
1685 */
1686static int attach_device(struct device *dev,
1687 struct protection_domain *domain)
1688{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001689 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001690 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001691 unsigned long flags;
1692 int ret;
1693
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001694 dev_data = get_dev_data(dev);
1695
1696 if (amd_iommu_iotlb_sup && pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
1697 dev_data->ats.enabled = true;
1698 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1699 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001700
Joerg Roedel15898bb2009-11-24 15:39:42 +01001701 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001702 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001703 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1704
1705 /*
1706 * We might boot into a crash-kernel here. The crashed kernel
1707 * left the caches in the IOMMU dirty. So we have to flush
1708 * here to evict all dirty stuff.
1709 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001710 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001711
1712 return ret;
1713}
1714
1715/*
1716 * Removes a device from a protection domain (unlocked)
1717 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001718static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01001719{
Joerg Roedel2ca76272010-01-22 16:45:31 +01001720 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001721 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001722
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001723 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001724
Joerg Roedel2ca76272010-01-22 16:45:31 +01001725 domain = dev_data->domain;
1726
1727 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01001728
Joerg Roedel71f77582011-06-09 19:03:15 +02001729 if (dev_data->alias_data != NULL) {
1730 struct iommu_dev_data *alias_data = dev_data->alias_data;
1731
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001732 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001733 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01001734 }
1735
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001736 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001737 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001738
Joerg Roedel2ca76272010-01-22 16:45:31 +01001739 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001740
Joerg Roedel21129f72009-09-01 11:59:42 +02001741 /*
1742 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001743 * passthrough domain if it is detached from any other domain.
1744 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02001745 */
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001746 if (iommu_pass_through &&
1747 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001748 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01001749}
1750
1751/*
1752 * Removes a device from a protection domain (with devtable_lock held)
1753 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01001754static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01001755{
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001756 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01001757 unsigned long flags;
1758
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001759 dev_data = get_dev_data(dev);
1760
Joerg Roedel355bf552008-12-08 12:02:41 +01001761 /* lock device table */
1762 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001763 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01001764 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001765
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001766 if (dev_data->ats.enabled) {
1767 pci_disable_ats(to_pci_dev(dev));
1768 dev_data->ats.enabled = false;
1769 }
Joerg Roedel355bf552008-12-08 12:02:41 +01001770}
Joerg Roedele275a2a2008-12-10 18:27:25 +01001771
Joerg Roedel15898bb2009-11-24 15:39:42 +01001772/*
1773 * Find out the protection domain structure for a given PCI device. This
1774 * will give us the pointer to the page table root for example.
1775 */
1776static struct protection_domain *domain_for_device(struct device *dev)
1777{
Joerg Roedel71f77582011-06-09 19:03:15 +02001778 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02001779 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001780 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001781
Joerg Roedel657cbb62009-11-23 15:26:46 +01001782 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001783
Joerg Roedel2b02b092011-06-09 17:48:39 +02001784 if (dev_data->domain)
1785 return dev_data->domain;
1786
Joerg Roedel71f77582011-06-09 19:03:15 +02001787 if (dev_data->alias_data != NULL) {
1788 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02001789
1790 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
1791 if (alias_data->domain != NULL) {
1792 __attach_device(dev_data, alias_data->domain);
1793 dom = alias_data->domain;
1794 }
1795 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001796 }
1797
Joerg Roedel15898bb2009-11-24 15:39:42 +01001798 return dom;
1799}
1800
Joerg Roedele275a2a2008-12-10 18:27:25 +01001801static int device_change_notifier(struct notifier_block *nb,
1802 unsigned long action, void *data)
1803{
1804 struct device *dev = data;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001805 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001806 struct protection_domain *domain;
1807 struct dma_ops_domain *dma_domain;
1808 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001809 unsigned long flags;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001810
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001811 if (!check_device(dev))
1812 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001813
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001814 devid = get_device_id(dev);
1815 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01001816
1817 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07001818 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001819
1820 domain = domain_for_device(dev);
1821
Joerg Roedele275a2a2008-12-10 18:27:25 +01001822 if (!domain)
1823 goto out;
Joerg Roedela1ca3312009-09-01 12:22:22 +02001824 if (iommu_pass_through)
1825 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001826 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001827 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001828 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001829
1830 iommu_init_device(dev);
1831
1832 domain = domain_for_device(dev);
1833
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001834 /* allocate a protection domain if a device is added */
1835 dma_domain = find_protection_domain(devid);
1836 if (dma_domain)
1837 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01001838 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001839 if (!dma_domain)
1840 goto out;
1841 dma_domain->target_dev = devid;
1842
1843 spin_lock_irqsave(&iommu_pd_list_lock, flags);
1844 list_add_tail(&dma_domain->list, &iommu_pd_list);
1845 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
1846
1847 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001848 case BUS_NOTIFY_DEL_DEVICE:
1849
1850 iommu_uninit_device(dev);
1851
Joerg Roedele275a2a2008-12-10 18:27:25 +01001852 default:
1853 goto out;
1854 }
1855
Joerg Roedele275a2a2008-12-10 18:27:25 +01001856 iommu_completion_wait(iommu);
1857
1858out:
1859 return 0;
1860}
1861
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05301862static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01001863 .notifier_call = device_change_notifier,
1864};
Joerg Roedel355bf552008-12-08 12:02:41 +01001865
Joerg Roedel8638c492009-12-10 11:12:25 +01001866void amd_iommu_init_notifier(void)
1867{
1868 bus_register_notifier(&pci_bus_type, &device_nb);
1869}
1870
Joerg Roedel431b2a22008-07-11 17:14:22 +02001871/*****************************************************************************
1872 *
1873 * The next functions belong to the dma_ops mapping/unmapping code.
1874 *
1875 *****************************************************************************/
1876
1877/*
1878 * In the dma_ops path we only have the struct device. This function
1879 * finds the corresponding IOMMU, the protection domain and the
1880 * requestor id for a given device.
1881 * If the device is not yet associated with a domain this is also done
1882 * in this function.
1883 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001884static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001885{
Joerg Roedel94f6d192009-11-24 16:40:02 +01001886 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001887 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01001888 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001889
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001890 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01001891 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001892
Joerg Roedel94f6d192009-11-24 16:40:02 +01001893 domain = domain_for_device(dev);
1894 if (domain != NULL && !dma_ops_domain(domain))
1895 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001896
Joerg Roedel94f6d192009-11-24 16:40:02 +01001897 if (domain != NULL)
1898 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001899
Joerg Roedel15898bb2009-11-24 15:39:42 +01001900 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001901 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001902 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01001903 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
1904 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001905 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01001906 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01001907
Joerg Roedel94f6d192009-11-24 16:40:02 +01001908 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001909}
1910
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001911static void update_device_table(struct protection_domain *domain)
1912{
Joerg Roedel492667d2009-11-27 13:25:47 +01001913 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001914
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001915 list_for_each_entry(dev_data, &domain->dev_list, list)
1916 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001917}
1918
1919static void update_domain(struct protection_domain *domain)
1920{
1921 if (!domain->updated)
1922 return;
1923
1924 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02001925
1926 domain_flush_devices(domain);
1927 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001928
1929 domain->updated = false;
1930}
1931
Joerg Roedel431b2a22008-07-11 17:14:22 +02001932/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02001933 * This function fetches the PTE for a given address in the aperture
1934 */
1935static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
1936 unsigned long address)
1937{
Joerg Roedel384de722009-05-15 12:30:05 +02001938 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02001939 u64 *pte, *pte_page;
1940
Joerg Roedel384de722009-05-15 12:30:05 +02001941 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1942 if (!aperture)
1943 return NULL;
1944
1945 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02001946 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001947 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001948 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02001949 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
1950 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001951 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001952
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001953 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001954
1955 return pte;
1956}
1957
1958/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001959 * This is the generic map function. It maps one 4kb page at paddr to
1960 * the given address in the DMA address space for the domain.
1961 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001962static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001963 unsigned long address,
1964 phys_addr_t paddr,
1965 int direction)
1966{
1967 u64 *pte, __pte;
1968
1969 WARN_ON(address > dom->aperture_size);
1970
1971 paddr &= PAGE_MASK;
1972
Joerg Roedel8bda3092009-05-12 12:02:46 +02001973 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02001974 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001975 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001976
1977 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
1978
1979 if (direction == DMA_TO_DEVICE)
1980 __pte |= IOMMU_PTE_IR;
1981 else if (direction == DMA_FROM_DEVICE)
1982 __pte |= IOMMU_PTE_IW;
1983 else if (direction == DMA_BIDIRECTIONAL)
1984 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
1985
1986 WARN_ON(*pte);
1987
1988 *pte = __pte;
1989
1990 return (dma_addr_t)address;
1991}
1992
Joerg Roedel431b2a22008-07-11 17:14:22 +02001993/*
1994 * The generic unmapping function for on page in the DMA address space.
1995 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001996static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001997 unsigned long address)
1998{
Joerg Roedel384de722009-05-15 12:30:05 +02001999 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002000 u64 *pte;
2001
2002 if (address >= dom->aperture_size)
2003 return;
2004
Joerg Roedel384de722009-05-15 12:30:05 +02002005 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2006 if (!aperture)
2007 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002008
Joerg Roedel384de722009-05-15 12:30:05 +02002009 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2010 if (!pte)
2011 return;
2012
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002013 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002014
2015 WARN_ON(!*pte);
2016
2017 *pte = 0ULL;
2018}
2019
Joerg Roedel431b2a22008-07-11 17:14:22 +02002020/*
2021 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002022 * contiguous memory region into DMA address space. It is used by all
2023 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002024 * Must be called with the domain lock held.
2025 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002026static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002027 struct dma_ops_domain *dma_dom,
2028 phys_addr_t paddr,
2029 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002030 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002031 bool align,
2032 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002033{
2034 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002035 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002036 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002037 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002038 int i;
2039
Joerg Roedele3c449f2008-10-15 22:02:11 -07002040 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002041 paddr &= PAGE_MASK;
2042
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002043 INC_STATS_COUNTER(total_map_requests);
2044
Joerg Roedelc1858972008-12-12 15:42:39 +01002045 if (pages > 1)
2046 INC_STATS_COUNTER(cross_page);
2047
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002048 if (align)
2049 align_mask = (1UL << get_order(size)) - 1;
2050
Joerg Roedel11b83882009-05-19 10:23:15 +02002051retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002052 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2053 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002054 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002055 /*
2056 * setting next_address here will let the address
2057 * allocator only scan the new allocated range in the
2058 * first run. This is a small optimization.
2059 */
2060 dma_dom->next_address = dma_dom->aperture_size;
2061
Joerg Roedel576175c2009-11-23 19:08:46 +01002062 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002063 goto out;
2064
2065 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002066 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002067 * allocation again
2068 */
2069 goto retry;
2070 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002071
2072 start = address;
2073 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002074 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002075 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002076 goto out_unmap;
2077
Joerg Roedelcb76c322008-06-26 21:28:00 +02002078 paddr += PAGE_SIZE;
2079 start += PAGE_SIZE;
2080 }
2081 address += offset;
2082
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002083 ADD_STATS_COUNTER(alloced_io_mem, size);
2084
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002085 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002086 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002087 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002088 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002089 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002090
Joerg Roedelcb76c322008-06-26 21:28:00 +02002091out:
2092 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002093
2094out_unmap:
2095
2096 for (--i; i >= 0; --i) {
2097 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002098 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002099 }
2100
2101 dma_ops_free_addresses(dma_dom, address, pages);
2102
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002103 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002104}
2105
Joerg Roedel431b2a22008-07-11 17:14:22 +02002106/*
2107 * Does the reverse of the __map_single function. Must be called with
2108 * the domain lock held too
2109 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002110static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002111 dma_addr_t dma_addr,
2112 size_t size,
2113 int dir)
2114{
Joerg Roedel04e04632010-09-23 16:12:48 +02002115 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002116 dma_addr_t i, start;
2117 unsigned int pages;
2118
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002119 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002120 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002121 return;
2122
Joerg Roedel04e04632010-09-23 16:12:48 +02002123 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002124 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002125 dma_addr &= PAGE_MASK;
2126 start = dma_addr;
2127
2128 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002129 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002130 start += PAGE_SIZE;
2131 }
2132
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002133 SUB_STATS_COUNTER(alloced_io_mem, size);
2134
Joerg Roedelcb76c322008-06-26 21:28:00 +02002135 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002136
Joerg Roedel80be3082008-11-06 14:59:05 +01002137 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002138 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002139 dma_dom->need_flush = false;
2140 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002141}
2142
Joerg Roedel431b2a22008-07-11 17:14:22 +02002143/*
2144 * The exported map_single function for dma_ops.
2145 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002146static dma_addr_t map_page(struct device *dev, struct page *page,
2147 unsigned long offset, size_t size,
2148 enum dma_data_direction dir,
2149 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002150{
2151 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002152 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002153 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002154 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002155 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002156
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002157 INC_STATS_COUNTER(cnt_map_single);
2158
Joerg Roedel94f6d192009-11-24 16:40:02 +01002159 domain = get_domain(dev);
2160 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002161 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002162 else if (IS_ERR(domain))
2163 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002164
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002165 dma_mask = *dev->dma_mask;
2166
Joerg Roedel4da70b92008-06-26 21:28:01 +02002167 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002168
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002169 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002170 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002171 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002172 goto out;
2173
Joerg Roedel17b124b2011-04-06 18:01:35 +02002174 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002175
2176out:
2177 spin_unlock_irqrestore(&domain->lock, flags);
2178
2179 return addr;
2180}
2181
Joerg Roedel431b2a22008-07-11 17:14:22 +02002182/*
2183 * The exported unmap_single function for dma_ops.
2184 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002185static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2186 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002187{
2188 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002189 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002190
Joerg Roedel146a6912008-12-12 15:07:12 +01002191 INC_STATS_COUNTER(cnt_unmap_single);
2192
Joerg Roedel94f6d192009-11-24 16:40:02 +01002193 domain = get_domain(dev);
2194 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002195 return;
2196
Joerg Roedel4da70b92008-06-26 21:28:01 +02002197 spin_lock_irqsave(&domain->lock, flags);
2198
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002199 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002200
Joerg Roedel17b124b2011-04-06 18:01:35 +02002201 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002202
2203 spin_unlock_irqrestore(&domain->lock, flags);
2204}
2205
Joerg Roedel431b2a22008-07-11 17:14:22 +02002206/*
2207 * This is a special map_sg function which is used if we should map a
2208 * device which is not handled by an AMD IOMMU in the system.
2209 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002210static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2211 int nelems, int dir)
2212{
2213 struct scatterlist *s;
2214 int i;
2215
2216 for_each_sg(sglist, s, nelems, i) {
2217 s->dma_address = (dma_addr_t)sg_phys(s);
2218 s->dma_length = s->length;
2219 }
2220
2221 return nelems;
2222}
2223
Joerg Roedel431b2a22008-07-11 17:14:22 +02002224/*
2225 * The exported map_sg function for dma_ops (handles scatter-gather
2226 * lists).
2227 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002228static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002229 int nelems, enum dma_data_direction dir,
2230 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002231{
2232 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002233 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002234 int i;
2235 struct scatterlist *s;
2236 phys_addr_t paddr;
2237 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002238 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002239
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002240 INC_STATS_COUNTER(cnt_map_sg);
2241
Joerg Roedel94f6d192009-11-24 16:40:02 +01002242 domain = get_domain(dev);
2243 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002244 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002245 else if (IS_ERR(domain))
2246 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002247
Joerg Roedel832a90c2008-09-18 15:54:23 +02002248 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002249
Joerg Roedel65b050a2008-06-26 21:28:02 +02002250 spin_lock_irqsave(&domain->lock, flags);
2251
2252 for_each_sg(sglist, s, nelems, i) {
2253 paddr = sg_phys(s);
2254
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002255 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002256 paddr, s->length, dir, false,
2257 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002258
2259 if (s->dma_address) {
2260 s->dma_length = s->length;
2261 mapped_elems++;
2262 } else
2263 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002264 }
2265
Joerg Roedel17b124b2011-04-06 18:01:35 +02002266 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002267
2268out:
2269 spin_unlock_irqrestore(&domain->lock, flags);
2270
2271 return mapped_elems;
2272unmap:
2273 for_each_sg(sglist, s, mapped_elems, i) {
2274 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002275 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002276 s->dma_length, dir);
2277 s->dma_address = s->dma_length = 0;
2278 }
2279
2280 mapped_elems = 0;
2281
2282 goto out;
2283}
2284
Joerg Roedel431b2a22008-07-11 17:14:22 +02002285/*
2286 * The exported map_sg function for dma_ops (handles scatter-gather
2287 * lists).
2288 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002289static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002290 int nelems, enum dma_data_direction dir,
2291 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002292{
2293 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002294 struct protection_domain *domain;
2295 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002296 int i;
2297
Joerg Roedel55877a62008-12-12 15:12:14 +01002298 INC_STATS_COUNTER(cnt_unmap_sg);
2299
Joerg Roedel94f6d192009-11-24 16:40:02 +01002300 domain = get_domain(dev);
2301 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002302 return;
2303
Joerg Roedel65b050a2008-06-26 21:28:02 +02002304 spin_lock_irqsave(&domain->lock, flags);
2305
2306 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002307 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002308 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002309 s->dma_address = s->dma_length = 0;
2310 }
2311
Joerg Roedel17b124b2011-04-06 18:01:35 +02002312 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002313
2314 spin_unlock_irqrestore(&domain->lock, flags);
2315}
2316
Joerg Roedel431b2a22008-07-11 17:14:22 +02002317/*
2318 * The exported alloc_coherent function for dma_ops.
2319 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002320static void *alloc_coherent(struct device *dev, size_t size,
2321 dma_addr_t *dma_addr, gfp_t flag)
2322{
2323 unsigned long flags;
2324 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002325 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002326 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002327 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002328
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002329 INC_STATS_COUNTER(cnt_alloc_coherent);
2330
Joerg Roedel94f6d192009-11-24 16:40:02 +01002331 domain = get_domain(dev);
2332 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002333 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2334 *dma_addr = __pa(virt_addr);
2335 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002336 } else if (IS_ERR(domain))
2337 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002338
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002339 dma_mask = dev->coherent_dma_mask;
2340 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2341 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002342
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002343 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2344 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302345 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002346
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002347 paddr = virt_to_phys(virt_addr);
2348
Joerg Roedel832a90c2008-09-18 15:54:23 +02002349 if (!dma_mask)
2350 dma_mask = *dev->dma_mask;
2351
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002352 spin_lock_irqsave(&domain->lock, flags);
2353
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002354 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002355 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002356
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002357 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002358 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002359 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002360 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002361
Joerg Roedel17b124b2011-04-06 18:01:35 +02002362 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002363
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002364 spin_unlock_irqrestore(&domain->lock, flags);
2365
2366 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002367
2368out_free:
2369
2370 free_pages((unsigned long)virt_addr, get_order(size));
2371
2372 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002373}
2374
Joerg Roedel431b2a22008-07-11 17:14:22 +02002375/*
2376 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002377 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002378static void free_coherent(struct device *dev, size_t size,
2379 void *virt_addr, dma_addr_t dma_addr)
2380{
2381 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002382 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002383
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002384 INC_STATS_COUNTER(cnt_free_coherent);
2385
Joerg Roedel94f6d192009-11-24 16:40:02 +01002386 domain = get_domain(dev);
2387 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002388 goto free_mem;
2389
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002390 spin_lock_irqsave(&domain->lock, flags);
2391
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002392 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002393
Joerg Roedel17b124b2011-04-06 18:01:35 +02002394 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002395
2396 spin_unlock_irqrestore(&domain->lock, flags);
2397
2398free_mem:
2399 free_pages((unsigned long)virt_addr, get_order(size));
2400}
2401
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002402/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002403 * This function is called by the DMA layer to find out if we can handle a
2404 * particular device. It is part of the dma_ops.
2405 */
2406static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2407{
Joerg Roedel420aef82009-11-23 16:14:57 +01002408 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002409}
2410
2411/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002412 * The function for pre-allocating protection domains.
2413 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002414 * If the driver core informs the DMA layer if a driver grabs a device
2415 * we don't need to preallocate the protection domains anymore.
2416 * For now we have to.
2417 */
Jaswinder Singh Rajput0e93dd82008-12-29 21:45:22 +05302418static void prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002419{
2420 struct pci_dev *dev = NULL;
2421 struct dma_ops_domain *dma_dom;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002422 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002423
Chris Wrightd18c69d2010-04-02 18:27:55 -07002424 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002425
2426 /* Do we handle this device? */
2427 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002428 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002429
2430 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002431 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002432 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002433
2434 devid = get_device_id(&dev->dev);
2435
Joerg Roedel87a64d52009-11-24 17:26:43 +01002436 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002437 if (!dma_dom)
2438 continue;
2439 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002440 dma_dom->target_dev = devid;
2441
Joerg Roedel15898bb2009-11-24 15:39:42 +01002442 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01002443
Joerg Roedelbd60b732008-09-11 10:24:48 +02002444 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002445 }
2446}
2447
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002448static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002449 .alloc_coherent = alloc_coherent,
2450 .free_coherent = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002451 .map_page = map_page,
2452 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002453 .map_sg = map_sg,
2454 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002455 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002456};
2457
Joerg Roedel27c21272011-05-30 15:56:24 +02002458static unsigned device_dma_ops_init(void)
2459{
2460 struct pci_dev *pdev = NULL;
2461 unsigned unhandled = 0;
2462
2463 for_each_pci_dev(pdev) {
2464 if (!check_device(&pdev->dev)) {
2465 unhandled += 1;
2466 continue;
2467 }
2468
2469 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
2470 }
2471
2472 return unhandled;
2473}
2474
Joerg Roedel431b2a22008-07-11 17:14:22 +02002475/*
2476 * The function which clues the AMD IOMMU driver into dma_ops.
2477 */
Joerg Roedelf5325092010-01-22 17:44:35 +01002478
2479void __init amd_iommu_init_api(void)
2480{
2481 register_iommu(&amd_iommu_ops);
2482}
2483
Joerg Roedel6631ee92008-06-26 21:28:05 +02002484int __init amd_iommu_init_dma_ops(void)
2485{
2486 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02002487 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002488
Joerg Roedel431b2a22008-07-11 17:14:22 +02002489 /*
2490 * first allocate a default protection domain for every IOMMU we
2491 * found in the system. Devices not assigned to any other
2492 * protection domain will be assigned to the default one.
2493 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02002494 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01002495 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002496 if (iommu->default_dom == NULL)
2497 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01002498 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002499 ret = iommu_init_unity_mappings(iommu);
2500 if (ret)
2501 goto free_domains;
2502 }
2503
Joerg Roedel431b2a22008-07-11 17:14:22 +02002504 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01002505 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002506 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01002507 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002508
2509 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002510 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002511
Joerg Roedel431b2a22008-07-11 17:14:22 +02002512 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02002513 unhandled = device_dma_ops_init();
2514 if (unhandled && max_pfn > MAX_DMA32_PFN) {
2515 /* There are unhandled devices - initialize swiotlb for them */
2516 swiotlb = 1;
2517 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02002518
Joerg Roedel7f265082008-12-12 13:50:21 +01002519 amd_iommu_stats_init();
2520
Joerg Roedel6631ee92008-06-26 21:28:05 +02002521 return 0;
2522
2523free_domains:
2524
Joerg Roedel3bd22172009-05-04 15:06:20 +02002525 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002526 if (iommu->default_dom)
2527 dma_ops_domain_free(iommu->default_dom);
2528 }
2529
2530 return ret;
2531}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002532
2533/*****************************************************************************
2534 *
2535 * The following functions belong to the exported interface of AMD IOMMU
2536 *
2537 * This interface allows access to lower level functions of the IOMMU
2538 * like protection domain handling and assignement of devices to domains
2539 * which is not possible with the dma_ops interface.
2540 *
2541 *****************************************************************************/
2542
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002543static void cleanup_domain(struct protection_domain *domain)
2544{
Joerg Roedel492667d2009-11-27 13:25:47 +01002545 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002546 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002547
2548 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2549
Joerg Roedel492667d2009-11-27 13:25:47 +01002550 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002551 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01002552 atomic_set(&dev_data->bind, 0);
2553 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002554
2555 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2556}
2557
Joerg Roedel26508152009-08-26 16:52:40 +02002558static void protection_domain_free(struct protection_domain *domain)
2559{
2560 if (!domain)
2561 return;
2562
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002563 del_domain_from_list(domain);
2564
Joerg Roedel26508152009-08-26 16:52:40 +02002565 if (domain->id)
2566 domain_id_free(domain->id);
2567
2568 kfree(domain);
2569}
2570
2571static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002572{
2573 struct protection_domain *domain;
2574
2575 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2576 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002577 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002578
2579 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002580 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01002581 domain->id = domain_id_alloc();
2582 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02002583 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002584 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02002585
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002586 add_domain_to_list(domain);
2587
Joerg Roedel26508152009-08-26 16:52:40 +02002588 return domain;
2589
2590out_err:
2591 kfree(domain);
2592
2593 return NULL;
2594}
2595
2596static int amd_iommu_domain_init(struct iommu_domain *dom)
2597{
2598 struct protection_domain *domain;
2599
2600 domain = protection_domain_alloc();
2601 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01002602 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02002603
2604 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002605 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2606 if (!domain->pt_root)
2607 goto out_free;
2608
2609 dom->priv = domain;
2610
2611 return 0;
2612
2613out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02002614 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01002615
2616 return -ENOMEM;
2617}
2618
Joerg Roedel98383fc2008-12-02 18:34:12 +01002619static void amd_iommu_domain_destroy(struct iommu_domain *dom)
2620{
2621 struct protection_domain *domain = dom->priv;
2622
2623 if (!domain)
2624 return;
2625
2626 if (domain->dev_cnt > 0)
2627 cleanup_domain(domain);
2628
2629 BUG_ON(domain->dev_cnt != 0);
2630
2631 free_pagetable(domain);
2632
Joerg Roedel8b408fe2010-03-08 14:20:07 +01002633 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002634
2635 dom->priv = NULL;
2636}
2637
Joerg Roedel684f2882008-12-08 12:07:44 +01002638static void amd_iommu_detach_device(struct iommu_domain *dom,
2639 struct device *dev)
2640{
Joerg Roedel657cbb62009-11-23 15:26:46 +01002641 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002642 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002643 u16 devid;
2644
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002645 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01002646 return;
2647
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002648 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002649
Joerg Roedel657cbb62009-11-23 15:26:46 +01002650 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002651 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002652
2653 iommu = amd_iommu_rlookup_table[devid];
2654 if (!iommu)
2655 return;
2656
Joerg Roedel684f2882008-12-08 12:07:44 +01002657 iommu_completion_wait(iommu);
2658}
2659
Joerg Roedel01106062008-12-02 19:34:11 +01002660static int amd_iommu_attach_device(struct iommu_domain *dom,
2661 struct device *dev)
2662{
2663 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002664 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01002665 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002666 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002667
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002668 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01002669 return -EINVAL;
2670
Joerg Roedel657cbb62009-11-23 15:26:46 +01002671 dev_data = dev->archdata.iommu;
2672
Joerg Roedelf62dda62011-06-09 12:55:35 +02002673 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01002674 if (!iommu)
2675 return -EINVAL;
2676
Joerg Roedel657cbb62009-11-23 15:26:46 +01002677 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002678 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002679
Joerg Roedel15898bb2009-11-24 15:39:42 +01002680 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01002681
2682 iommu_completion_wait(iommu);
2683
Joerg Roedel15898bb2009-11-24 15:39:42 +01002684 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002685}
2686
Joerg Roedel468e2362010-01-21 16:37:36 +01002687static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
2688 phys_addr_t paddr, int gfp_order, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002689{
Joerg Roedel468e2362010-01-21 16:37:36 +01002690 unsigned long page_size = 0x1000UL << gfp_order;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002691 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002692 int prot = 0;
2693 int ret;
2694
2695 if (iommu_prot & IOMMU_READ)
2696 prot |= IOMMU_PROT_IR;
2697 if (iommu_prot & IOMMU_WRITE)
2698 prot |= IOMMU_PROT_IW;
2699
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002700 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002701 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002702 mutex_unlock(&domain->api_lock);
2703
Joerg Roedel795e74f72010-05-11 17:40:57 +02002704 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002705}
2706
Joerg Roedel468e2362010-01-21 16:37:36 +01002707static int amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2708 int gfp_order)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002709{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002710 struct protection_domain *domain = dom->priv;
Joerg Roedel468e2362010-01-21 16:37:36 +01002711 unsigned long page_size, unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002712
Joerg Roedel468e2362010-01-21 16:37:36 +01002713 page_size = 0x1000UL << gfp_order;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002714
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002715 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01002716 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002717 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002718
Joerg Roedel17b124b2011-04-06 18:01:35 +02002719 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002720
Joerg Roedel468e2362010-01-21 16:37:36 +01002721 return get_order(unmap_size);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002722}
2723
Joerg Roedel645c4c82008-12-02 20:05:50 +01002724static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2725 unsigned long iova)
2726{
2727 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002728 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002729 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002730 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002731
Joerg Roedel24cd7722010-01-19 17:27:39 +01002732 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002733
Joerg Roedela6d41a42009-09-02 17:08:55 +02002734 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01002735 return 0;
2736
Joerg Roedelf03152b2010-01-21 16:15:24 +01002737 if (PM_PTE_LEVEL(*pte) == 0)
2738 offset_mask = PAGE_SIZE - 1;
2739 else
2740 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
2741
2742 __pte = *pte & PM_ADDR_MASK;
2743 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002744
2745 return paddr;
2746}
2747
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002748static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
2749 unsigned long cap)
2750{
Joerg Roedel80a506b2010-07-27 17:14:24 +02002751 switch (cap) {
2752 case IOMMU_CAP_CACHE_COHERENCY:
2753 return 1;
2754 }
2755
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002756 return 0;
2757}
2758
Joerg Roedel26961ef2008-12-03 17:00:17 +01002759static struct iommu_ops amd_iommu_ops = {
2760 .domain_init = amd_iommu_domain_init,
2761 .domain_destroy = amd_iommu_domain_destroy,
2762 .attach_dev = amd_iommu_attach_device,
2763 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01002764 .map = amd_iommu_map,
2765 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002766 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002767 .domain_has_cap = amd_iommu_domain_has_cap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002768};
2769
Joerg Roedel0feae532009-08-26 15:26:30 +02002770/*****************************************************************************
2771 *
2772 * The next functions do a basic initialization of IOMMU for pass through
2773 * mode
2774 *
2775 * In passthrough mode the IOMMU is initialized and enabled but not used for
2776 * DMA-API translation.
2777 *
2778 *****************************************************************************/
2779
2780int __init amd_iommu_init_passthrough(void)
2781{
Joerg Roedel15898bb2009-11-24 15:39:42 +01002782 struct amd_iommu *iommu;
Joerg Roedel0feae532009-08-26 15:26:30 +02002783 struct pci_dev *dev = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002784 u16 devid;
Joerg Roedel0feae532009-08-26 15:26:30 +02002785
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002786 /* allocate passthrough domain */
Joerg Roedel0feae532009-08-26 15:26:30 +02002787 pt_domain = protection_domain_alloc();
2788 if (!pt_domain)
2789 return -ENOMEM;
2790
2791 pt_domain->mode |= PAGE_MODE_NONE;
2792
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04002793 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002794 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02002795 continue;
2796
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002797 devid = get_device_id(&dev->dev);
2798
Joerg Roedel15898bb2009-11-24 15:39:42 +01002799 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02002800 if (!iommu)
2801 continue;
2802
Joerg Roedel15898bb2009-11-24 15:39:42 +01002803 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02002804 }
2805
2806 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
2807
2808 return 0;
2809}