blob: 0a5b46504b5d3311159321f64894ec788e4a7c7b [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 Roedeld03f067a2008-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 Roedeld03f067a2008-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 */
Joerg Roedel6c542042011-06-09 17:07:31 +0200696static int device_flush_iotlb(struct iommu_dev_data *dev_data,
697 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200698{
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 qdep = dev_data->ats.qdep;
704 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200705
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200706 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200707
708 return iommu_queue_command(iommu, &cmd);
709}
710
711/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200712 * Command send function for invalidating a device table entry
713 */
Joerg Roedel6c542042011-06-09 17:07:31 +0200714static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +0100715{
716 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200717 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100718
Joerg Roedel6c542042011-06-09 17:07:31 +0200719 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +0100720
Joerg Roedelf62dda62011-06-09 12:55:35 +0200721 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200722 if (ret)
723 return ret;
724
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200725 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +0200726 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200727
728 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100729}
730
Joerg Roedel431b2a22008-07-11 17:14:22 +0200731/*
732 * TLB invalidation function which is called from the mapping functions.
733 * It invalidates a single PTE if the range to flush is within a single
734 * page. Otherwise it flushes the whole TLB of the IOMMU.
735 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200736static void __domain_flush_pages(struct protection_domain *domain,
737 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200738{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200739 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +0200740 struct iommu_cmd cmd;
741 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200742
Joerg Roedel11b64022011-04-06 11:49:28 +0200743 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +0200744
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100745 for (i = 0; i < amd_iommus_present; ++i) {
746 if (!domain->dev_iommu[i])
747 continue;
748
749 /*
750 * Devices of this domain are behind this IOMMU
751 * We need a TLB flush
752 */
Joerg Roedel11b64022011-04-06 11:49:28 +0200753 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100754 }
755
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200756 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200757
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200758 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200759 continue;
760
Joerg Roedel6c542042011-06-09 17:07:31 +0200761 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200762 }
763
Joerg Roedel11b64022011-04-06 11:49:28 +0200764 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100765}
766
Joerg Roedel17b124b2011-04-06 18:01:35 +0200767static void domain_flush_pages(struct protection_domain *domain,
768 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100769{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200770 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200771}
Joerg Roedelb6c02712008-06-26 21:27:53 +0200772
Joerg Roedel1c655772008-09-04 18:40:05 +0200773/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200774static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +0200775{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200776 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +0200777}
778
Chris Wright42a49f92009-06-15 15:42:00 +0200779/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200780static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +0200781{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200782 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
783}
784
785static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +0200786{
787 int i;
788
789 for (i = 0; i < amd_iommus_present; ++i) {
790 if (!domain->dev_iommu[i])
791 continue;
792
793 /*
794 * Devices of this domain are behind this IOMMU
795 * We need to wait for completion of all commands.
796 */
797 iommu_completion_wait(amd_iommus[i]);
798 }
799}
800
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100801
Joerg Roedel43f49602008-12-02 21:01:12 +0100802/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100803 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +0100804 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200805static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200806{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100807 struct iommu_dev_data *dev_data;
808 unsigned long flags;
809
810 spin_lock_irqsave(&domain->lock, flags);
811
812 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +0200813 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100814
815 spin_unlock_irqrestore(&domain->lock, flags);
816}
817
Joerg Roedel431b2a22008-07-11 17:14:22 +0200818/****************************************************************************
819 *
820 * The functions below are used the create the page table mappings for
821 * unity mapped regions.
822 *
823 ****************************************************************************/
824
825/*
Joerg Roedel308973d2009-11-24 17:43:32 +0100826 * This function is used to add another level to an IO page table. Adding
827 * another level increases the size of the address space by 9 bits to a size up
828 * to 64 bits.
829 */
830static bool increase_address_space(struct protection_domain *domain,
831 gfp_t gfp)
832{
833 u64 *pte;
834
835 if (domain->mode == PAGE_MODE_6_LEVEL)
836 /* address space already 64 bit large */
837 return false;
838
839 pte = (void *)get_zeroed_page(gfp);
840 if (!pte)
841 return false;
842
843 *pte = PM_LEVEL_PDE(domain->mode,
844 virt_to_phys(domain->pt_root));
845 domain->pt_root = pte;
846 domain->mode += 1;
847 domain->updated = true;
848
849 return true;
850}
851
852static u64 *alloc_pte(struct protection_domain *domain,
853 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100854 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +0100855 u64 **pte_page,
856 gfp_t gfp)
857{
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100858 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +0100859 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100860
861 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +0100862
863 while (address > PM_LEVEL_SIZE(domain->mode))
864 increase_address_space(domain, gfp);
865
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100866 level = domain->mode - 1;
867 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
868 address = PAGE_SIZE_ALIGN(address, page_size);
869 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +0100870
871 while (level > end_lvl) {
872 if (!IOMMU_PTE_PRESENT(*pte)) {
873 page = (u64 *)get_zeroed_page(gfp);
874 if (!page)
875 return NULL;
876 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
877 }
878
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100879 /* No level skipping support yet */
880 if (PM_PTE_LEVEL(*pte) != level)
881 return NULL;
882
Joerg Roedel308973d2009-11-24 17:43:32 +0100883 level -= 1;
884
885 pte = IOMMU_PTE_PAGE(*pte);
886
887 if (pte_page && level == end_lvl)
888 *pte_page = pte;
889
890 pte = &pte[PM_LEVEL_INDEX(level, address)];
891 }
892
893 return pte;
894}
895
896/*
897 * This function checks if there is a PTE for a given dma address. If
898 * there is one, it returns the pointer to it.
899 */
Joerg Roedel24cd7722010-01-19 17:27:39 +0100900static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +0100901{
902 int level;
903 u64 *pte;
904
Joerg Roedel24cd7722010-01-19 17:27:39 +0100905 if (address > PM_LEVEL_SIZE(domain->mode))
906 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +0100907
Joerg Roedel24cd7722010-01-19 17:27:39 +0100908 level = domain->mode - 1;
909 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
910
911 while (level > 0) {
912
913 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +0100914 if (!IOMMU_PTE_PRESENT(*pte))
915 return NULL;
916
Joerg Roedel24cd7722010-01-19 17:27:39 +0100917 /* Large PTE */
918 if (PM_PTE_LEVEL(*pte) == 0x07) {
919 unsigned long pte_mask, __pte;
920
921 /*
922 * If we have a series of large PTEs, make
923 * sure to return a pointer to the first one.
924 */
925 pte_mask = PTE_PAGE_SIZE(*pte);
926 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
927 __pte = ((unsigned long)pte) & pte_mask;
928
929 return (u64 *)__pte;
930 }
931
932 /* No level skipping support yet */
933 if (PM_PTE_LEVEL(*pte) != level)
934 return NULL;
935
Joerg Roedel308973d2009-11-24 17:43:32 +0100936 level -= 1;
937
Joerg Roedel24cd7722010-01-19 17:27:39 +0100938 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +0100939 pte = IOMMU_PTE_PAGE(*pte);
940 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +0100941 }
942
943 return pte;
944}
945
946/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200947 * Generic mapping functions. It maps a physical address into a DMA
948 * address space. It allocates the page table pages if necessary.
949 * In the future it can be extended to a generic mapping function
950 * supporting all features of AMD IOMMU page tables like level skipping
951 * and full 64 bit address spaces.
952 */
Joerg Roedel38e817f2008-12-02 17:27:52 +0100953static int iommu_map_page(struct protection_domain *dom,
954 unsigned long bus_addr,
955 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200956 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100957 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200958{
Joerg Roedel8bda3092009-05-12 12:02:46 +0200959 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100960 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200961
Joerg Roedelbad1cac2009-09-02 16:52:23 +0200962 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200963 return -EINVAL;
964
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100965 bus_addr = PAGE_ALIGN(bus_addr);
966 phys_addr = PAGE_ALIGN(phys_addr);
967 count = PAGE_SIZE_PTE_COUNT(page_size);
968 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200969
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100970 for (i = 0; i < count; ++i)
971 if (IOMMU_PTE_PRESENT(pte[i]))
972 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200973
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100974 if (page_size > PAGE_SIZE) {
975 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
976 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
977 } else
978 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
979
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200980 if (prot & IOMMU_PROT_IR)
981 __pte |= IOMMU_PTE_IR;
982 if (prot & IOMMU_PROT_IW)
983 __pte |= IOMMU_PTE_IW;
984
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100985 for (i = 0; i < count; ++i)
986 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200987
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200988 update_domain(dom);
989
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200990 return 0;
991}
992
Joerg Roedel24cd7722010-01-19 17:27:39 +0100993static unsigned long iommu_unmap_page(struct protection_domain *dom,
994 unsigned long bus_addr,
995 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100996{
Joerg Roedel24cd7722010-01-19 17:27:39 +0100997 unsigned long long unmap_size, unmapped;
998 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100999
Joerg Roedel24cd7722010-01-19 17:27:39 +01001000 BUG_ON(!is_power_of_2(page_size));
1001
1002 unmapped = 0;
1003
1004 while (unmapped < page_size) {
1005
1006 pte = fetch_pte(dom, bus_addr);
1007
1008 if (!pte) {
1009 /*
1010 * No PTE for this address
1011 * move forward in 4kb steps
1012 */
1013 unmap_size = PAGE_SIZE;
1014 } else if (PM_PTE_LEVEL(*pte) == 0) {
1015 /* 4kb PTE found for this address */
1016 unmap_size = PAGE_SIZE;
1017 *pte = 0ULL;
1018 } else {
1019 int count, i;
1020
1021 /* Large PTE found which maps this address */
1022 unmap_size = PTE_PAGE_SIZE(*pte);
1023 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1024 for (i = 0; i < count; i++)
1025 pte[i] = 0ULL;
1026 }
1027
1028 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1029 unmapped += unmap_size;
1030 }
1031
1032 BUG_ON(!is_power_of_2(unmapped));
1033
1034 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001035}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001036
Joerg Roedel431b2a22008-07-11 17:14:22 +02001037/*
1038 * This function checks if a specific unity mapping entry is needed for
1039 * this specific IOMMU.
1040 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001041static int iommu_for_unity_map(struct amd_iommu *iommu,
1042 struct unity_map_entry *entry)
1043{
1044 u16 bdf, i;
1045
1046 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1047 bdf = amd_iommu_alias_table[i];
1048 if (amd_iommu_rlookup_table[bdf] == iommu)
1049 return 1;
1050 }
1051
1052 return 0;
1053}
1054
Joerg Roedel431b2a22008-07-11 17:14:22 +02001055/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001056 * This function actually applies the mapping to the page table of the
1057 * dma_ops domain.
1058 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001059static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1060 struct unity_map_entry *e)
1061{
1062 u64 addr;
1063 int ret;
1064
1065 for (addr = e->address_start; addr < e->address_end;
1066 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001067 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001068 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001069 if (ret)
1070 return ret;
1071 /*
1072 * if unity mapping is in aperture range mark the page
1073 * as allocated in the aperture
1074 */
1075 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001076 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001077 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001078 }
1079
1080 return 0;
1081}
1082
Joerg Roedel431b2a22008-07-11 17:14:22 +02001083/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001084 * Init the unity mappings for a specific IOMMU in the system
1085 *
1086 * Basically iterates over all unity mapping entries and applies them to
1087 * the default domain DMA of that IOMMU if necessary.
1088 */
1089static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1090{
1091 struct unity_map_entry *entry;
1092 int ret;
1093
1094 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1095 if (!iommu_for_unity_map(iommu, entry))
1096 continue;
1097 ret = dma_ops_unity_map(iommu->default_dom, entry);
1098 if (ret)
1099 return ret;
1100 }
1101
1102 return 0;
1103}
1104
1105/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001106 * Inits the unity mappings required for a specific device
1107 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001108static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1109 u16 devid)
1110{
1111 struct unity_map_entry *e;
1112 int ret;
1113
1114 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1115 if (!(devid >= e->devid_start && devid <= e->devid_end))
1116 continue;
1117 ret = dma_ops_unity_map(dma_dom, e);
1118 if (ret)
1119 return ret;
1120 }
1121
1122 return 0;
1123}
1124
Joerg Roedel431b2a22008-07-11 17:14:22 +02001125/****************************************************************************
1126 *
1127 * The next functions belong to the address allocator for the dma_ops
1128 * interface functions. They work like the allocators in the other IOMMU
1129 * drivers. Its basically a bitmap which marks the allocated pages in
1130 * the aperture. Maybe it could be enhanced in the future to a more
1131 * efficient allocator.
1132 *
1133 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001134
Joerg Roedel431b2a22008-07-11 17:14:22 +02001135/*
Joerg Roedel384de722009-05-15 12:30:05 +02001136 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001137 *
1138 * called with domain->lock held
1139 */
Joerg Roedel384de722009-05-15 12:30:05 +02001140
Joerg Roedel9cabe892009-05-18 16:38:55 +02001141/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001142 * Used to reserve address ranges in the aperture (e.g. for exclusion
1143 * ranges.
1144 */
1145static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1146 unsigned long start_page,
1147 unsigned int pages)
1148{
1149 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1150
1151 if (start_page + pages > last_page)
1152 pages = last_page - start_page;
1153
1154 for (i = start_page; i < start_page + pages; ++i) {
1155 int index = i / APERTURE_RANGE_PAGES;
1156 int page = i % APERTURE_RANGE_PAGES;
1157 __set_bit(page, dom->aperture[index]->bitmap);
1158 }
1159}
1160
1161/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001162 * This function is used to add a new aperture range to an existing
1163 * aperture in case of dma_ops domain allocation or address allocation
1164 * failure.
1165 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001166static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001167 bool populate, gfp_t gfp)
1168{
1169 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001170 struct amd_iommu *iommu;
Joerg Roedeld91afd12010-01-22 16:40:20 +01001171 unsigned long i;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001172
Joerg Roedelf5e97052009-05-22 12:31:53 +02001173#ifdef CONFIG_IOMMU_STRESS
1174 populate = false;
1175#endif
1176
Joerg Roedel9cabe892009-05-18 16:38:55 +02001177 if (index >= APERTURE_MAX_RANGES)
1178 return -ENOMEM;
1179
1180 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1181 if (!dma_dom->aperture[index])
1182 return -ENOMEM;
1183
1184 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1185 if (!dma_dom->aperture[index]->bitmap)
1186 goto out_free;
1187
1188 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1189
1190 if (populate) {
1191 unsigned long address = dma_dom->aperture_size;
1192 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1193 u64 *pte, *pte_page;
1194
1195 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001196 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001197 &pte_page, gfp);
1198 if (!pte)
1199 goto out_free;
1200
1201 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1202
1203 address += APERTURE_RANGE_SIZE / 64;
1204 }
1205 }
1206
1207 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1208
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001209 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001210 for_each_iommu(iommu) {
1211 if (iommu->exclusion_start &&
1212 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1213 && iommu->exclusion_start < dma_dom->aperture_size) {
1214 unsigned long startpage;
1215 int pages = iommu_num_pages(iommu->exclusion_start,
1216 iommu->exclusion_length,
1217 PAGE_SIZE);
1218 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1219 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1220 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001221 }
1222
1223 /*
1224 * Check for areas already mapped as present in the new aperture
1225 * range and mark those pages as reserved in the allocator. Such
1226 * mappings may already exist as a result of requested unity
1227 * mappings for devices.
1228 */
1229 for (i = dma_dom->aperture[index]->offset;
1230 i < dma_dom->aperture_size;
1231 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001232 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001233 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1234 continue;
1235
1236 dma_ops_reserve_addresses(dma_dom, i << PAGE_SHIFT, 1);
1237 }
1238
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001239 update_domain(&dma_dom->domain);
1240
Joerg Roedel9cabe892009-05-18 16:38:55 +02001241 return 0;
1242
1243out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001244 update_domain(&dma_dom->domain);
1245
Joerg Roedel9cabe892009-05-18 16:38:55 +02001246 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1247
1248 kfree(dma_dom->aperture[index]);
1249 dma_dom->aperture[index] = NULL;
1250
1251 return -ENOMEM;
1252}
1253
Joerg Roedel384de722009-05-15 12:30:05 +02001254static unsigned long dma_ops_area_alloc(struct device *dev,
1255 struct dma_ops_domain *dom,
1256 unsigned int pages,
1257 unsigned long align_mask,
1258 u64 dma_mask,
1259 unsigned long start)
1260{
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001261 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001262 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1263 int i = start >> APERTURE_RANGE_SHIFT;
1264 unsigned long boundary_size;
1265 unsigned long address = -1;
1266 unsigned long limit;
1267
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001268 next_bit >>= PAGE_SHIFT;
1269
Joerg Roedel384de722009-05-15 12:30:05 +02001270 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1271 PAGE_SIZE) >> PAGE_SHIFT;
1272
1273 for (;i < max_index; ++i) {
1274 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1275
1276 if (dom->aperture[i]->offset >= dma_mask)
1277 break;
1278
1279 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1280 dma_mask >> PAGE_SHIFT);
1281
1282 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1283 limit, next_bit, pages, 0,
1284 boundary_size, align_mask);
1285 if (address != -1) {
1286 address = dom->aperture[i]->offset +
1287 (address << PAGE_SHIFT);
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001288 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001289 break;
1290 }
1291
1292 next_bit = 0;
1293 }
1294
1295 return address;
1296}
1297
Joerg Roedeld3086442008-06-26 21:27:57 +02001298static unsigned long dma_ops_alloc_addresses(struct device *dev,
1299 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001300 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001301 unsigned long align_mask,
1302 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001303{
Joerg Roedeld3086442008-06-26 21:27:57 +02001304 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001305
Joerg Roedelfe16f082009-05-22 12:27:53 +02001306#ifdef CONFIG_IOMMU_STRESS
1307 dom->next_address = 0;
1308 dom->need_flush = true;
1309#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001310
Joerg Roedel384de722009-05-15 12:30:05 +02001311 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001312 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001313
Joerg Roedel1c655772008-09-04 18:40:05 +02001314 if (address == -1) {
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001315 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001316 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1317 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001318 dom->need_flush = true;
1319 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001320
Joerg Roedel384de722009-05-15 12:30:05 +02001321 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001322 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001323
1324 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1325
1326 return address;
1327}
1328
Joerg Roedel431b2a22008-07-11 17:14:22 +02001329/*
1330 * The address free function.
1331 *
1332 * called with domain->lock held
1333 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001334static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1335 unsigned long address,
1336 unsigned int pages)
1337{
Joerg Roedel384de722009-05-15 12:30:05 +02001338 unsigned i = address >> APERTURE_RANGE_SHIFT;
1339 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001340
Joerg Roedel384de722009-05-15 12:30:05 +02001341 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1342
Joerg Roedel47bccd62009-05-22 12:40:54 +02001343#ifdef CONFIG_IOMMU_STRESS
1344 if (i < 4)
1345 return;
1346#endif
1347
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001348 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001349 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001350
1351 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001352
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001353 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001354
Joerg Roedeld3086442008-06-26 21:27:57 +02001355}
1356
Joerg Roedel431b2a22008-07-11 17:14:22 +02001357/****************************************************************************
1358 *
1359 * The next functions belong to the domain allocation. A domain is
1360 * allocated for every IOMMU as the default domain. If device isolation
1361 * is enabled, every device get its own domain. The most important thing
1362 * about domains is the page table mapping the DMA address space they
1363 * contain.
1364 *
1365 ****************************************************************************/
1366
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001367/*
1368 * This function adds a protection domain to the global protection domain list
1369 */
1370static void add_domain_to_list(struct protection_domain *domain)
1371{
1372 unsigned long flags;
1373
1374 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1375 list_add(&domain->list, &amd_iommu_pd_list);
1376 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1377}
1378
1379/*
1380 * This function removes a protection domain to the global
1381 * protection domain list
1382 */
1383static void del_domain_from_list(struct protection_domain *domain)
1384{
1385 unsigned long flags;
1386
1387 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1388 list_del(&domain->list);
1389 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1390}
1391
Joerg Roedelec487d12008-06-26 21:27:58 +02001392static u16 domain_id_alloc(void)
1393{
1394 unsigned long flags;
1395 int id;
1396
1397 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1398 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1399 BUG_ON(id == 0);
1400 if (id > 0 && id < MAX_DOMAIN_ID)
1401 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1402 else
1403 id = 0;
1404 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1405
1406 return id;
1407}
1408
Joerg Roedela2acfb72008-12-02 18:28:53 +01001409static void domain_id_free(int id)
1410{
1411 unsigned long flags;
1412
1413 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1414 if (id > 0 && id < MAX_DOMAIN_ID)
1415 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1416 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1417}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001418
Joerg Roedel86db2e52008-12-02 18:20:21 +01001419static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001420{
1421 int i, j;
1422 u64 *p1, *p2, *p3;
1423
Joerg Roedel86db2e52008-12-02 18:20:21 +01001424 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001425
1426 if (!p1)
1427 return;
1428
1429 for (i = 0; i < 512; ++i) {
1430 if (!IOMMU_PTE_PRESENT(p1[i]))
1431 continue;
1432
1433 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001434 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001435 if (!IOMMU_PTE_PRESENT(p2[j]))
1436 continue;
1437 p3 = IOMMU_PTE_PAGE(p2[j]);
1438 free_page((unsigned long)p3);
1439 }
1440
1441 free_page((unsigned long)p2);
1442 }
1443
1444 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001445
1446 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001447}
1448
Joerg Roedel431b2a22008-07-11 17:14:22 +02001449/*
1450 * Free a domain, only used if something went wrong in the
1451 * allocation path and we need to free an already allocated page table
1452 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001453static void dma_ops_domain_free(struct dma_ops_domain *dom)
1454{
Joerg Roedel384de722009-05-15 12:30:05 +02001455 int i;
1456
Joerg Roedelec487d12008-06-26 21:27:58 +02001457 if (!dom)
1458 return;
1459
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001460 del_domain_from_list(&dom->domain);
1461
Joerg Roedel86db2e52008-12-02 18:20:21 +01001462 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001463
Joerg Roedel384de722009-05-15 12:30:05 +02001464 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1465 if (!dom->aperture[i])
1466 continue;
1467 free_page((unsigned long)dom->aperture[i]->bitmap);
1468 kfree(dom->aperture[i]);
1469 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001470
1471 kfree(dom);
1472}
1473
Joerg Roedel431b2a22008-07-11 17:14:22 +02001474/*
1475 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001476 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001477 * structures required for the dma_ops interface
1478 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001479static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001480{
1481 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001482
1483 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1484 if (!dma_dom)
1485 return NULL;
1486
1487 spin_lock_init(&dma_dom->domain.lock);
1488
1489 dma_dom->domain.id = domain_id_alloc();
1490 if (dma_dom->domain.id == 0)
1491 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001492 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001493 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001494 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001495 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001496 dma_dom->domain.priv = dma_dom;
1497 if (!dma_dom->domain.pt_root)
1498 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001499
Joerg Roedel1c655772008-09-04 18:40:05 +02001500 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001501 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001502
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001503 add_domain_to_list(&dma_dom->domain);
1504
Joerg Roedel576175c2009-11-23 19:08:46 +01001505 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001506 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001507
Joerg Roedel431b2a22008-07-11 17:14:22 +02001508 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001509 * mark the first page as allocated so we never return 0 as
1510 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001511 */
Joerg Roedel384de722009-05-15 12:30:05 +02001512 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001513 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001514
Joerg Roedelec487d12008-06-26 21:27:58 +02001515
1516 return dma_dom;
1517
1518free_dma_dom:
1519 dma_ops_domain_free(dma_dom);
1520
1521 return NULL;
1522}
1523
Joerg Roedel431b2a22008-07-11 17:14:22 +02001524/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001525 * little helper function to check whether a given protection domain is a
1526 * dma_ops domain
1527 */
1528static bool dma_ops_domain(struct protection_domain *domain)
1529{
1530 return domain->flags & PD_DMA_OPS_MASK;
1531}
1532
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001533static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001534{
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001535 u64 pte_root = virt_to_phys(domain->pt_root);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001536 u32 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001537
Joerg Roedel38ddf412008-09-11 10:38:32 +02001538 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1539 << DEV_ENTRY_MODE_SHIFT;
1540 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001541
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001542 if (ats)
1543 flags |= DTE_FLAG_IOTLB;
1544
1545 amd_iommu_dev_table[devid].data[3] |= flags;
1546 amd_iommu_dev_table[devid].data[2] = domain->id;
1547 amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
1548 amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001549}
1550
Joerg Roedel15898bb2009-11-24 15:39:42 +01001551static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001552{
Joerg Roedel355bf552008-12-08 12:02:41 +01001553 /* remove entry from the device table seen by the hardware */
1554 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1555 amd_iommu_dev_table[devid].data[1] = 0;
1556 amd_iommu_dev_table[devid].data[2] = 0;
1557
Joerg Roedelc5cca142009-10-09 18:31:20 +02001558 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001559}
1560
1561static void do_attach(struct device *dev, struct protection_domain *domain)
1562{
1563 struct iommu_dev_data *dev_data;
1564 struct amd_iommu *iommu;
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001565 struct pci_dev *pdev;
1566 bool ats = false;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001567
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001568 dev_data = get_dev_data(dev);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001569 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001570 pdev = to_pci_dev(dev);
1571
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001572 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001573
1574 /* Update data structures */
1575 dev_data->domain = domain;
1576 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001577 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001578
1579 /* Do reference counting */
1580 domain->dev_iommu[iommu->index] += 1;
1581 domain->dev_cnt += 1;
1582
1583 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001584 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001585}
1586
1587static void do_detach(struct device *dev)
1588{
1589 struct iommu_dev_data *dev_data;
1590 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001591
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001592 dev_data = get_dev_data(dev);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001593 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02001594
Joerg Roedelc4596112009-11-20 14:57:32 +01001595 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001596 dev_data->domain->dev_iommu[iommu->index] -= 1;
1597 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001598
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001599 /* Update data structures */
1600 dev_data->domain = NULL;
1601 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001602 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001603
1604 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001605 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001606}
1607
1608/*
1609 * If a device is not yet associated with a domain, this function does
1610 * assigns it visible for the hardware
1611 */
1612static int __attach_device(struct device *dev,
1613 struct protection_domain *domain)
1614{
Joerg Roedel657cbb62009-11-23 15:26:46 +01001615 struct iommu_dev_data *dev_data, *alias_data;
Julia Lawall84fe6c12010-05-27 12:31:51 +02001616 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001617
Joerg Roedel657cbb62009-11-23 15:26:46 +01001618 dev_data = get_dev_data(dev);
1619 alias_data = get_dev_data(dev_data->alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001620
Joerg Roedel657cbb62009-11-23 15:26:46 +01001621 if (!alias_data)
1622 return -EINVAL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001623
1624 /* lock domain */
1625 spin_lock(&domain->lock);
1626
1627 /* Some sanity checks */
Julia Lawall84fe6c12010-05-27 12:31:51 +02001628 ret = -EBUSY;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001629 if (alias_data->domain != NULL &&
1630 alias_data->domain != domain)
Julia Lawall84fe6c12010-05-27 12:31:51 +02001631 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001632
Joerg Roedel657cbb62009-11-23 15:26:46 +01001633 if (dev_data->domain != NULL &&
1634 dev_data->domain != domain)
Julia Lawall84fe6c12010-05-27 12:31:51 +02001635 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001636
1637 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001638 if (dev_data->alias != dev) {
1639 alias_data = get_dev_data(dev_data->alias);
1640 if (alias_data->domain == NULL)
1641 do_attach(dev_data->alias, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01001642
1643 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001644 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01001645
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001646 if (dev_data->domain == NULL)
1647 do_attach(dev, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001648
Joerg Roedel24100052009-11-25 15:59:57 +01001649 atomic_inc(&dev_data->bind);
1650
Julia Lawall84fe6c12010-05-27 12:31:51 +02001651 ret = 0;
1652
1653out_unlock:
1654
Joerg Roedel355bf552008-12-08 12:02:41 +01001655 /* ready */
1656 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001657
Julia Lawall84fe6c12010-05-27 12:31:51 +02001658 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001659}
1660
1661/*
1662 * If a device is not yet associated with a domain, this function does
1663 * assigns it visible for the hardware
1664 */
1665static int attach_device(struct device *dev,
1666 struct protection_domain *domain)
1667{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001668 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001669 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001670 unsigned long flags;
1671 int ret;
1672
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001673 dev_data = get_dev_data(dev);
1674
1675 if (amd_iommu_iotlb_sup && pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
1676 dev_data->ats.enabled = true;
1677 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1678 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001679
Joerg Roedel15898bb2009-11-24 15:39:42 +01001680 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1681 ret = __attach_device(dev, domain);
1682 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1683
1684 /*
1685 * We might boot into a crash-kernel here. The crashed kernel
1686 * left the caches in the IOMMU dirty. So we have to flush
1687 * here to evict all dirty stuff.
1688 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001689 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001690
1691 return ret;
1692}
1693
1694/*
1695 * Removes a device from a protection domain (unlocked)
1696 */
1697static void __detach_device(struct device *dev)
1698{
Joerg Roedel657cbb62009-11-23 15:26:46 +01001699 struct iommu_dev_data *dev_data = get_dev_data(dev);
Joerg Roedel24100052009-11-25 15:59:57 +01001700 struct iommu_dev_data *alias_data;
Joerg Roedel2ca76272010-01-22 16:45:31 +01001701 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001702 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001703
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001704 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001705
Joerg Roedel2ca76272010-01-22 16:45:31 +01001706 domain = dev_data->domain;
1707
1708 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01001709
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001710 if (dev_data->alias != dev) {
Joerg Roedel24100052009-11-25 15:59:57 +01001711 alias_data = get_dev_data(dev_data->alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001712 if (atomic_dec_and_test(&alias_data->bind))
1713 do_detach(dev_data->alias);
Joerg Roedel24100052009-11-25 15:59:57 +01001714 }
1715
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001716 if (atomic_dec_and_test(&dev_data->bind))
1717 do_detach(dev);
1718
Joerg Roedel2ca76272010-01-22 16:45:31 +01001719 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001720
Joerg Roedel21129f72009-09-01 11:59:42 +02001721 /*
1722 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001723 * passthrough domain if it is detached from any other domain.
1724 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02001725 */
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001726 if (iommu_pass_through &&
1727 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedel15898bb2009-11-24 15:39:42 +01001728 __attach_device(dev, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01001729}
1730
1731/*
1732 * Removes a device from a protection domain (with devtable_lock held)
1733 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01001734static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01001735{
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001736 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01001737 unsigned long flags;
1738
1739 /* lock device table */
1740 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001741 __detach_device(dev);
Joerg Roedel355bf552008-12-08 12:02:41 +01001742 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001743
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001744 dev_data = get_dev_data(dev);
1745
1746 if (dev_data->ats.enabled) {
1747 pci_disable_ats(to_pci_dev(dev));
1748 dev_data->ats.enabled = false;
1749 }
Joerg Roedel355bf552008-12-08 12:02:41 +01001750}
Joerg Roedele275a2a2008-12-10 18:27:25 +01001751
Joerg Roedel15898bb2009-11-24 15:39:42 +01001752/*
1753 * Find out the protection domain structure for a given PCI device. This
1754 * will give us the pointer to the page table root for example.
1755 */
1756static struct protection_domain *domain_for_device(struct device *dev)
1757{
1758 struct protection_domain *dom;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001759 struct iommu_dev_data *dev_data, *alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001760 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001761
Joerg Roedel657cbb62009-11-23 15:26:46 +01001762 dev_data = get_dev_data(dev);
1763 alias_data = get_dev_data(dev_data->alias);
1764 if (!alias_data)
1765 return NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001766
1767 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001768 dom = dev_data->domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001769 if (dom == NULL &&
Joerg Roedel657cbb62009-11-23 15:26:46 +01001770 alias_data->domain != NULL) {
1771 __attach_device(dev, alias_data->domain);
1772 dom = alias_data->domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001773 }
1774
1775 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1776
1777 return dom;
1778}
1779
Joerg Roedele275a2a2008-12-10 18:27:25 +01001780static int device_change_notifier(struct notifier_block *nb,
1781 unsigned long action, void *data)
1782{
1783 struct device *dev = data;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001784 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001785 struct protection_domain *domain;
1786 struct dma_ops_domain *dma_domain;
1787 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001788 unsigned long flags;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001789
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001790 if (!check_device(dev))
1791 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001792
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001793 devid = get_device_id(dev);
1794 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01001795
1796 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07001797 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001798
1799 domain = domain_for_device(dev);
1800
Joerg Roedele275a2a2008-12-10 18:27:25 +01001801 if (!domain)
1802 goto out;
Joerg Roedela1ca3312009-09-01 12:22:22 +02001803 if (iommu_pass_through)
1804 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001805 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001806 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001807 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001808
1809 iommu_init_device(dev);
1810
1811 domain = domain_for_device(dev);
1812
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001813 /* allocate a protection domain if a device is added */
1814 dma_domain = find_protection_domain(devid);
1815 if (dma_domain)
1816 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01001817 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001818 if (!dma_domain)
1819 goto out;
1820 dma_domain->target_dev = devid;
1821
1822 spin_lock_irqsave(&iommu_pd_list_lock, flags);
1823 list_add_tail(&dma_domain->list, &iommu_pd_list);
1824 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
1825
1826 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001827 case BUS_NOTIFY_DEL_DEVICE:
1828
1829 iommu_uninit_device(dev);
1830
Joerg Roedele275a2a2008-12-10 18:27:25 +01001831 default:
1832 goto out;
1833 }
1834
Joerg Roedele275a2a2008-12-10 18:27:25 +01001835 iommu_completion_wait(iommu);
1836
1837out:
1838 return 0;
1839}
1840
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05301841static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01001842 .notifier_call = device_change_notifier,
1843};
Joerg Roedel355bf552008-12-08 12:02:41 +01001844
Joerg Roedel8638c492009-12-10 11:12:25 +01001845void amd_iommu_init_notifier(void)
1846{
1847 bus_register_notifier(&pci_bus_type, &device_nb);
1848}
1849
Joerg Roedel431b2a22008-07-11 17:14:22 +02001850/*****************************************************************************
1851 *
1852 * The next functions belong to the dma_ops mapping/unmapping code.
1853 *
1854 *****************************************************************************/
1855
1856/*
1857 * In the dma_ops path we only have the struct device. This function
1858 * finds the corresponding IOMMU, the protection domain and the
1859 * requestor id for a given device.
1860 * If the device is not yet associated with a domain this is also done
1861 * in this function.
1862 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001863static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001864{
Joerg Roedel94f6d192009-11-24 16:40:02 +01001865 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001866 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01001867 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001868
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001869 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01001870 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001871
Joerg Roedel94f6d192009-11-24 16:40:02 +01001872 domain = domain_for_device(dev);
1873 if (domain != NULL && !dma_ops_domain(domain))
1874 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001875
Joerg Roedel94f6d192009-11-24 16:40:02 +01001876 if (domain != NULL)
1877 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001878
Joerg Roedel15898bb2009-11-24 15:39:42 +01001879 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001880 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001881 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01001882 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
1883 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001884 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01001885 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01001886
Joerg Roedel94f6d192009-11-24 16:40:02 +01001887 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001888}
1889
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001890static void update_device_table(struct protection_domain *domain)
1891{
Joerg Roedel492667d2009-11-27 13:25:47 +01001892 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001893
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001894 list_for_each_entry(dev_data, &domain->dev_list, list)
1895 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001896}
1897
1898static void update_domain(struct protection_domain *domain)
1899{
1900 if (!domain->updated)
1901 return;
1902
1903 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02001904
1905 domain_flush_devices(domain);
1906 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001907
1908 domain->updated = false;
1909}
1910
Joerg Roedel431b2a22008-07-11 17:14:22 +02001911/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02001912 * This function fetches the PTE for a given address in the aperture
1913 */
1914static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
1915 unsigned long address)
1916{
Joerg Roedel384de722009-05-15 12:30:05 +02001917 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02001918 u64 *pte, *pte_page;
1919
Joerg Roedel384de722009-05-15 12:30:05 +02001920 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1921 if (!aperture)
1922 return NULL;
1923
1924 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02001925 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001926 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001927 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02001928 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
1929 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001930 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001931
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001932 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001933
1934 return pte;
1935}
1936
1937/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001938 * This is the generic map function. It maps one 4kb page at paddr to
1939 * the given address in the DMA address space for the domain.
1940 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001941static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001942 unsigned long address,
1943 phys_addr_t paddr,
1944 int direction)
1945{
1946 u64 *pte, __pte;
1947
1948 WARN_ON(address > dom->aperture_size);
1949
1950 paddr &= PAGE_MASK;
1951
Joerg Roedel8bda3092009-05-12 12:02:46 +02001952 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02001953 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001954 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001955
1956 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
1957
1958 if (direction == DMA_TO_DEVICE)
1959 __pte |= IOMMU_PTE_IR;
1960 else if (direction == DMA_FROM_DEVICE)
1961 __pte |= IOMMU_PTE_IW;
1962 else if (direction == DMA_BIDIRECTIONAL)
1963 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
1964
1965 WARN_ON(*pte);
1966
1967 *pte = __pte;
1968
1969 return (dma_addr_t)address;
1970}
1971
Joerg Roedel431b2a22008-07-11 17:14:22 +02001972/*
1973 * The generic unmapping function for on page in the DMA address space.
1974 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001975static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001976 unsigned long address)
1977{
Joerg Roedel384de722009-05-15 12:30:05 +02001978 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001979 u64 *pte;
1980
1981 if (address >= dom->aperture_size)
1982 return;
1983
Joerg Roedel384de722009-05-15 12:30:05 +02001984 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1985 if (!aperture)
1986 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001987
Joerg Roedel384de722009-05-15 12:30:05 +02001988 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
1989 if (!pte)
1990 return;
1991
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001992 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001993
1994 WARN_ON(!*pte);
1995
1996 *pte = 0ULL;
1997}
1998
Joerg Roedel431b2a22008-07-11 17:14:22 +02001999/*
2000 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002001 * contiguous memory region into DMA address space. It is used by all
2002 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002003 * Must be called with the domain lock held.
2004 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002005static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002006 struct dma_ops_domain *dma_dom,
2007 phys_addr_t paddr,
2008 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002009 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002010 bool align,
2011 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002012{
2013 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002014 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002015 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002016 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002017 int i;
2018
Joerg Roedele3c449f2008-10-15 22:02:11 -07002019 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002020 paddr &= PAGE_MASK;
2021
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002022 INC_STATS_COUNTER(total_map_requests);
2023
Joerg Roedelc1858972008-12-12 15:42:39 +01002024 if (pages > 1)
2025 INC_STATS_COUNTER(cross_page);
2026
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002027 if (align)
2028 align_mask = (1UL << get_order(size)) - 1;
2029
Joerg Roedel11b83882009-05-19 10:23:15 +02002030retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002031 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2032 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002033 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002034 /*
2035 * setting next_address here will let the address
2036 * allocator only scan the new allocated range in the
2037 * first run. This is a small optimization.
2038 */
2039 dma_dom->next_address = dma_dom->aperture_size;
2040
Joerg Roedel576175c2009-11-23 19:08:46 +01002041 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002042 goto out;
2043
2044 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002045 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002046 * allocation again
2047 */
2048 goto retry;
2049 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002050
2051 start = address;
2052 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002053 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002054 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002055 goto out_unmap;
2056
Joerg Roedelcb76c322008-06-26 21:28:00 +02002057 paddr += PAGE_SIZE;
2058 start += PAGE_SIZE;
2059 }
2060 address += offset;
2061
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002062 ADD_STATS_COUNTER(alloced_io_mem, size);
2063
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002064 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002065 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002066 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002067 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002068 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002069
Joerg Roedelcb76c322008-06-26 21:28:00 +02002070out:
2071 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002072
2073out_unmap:
2074
2075 for (--i; i >= 0; --i) {
2076 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002077 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002078 }
2079
2080 dma_ops_free_addresses(dma_dom, address, pages);
2081
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002082 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002083}
2084
Joerg Roedel431b2a22008-07-11 17:14:22 +02002085/*
2086 * Does the reverse of the __map_single function. Must be called with
2087 * the domain lock held too
2088 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002089static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002090 dma_addr_t dma_addr,
2091 size_t size,
2092 int dir)
2093{
Joerg Roedel04e04632010-09-23 16:12:48 +02002094 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002095 dma_addr_t i, start;
2096 unsigned int pages;
2097
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002098 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002099 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002100 return;
2101
Joerg Roedel04e04632010-09-23 16:12:48 +02002102 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002103 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002104 dma_addr &= PAGE_MASK;
2105 start = dma_addr;
2106
2107 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002108 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002109 start += PAGE_SIZE;
2110 }
2111
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002112 SUB_STATS_COUNTER(alloced_io_mem, size);
2113
Joerg Roedelcb76c322008-06-26 21:28:00 +02002114 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002115
Joerg Roedel80be3082008-11-06 14:59:05 +01002116 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002117 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002118 dma_dom->need_flush = false;
2119 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002120}
2121
Joerg Roedel431b2a22008-07-11 17:14:22 +02002122/*
2123 * The exported map_single function for dma_ops.
2124 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002125static dma_addr_t map_page(struct device *dev, struct page *page,
2126 unsigned long offset, size_t size,
2127 enum dma_data_direction dir,
2128 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002129{
2130 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002131 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002132 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002133 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002134 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002135
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002136 INC_STATS_COUNTER(cnt_map_single);
2137
Joerg Roedel94f6d192009-11-24 16:40:02 +01002138 domain = get_domain(dev);
2139 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002140 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002141 else if (IS_ERR(domain))
2142 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002143
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002144 dma_mask = *dev->dma_mask;
2145
Joerg Roedel4da70b92008-06-26 21:28:01 +02002146 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002147
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002148 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002149 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002150 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002151 goto out;
2152
Joerg Roedel17b124b2011-04-06 18:01:35 +02002153 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002154
2155out:
2156 spin_unlock_irqrestore(&domain->lock, flags);
2157
2158 return addr;
2159}
2160
Joerg Roedel431b2a22008-07-11 17:14:22 +02002161/*
2162 * The exported unmap_single function for dma_ops.
2163 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002164static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2165 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002166{
2167 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002168 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002169
Joerg Roedel146a6912008-12-12 15:07:12 +01002170 INC_STATS_COUNTER(cnt_unmap_single);
2171
Joerg Roedel94f6d192009-11-24 16:40:02 +01002172 domain = get_domain(dev);
2173 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002174 return;
2175
Joerg Roedel4da70b92008-06-26 21:28:01 +02002176 spin_lock_irqsave(&domain->lock, flags);
2177
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002178 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002179
Joerg Roedel17b124b2011-04-06 18:01:35 +02002180 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002181
2182 spin_unlock_irqrestore(&domain->lock, flags);
2183}
2184
Joerg Roedel431b2a22008-07-11 17:14:22 +02002185/*
2186 * This is a special map_sg function which is used if we should map a
2187 * device which is not handled by an AMD IOMMU in the system.
2188 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002189static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2190 int nelems, int dir)
2191{
2192 struct scatterlist *s;
2193 int i;
2194
2195 for_each_sg(sglist, s, nelems, i) {
2196 s->dma_address = (dma_addr_t)sg_phys(s);
2197 s->dma_length = s->length;
2198 }
2199
2200 return nelems;
2201}
2202
Joerg Roedel431b2a22008-07-11 17:14:22 +02002203/*
2204 * The exported map_sg function for dma_ops (handles scatter-gather
2205 * lists).
2206 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002207static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002208 int nelems, enum dma_data_direction dir,
2209 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002210{
2211 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002212 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002213 int i;
2214 struct scatterlist *s;
2215 phys_addr_t paddr;
2216 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002217 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002218
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002219 INC_STATS_COUNTER(cnt_map_sg);
2220
Joerg Roedel94f6d192009-11-24 16:40:02 +01002221 domain = get_domain(dev);
2222 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002223 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002224 else if (IS_ERR(domain))
2225 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002226
Joerg Roedel832a90c2008-09-18 15:54:23 +02002227 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002228
Joerg Roedel65b050a2008-06-26 21:28:02 +02002229 spin_lock_irqsave(&domain->lock, flags);
2230
2231 for_each_sg(sglist, s, nelems, i) {
2232 paddr = sg_phys(s);
2233
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002234 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002235 paddr, s->length, dir, false,
2236 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002237
2238 if (s->dma_address) {
2239 s->dma_length = s->length;
2240 mapped_elems++;
2241 } else
2242 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002243 }
2244
Joerg Roedel17b124b2011-04-06 18:01:35 +02002245 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002246
2247out:
2248 spin_unlock_irqrestore(&domain->lock, flags);
2249
2250 return mapped_elems;
2251unmap:
2252 for_each_sg(sglist, s, mapped_elems, i) {
2253 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002254 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002255 s->dma_length, dir);
2256 s->dma_address = s->dma_length = 0;
2257 }
2258
2259 mapped_elems = 0;
2260
2261 goto out;
2262}
2263
Joerg Roedel431b2a22008-07-11 17:14:22 +02002264/*
2265 * The exported map_sg function for dma_ops (handles scatter-gather
2266 * lists).
2267 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002268static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002269 int nelems, enum dma_data_direction dir,
2270 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002271{
2272 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002273 struct protection_domain *domain;
2274 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002275 int i;
2276
Joerg Roedel55877a62008-12-12 15:12:14 +01002277 INC_STATS_COUNTER(cnt_unmap_sg);
2278
Joerg Roedel94f6d192009-11-24 16:40:02 +01002279 domain = get_domain(dev);
2280 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002281 return;
2282
Joerg Roedel65b050a2008-06-26 21:28:02 +02002283 spin_lock_irqsave(&domain->lock, flags);
2284
2285 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002286 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002287 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002288 s->dma_address = s->dma_length = 0;
2289 }
2290
Joerg Roedel17b124b2011-04-06 18:01:35 +02002291 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002292
2293 spin_unlock_irqrestore(&domain->lock, flags);
2294}
2295
Joerg Roedel431b2a22008-07-11 17:14:22 +02002296/*
2297 * The exported alloc_coherent function for dma_ops.
2298 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002299static void *alloc_coherent(struct device *dev, size_t size,
2300 dma_addr_t *dma_addr, gfp_t flag)
2301{
2302 unsigned long flags;
2303 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002304 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002305 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002306 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002307
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002308 INC_STATS_COUNTER(cnt_alloc_coherent);
2309
Joerg Roedel94f6d192009-11-24 16:40:02 +01002310 domain = get_domain(dev);
2311 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002312 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2313 *dma_addr = __pa(virt_addr);
2314 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002315 } else if (IS_ERR(domain))
2316 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002317
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002318 dma_mask = dev->coherent_dma_mask;
2319 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2320 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002321
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002322 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2323 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302324 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002325
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002326 paddr = virt_to_phys(virt_addr);
2327
Joerg Roedel832a90c2008-09-18 15:54:23 +02002328 if (!dma_mask)
2329 dma_mask = *dev->dma_mask;
2330
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002331 spin_lock_irqsave(&domain->lock, flags);
2332
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002333 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002334 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002335
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002336 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002337 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002338 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002339 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002340
Joerg Roedel17b124b2011-04-06 18:01:35 +02002341 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002342
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002343 spin_unlock_irqrestore(&domain->lock, flags);
2344
2345 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002346
2347out_free:
2348
2349 free_pages((unsigned long)virt_addr, get_order(size));
2350
2351 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002352}
2353
Joerg Roedel431b2a22008-07-11 17:14:22 +02002354/*
2355 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002356 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002357static void free_coherent(struct device *dev, size_t size,
2358 void *virt_addr, dma_addr_t dma_addr)
2359{
2360 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002361 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002362
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002363 INC_STATS_COUNTER(cnt_free_coherent);
2364
Joerg Roedel94f6d192009-11-24 16:40:02 +01002365 domain = get_domain(dev);
2366 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002367 goto free_mem;
2368
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002369 spin_lock_irqsave(&domain->lock, flags);
2370
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002371 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002372
Joerg Roedel17b124b2011-04-06 18:01:35 +02002373 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002374
2375 spin_unlock_irqrestore(&domain->lock, flags);
2376
2377free_mem:
2378 free_pages((unsigned long)virt_addr, get_order(size));
2379}
2380
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002381/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002382 * This function is called by the DMA layer to find out if we can handle a
2383 * particular device. It is part of the dma_ops.
2384 */
2385static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2386{
Joerg Roedel420aef82009-11-23 16:14:57 +01002387 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002388}
2389
2390/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002391 * The function for pre-allocating protection domains.
2392 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002393 * If the driver core informs the DMA layer if a driver grabs a device
2394 * we don't need to preallocate the protection domains anymore.
2395 * For now we have to.
2396 */
Jaswinder Singh Rajput0e93dd82008-12-29 21:45:22 +05302397static void prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002398{
2399 struct pci_dev *dev = NULL;
2400 struct dma_ops_domain *dma_dom;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002401 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002402
Chris Wrightd18c69d2010-04-02 18:27:55 -07002403 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002404
2405 /* Do we handle this device? */
2406 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002407 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002408
2409 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002410 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002411 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002412
2413 devid = get_device_id(&dev->dev);
2414
Joerg Roedel87a64d52009-11-24 17:26:43 +01002415 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002416 if (!dma_dom)
2417 continue;
2418 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002419 dma_dom->target_dev = devid;
2420
Joerg Roedel15898bb2009-11-24 15:39:42 +01002421 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01002422
Joerg Roedelbd60b732008-09-11 10:24:48 +02002423 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002424 }
2425}
2426
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002427static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002428 .alloc_coherent = alloc_coherent,
2429 .free_coherent = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002430 .map_page = map_page,
2431 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002432 .map_sg = map_sg,
2433 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002434 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002435};
2436
Joerg Roedel27c21272011-05-30 15:56:24 +02002437static unsigned device_dma_ops_init(void)
2438{
2439 struct pci_dev *pdev = NULL;
2440 unsigned unhandled = 0;
2441
2442 for_each_pci_dev(pdev) {
2443 if (!check_device(&pdev->dev)) {
2444 unhandled += 1;
2445 continue;
2446 }
2447
2448 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
2449 }
2450
2451 return unhandled;
2452}
2453
Joerg Roedel431b2a22008-07-11 17:14:22 +02002454/*
2455 * The function which clues the AMD IOMMU driver into dma_ops.
2456 */
Joerg Roedelf5325092010-01-22 17:44:35 +01002457
2458void __init amd_iommu_init_api(void)
2459{
2460 register_iommu(&amd_iommu_ops);
2461}
2462
Joerg Roedel6631ee92008-06-26 21:28:05 +02002463int __init amd_iommu_init_dma_ops(void)
2464{
2465 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02002466 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002467
Joerg Roedel431b2a22008-07-11 17:14:22 +02002468 /*
2469 * first allocate a default protection domain for every IOMMU we
2470 * found in the system. Devices not assigned to any other
2471 * protection domain will be assigned to the default one.
2472 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02002473 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01002474 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002475 if (iommu->default_dom == NULL)
2476 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01002477 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002478 ret = iommu_init_unity_mappings(iommu);
2479 if (ret)
2480 goto free_domains;
2481 }
2482
Joerg Roedel431b2a22008-07-11 17:14:22 +02002483 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01002484 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002485 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01002486 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002487
2488 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002489 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002490
Joerg Roedel431b2a22008-07-11 17:14:22 +02002491 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02002492 unhandled = device_dma_ops_init();
2493 if (unhandled && max_pfn > MAX_DMA32_PFN) {
2494 /* There are unhandled devices - initialize swiotlb for them */
2495 swiotlb = 1;
2496 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02002497
Joerg Roedel7f265082008-12-12 13:50:21 +01002498 amd_iommu_stats_init();
2499
Joerg Roedel6631ee92008-06-26 21:28:05 +02002500 return 0;
2501
2502free_domains:
2503
Joerg Roedel3bd22172009-05-04 15:06:20 +02002504 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002505 if (iommu->default_dom)
2506 dma_ops_domain_free(iommu->default_dom);
2507 }
2508
2509 return ret;
2510}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002511
2512/*****************************************************************************
2513 *
2514 * The following functions belong to the exported interface of AMD IOMMU
2515 *
2516 * This interface allows access to lower level functions of the IOMMU
2517 * like protection domain handling and assignement of devices to domains
2518 * which is not possible with the dma_ops interface.
2519 *
2520 *****************************************************************************/
2521
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002522static void cleanup_domain(struct protection_domain *domain)
2523{
Joerg Roedel492667d2009-11-27 13:25:47 +01002524 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002525 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002526
2527 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2528
Joerg Roedel492667d2009-11-27 13:25:47 +01002529 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
2530 struct device *dev = dev_data->dev;
2531
Chris Wright04e856c2010-02-17 08:51:20 -08002532 __detach_device(dev);
Joerg Roedel492667d2009-11-27 13:25:47 +01002533 atomic_set(&dev_data->bind, 0);
2534 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002535
2536 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2537}
2538
Joerg Roedel26508152009-08-26 16:52:40 +02002539static void protection_domain_free(struct protection_domain *domain)
2540{
2541 if (!domain)
2542 return;
2543
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002544 del_domain_from_list(domain);
2545
Joerg Roedel26508152009-08-26 16:52:40 +02002546 if (domain->id)
2547 domain_id_free(domain->id);
2548
2549 kfree(domain);
2550}
2551
2552static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002553{
2554 struct protection_domain *domain;
2555
2556 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2557 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002558 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002559
2560 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002561 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01002562 domain->id = domain_id_alloc();
2563 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02002564 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002565 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02002566
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002567 add_domain_to_list(domain);
2568
Joerg Roedel26508152009-08-26 16:52:40 +02002569 return domain;
2570
2571out_err:
2572 kfree(domain);
2573
2574 return NULL;
2575}
2576
2577static int amd_iommu_domain_init(struct iommu_domain *dom)
2578{
2579 struct protection_domain *domain;
2580
2581 domain = protection_domain_alloc();
2582 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01002583 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02002584
2585 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002586 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2587 if (!domain->pt_root)
2588 goto out_free;
2589
2590 dom->priv = domain;
2591
2592 return 0;
2593
2594out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02002595 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01002596
2597 return -ENOMEM;
2598}
2599
Joerg Roedel98383fc2008-12-02 18:34:12 +01002600static void amd_iommu_domain_destroy(struct iommu_domain *dom)
2601{
2602 struct protection_domain *domain = dom->priv;
2603
2604 if (!domain)
2605 return;
2606
2607 if (domain->dev_cnt > 0)
2608 cleanup_domain(domain);
2609
2610 BUG_ON(domain->dev_cnt != 0);
2611
2612 free_pagetable(domain);
2613
Joerg Roedel8b408fe2010-03-08 14:20:07 +01002614 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002615
2616 dom->priv = NULL;
2617}
2618
Joerg Roedel684f2882008-12-08 12:07:44 +01002619static void amd_iommu_detach_device(struct iommu_domain *dom,
2620 struct device *dev)
2621{
Joerg Roedel657cbb62009-11-23 15:26:46 +01002622 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002623 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002624 u16 devid;
2625
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002626 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01002627 return;
2628
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002629 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002630
Joerg Roedel657cbb62009-11-23 15:26:46 +01002631 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002632 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002633
2634 iommu = amd_iommu_rlookup_table[devid];
2635 if (!iommu)
2636 return;
2637
Joerg Roedel684f2882008-12-08 12:07:44 +01002638 iommu_completion_wait(iommu);
2639}
2640
Joerg Roedel01106062008-12-02 19:34:11 +01002641static int amd_iommu_attach_device(struct iommu_domain *dom,
2642 struct device *dev)
2643{
2644 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002645 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01002646 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002647 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002648
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002649 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01002650 return -EINVAL;
2651
Joerg Roedel657cbb62009-11-23 15:26:46 +01002652 dev_data = dev->archdata.iommu;
2653
Joerg Roedelf62dda62011-06-09 12:55:35 +02002654 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01002655 if (!iommu)
2656 return -EINVAL;
2657
Joerg Roedel657cbb62009-11-23 15:26:46 +01002658 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002659 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002660
Joerg Roedel15898bb2009-11-24 15:39:42 +01002661 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01002662
2663 iommu_completion_wait(iommu);
2664
Joerg Roedel15898bb2009-11-24 15:39:42 +01002665 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002666}
2667
Joerg Roedel468e2362010-01-21 16:37:36 +01002668static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
2669 phys_addr_t paddr, int gfp_order, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002670{
Joerg Roedel468e2362010-01-21 16:37:36 +01002671 unsigned long page_size = 0x1000UL << gfp_order;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002672 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002673 int prot = 0;
2674 int ret;
2675
2676 if (iommu_prot & IOMMU_READ)
2677 prot |= IOMMU_PROT_IR;
2678 if (iommu_prot & IOMMU_WRITE)
2679 prot |= IOMMU_PROT_IW;
2680
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002681 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002682 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002683 mutex_unlock(&domain->api_lock);
2684
Joerg Roedel795e74f72010-05-11 17:40:57 +02002685 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002686}
2687
Joerg Roedel468e2362010-01-21 16:37:36 +01002688static int amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2689 int gfp_order)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002690{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002691 struct protection_domain *domain = dom->priv;
Joerg Roedel468e2362010-01-21 16:37:36 +01002692 unsigned long page_size, unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002693
Joerg Roedel468e2362010-01-21 16:37:36 +01002694 page_size = 0x1000UL << gfp_order;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002695
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002696 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01002697 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002698 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002699
Joerg Roedel17b124b2011-04-06 18:01:35 +02002700 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002701
Joerg Roedel468e2362010-01-21 16:37:36 +01002702 return get_order(unmap_size);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002703}
2704
Joerg Roedel645c4c82008-12-02 20:05:50 +01002705static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2706 unsigned long iova)
2707{
2708 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002709 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002710 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002711 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002712
Joerg Roedel24cd7722010-01-19 17:27:39 +01002713 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002714
Joerg Roedela6d41a42009-09-02 17:08:55 +02002715 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01002716 return 0;
2717
Joerg Roedelf03152b2010-01-21 16:15:24 +01002718 if (PM_PTE_LEVEL(*pte) == 0)
2719 offset_mask = PAGE_SIZE - 1;
2720 else
2721 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
2722
2723 __pte = *pte & PM_ADDR_MASK;
2724 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002725
2726 return paddr;
2727}
2728
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002729static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
2730 unsigned long cap)
2731{
Joerg Roedel80a506b2010-07-27 17:14:24 +02002732 switch (cap) {
2733 case IOMMU_CAP_CACHE_COHERENCY:
2734 return 1;
2735 }
2736
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002737 return 0;
2738}
2739
Joerg Roedel26961ef2008-12-03 17:00:17 +01002740static struct iommu_ops amd_iommu_ops = {
2741 .domain_init = amd_iommu_domain_init,
2742 .domain_destroy = amd_iommu_domain_destroy,
2743 .attach_dev = amd_iommu_attach_device,
2744 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01002745 .map = amd_iommu_map,
2746 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002747 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002748 .domain_has_cap = amd_iommu_domain_has_cap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002749};
2750
Joerg Roedel0feae532009-08-26 15:26:30 +02002751/*****************************************************************************
2752 *
2753 * The next functions do a basic initialization of IOMMU for pass through
2754 * mode
2755 *
2756 * In passthrough mode the IOMMU is initialized and enabled but not used for
2757 * DMA-API translation.
2758 *
2759 *****************************************************************************/
2760
2761int __init amd_iommu_init_passthrough(void)
2762{
Joerg Roedel15898bb2009-11-24 15:39:42 +01002763 struct amd_iommu *iommu;
Joerg Roedel0feae532009-08-26 15:26:30 +02002764 struct pci_dev *dev = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002765 u16 devid;
Joerg Roedel0feae532009-08-26 15:26:30 +02002766
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002767 /* allocate passthrough domain */
Joerg Roedel0feae532009-08-26 15:26:30 +02002768 pt_domain = protection_domain_alloc();
2769 if (!pt_domain)
2770 return -ENOMEM;
2771
2772 pt_domain->mode |= PAGE_MODE_NONE;
2773
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04002774 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002775 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02002776 continue;
2777
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002778 devid = get_device_id(&dev->dev);
2779
Joerg Roedel15898bb2009-11-24 15:39:42 +01002780 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02002781 if (!iommu)
2782 continue;
2783
Joerg Roedel15898bb2009-11-24 15:39:42 +01002784 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02002785 }
2786
2787 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
2788
2789 return 0;
2790}