blob: 9e07ef65a0160ce527c98be9c5821111b4a4cd95 [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 Roedel15898bb2009-11-24 15:39:42 +0100105static inline u16 get_device_id(struct device *dev)
106{
107 struct pci_dev *pdev = to_pci_dev(dev);
108
109 return calc_devid(pdev->bus->number, pdev->devfn);
110}
111
Joerg Roedel657cbb62009-11-23 15:26:46 +0100112static struct iommu_dev_data *get_dev_data(struct device *dev)
113{
114 return dev->archdata.iommu;
115}
116
Joerg Roedel71c70982009-11-24 16:43:06 +0100117/*
118 * In this function the list of preallocated protection domains is traversed to
119 * find the domain for a specific device
120 */
121static struct dma_ops_domain *find_protection_domain(u16 devid)
122{
123 struct dma_ops_domain *entry, *ret = NULL;
124 unsigned long flags;
125 u16 alias = amd_iommu_alias_table[devid];
126
127 if (list_empty(&iommu_pd_list))
128 return NULL;
129
130 spin_lock_irqsave(&iommu_pd_list_lock, flags);
131
132 list_for_each_entry(entry, &iommu_pd_list, list) {
133 if (entry->target_dev == devid ||
134 entry->target_dev == alias) {
135 ret = entry;
136 break;
137 }
138 }
139
140 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
141
142 return ret;
143}
144
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100145/*
146 * This function checks if the driver got a valid device from the caller to
147 * avoid dereferencing invalid pointers.
148 */
149static bool check_device(struct device *dev)
150{
151 u16 devid;
152
153 if (!dev || !dev->dma_mask)
154 return false;
155
156 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100157 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100158 return false;
159
160 devid = get_device_id(dev);
161
162 /* Out of our scope? */
163 if (devid > amd_iommu_last_bdf)
164 return false;
165
166 if (amd_iommu_rlookup_table[devid] == NULL)
167 return false;
168
169 return true;
170}
171
Joerg Roedel657cbb62009-11-23 15:26:46 +0100172static int iommu_init_device(struct device *dev)
173{
174 struct iommu_dev_data *dev_data;
175 struct pci_dev *pdev;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200176 u16 alias;
Joerg Roedel657cbb62009-11-23 15:26:46 +0100177
178 if (dev->archdata.iommu)
179 return 0;
180
Joerg Roedelf62dda62011-06-09 12:55:35 +0200181 dev_data = alloc_dev_data(get_device_id(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +0100182 if (!dev_data)
183 return -ENOMEM;
184
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100185 dev_data->dev = dev;
186
Joerg Roedelf62dda62011-06-09 12:55:35 +0200187 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedel657cbb62009-11-23 15:26:46 +0100188 pdev = pci_get_bus_and_slot(PCI_BUS(alias), alias & 0xff);
189 if (pdev)
190 dev_data->alias = &pdev->dev;
Joerg Roedel26018872011-06-06 16:50:14 +0200191 else {
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200192 free_dev_data(dev_data);
Joerg Roedel26018872011-06-06 16:50:14 +0200193 return -ENOTSUPP;
194 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100195
196 dev->archdata.iommu = dev_data;
197
Joerg Roedel657cbb62009-11-23 15:26:46 +0100198 return 0;
199}
200
Joerg Roedel26018872011-06-06 16:50:14 +0200201static void iommu_ignore_device(struct device *dev)
202{
203 u16 devid, alias;
204
205 devid = get_device_id(dev);
206 alias = amd_iommu_alias_table[devid];
207
208 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
209 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
210
211 amd_iommu_rlookup_table[devid] = NULL;
212 amd_iommu_rlookup_table[alias] = NULL;
213}
214
Joerg Roedel657cbb62009-11-23 15:26:46 +0100215static void iommu_uninit_device(struct device *dev)
216{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200217 /*
218 * Nothing to do here - we keep dev_data around for unplugged devices
219 * and reuse it when the device is re-plugged - not doing so would
220 * introduce a ton of races.
221 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100222}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100223
224void __init amd_iommu_uninit_devices(void)
225{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200226 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100227 struct pci_dev *pdev = NULL;
228
229 for_each_pci_dev(pdev) {
230
231 if (!check_device(&pdev->dev))
232 continue;
233
234 iommu_uninit_device(&pdev->dev);
235 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200236
237 /* Free all of our dev_data structures */
238 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
239 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100240}
241
242int __init amd_iommu_init_devices(void)
243{
244 struct pci_dev *pdev = NULL;
245 int ret = 0;
246
247 for_each_pci_dev(pdev) {
248
249 if (!check_device(&pdev->dev))
250 continue;
251
252 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200253 if (ret == -ENOTSUPP)
254 iommu_ignore_device(&pdev->dev);
255 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100256 goto out_free;
257 }
258
259 return 0;
260
261out_free:
262
263 amd_iommu_uninit_devices();
264
265 return ret;
266}
Joerg Roedel7f265082008-12-12 13:50:21 +0100267#ifdef CONFIG_AMD_IOMMU_STATS
268
269/*
270 * Initialization code for statistics collection
271 */
272
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100273DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100274DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100275DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100276DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100277DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100278DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100279DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100280DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100281DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100282DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100283DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100284DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100285
Joerg Roedel7f265082008-12-12 13:50:21 +0100286static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100287static struct dentry *de_fflush;
288
289static void amd_iommu_stats_add(struct __iommu_counter *cnt)
290{
291 if (stats_dir == NULL)
292 return;
293
294 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
295 &cnt->value);
296}
297
298static void amd_iommu_stats_init(void)
299{
300 stats_dir = debugfs_create_dir("amd-iommu", NULL);
301 if (stats_dir == NULL)
302 return;
303
Joerg Roedel7f265082008-12-12 13:50:21 +0100304 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
305 (u32 *)&amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100306
307 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100308 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100309 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100310 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100311 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100312 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100313 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100314 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100315 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100316 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100317 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100318 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100319}
320
321#endif
322
Joerg Roedel431b2a22008-07-11 17:14:22 +0200323/****************************************************************************
324 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200325 * Interrupt handling functions
326 *
327 ****************************************************************************/
328
Joerg Roedele3e59872009-09-03 14:02:10 +0200329static void dump_dte_entry(u16 devid)
330{
331 int i;
332
333 for (i = 0; i < 8; ++i)
334 pr_err("AMD-Vi: DTE[%d]: %08x\n", i,
335 amd_iommu_dev_table[devid].data[i]);
336}
337
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200338static void dump_command(unsigned long phys_addr)
339{
340 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
341 int i;
342
343 for (i = 0; i < 4; ++i)
344 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
345}
346
Joerg Roedela345b232009-09-03 15:01:43 +0200347static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200348{
349 u32 *event = __evt;
350 int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
351 int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
352 int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
353 int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
354 u64 address = (u64)(((u64)event[3]) << 32) | event[2];
355
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200356 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200357
358 switch (type) {
359 case EVENT_TYPE_ILL_DEV:
360 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
361 "address=0x%016llx flags=0x%04x]\n",
362 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
363 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200364 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200365 break;
366 case EVENT_TYPE_IO_FAULT:
367 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
368 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
369 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
370 domid, address, flags);
371 break;
372 case EVENT_TYPE_DEV_TAB_ERR:
373 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
374 "address=0x%016llx flags=0x%04x]\n",
375 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
376 address, flags);
377 break;
378 case EVENT_TYPE_PAGE_TAB_ERR:
379 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
380 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
381 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
382 domid, address, flags);
383 break;
384 case EVENT_TYPE_ILL_CMD:
385 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200386 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200387 break;
388 case EVENT_TYPE_CMD_HARD_ERR:
389 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
390 "flags=0x%04x]\n", address, flags);
391 break;
392 case EVENT_TYPE_IOTLB_INV_TO:
393 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
394 "address=0x%016llx]\n",
395 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
396 address);
397 break;
398 case EVENT_TYPE_INV_DEV_REQ:
399 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
400 "address=0x%016llx flags=0x%04x]\n",
401 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
402 address, flags);
403 break;
404 default:
405 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
406 }
407}
408
409static void iommu_poll_events(struct amd_iommu *iommu)
410{
411 u32 head, tail;
412 unsigned long flags;
413
414 spin_lock_irqsave(&iommu->lock, flags);
415
416 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
417 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
418
419 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200420 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200421 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
422 }
423
424 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
425
426 spin_unlock_irqrestore(&iommu->lock, flags);
427}
428
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200429irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200430{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200431 struct amd_iommu *iommu;
432
Joerg Roedel3bd22172009-05-04 15:06:20 +0200433 for_each_iommu(iommu)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200434 iommu_poll_events(iommu);
435
436 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200437}
438
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200439irqreturn_t amd_iommu_int_handler(int irq, void *data)
440{
441 return IRQ_WAKE_THREAD;
442}
443
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200444/****************************************************************************
445 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200446 * IOMMU command queuing functions
447 *
448 ****************************************************************************/
449
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200450static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200451{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200452 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200453
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200454 while (*sem == 0 && i < LOOP_TIMEOUT) {
455 udelay(1);
456 i += 1;
457 }
458
459 if (i == LOOP_TIMEOUT) {
460 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
461 return -EIO;
462 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200463
464 return 0;
465}
466
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200467static void copy_cmd_to_buffer(struct amd_iommu *iommu,
468 struct iommu_cmd *cmd,
469 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200470{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200471 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200472
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200473 target = iommu->cmd_buf + tail;
474 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200475
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200476 /* Copy command to buffer */
477 memcpy(target, cmd, sizeof(*cmd));
478
479 /* Tell the IOMMU about it */
480 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
481}
482
Joerg Roedel815b33f2011-04-06 17:26:49 +0200483static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200484{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200485 WARN_ON(address & 0x7ULL);
486
Joerg Roedelded46732011-04-06 10:53:48 +0200487 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200488 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
489 cmd->data[1] = upper_32_bits(__pa(address));
490 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200491 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
492}
493
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200494static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
495{
496 memset(cmd, 0, sizeof(*cmd));
497 cmd->data[0] = devid;
498 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
499}
500
Joerg Roedel11b64022011-04-06 11:49:28 +0200501static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
502 size_t size, u16 domid, int pde)
503{
504 u64 pages;
505 int s;
506
507 pages = iommu_num_pages(address, size, PAGE_SIZE);
508 s = 0;
509
510 if (pages > 1) {
511 /*
512 * If we have to flush more than one page, flush all
513 * TLB entries for this domain
514 */
515 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
516 s = 1;
517 }
518
519 address &= PAGE_MASK;
520
521 memset(cmd, 0, sizeof(*cmd));
522 cmd->data[1] |= domid;
523 cmd->data[2] = lower_32_bits(address);
524 cmd->data[3] = upper_32_bits(address);
525 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
526 if (s) /* size bit - we flush more than one 4kb page */
527 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
528 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
529 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
530}
531
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200532static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
533 u64 address, size_t size)
534{
535 u64 pages;
536 int s;
537
538 pages = iommu_num_pages(address, size, PAGE_SIZE);
539 s = 0;
540
541 if (pages > 1) {
542 /*
543 * If we have to flush more than one page, flush all
544 * TLB entries for this domain
545 */
546 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
547 s = 1;
548 }
549
550 address &= PAGE_MASK;
551
552 memset(cmd, 0, sizeof(*cmd));
553 cmd->data[0] = devid;
554 cmd->data[0] |= (qdep & 0xff) << 24;
555 cmd->data[1] = devid;
556 cmd->data[2] = lower_32_bits(address);
557 cmd->data[3] = upper_32_bits(address);
558 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
559 if (s)
560 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
561}
562
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200563static void build_inv_all(struct iommu_cmd *cmd)
564{
565 memset(cmd, 0, sizeof(*cmd));
566 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200567}
568
Joerg Roedel431b2a22008-07-11 17:14:22 +0200569/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200570 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200571 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200572 */
Joerg Roedel815b33f2011-04-06 17:26:49 +0200573static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200574{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200575 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200576 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200577
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200578 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100579
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200580again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200581 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200582
583 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
584 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
585 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
586 left = (head - next_tail) % iommu->cmd_buf_size;
587
588 if (left <= 2) {
589 struct iommu_cmd sync_cmd;
590 volatile u64 sem = 0;
591 int ret;
592
593 build_completion_wait(&sync_cmd, (u64)&sem);
594 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
595
596 spin_unlock_irqrestore(&iommu->lock, flags);
597
598 if ((ret = wait_on_sem(&sem)) != 0)
599 return ret;
600
601 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200602 }
603
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200604 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200605
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200606 /* We need to sync now to make sure all commands are processed */
Joerg Roedel815b33f2011-04-06 17:26:49 +0200607 iommu->need_sync = true;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200608
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200609 spin_unlock_irqrestore(&iommu->lock, flags);
610
Joerg Roedel815b33f2011-04-06 17:26:49 +0200611 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100612}
613
614/*
615 * This function queues a completion wait command into the command
616 * buffer of an IOMMU
617 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100618static int iommu_completion_wait(struct amd_iommu *iommu)
619{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200620 struct iommu_cmd cmd;
621 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200622 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100623
624 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200625 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100626
Joerg Roedel815b33f2011-04-06 17:26:49 +0200627 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100628
Joerg Roedel815b33f2011-04-06 17:26:49 +0200629 ret = iommu_queue_command(iommu, &cmd);
Joerg Roedel8d201962008-12-02 20:34:41 +0100630 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200631 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100632
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200633 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200634}
635
Joerg Roedeld8c13082011-04-06 18:51:26 +0200636static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200637{
638 struct iommu_cmd cmd;
639
Joerg Roedeld8c13082011-04-06 18:51:26 +0200640 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200641
Joerg Roedeld8c13082011-04-06 18:51:26 +0200642 return iommu_queue_command(iommu, &cmd);
643}
644
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200645static void iommu_flush_dte_all(struct amd_iommu *iommu)
646{
647 u32 devid;
648
649 for (devid = 0; devid <= 0xffff; ++devid)
650 iommu_flush_dte(iommu, devid);
651
652 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200653}
654
655/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200656 * This function uses heavy locking and may disable irqs for some time. But
657 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200658 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200659static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200660{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200661 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200662
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200663 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
664 struct iommu_cmd cmd;
665 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
666 dom_id, 1);
667 iommu_queue_command(iommu, &cmd);
668 }
Joerg Roedel431b2a22008-07-11 17:14:22 +0200669
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200670 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200671}
672
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200673static void iommu_flush_all(struct amd_iommu *iommu)
674{
675 struct iommu_cmd cmd;
676
677 build_inv_all(&cmd);
678
679 iommu_queue_command(iommu, &cmd);
680 iommu_completion_wait(iommu);
681}
682
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200683void iommu_flush_all_caches(struct amd_iommu *iommu)
684{
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200685 if (iommu_feature(iommu, FEATURE_IA)) {
686 iommu_flush_all(iommu);
687 } else {
688 iommu_flush_dte_all(iommu);
689 iommu_flush_tlb_all(iommu);
690 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200691}
692
Joerg Roedel431b2a22008-07-11 17:14:22 +0200693/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200694 * Command send function for flushing on-device TLB
695 */
696static int device_flush_iotlb(struct device *dev, u64 address, size_t size)
697{
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200698 struct iommu_dev_data *dev_data;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200699 struct amd_iommu *iommu;
700 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200701 int qdep;
702
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200703 dev_data = get_dev_data(dev);
704 qdep = dev_data->ats.qdep;
705 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200706
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200707 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200708
709 return iommu_queue_command(iommu, &cmd);
710}
711
712/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200713 * Command send function for invalidating a device table entry
714 */
Joerg Roedeld8c13082011-04-06 18:51:26 +0200715static int device_flush_dte(struct device *dev)
Joerg Roedel3fa43652009-11-26 15:04:38 +0100716{
Joerg Roedelf62dda62011-06-09 12:55:35 +0200717 struct iommu_dev_data *dev_data;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100718 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200719 struct pci_dev *pdev;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200720 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100721
Joerg Roedelf62dda62011-06-09 12:55:35 +0200722 pdev = to_pci_dev(dev);
723 dev_data = get_dev_data(dev);
724 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +0100725
Joerg Roedelf62dda62011-06-09 12:55:35 +0200726 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200727 if (ret)
728 return ret;
729
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200730 if (dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200731 ret = device_flush_iotlb(dev, 0, ~0UL);
732
733 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100734}
735
Joerg Roedel431b2a22008-07-11 17:14:22 +0200736/*
737 * TLB invalidation function which is called from the mapping functions.
738 * It invalidates a single PTE if the range to flush is within a single
739 * page. Otherwise it flushes the whole TLB of the IOMMU.
740 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200741static void __domain_flush_pages(struct protection_domain *domain,
742 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200743{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200744 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +0200745 struct iommu_cmd cmd;
746 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200747
Joerg Roedel11b64022011-04-06 11:49:28 +0200748 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +0200749
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100750 for (i = 0; i < amd_iommus_present; ++i) {
751 if (!domain->dev_iommu[i])
752 continue;
753
754 /*
755 * Devices of this domain are behind this IOMMU
756 * We need a TLB flush
757 */
Joerg Roedel11b64022011-04-06 11:49:28 +0200758 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100759 }
760
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200761 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200762
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200763 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200764 continue;
765
766 ret |= device_flush_iotlb(dev_data->dev, address, size);
767 }
768
Joerg Roedel11b64022011-04-06 11:49:28 +0200769 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100770}
771
Joerg Roedel17b124b2011-04-06 18:01:35 +0200772static void domain_flush_pages(struct protection_domain *domain,
773 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100774{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200775 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200776}
Joerg Roedelb6c02712008-06-26 21:27:53 +0200777
Joerg Roedel1c655772008-09-04 18:40:05 +0200778/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200779static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +0200780{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200781 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +0200782}
783
Chris Wright42a49f92009-06-15 15:42:00 +0200784/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200785static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +0200786{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200787 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
788}
789
790static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +0200791{
792 int i;
793
794 for (i = 0; i < amd_iommus_present; ++i) {
795 if (!domain->dev_iommu[i])
796 continue;
797
798 /*
799 * Devices of this domain are behind this IOMMU
800 * We need to wait for completion of all commands.
801 */
802 iommu_completion_wait(amd_iommus[i]);
803 }
804}
805
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100806
Joerg Roedel43f49602008-12-02 21:01:12 +0100807/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100808 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +0100809 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200810static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200811{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100812 struct iommu_dev_data *dev_data;
813 unsigned long flags;
814
815 spin_lock_irqsave(&domain->lock, flags);
816
817 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedeld8c13082011-04-06 18:51:26 +0200818 device_flush_dte(dev_data->dev);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100819
820 spin_unlock_irqrestore(&domain->lock, flags);
821}
822
Joerg Roedel431b2a22008-07-11 17:14:22 +0200823/****************************************************************************
824 *
825 * The functions below are used the create the page table mappings for
826 * unity mapped regions.
827 *
828 ****************************************************************************/
829
830/*
Joerg Roedel308973d2009-11-24 17:43:32 +0100831 * This function is used to add another level to an IO page table. Adding
832 * another level increases the size of the address space by 9 bits to a size up
833 * to 64 bits.
834 */
835static bool increase_address_space(struct protection_domain *domain,
836 gfp_t gfp)
837{
838 u64 *pte;
839
840 if (domain->mode == PAGE_MODE_6_LEVEL)
841 /* address space already 64 bit large */
842 return false;
843
844 pte = (void *)get_zeroed_page(gfp);
845 if (!pte)
846 return false;
847
848 *pte = PM_LEVEL_PDE(domain->mode,
849 virt_to_phys(domain->pt_root));
850 domain->pt_root = pte;
851 domain->mode += 1;
852 domain->updated = true;
853
854 return true;
855}
856
857static u64 *alloc_pte(struct protection_domain *domain,
858 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100859 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +0100860 u64 **pte_page,
861 gfp_t gfp)
862{
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100863 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +0100864 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100865
866 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +0100867
868 while (address > PM_LEVEL_SIZE(domain->mode))
869 increase_address_space(domain, gfp);
870
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100871 level = domain->mode - 1;
872 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
873 address = PAGE_SIZE_ALIGN(address, page_size);
874 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +0100875
876 while (level > end_lvl) {
877 if (!IOMMU_PTE_PRESENT(*pte)) {
878 page = (u64 *)get_zeroed_page(gfp);
879 if (!page)
880 return NULL;
881 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
882 }
883
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100884 /* No level skipping support yet */
885 if (PM_PTE_LEVEL(*pte) != level)
886 return NULL;
887
Joerg Roedel308973d2009-11-24 17:43:32 +0100888 level -= 1;
889
890 pte = IOMMU_PTE_PAGE(*pte);
891
892 if (pte_page && level == end_lvl)
893 *pte_page = pte;
894
895 pte = &pte[PM_LEVEL_INDEX(level, address)];
896 }
897
898 return pte;
899}
900
901/*
902 * This function checks if there is a PTE for a given dma address. If
903 * there is one, it returns the pointer to it.
904 */
Joerg Roedel24cd7722010-01-19 17:27:39 +0100905static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +0100906{
907 int level;
908 u64 *pte;
909
Joerg Roedel24cd7722010-01-19 17:27:39 +0100910 if (address > PM_LEVEL_SIZE(domain->mode))
911 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +0100912
Joerg Roedel24cd7722010-01-19 17:27:39 +0100913 level = domain->mode - 1;
914 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
915
916 while (level > 0) {
917
918 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +0100919 if (!IOMMU_PTE_PRESENT(*pte))
920 return NULL;
921
Joerg Roedel24cd7722010-01-19 17:27:39 +0100922 /* Large PTE */
923 if (PM_PTE_LEVEL(*pte) == 0x07) {
924 unsigned long pte_mask, __pte;
925
926 /*
927 * If we have a series of large PTEs, make
928 * sure to return a pointer to the first one.
929 */
930 pte_mask = PTE_PAGE_SIZE(*pte);
931 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
932 __pte = ((unsigned long)pte) & pte_mask;
933
934 return (u64 *)__pte;
935 }
936
937 /* No level skipping support yet */
938 if (PM_PTE_LEVEL(*pte) != level)
939 return NULL;
940
Joerg Roedel308973d2009-11-24 17:43:32 +0100941 level -= 1;
942
Joerg Roedel24cd7722010-01-19 17:27:39 +0100943 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +0100944 pte = IOMMU_PTE_PAGE(*pte);
945 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +0100946 }
947
948 return pte;
949}
950
951/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200952 * Generic mapping functions. It maps a physical address into a DMA
953 * address space. It allocates the page table pages if necessary.
954 * In the future it can be extended to a generic mapping function
955 * supporting all features of AMD IOMMU page tables like level skipping
956 * and full 64 bit address spaces.
957 */
Joerg Roedel38e817f2008-12-02 17:27:52 +0100958static int iommu_map_page(struct protection_domain *dom,
959 unsigned long bus_addr,
960 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200961 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100962 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200963{
Joerg Roedel8bda3092009-05-12 12:02:46 +0200964 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100965 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200966
Joerg Roedelbad1cac2009-09-02 16:52:23 +0200967 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200968 return -EINVAL;
969
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100970 bus_addr = PAGE_ALIGN(bus_addr);
971 phys_addr = PAGE_ALIGN(phys_addr);
972 count = PAGE_SIZE_PTE_COUNT(page_size);
973 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200974
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100975 for (i = 0; i < count; ++i)
976 if (IOMMU_PTE_PRESENT(pte[i]))
977 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200978
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100979 if (page_size > PAGE_SIZE) {
980 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
981 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
982 } else
983 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
984
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200985 if (prot & IOMMU_PROT_IR)
986 __pte |= IOMMU_PTE_IR;
987 if (prot & IOMMU_PROT_IW)
988 __pte |= IOMMU_PTE_IW;
989
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100990 for (i = 0; i < count; ++i)
991 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200992
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200993 update_domain(dom);
994
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200995 return 0;
996}
997
Joerg Roedel24cd7722010-01-19 17:27:39 +0100998static unsigned long iommu_unmap_page(struct protection_domain *dom,
999 unsigned long bus_addr,
1000 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001001{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001002 unsigned long long unmap_size, unmapped;
1003 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001004
Joerg Roedel24cd7722010-01-19 17:27:39 +01001005 BUG_ON(!is_power_of_2(page_size));
1006
1007 unmapped = 0;
1008
1009 while (unmapped < page_size) {
1010
1011 pte = fetch_pte(dom, bus_addr);
1012
1013 if (!pte) {
1014 /*
1015 * No PTE for this address
1016 * move forward in 4kb steps
1017 */
1018 unmap_size = PAGE_SIZE;
1019 } else if (PM_PTE_LEVEL(*pte) == 0) {
1020 /* 4kb PTE found for this address */
1021 unmap_size = PAGE_SIZE;
1022 *pte = 0ULL;
1023 } else {
1024 int count, i;
1025
1026 /* Large PTE found which maps this address */
1027 unmap_size = PTE_PAGE_SIZE(*pte);
1028 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1029 for (i = 0; i < count; i++)
1030 pte[i] = 0ULL;
1031 }
1032
1033 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1034 unmapped += unmap_size;
1035 }
1036
1037 BUG_ON(!is_power_of_2(unmapped));
1038
1039 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001040}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001041
Joerg Roedel431b2a22008-07-11 17:14:22 +02001042/*
1043 * This function checks if a specific unity mapping entry is needed for
1044 * this specific IOMMU.
1045 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001046static int iommu_for_unity_map(struct amd_iommu *iommu,
1047 struct unity_map_entry *entry)
1048{
1049 u16 bdf, i;
1050
1051 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1052 bdf = amd_iommu_alias_table[i];
1053 if (amd_iommu_rlookup_table[bdf] == iommu)
1054 return 1;
1055 }
1056
1057 return 0;
1058}
1059
Joerg Roedel431b2a22008-07-11 17:14:22 +02001060/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001061 * This function actually applies the mapping to the page table of the
1062 * dma_ops domain.
1063 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001064static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1065 struct unity_map_entry *e)
1066{
1067 u64 addr;
1068 int ret;
1069
1070 for (addr = e->address_start; addr < e->address_end;
1071 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001072 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001073 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001074 if (ret)
1075 return ret;
1076 /*
1077 * if unity mapping is in aperture range mark the page
1078 * as allocated in the aperture
1079 */
1080 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001081 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001082 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001083 }
1084
1085 return 0;
1086}
1087
Joerg Roedel431b2a22008-07-11 17:14:22 +02001088/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001089 * Init the unity mappings for a specific IOMMU in the system
1090 *
1091 * Basically iterates over all unity mapping entries and applies them to
1092 * the default domain DMA of that IOMMU if necessary.
1093 */
1094static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1095{
1096 struct unity_map_entry *entry;
1097 int ret;
1098
1099 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1100 if (!iommu_for_unity_map(iommu, entry))
1101 continue;
1102 ret = dma_ops_unity_map(iommu->default_dom, entry);
1103 if (ret)
1104 return ret;
1105 }
1106
1107 return 0;
1108}
1109
1110/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001111 * Inits the unity mappings required for a specific device
1112 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001113static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1114 u16 devid)
1115{
1116 struct unity_map_entry *e;
1117 int ret;
1118
1119 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1120 if (!(devid >= e->devid_start && devid <= e->devid_end))
1121 continue;
1122 ret = dma_ops_unity_map(dma_dom, e);
1123 if (ret)
1124 return ret;
1125 }
1126
1127 return 0;
1128}
1129
Joerg Roedel431b2a22008-07-11 17:14:22 +02001130/****************************************************************************
1131 *
1132 * The next functions belong to the address allocator for the dma_ops
1133 * interface functions. They work like the allocators in the other IOMMU
1134 * drivers. Its basically a bitmap which marks the allocated pages in
1135 * the aperture. Maybe it could be enhanced in the future to a more
1136 * efficient allocator.
1137 *
1138 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001139
Joerg Roedel431b2a22008-07-11 17:14:22 +02001140/*
Joerg Roedel384de722009-05-15 12:30:05 +02001141 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001142 *
1143 * called with domain->lock held
1144 */
Joerg Roedel384de722009-05-15 12:30:05 +02001145
Joerg Roedel9cabe892009-05-18 16:38:55 +02001146/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001147 * Used to reserve address ranges in the aperture (e.g. for exclusion
1148 * ranges.
1149 */
1150static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1151 unsigned long start_page,
1152 unsigned int pages)
1153{
1154 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1155
1156 if (start_page + pages > last_page)
1157 pages = last_page - start_page;
1158
1159 for (i = start_page; i < start_page + pages; ++i) {
1160 int index = i / APERTURE_RANGE_PAGES;
1161 int page = i % APERTURE_RANGE_PAGES;
1162 __set_bit(page, dom->aperture[index]->bitmap);
1163 }
1164}
1165
1166/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001167 * This function is used to add a new aperture range to an existing
1168 * aperture in case of dma_ops domain allocation or address allocation
1169 * failure.
1170 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001171static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001172 bool populate, gfp_t gfp)
1173{
1174 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001175 struct amd_iommu *iommu;
Joerg Roedeld91afd12010-01-22 16:40:20 +01001176 unsigned long i;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001177
Joerg Roedelf5e97052009-05-22 12:31:53 +02001178#ifdef CONFIG_IOMMU_STRESS
1179 populate = false;
1180#endif
1181
Joerg Roedel9cabe892009-05-18 16:38:55 +02001182 if (index >= APERTURE_MAX_RANGES)
1183 return -ENOMEM;
1184
1185 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1186 if (!dma_dom->aperture[index])
1187 return -ENOMEM;
1188
1189 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1190 if (!dma_dom->aperture[index]->bitmap)
1191 goto out_free;
1192
1193 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1194
1195 if (populate) {
1196 unsigned long address = dma_dom->aperture_size;
1197 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1198 u64 *pte, *pte_page;
1199
1200 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001201 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001202 &pte_page, gfp);
1203 if (!pte)
1204 goto out_free;
1205
1206 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1207
1208 address += APERTURE_RANGE_SIZE / 64;
1209 }
1210 }
1211
1212 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1213
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001214 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001215 for_each_iommu(iommu) {
1216 if (iommu->exclusion_start &&
1217 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1218 && iommu->exclusion_start < dma_dom->aperture_size) {
1219 unsigned long startpage;
1220 int pages = iommu_num_pages(iommu->exclusion_start,
1221 iommu->exclusion_length,
1222 PAGE_SIZE);
1223 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1224 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1225 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001226 }
1227
1228 /*
1229 * Check for areas already mapped as present in the new aperture
1230 * range and mark those pages as reserved in the allocator. Such
1231 * mappings may already exist as a result of requested unity
1232 * mappings for devices.
1233 */
1234 for (i = dma_dom->aperture[index]->offset;
1235 i < dma_dom->aperture_size;
1236 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001237 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001238 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1239 continue;
1240
1241 dma_ops_reserve_addresses(dma_dom, i << PAGE_SHIFT, 1);
1242 }
1243
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001244 update_domain(&dma_dom->domain);
1245
Joerg Roedel9cabe892009-05-18 16:38:55 +02001246 return 0;
1247
1248out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001249 update_domain(&dma_dom->domain);
1250
Joerg Roedel9cabe892009-05-18 16:38:55 +02001251 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1252
1253 kfree(dma_dom->aperture[index]);
1254 dma_dom->aperture[index] = NULL;
1255
1256 return -ENOMEM;
1257}
1258
Joerg Roedel384de722009-05-15 12:30:05 +02001259static unsigned long dma_ops_area_alloc(struct device *dev,
1260 struct dma_ops_domain *dom,
1261 unsigned int pages,
1262 unsigned long align_mask,
1263 u64 dma_mask,
1264 unsigned long start)
1265{
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001266 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001267 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1268 int i = start >> APERTURE_RANGE_SHIFT;
1269 unsigned long boundary_size;
1270 unsigned long address = -1;
1271 unsigned long limit;
1272
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001273 next_bit >>= PAGE_SHIFT;
1274
Joerg Roedel384de722009-05-15 12:30:05 +02001275 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1276 PAGE_SIZE) >> PAGE_SHIFT;
1277
1278 for (;i < max_index; ++i) {
1279 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1280
1281 if (dom->aperture[i]->offset >= dma_mask)
1282 break;
1283
1284 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1285 dma_mask >> PAGE_SHIFT);
1286
1287 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1288 limit, next_bit, pages, 0,
1289 boundary_size, align_mask);
1290 if (address != -1) {
1291 address = dom->aperture[i]->offset +
1292 (address << PAGE_SHIFT);
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001293 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001294 break;
1295 }
1296
1297 next_bit = 0;
1298 }
1299
1300 return address;
1301}
1302
Joerg Roedeld3086442008-06-26 21:27:57 +02001303static unsigned long dma_ops_alloc_addresses(struct device *dev,
1304 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001305 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001306 unsigned long align_mask,
1307 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001308{
Joerg Roedeld3086442008-06-26 21:27:57 +02001309 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001310
Joerg Roedelfe16f082009-05-22 12:27:53 +02001311#ifdef CONFIG_IOMMU_STRESS
1312 dom->next_address = 0;
1313 dom->need_flush = true;
1314#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001315
Joerg Roedel384de722009-05-15 12:30:05 +02001316 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001317 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001318
Joerg Roedel1c655772008-09-04 18:40:05 +02001319 if (address == -1) {
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001320 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001321 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1322 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001323 dom->need_flush = true;
1324 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001325
Joerg Roedel384de722009-05-15 12:30:05 +02001326 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001327 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001328
1329 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1330
1331 return address;
1332}
1333
Joerg Roedel431b2a22008-07-11 17:14:22 +02001334/*
1335 * The address free function.
1336 *
1337 * called with domain->lock held
1338 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001339static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1340 unsigned long address,
1341 unsigned int pages)
1342{
Joerg Roedel384de722009-05-15 12:30:05 +02001343 unsigned i = address >> APERTURE_RANGE_SHIFT;
1344 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001345
Joerg Roedel384de722009-05-15 12:30:05 +02001346 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1347
Joerg Roedel47bccd62009-05-22 12:40:54 +02001348#ifdef CONFIG_IOMMU_STRESS
1349 if (i < 4)
1350 return;
1351#endif
1352
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001353 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001354 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001355
1356 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001357
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001358 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001359
Joerg Roedeld3086442008-06-26 21:27:57 +02001360}
1361
Joerg Roedel431b2a22008-07-11 17:14:22 +02001362/****************************************************************************
1363 *
1364 * The next functions belong to the domain allocation. A domain is
1365 * allocated for every IOMMU as the default domain. If device isolation
1366 * is enabled, every device get its own domain. The most important thing
1367 * about domains is the page table mapping the DMA address space they
1368 * contain.
1369 *
1370 ****************************************************************************/
1371
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001372/*
1373 * This function adds a protection domain to the global protection domain list
1374 */
1375static void add_domain_to_list(struct protection_domain *domain)
1376{
1377 unsigned long flags;
1378
1379 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1380 list_add(&domain->list, &amd_iommu_pd_list);
1381 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1382}
1383
1384/*
1385 * This function removes a protection domain to the global
1386 * protection domain list
1387 */
1388static void del_domain_from_list(struct protection_domain *domain)
1389{
1390 unsigned long flags;
1391
1392 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1393 list_del(&domain->list);
1394 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1395}
1396
Joerg Roedelec487d12008-06-26 21:27:58 +02001397static u16 domain_id_alloc(void)
1398{
1399 unsigned long flags;
1400 int id;
1401
1402 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1403 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1404 BUG_ON(id == 0);
1405 if (id > 0 && id < MAX_DOMAIN_ID)
1406 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1407 else
1408 id = 0;
1409 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1410
1411 return id;
1412}
1413
Joerg Roedela2acfb72008-12-02 18:28:53 +01001414static void domain_id_free(int id)
1415{
1416 unsigned long flags;
1417
1418 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1419 if (id > 0 && id < MAX_DOMAIN_ID)
1420 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1421 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1422}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001423
Joerg Roedel86db2e52008-12-02 18:20:21 +01001424static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001425{
1426 int i, j;
1427 u64 *p1, *p2, *p3;
1428
Joerg Roedel86db2e52008-12-02 18:20:21 +01001429 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001430
1431 if (!p1)
1432 return;
1433
1434 for (i = 0; i < 512; ++i) {
1435 if (!IOMMU_PTE_PRESENT(p1[i]))
1436 continue;
1437
1438 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001439 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001440 if (!IOMMU_PTE_PRESENT(p2[j]))
1441 continue;
1442 p3 = IOMMU_PTE_PAGE(p2[j]);
1443 free_page((unsigned long)p3);
1444 }
1445
1446 free_page((unsigned long)p2);
1447 }
1448
1449 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001450
1451 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001452}
1453
Joerg Roedel431b2a22008-07-11 17:14:22 +02001454/*
1455 * Free a domain, only used if something went wrong in the
1456 * allocation path and we need to free an already allocated page table
1457 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001458static void dma_ops_domain_free(struct dma_ops_domain *dom)
1459{
Joerg Roedel384de722009-05-15 12:30:05 +02001460 int i;
1461
Joerg Roedelec487d12008-06-26 21:27:58 +02001462 if (!dom)
1463 return;
1464
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001465 del_domain_from_list(&dom->domain);
1466
Joerg Roedel86db2e52008-12-02 18:20:21 +01001467 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001468
Joerg Roedel384de722009-05-15 12:30:05 +02001469 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1470 if (!dom->aperture[i])
1471 continue;
1472 free_page((unsigned long)dom->aperture[i]->bitmap);
1473 kfree(dom->aperture[i]);
1474 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001475
1476 kfree(dom);
1477}
1478
Joerg Roedel431b2a22008-07-11 17:14:22 +02001479/*
1480 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001481 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001482 * structures required for the dma_ops interface
1483 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001484static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001485{
1486 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001487
1488 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1489 if (!dma_dom)
1490 return NULL;
1491
1492 spin_lock_init(&dma_dom->domain.lock);
1493
1494 dma_dom->domain.id = domain_id_alloc();
1495 if (dma_dom->domain.id == 0)
1496 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001497 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001498 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001499 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001500 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001501 dma_dom->domain.priv = dma_dom;
1502 if (!dma_dom->domain.pt_root)
1503 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001504
Joerg Roedel1c655772008-09-04 18:40:05 +02001505 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001506 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001507
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001508 add_domain_to_list(&dma_dom->domain);
1509
Joerg Roedel576175c2009-11-23 19:08:46 +01001510 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001511 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001512
Joerg Roedel431b2a22008-07-11 17:14:22 +02001513 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001514 * mark the first page as allocated so we never return 0 as
1515 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001516 */
Joerg Roedel384de722009-05-15 12:30:05 +02001517 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001518 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001519
Joerg Roedelec487d12008-06-26 21:27:58 +02001520
1521 return dma_dom;
1522
1523free_dma_dom:
1524 dma_ops_domain_free(dma_dom);
1525
1526 return NULL;
1527}
1528
Joerg Roedel431b2a22008-07-11 17:14:22 +02001529/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001530 * little helper function to check whether a given protection domain is a
1531 * dma_ops domain
1532 */
1533static bool dma_ops_domain(struct protection_domain *domain)
1534{
1535 return domain->flags & PD_DMA_OPS_MASK;
1536}
1537
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001538static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001539{
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001540 u64 pte_root = virt_to_phys(domain->pt_root);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001541 u32 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001542
Joerg Roedel38ddf412008-09-11 10:38:32 +02001543 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1544 << DEV_ENTRY_MODE_SHIFT;
1545 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001546
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001547 if (ats)
1548 flags |= DTE_FLAG_IOTLB;
1549
1550 amd_iommu_dev_table[devid].data[3] |= flags;
1551 amd_iommu_dev_table[devid].data[2] = domain->id;
1552 amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
1553 amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001554}
1555
Joerg Roedel15898bb2009-11-24 15:39:42 +01001556static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001557{
Joerg Roedel355bf552008-12-08 12:02:41 +01001558 /* remove entry from the device table seen by the hardware */
1559 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1560 amd_iommu_dev_table[devid].data[1] = 0;
1561 amd_iommu_dev_table[devid].data[2] = 0;
1562
Joerg Roedelc5cca142009-10-09 18:31:20 +02001563 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001564}
1565
1566static void do_attach(struct device *dev, struct protection_domain *domain)
1567{
1568 struct iommu_dev_data *dev_data;
1569 struct amd_iommu *iommu;
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001570 struct pci_dev *pdev;
1571 bool ats = false;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001572
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001573 dev_data = get_dev_data(dev);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001574 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001575 pdev = to_pci_dev(dev);
1576
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001577 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001578
1579 /* Update data structures */
1580 dev_data->domain = domain;
1581 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001582 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001583
1584 /* Do reference counting */
1585 domain->dev_iommu[iommu->index] += 1;
1586 domain->dev_cnt += 1;
1587
1588 /* Flush the DTE entry */
Joerg Roedeld8c13082011-04-06 18:51:26 +02001589 device_flush_dte(dev);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001590}
1591
1592static void do_detach(struct device *dev)
1593{
1594 struct iommu_dev_data *dev_data;
1595 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001596
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001597 dev_data = get_dev_data(dev);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001598 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02001599
Joerg Roedelc4596112009-11-20 14:57:32 +01001600 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001601 dev_data->domain->dev_iommu[iommu->index] -= 1;
1602 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001603
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001604 /* Update data structures */
1605 dev_data->domain = NULL;
1606 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001607 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001608
1609 /* Flush the DTE entry */
Joerg Roedeld8c13082011-04-06 18:51:26 +02001610 device_flush_dte(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001611}
1612
1613/*
1614 * If a device is not yet associated with a domain, this function does
1615 * assigns it visible for the hardware
1616 */
1617static int __attach_device(struct device *dev,
1618 struct protection_domain *domain)
1619{
Joerg Roedel657cbb62009-11-23 15:26:46 +01001620 struct iommu_dev_data *dev_data, *alias_data;
Julia Lawall84fe6c12010-05-27 12:31:51 +02001621 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001622
Joerg Roedel657cbb62009-11-23 15:26:46 +01001623 dev_data = get_dev_data(dev);
1624 alias_data = get_dev_data(dev_data->alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001625
Joerg Roedel657cbb62009-11-23 15:26:46 +01001626 if (!alias_data)
1627 return -EINVAL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001628
1629 /* lock domain */
1630 spin_lock(&domain->lock);
1631
1632 /* Some sanity checks */
Julia Lawall84fe6c12010-05-27 12:31:51 +02001633 ret = -EBUSY;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001634 if (alias_data->domain != NULL &&
1635 alias_data->domain != domain)
Julia Lawall84fe6c12010-05-27 12:31:51 +02001636 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001637
Joerg Roedel657cbb62009-11-23 15:26:46 +01001638 if (dev_data->domain != NULL &&
1639 dev_data->domain != domain)
Julia Lawall84fe6c12010-05-27 12:31:51 +02001640 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001641
1642 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001643 if (dev_data->alias != dev) {
1644 alias_data = get_dev_data(dev_data->alias);
1645 if (alias_data->domain == NULL)
1646 do_attach(dev_data->alias, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01001647
1648 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001649 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01001650
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001651 if (dev_data->domain == NULL)
1652 do_attach(dev, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001653
Joerg Roedel24100052009-11-25 15:59:57 +01001654 atomic_inc(&dev_data->bind);
1655
Julia Lawall84fe6c12010-05-27 12:31:51 +02001656 ret = 0;
1657
1658out_unlock:
1659
Joerg Roedel355bf552008-12-08 12:02:41 +01001660 /* ready */
1661 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001662
Julia Lawall84fe6c12010-05-27 12:31:51 +02001663 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001664}
1665
1666/*
1667 * If a device is not yet associated with a domain, this function does
1668 * assigns it visible for the hardware
1669 */
1670static int attach_device(struct device *dev,
1671 struct protection_domain *domain)
1672{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001673 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001674 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001675 unsigned long flags;
1676 int ret;
1677
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001678 dev_data = get_dev_data(dev);
1679
1680 if (amd_iommu_iotlb_sup && pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
1681 dev_data->ats.enabled = true;
1682 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1683 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001684
Joerg Roedel15898bb2009-11-24 15:39:42 +01001685 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1686 ret = __attach_device(dev, domain);
1687 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1688
1689 /*
1690 * We might boot into a crash-kernel here. The crashed kernel
1691 * left the caches in the IOMMU dirty. So we have to flush
1692 * here to evict all dirty stuff.
1693 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001694 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001695
1696 return ret;
1697}
1698
1699/*
1700 * Removes a device from a protection domain (unlocked)
1701 */
1702static void __detach_device(struct device *dev)
1703{
Joerg Roedel657cbb62009-11-23 15:26:46 +01001704 struct iommu_dev_data *dev_data = get_dev_data(dev);
Joerg Roedel24100052009-11-25 15:59:57 +01001705 struct iommu_dev_data *alias_data;
Joerg Roedel2ca76272010-01-22 16:45:31 +01001706 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001707 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001708
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001709 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001710
Joerg Roedel2ca76272010-01-22 16:45:31 +01001711 domain = dev_data->domain;
1712
1713 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01001714
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001715 if (dev_data->alias != dev) {
Joerg Roedel24100052009-11-25 15:59:57 +01001716 alias_data = get_dev_data(dev_data->alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001717 if (atomic_dec_and_test(&alias_data->bind))
1718 do_detach(dev_data->alias);
Joerg Roedel24100052009-11-25 15:59:57 +01001719 }
1720
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001721 if (atomic_dec_and_test(&dev_data->bind))
1722 do_detach(dev);
1723
Joerg Roedel2ca76272010-01-22 16:45:31 +01001724 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001725
Joerg Roedel21129f72009-09-01 11:59:42 +02001726 /*
1727 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001728 * passthrough domain if it is detached from any other domain.
1729 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02001730 */
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001731 if (iommu_pass_through &&
1732 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedel15898bb2009-11-24 15:39:42 +01001733 __attach_device(dev, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01001734}
1735
1736/*
1737 * Removes a device from a protection domain (with devtable_lock held)
1738 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01001739static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01001740{
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001741 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01001742 unsigned long flags;
1743
1744 /* lock device table */
1745 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001746 __detach_device(dev);
Joerg Roedel355bf552008-12-08 12:02:41 +01001747 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001748
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001749 dev_data = get_dev_data(dev);
1750
1751 if (dev_data->ats.enabled) {
1752 pci_disable_ats(to_pci_dev(dev));
1753 dev_data->ats.enabled = false;
1754 }
Joerg Roedel355bf552008-12-08 12:02:41 +01001755}
Joerg Roedele275a2a2008-12-10 18:27:25 +01001756
Joerg Roedel15898bb2009-11-24 15:39:42 +01001757/*
1758 * Find out the protection domain structure for a given PCI device. This
1759 * will give us the pointer to the page table root for example.
1760 */
1761static struct protection_domain *domain_for_device(struct device *dev)
1762{
1763 struct protection_domain *dom;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001764 struct iommu_dev_data *dev_data, *alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001765 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001766
Joerg Roedel657cbb62009-11-23 15:26:46 +01001767 dev_data = get_dev_data(dev);
1768 alias_data = get_dev_data(dev_data->alias);
1769 if (!alias_data)
1770 return NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001771
1772 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001773 dom = dev_data->domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001774 if (dom == NULL &&
Joerg Roedel657cbb62009-11-23 15:26:46 +01001775 alias_data->domain != NULL) {
1776 __attach_device(dev, alias_data->domain);
1777 dom = alias_data->domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001778 }
1779
1780 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1781
1782 return dom;
1783}
1784
Joerg Roedele275a2a2008-12-10 18:27:25 +01001785static int device_change_notifier(struct notifier_block *nb,
1786 unsigned long action, void *data)
1787{
1788 struct device *dev = data;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001789 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001790 struct protection_domain *domain;
1791 struct dma_ops_domain *dma_domain;
1792 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001793 unsigned long flags;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001794
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001795 if (!check_device(dev))
1796 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001797
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001798 devid = get_device_id(dev);
1799 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01001800
1801 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07001802 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001803
1804 domain = domain_for_device(dev);
1805
Joerg Roedele275a2a2008-12-10 18:27:25 +01001806 if (!domain)
1807 goto out;
Joerg Roedela1ca3312009-09-01 12:22:22 +02001808 if (iommu_pass_through)
1809 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001810 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001811 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001812 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001813
1814 iommu_init_device(dev);
1815
1816 domain = domain_for_device(dev);
1817
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001818 /* allocate a protection domain if a device is added */
1819 dma_domain = find_protection_domain(devid);
1820 if (dma_domain)
1821 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01001822 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001823 if (!dma_domain)
1824 goto out;
1825 dma_domain->target_dev = devid;
1826
1827 spin_lock_irqsave(&iommu_pd_list_lock, flags);
1828 list_add_tail(&dma_domain->list, &iommu_pd_list);
1829 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
1830
1831 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001832 case BUS_NOTIFY_DEL_DEVICE:
1833
1834 iommu_uninit_device(dev);
1835
Joerg Roedele275a2a2008-12-10 18:27:25 +01001836 default:
1837 goto out;
1838 }
1839
Joerg Roedele275a2a2008-12-10 18:27:25 +01001840 iommu_completion_wait(iommu);
1841
1842out:
1843 return 0;
1844}
1845
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05301846static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01001847 .notifier_call = device_change_notifier,
1848};
Joerg Roedel355bf552008-12-08 12:02:41 +01001849
Joerg Roedel8638c492009-12-10 11:12:25 +01001850void amd_iommu_init_notifier(void)
1851{
1852 bus_register_notifier(&pci_bus_type, &device_nb);
1853}
1854
Joerg Roedel431b2a22008-07-11 17:14:22 +02001855/*****************************************************************************
1856 *
1857 * The next functions belong to the dma_ops mapping/unmapping code.
1858 *
1859 *****************************************************************************/
1860
1861/*
1862 * In the dma_ops path we only have the struct device. This function
1863 * finds the corresponding IOMMU, the protection domain and the
1864 * requestor id for a given device.
1865 * If the device is not yet associated with a domain this is also done
1866 * in this function.
1867 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001868static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001869{
Joerg Roedel94f6d192009-11-24 16:40:02 +01001870 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001871 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01001872 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001873
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001874 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01001875 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001876
Joerg Roedel94f6d192009-11-24 16:40:02 +01001877 domain = domain_for_device(dev);
1878 if (domain != NULL && !dma_ops_domain(domain))
1879 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001880
Joerg Roedel94f6d192009-11-24 16:40:02 +01001881 if (domain != NULL)
1882 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001883
Joerg Roedel15898bb2009-11-24 15:39:42 +01001884 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001885 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001886 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01001887 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
1888 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001889 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01001890 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01001891
Joerg Roedel94f6d192009-11-24 16:40:02 +01001892 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001893}
1894
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001895static void update_device_table(struct protection_domain *domain)
1896{
Joerg Roedel492667d2009-11-27 13:25:47 +01001897 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001898
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001899 list_for_each_entry(dev_data, &domain->dev_list, list)
1900 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001901}
1902
1903static void update_domain(struct protection_domain *domain)
1904{
1905 if (!domain->updated)
1906 return;
1907
1908 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02001909
1910 domain_flush_devices(domain);
1911 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001912
1913 domain->updated = false;
1914}
1915
Joerg Roedel431b2a22008-07-11 17:14:22 +02001916/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02001917 * This function fetches the PTE for a given address in the aperture
1918 */
1919static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
1920 unsigned long address)
1921{
Joerg Roedel384de722009-05-15 12:30:05 +02001922 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02001923 u64 *pte, *pte_page;
1924
Joerg Roedel384de722009-05-15 12:30:05 +02001925 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1926 if (!aperture)
1927 return NULL;
1928
1929 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02001930 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001931 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001932 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02001933 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
1934 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001935 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001936
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001937 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001938
1939 return pte;
1940}
1941
1942/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001943 * This is the generic map function. It maps one 4kb page at paddr to
1944 * the given address in the DMA address space for the domain.
1945 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001946static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001947 unsigned long address,
1948 phys_addr_t paddr,
1949 int direction)
1950{
1951 u64 *pte, __pte;
1952
1953 WARN_ON(address > dom->aperture_size);
1954
1955 paddr &= PAGE_MASK;
1956
Joerg Roedel8bda3092009-05-12 12:02:46 +02001957 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02001958 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001959 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001960
1961 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
1962
1963 if (direction == DMA_TO_DEVICE)
1964 __pte |= IOMMU_PTE_IR;
1965 else if (direction == DMA_FROM_DEVICE)
1966 __pte |= IOMMU_PTE_IW;
1967 else if (direction == DMA_BIDIRECTIONAL)
1968 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
1969
1970 WARN_ON(*pte);
1971
1972 *pte = __pte;
1973
1974 return (dma_addr_t)address;
1975}
1976
Joerg Roedel431b2a22008-07-11 17:14:22 +02001977/*
1978 * The generic unmapping function for on page in the DMA address space.
1979 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001980static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001981 unsigned long address)
1982{
Joerg Roedel384de722009-05-15 12:30:05 +02001983 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001984 u64 *pte;
1985
1986 if (address >= dom->aperture_size)
1987 return;
1988
Joerg Roedel384de722009-05-15 12:30:05 +02001989 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1990 if (!aperture)
1991 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001992
Joerg Roedel384de722009-05-15 12:30:05 +02001993 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
1994 if (!pte)
1995 return;
1996
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001997 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001998
1999 WARN_ON(!*pte);
2000
2001 *pte = 0ULL;
2002}
2003
Joerg Roedel431b2a22008-07-11 17:14:22 +02002004/*
2005 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002006 * contiguous memory region into DMA address space. It is used by all
2007 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002008 * Must be called with the domain lock held.
2009 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002010static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002011 struct dma_ops_domain *dma_dom,
2012 phys_addr_t paddr,
2013 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002014 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002015 bool align,
2016 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002017{
2018 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002019 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002020 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002021 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002022 int i;
2023
Joerg Roedele3c449f2008-10-15 22:02:11 -07002024 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002025 paddr &= PAGE_MASK;
2026
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002027 INC_STATS_COUNTER(total_map_requests);
2028
Joerg Roedelc1858972008-12-12 15:42:39 +01002029 if (pages > 1)
2030 INC_STATS_COUNTER(cross_page);
2031
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002032 if (align)
2033 align_mask = (1UL << get_order(size)) - 1;
2034
Joerg Roedel11b83882009-05-19 10:23:15 +02002035retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002036 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2037 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002038 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002039 /*
2040 * setting next_address here will let the address
2041 * allocator only scan the new allocated range in the
2042 * first run. This is a small optimization.
2043 */
2044 dma_dom->next_address = dma_dom->aperture_size;
2045
Joerg Roedel576175c2009-11-23 19:08:46 +01002046 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002047 goto out;
2048
2049 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002050 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002051 * allocation again
2052 */
2053 goto retry;
2054 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002055
2056 start = address;
2057 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002058 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002059 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002060 goto out_unmap;
2061
Joerg Roedelcb76c322008-06-26 21:28:00 +02002062 paddr += PAGE_SIZE;
2063 start += PAGE_SIZE;
2064 }
2065 address += offset;
2066
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002067 ADD_STATS_COUNTER(alloced_io_mem, size);
2068
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002069 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002070 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002071 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002072 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002073 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002074
Joerg Roedelcb76c322008-06-26 21:28:00 +02002075out:
2076 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002077
2078out_unmap:
2079
2080 for (--i; i >= 0; --i) {
2081 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002082 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002083 }
2084
2085 dma_ops_free_addresses(dma_dom, address, pages);
2086
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002087 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002088}
2089
Joerg Roedel431b2a22008-07-11 17:14:22 +02002090/*
2091 * Does the reverse of the __map_single function. Must be called with
2092 * the domain lock held too
2093 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002094static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002095 dma_addr_t dma_addr,
2096 size_t size,
2097 int dir)
2098{
Joerg Roedel04e04632010-09-23 16:12:48 +02002099 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002100 dma_addr_t i, start;
2101 unsigned int pages;
2102
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002103 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002104 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002105 return;
2106
Joerg Roedel04e04632010-09-23 16:12:48 +02002107 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002108 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002109 dma_addr &= PAGE_MASK;
2110 start = dma_addr;
2111
2112 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002113 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002114 start += PAGE_SIZE;
2115 }
2116
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002117 SUB_STATS_COUNTER(alloced_io_mem, size);
2118
Joerg Roedelcb76c322008-06-26 21:28:00 +02002119 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002120
Joerg Roedel80be3082008-11-06 14:59:05 +01002121 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002122 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002123 dma_dom->need_flush = false;
2124 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002125}
2126
Joerg Roedel431b2a22008-07-11 17:14:22 +02002127/*
2128 * The exported map_single function for dma_ops.
2129 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002130static dma_addr_t map_page(struct device *dev, struct page *page,
2131 unsigned long offset, size_t size,
2132 enum dma_data_direction dir,
2133 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002134{
2135 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002136 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002137 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002138 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002139 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002140
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002141 INC_STATS_COUNTER(cnt_map_single);
2142
Joerg Roedel94f6d192009-11-24 16:40:02 +01002143 domain = get_domain(dev);
2144 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002145 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002146 else if (IS_ERR(domain))
2147 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002148
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002149 dma_mask = *dev->dma_mask;
2150
Joerg Roedel4da70b92008-06-26 21:28:01 +02002151 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002152
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002153 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002154 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002155 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002156 goto out;
2157
Joerg Roedel17b124b2011-04-06 18:01:35 +02002158 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002159
2160out:
2161 spin_unlock_irqrestore(&domain->lock, flags);
2162
2163 return addr;
2164}
2165
Joerg Roedel431b2a22008-07-11 17:14:22 +02002166/*
2167 * The exported unmap_single function for dma_ops.
2168 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002169static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2170 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002171{
2172 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002173 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002174
Joerg Roedel146a6912008-12-12 15:07:12 +01002175 INC_STATS_COUNTER(cnt_unmap_single);
2176
Joerg Roedel94f6d192009-11-24 16:40:02 +01002177 domain = get_domain(dev);
2178 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002179 return;
2180
Joerg Roedel4da70b92008-06-26 21:28:01 +02002181 spin_lock_irqsave(&domain->lock, flags);
2182
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002183 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002184
Joerg Roedel17b124b2011-04-06 18:01:35 +02002185 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002186
2187 spin_unlock_irqrestore(&domain->lock, flags);
2188}
2189
Joerg Roedel431b2a22008-07-11 17:14:22 +02002190/*
2191 * This is a special map_sg function which is used if we should map a
2192 * device which is not handled by an AMD IOMMU in the system.
2193 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002194static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2195 int nelems, int dir)
2196{
2197 struct scatterlist *s;
2198 int i;
2199
2200 for_each_sg(sglist, s, nelems, i) {
2201 s->dma_address = (dma_addr_t)sg_phys(s);
2202 s->dma_length = s->length;
2203 }
2204
2205 return nelems;
2206}
2207
Joerg Roedel431b2a22008-07-11 17:14:22 +02002208/*
2209 * The exported map_sg function for dma_ops (handles scatter-gather
2210 * lists).
2211 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002212static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002213 int nelems, enum dma_data_direction dir,
2214 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002215{
2216 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002217 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002218 int i;
2219 struct scatterlist *s;
2220 phys_addr_t paddr;
2221 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002222 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002223
Joerg Roedeld03f0672008-12-12 15:09:48 +01002224 INC_STATS_COUNTER(cnt_map_sg);
2225
Joerg Roedel94f6d192009-11-24 16:40:02 +01002226 domain = get_domain(dev);
2227 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002228 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002229 else if (IS_ERR(domain))
2230 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002231
Joerg Roedel832a90c2008-09-18 15:54:23 +02002232 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002233
Joerg Roedel65b050a2008-06-26 21:28:02 +02002234 spin_lock_irqsave(&domain->lock, flags);
2235
2236 for_each_sg(sglist, s, nelems, i) {
2237 paddr = sg_phys(s);
2238
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002239 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002240 paddr, s->length, dir, false,
2241 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002242
2243 if (s->dma_address) {
2244 s->dma_length = s->length;
2245 mapped_elems++;
2246 } else
2247 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002248 }
2249
Joerg Roedel17b124b2011-04-06 18:01:35 +02002250 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002251
2252out:
2253 spin_unlock_irqrestore(&domain->lock, flags);
2254
2255 return mapped_elems;
2256unmap:
2257 for_each_sg(sglist, s, mapped_elems, i) {
2258 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002259 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002260 s->dma_length, dir);
2261 s->dma_address = s->dma_length = 0;
2262 }
2263
2264 mapped_elems = 0;
2265
2266 goto out;
2267}
2268
Joerg Roedel431b2a22008-07-11 17:14:22 +02002269/*
2270 * The exported map_sg function for dma_ops (handles scatter-gather
2271 * lists).
2272 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002273static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002274 int nelems, enum dma_data_direction dir,
2275 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002276{
2277 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002278 struct protection_domain *domain;
2279 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002280 int i;
2281
Joerg Roedel55877a62008-12-12 15:12:14 +01002282 INC_STATS_COUNTER(cnt_unmap_sg);
2283
Joerg Roedel94f6d192009-11-24 16:40:02 +01002284 domain = get_domain(dev);
2285 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002286 return;
2287
Joerg Roedel65b050a2008-06-26 21:28:02 +02002288 spin_lock_irqsave(&domain->lock, flags);
2289
2290 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002291 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002292 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002293 s->dma_address = s->dma_length = 0;
2294 }
2295
Joerg Roedel17b124b2011-04-06 18:01:35 +02002296 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002297
2298 spin_unlock_irqrestore(&domain->lock, flags);
2299}
2300
Joerg Roedel431b2a22008-07-11 17:14:22 +02002301/*
2302 * The exported alloc_coherent function for dma_ops.
2303 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002304static void *alloc_coherent(struct device *dev, size_t size,
2305 dma_addr_t *dma_addr, gfp_t flag)
2306{
2307 unsigned long flags;
2308 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002309 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002310 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002311 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002312
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002313 INC_STATS_COUNTER(cnt_alloc_coherent);
2314
Joerg Roedel94f6d192009-11-24 16:40:02 +01002315 domain = get_domain(dev);
2316 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002317 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2318 *dma_addr = __pa(virt_addr);
2319 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002320 } else if (IS_ERR(domain))
2321 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002322
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002323 dma_mask = dev->coherent_dma_mask;
2324 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2325 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002326
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002327 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2328 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302329 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002330
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002331 paddr = virt_to_phys(virt_addr);
2332
Joerg Roedel832a90c2008-09-18 15:54:23 +02002333 if (!dma_mask)
2334 dma_mask = *dev->dma_mask;
2335
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002336 spin_lock_irqsave(&domain->lock, flags);
2337
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002338 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002339 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002340
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002341 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002342 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002343 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002344 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002345
Joerg Roedel17b124b2011-04-06 18:01:35 +02002346 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002347
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002348 spin_unlock_irqrestore(&domain->lock, flags);
2349
2350 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002351
2352out_free:
2353
2354 free_pages((unsigned long)virt_addr, get_order(size));
2355
2356 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002357}
2358
Joerg Roedel431b2a22008-07-11 17:14:22 +02002359/*
2360 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002361 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002362static void free_coherent(struct device *dev, size_t size,
2363 void *virt_addr, dma_addr_t dma_addr)
2364{
2365 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002366 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002367
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002368 INC_STATS_COUNTER(cnt_free_coherent);
2369
Joerg Roedel94f6d192009-11-24 16:40:02 +01002370 domain = get_domain(dev);
2371 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002372 goto free_mem;
2373
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002374 spin_lock_irqsave(&domain->lock, flags);
2375
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002376 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002377
Joerg Roedel17b124b2011-04-06 18:01:35 +02002378 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002379
2380 spin_unlock_irqrestore(&domain->lock, flags);
2381
2382free_mem:
2383 free_pages((unsigned long)virt_addr, get_order(size));
2384}
2385
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002386/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002387 * This function is called by the DMA layer to find out if we can handle a
2388 * particular device. It is part of the dma_ops.
2389 */
2390static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2391{
Joerg Roedel420aef82009-11-23 16:14:57 +01002392 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002393}
2394
2395/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002396 * The function for pre-allocating protection domains.
2397 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002398 * If the driver core informs the DMA layer if a driver grabs a device
2399 * we don't need to preallocate the protection domains anymore.
2400 * For now we have to.
2401 */
Jaswinder Singh Rajput0e93dd82008-12-29 21:45:22 +05302402static void prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002403{
2404 struct pci_dev *dev = NULL;
2405 struct dma_ops_domain *dma_dom;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002406 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002407
Chris Wrightd18c69d2010-04-02 18:27:55 -07002408 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002409
2410 /* Do we handle this device? */
2411 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002412 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002413
2414 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002415 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002416 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002417
2418 devid = get_device_id(&dev->dev);
2419
Joerg Roedel87a64d52009-11-24 17:26:43 +01002420 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002421 if (!dma_dom)
2422 continue;
2423 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002424 dma_dom->target_dev = devid;
2425
Joerg Roedel15898bb2009-11-24 15:39:42 +01002426 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01002427
Joerg Roedelbd60b732008-09-11 10:24:48 +02002428 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002429 }
2430}
2431
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002432static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002433 .alloc_coherent = alloc_coherent,
2434 .free_coherent = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002435 .map_page = map_page,
2436 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002437 .map_sg = map_sg,
2438 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002439 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002440};
2441
Joerg Roedel27c21272011-05-30 15:56:24 +02002442static unsigned device_dma_ops_init(void)
2443{
2444 struct pci_dev *pdev = NULL;
2445 unsigned unhandled = 0;
2446
2447 for_each_pci_dev(pdev) {
2448 if (!check_device(&pdev->dev)) {
2449 unhandled += 1;
2450 continue;
2451 }
2452
2453 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
2454 }
2455
2456 return unhandled;
2457}
2458
Joerg Roedel431b2a22008-07-11 17:14:22 +02002459/*
2460 * The function which clues the AMD IOMMU driver into dma_ops.
2461 */
Joerg Roedelf5325092010-01-22 17:44:35 +01002462
2463void __init amd_iommu_init_api(void)
2464{
2465 register_iommu(&amd_iommu_ops);
2466}
2467
Joerg Roedel6631ee92008-06-26 21:28:05 +02002468int __init amd_iommu_init_dma_ops(void)
2469{
2470 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02002471 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002472
Joerg Roedel431b2a22008-07-11 17:14:22 +02002473 /*
2474 * first allocate a default protection domain for every IOMMU we
2475 * found in the system. Devices not assigned to any other
2476 * protection domain will be assigned to the default one.
2477 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02002478 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01002479 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002480 if (iommu->default_dom == NULL)
2481 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01002482 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002483 ret = iommu_init_unity_mappings(iommu);
2484 if (ret)
2485 goto free_domains;
2486 }
2487
Joerg Roedel431b2a22008-07-11 17:14:22 +02002488 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01002489 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002490 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01002491 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002492
2493 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002494 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002495
Joerg Roedel431b2a22008-07-11 17:14:22 +02002496 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02002497 unhandled = device_dma_ops_init();
2498 if (unhandled && max_pfn > MAX_DMA32_PFN) {
2499 /* There are unhandled devices - initialize swiotlb for them */
2500 swiotlb = 1;
2501 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02002502
Joerg Roedel7f265082008-12-12 13:50:21 +01002503 amd_iommu_stats_init();
2504
Joerg Roedel6631ee92008-06-26 21:28:05 +02002505 return 0;
2506
2507free_domains:
2508
Joerg Roedel3bd22172009-05-04 15:06:20 +02002509 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002510 if (iommu->default_dom)
2511 dma_ops_domain_free(iommu->default_dom);
2512 }
2513
2514 return ret;
2515}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002516
2517/*****************************************************************************
2518 *
2519 * The following functions belong to the exported interface of AMD IOMMU
2520 *
2521 * This interface allows access to lower level functions of the IOMMU
2522 * like protection domain handling and assignement of devices to domains
2523 * which is not possible with the dma_ops interface.
2524 *
2525 *****************************************************************************/
2526
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002527static void cleanup_domain(struct protection_domain *domain)
2528{
Joerg Roedel492667d2009-11-27 13:25:47 +01002529 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002530 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002531
2532 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2533
Joerg Roedel492667d2009-11-27 13:25:47 +01002534 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
2535 struct device *dev = dev_data->dev;
2536
Chris Wright04e856c2010-02-17 08:51:20 -08002537 __detach_device(dev);
Joerg Roedel492667d2009-11-27 13:25:47 +01002538 atomic_set(&dev_data->bind, 0);
2539 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002540
2541 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2542}
2543
Joerg Roedel26508152009-08-26 16:52:40 +02002544static void protection_domain_free(struct protection_domain *domain)
2545{
2546 if (!domain)
2547 return;
2548
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002549 del_domain_from_list(domain);
2550
Joerg Roedel26508152009-08-26 16:52:40 +02002551 if (domain->id)
2552 domain_id_free(domain->id);
2553
2554 kfree(domain);
2555}
2556
2557static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002558{
2559 struct protection_domain *domain;
2560
2561 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2562 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002563 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002564
2565 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002566 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01002567 domain->id = domain_id_alloc();
2568 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02002569 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002570 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02002571
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002572 add_domain_to_list(domain);
2573
Joerg Roedel26508152009-08-26 16:52:40 +02002574 return domain;
2575
2576out_err:
2577 kfree(domain);
2578
2579 return NULL;
2580}
2581
2582static int amd_iommu_domain_init(struct iommu_domain *dom)
2583{
2584 struct protection_domain *domain;
2585
2586 domain = protection_domain_alloc();
2587 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01002588 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02002589
2590 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002591 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2592 if (!domain->pt_root)
2593 goto out_free;
2594
2595 dom->priv = domain;
2596
2597 return 0;
2598
2599out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02002600 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01002601
2602 return -ENOMEM;
2603}
2604
Joerg Roedel98383fc2008-12-02 18:34:12 +01002605static void amd_iommu_domain_destroy(struct iommu_domain *dom)
2606{
2607 struct protection_domain *domain = dom->priv;
2608
2609 if (!domain)
2610 return;
2611
2612 if (domain->dev_cnt > 0)
2613 cleanup_domain(domain);
2614
2615 BUG_ON(domain->dev_cnt != 0);
2616
2617 free_pagetable(domain);
2618
Joerg Roedel8b408fe2010-03-08 14:20:07 +01002619 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002620
2621 dom->priv = NULL;
2622}
2623
Joerg Roedel684f2882008-12-08 12:07:44 +01002624static void amd_iommu_detach_device(struct iommu_domain *dom,
2625 struct device *dev)
2626{
Joerg Roedel657cbb62009-11-23 15:26:46 +01002627 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002628 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002629 u16 devid;
2630
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002631 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01002632 return;
2633
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002634 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002635
Joerg Roedel657cbb62009-11-23 15:26:46 +01002636 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002637 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002638
2639 iommu = amd_iommu_rlookup_table[devid];
2640 if (!iommu)
2641 return;
2642
Joerg Roedel684f2882008-12-08 12:07:44 +01002643 iommu_completion_wait(iommu);
2644}
2645
Joerg Roedel01106062008-12-02 19:34:11 +01002646static int amd_iommu_attach_device(struct iommu_domain *dom,
2647 struct device *dev)
2648{
2649 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002650 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01002651 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002652 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002653
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002654 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01002655 return -EINVAL;
2656
Joerg Roedel657cbb62009-11-23 15:26:46 +01002657 dev_data = dev->archdata.iommu;
2658
Joerg Roedelf62dda62011-06-09 12:55:35 +02002659 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01002660 if (!iommu)
2661 return -EINVAL;
2662
Joerg Roedel657cbb62009-11-23 15:26:46 +01002663 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002664 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002665
Joerg Roedel15898bb2009-11-24 15:39:42 +01002666 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01002667
2668 iommu_completion_wait(iommu);
2669
Joerg Roedel15898bb2009-11-24 15:39:42 +01002670 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002671}
2672
Joerg Roedel468e2362010-01-21 16:37:36 +01002673static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
2674 phys_addr_t paddr, int gfp_order, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002675{
Joerg Roedel468e2362010-01-21 16:37:36 +01002676 unsigned long page_size = 0x1000UL << gfp_order;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002677 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002678 int prot = 0;
2679 int ret;
2680
2681 if (iommu_prot & IOMMU_READ)
2682 prot |= IOMMU_PROT_IR;
2683 if (iommu_prot & IOMMU_WRITE)
2684 prot |= IOMMU_PROT_IW;
2685
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002686 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f2010-05-11 17:40:57 +02002687 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002688 mutex_unlock(&domain->api_lock);
2689
Joerg Roedel795e74f2010-05-11 17:40:57 +02002690 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002691}
2692
Joerg Roedel468e2362010-01-21 16:37:36 +01002693static int amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2694 int gfp_order)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002695{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002696 struct protection_domain *domain = dom->priv;
Joerg Roedel468e2362010-01-21 16:37:36 +01002697 unsigned long page_size, unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002698
Joerg Roedel468e2362010-01-21 16:37:36 +01002699 page_size = 0x1000UL << gfp_order;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002700
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002701 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01002702 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f2010-05-11 17:40:57 +02002703 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002704
Joerg Roedel17b124b2011-04-06 18:01:35 +02002705 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002706
Joerg Roedel468e2362010-01-21 16:37:36 +01002707 return get_order(unmap_size);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002708}
2709
Joerg Roedel645c4c82008-12-02 20:05:50 +01002710static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2711 unsigned long iova)
2712{
2713 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002714 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002715 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002716 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002717
Joerg Roedel24cd7722010-01-19 17:27:39 +01002718 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002719
Joerg Roedela6d41a42009-09-02 17:08:55 +02002720 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01002721 return 0;
2722
Joerg Roedelf03152b2010-01-21 16:15:24 +01002723 if (PM_PTE_LEVEL(*pte) == 0)
2724 offset_mask = PAGE_SIZE - 1;
2725 else
2726 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
2727
2728 __pte = *pte & PM_ADDR_MASK;
2729 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002730
2731 return paddr;
2732}
2733
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002734static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
2735 unsigned long cap)
2736{
Joerg Roedel80a506b2010-07-27 17:14:24 +02002737 switch (cap) {
2738 case IOMMU_CAP_CACHE_COHERENCY:
2739 return 1;
2740 }
2741
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002742 return 0;
2743}
2744
Joerg Roedel26961ef2008-12-03 17:00:17 +01002745static struct iommu_ops amd_iommu_ops = {
2746 .domain_init = amd_iommu_domain_init,
2747 .domain_destroy = amd_iommu_domain_destroy,
2748 .attach_dev = amd_iommu_attach_device,
2749 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01002750 .map = amd_iommu_map,
2751 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002752 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002753 .domain_has_cap = amd_iommu_domain_has_cap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002754};
2755
Joerg Roedel0feae532009-08-26 15:26:30 +02002756/*****************************************************************************
2757 *
2758 * The next functions do a basic initialization of IOMMU for pass through
2759 * mode
2760 *
2761 * In passthrough mode the IOMMU is initialized and enabled but not used for
2762 * DMA-API translation.
2763 *
2764 *****************************************************************************/
2765
2766int __init amd_iommu_init_passthrough(void)
2767{
Joerg Roedel15898bb2009-11-24 15:39:42 +01002768 struct amd_iommu *iommu;
Joerg Roedel0feae532009-08-26 15:26:30 +02002769 struct pci_dev *dev = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002770 u16 devid;
Joerg Roedel0feae532009-08-26 15:26:30 +02002771
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002772 /* allocate passthrough domain */
Joerg Roedel0feae532009-08-26 15:26:30 +02002773 pt_domain = protection_domain_alloc();
2774 if (!pt_domain)
2775 return -ENOMEM;
2776
2777 pt_domain->mode |= PAGE_MODE_NONE;
2778
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04002779 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002780 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02002781 continue;
2782
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002783 devid = get_device_id(&dev->dev);
2784
Joerg Roedel15898bb2009-11-24 15:39:42 +01002785 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02002786 if (!iommu)
2787 continue;
2788
Joerg Roedel15898bb2009-11-24 15:39:42 +01002789 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02002790 }
2791
2792 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
2793
2794 return 0;
2795}