blob: 8b84890846499801dda0d69df7799a278262a112 [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 Roedel8fa5f802011-06-09 12:24:45 +020075static struct iommu_dev_data *alloc_dev_data(void)
76{
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
84 atomic_set(&dev_data->bind, 0);
85
86 spin_lock_irqsave(&dev_data_list_lock, flags);
87 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
88 spin_unlock_irqrestore(&dev_data_list_lock, flags);
89
90 return dev_data;
91}
92
93static void free_dev_data(struct iommu_dev_data *dev_data)
94{
95 unsigned long flags;
96
97 spin_lock_irqsave(&dev_data_list_lock, flags);
98 list_del(&dev_data->dev_data_list);
99 spin_unlock_irqrestore(&dev_data_list_lock, flags);
100
101 kfree(dev_data);
102}
103
Joerg Roedel15898bb2009-11-24 15:39:42 +0100104static inline u16 get_device_id(struct device *dev)
105{
106 struct pci_dev *pdev = to_pci_dev(dev);
107
108 return calc_devid(pdev->bus->number, pdev->devfn);
109}
110
Joerg Roedel657cbb62009-11-23 15:26:46 +0100111static struct iommu_dev_data *get_dev_data(struct device *dev)
112{
113 return dev->archdata.iommu;
114}
115
Joerg Roedel71c70982009-11-24 16:43:06 +0100116/*
117 * In this function the list of preallocated protection domains is traversed to
118 * find the domain for a specific device
119 */
120static struct dma_ops_domain *find_protection_domain(u16 devid)
121{
122 struct dma_ops_domain *entry, *ret = NULL;
123 unsigned long flags;
124 u16 alias = amd_iommu_alias_table[devid];
125
126 if (list_empty(&iommu_pd_list))
127 return NULL;
128
129 spin_lock_irqsave(&iommu_pd_list_lock, flags);
130
131 list_for_each_entry(entry, &iommu_pd_list, list) {
132 if (entry->target_dev == devid ||
133 entry->target_dev == alias) {
134 ret = entry;
135 break;
136 }
137 }
138
139 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
140
141 return ret;
142}
143
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100144/*
145 * This function checks if the driver got a valid device from the caller to
146 * avoid dereferencing invalid pointers.
147 */
148static bool check_device(struct device *dev)
149{
150 u16 devid;
151
152 if (!dev || !dev->dma_mask)
153 return false;
154
155 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100156 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100157 return false;
158
159 devid = get_device_id(dev);
160
161 /* Out of our scope? */
162 if (devid > amd_iommu_last_bdf)
163 return false;
164
165 if (amd_iommu_rlookup_table[devid] == NULL)
166 return false;
167
168 return true;
169}
170
Joerg Roedel657cbb62009-11-23 15:26:46 +0100171static int iommu_init_device(struct device *dev)
172{
173 struct iommu_dev_data *dev_data;
174 struct pci_dev *pdev;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200175 u16 alias;
Joerg Roedel657cbb62009-11-23 15:26:46 +0100176
177 if (dev->archdata.iommu)
178 return 0;
179
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200180 dev_data = alloc_dev_data();
Joerg Roedel657cbb62009-11-23 15:26:46 +0100181 if (!dev_data)
182 return -ENOMEM;
183
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100184 dev_data->dev = dev;
185
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200186 alias = amd_iommu_alias_table[get_device_id(dev)];
Joerg Roedel657cbb62009-11-23 15:26:46 +0100187 pdev = pci_get_bus_and_slot(PCI_BUS(alias), alias & 0xff);
188 if (pdev)
189 dev_data->alias = &pdev->dev;
Joerg Roedel26018872011-06-06 16:50:14 +0200190 else {
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200191 free_dev_data(dev_data);
Joerg Roedel26018872011-06-06 16:50:14 +0200192 return -ENOTSUPP;
193 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100194
195 dev->archdata.iommu = dev_data;
196
Joerg Roedel657cbb62009-11-23 15:26:46 +0100197 return 0;
198}
199
Joerg Roedel26018872011-06-06 16:50:14 +0200200static void iommu_ignore_device(struct device *dev)
201{
202 u16 devid, alias;
203
204 devid = get_device_id(dev);
205 alias = amd_iommu_alias_table[devid];
206
207 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
208 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
209
210 amd_iommu_rlookup_table[devid] = NULL;
211 amd_iommu_rlookup_table[alias] = NULL;
212}
213
Joerg Roedel657cbb62009-11-23 15:26:46 +0100214static void iommu_uninit_device(struct device *dev)
215{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200216 /*
217 * Nothing to do here - we keep dev_data around for unplugged devices
218 * and reuse it when the device is re-plugged - not doing so would
219 * introduce a ton of races.
220 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100221}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100222
223void __init amd_iommu_uninit_devices(void)
224{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200225 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100226 struct pci_dev *pdev = NULL;
227
228 for_each_pci_dev(pdev) {
229
230 if (!check_device(&pdev->dev))
231 continue;
232
233 iommu_uninit_device(&pdev->dev);
234 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200235
236 /* Free all of our dev_data structures */
237 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
238 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100239}
240
241int __init amd_iommu_init_devices(void)
242{
243 struct pci_dev *pdev = NULL;
244 int ret = 0;
245
246 for_each_pci_dev(pdev) {
247
248 if (!check_device(&pdev->dev))
249 continue;
250
251 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200252 if (ret == -ENOTSUPP)
253 iommu_ignore_device(&pdev->dev);
254 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100255 goto out_free;
256 }
257
258 return 0;
259
260out_free:
261
262 amd_iommu_uninit_devices();
263
264 return ret;
265}
Joerg Roedel7f265082008-12-12 13:50:21 +0100266#ifdef CONFIG_AMD_IOMMU_STATS
267
268/*
269 * Initialization code for statistics collection
270 */
271
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100272DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100273DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100274DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100275DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100276DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100277DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100278DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100279DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100280DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100281DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100282DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100283DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100284
Joerg Roedel7f265082008-12-12 13:50:21 +0100285static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100286static struct dentry *de_fflush;
287
288static void amd_iommu_stats_add(struct __iommu_counter *cnt)
289{
290 if (stats_dir == NULL)
291 return;
292
293 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
294 &cnt->value);
295}
296
297static void amd_iommu_stats_init(void)
298{
299 stats_dir = debugfs_create_dir("amd-iommu", NULL);
300 if (stats_dir == NULL)
301 return;
302
Joerg Roedel7f265082008-12-12 13:50:21 +0100303 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
304 (u32 *)&amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100305
306 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100307 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100308 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100309 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100310 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100311 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100312 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100313 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100314 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100315 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100316 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100317 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100318}
319
320#endif
321
Joerg Roedel431b2a22008-07-11 17:14:22 +0200322/****************************************************************************
323 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200324 * Interrupt handling functions
325 *
326 ****************************************************************************/
327
Joerg Roedele3e59872009-09-03 14:02:10 +0200328static void dump_dte_entry(u16 devid)
329{
330 int i;
331
332 for (i = 0; i < 8; ++i)
333 pr_err("AMD-Vi: DTE[%d]: %08x\n", i,
334 amd_iommu_dev_table[devid].data[i]);
335}
336
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200337static void dump_command(unsigned long phys_addr)
338{
339 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
340 int i;
341
342 for (i = 0; i < 4; ++i)
343 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
344}
345
Joerg Roedela345b232009-09-03 15:01:43 +0200346static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200347{
348 u32 *event = __evt;
349 int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
350 int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
351 int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
352 int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
353 u64 address = (u64)(((u64)event[3]) << 32) | event[2];
354
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200355 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200356
357 switch (type) {
358 case EVENT_TYPE_ILL_DEV:
359 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
360 "address=0x%016llx flags=0x%04x]\n",
361 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
362 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200363 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200364 break;
365 case EVENT_TYPE_IO_FAULT:
366 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
367 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
368 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
369 domid, address, flags);
370 break;
371 case EVENT_TYPE_DEV_TAB_ERR:
372 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
373 "address=0x%016llx flags=0x%04x]\n",
374 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
375 address, flags);
376 break;
377 case EVENT_TYPE_PAGE_TAB_ERR:
378 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
379 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
380 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
381 domid, address, flags);
382 break;
383 case EVENT_TYPE_ILL_CMD:
384 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200385 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200386 break;
387 case EVENT_TYPE_CMD_HARD_ERR:
388 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
389 "flags=0x%04x]\n", address, flags);
390 break;
391 case EVENT_TYPE_IOTLB_INV_TO:
392 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
393 "address=0x%016llx]\n",
394 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
395 address);
396 break;
397 case EVENT_TYPE_INV_DEV_REQ:
398 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
399 "address=0x%016llx flags=0x%04x]\n",
400 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
401 address, flags);
402 break;
403 default:
404 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
405 }
406}
407
408static void iommu_poll_events(struct amd_iommu *iommu)
409{
410 u32 head, tail;
411 unsigned long flags;
412
413 spin_lock_irqsave(&iommu->lock, flags);
414
415 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
416 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
417
418 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200419 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200420 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
421 }
422
423 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
424
425 spin_unlock_irqrestore(&iommu->lock, flags);
426}
427
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200428irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200429{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200430 struct amd_iommu *iommu;
431
Joerg Roedel3bd22172009-05-04 15:06:20 +0200432 for_each_iommu(iommu)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200433 iommu_poll_events(iommu);
434
435 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200436}
437
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200438irqreturn_t amd_iommu_int_handler(int irq, void *data)
439{
440 return IRQ_WAKE_THREAD;
441}
442
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200443/****************************************************************************
444 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200445 * IOMMU command queuing functions
446 *
447 ****************************************************************************/
448
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200449static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200450{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200451 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200452
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200453 while (*sem == 0 && i < LOOP_TIMEOUT) {
454 udelay(1);
455 i += 1;
456 }
457
458 if (i == LOOP_TIMEOUT) {
459 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
460 return -EIO;
461 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200462
463 return 0;
464}
465
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200466static void copy_cmd_to_buffer(struct amd_iommu *iommu,
467 struct iommu_cmd *cmd,
468 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200469{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200470 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200471
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200472 target = iommu->cmd_buf + tail;
473 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200474
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200475 /* Copy command to buffer */
476 memcpy(target, cmd, sizeof(*cmd));
477
478 /* Tell the IOMMU about it */
479 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
480}
481
Joerg Roedel815b33f2011-04-06 17:26:49 +0200482static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200483{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200484 WARN_ON(address & 0x7ULL);
485
Joerg Roedelded46732011-04-06 10:53:48 +0200486 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200487 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
488 cmd->data[1] = upper_32_bits(__pa(address));
489 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200490 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
491}
492
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200493static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
494{
495 memset(cmd, 0, sizeof(*cmd));
496 cmd->data[0] = devid;
497 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
498}
499
Joerg Roedel11b64022011-04-06 11:49:28 +0200500static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
501 size_t size, u16 domid, int pde)
502{
503 u64 pages;
504 int s;
505
506 pages = iommu_num_pages(address, size, PAGE_SIZE);
507 s = 0;
508
509 if (pages > 1) {
510 /*
511 * If we have to flush more than one page, flush all
512 * TLB entries for this domain
513 */
514 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
515 s = 1;
516 }
517
518 address &= PAGE_MASK;
519
520 memset(cmd, 0, sizeof(*cmd));
521 cmd->data[1] |= domid;
522 cmd->data[2] = lower_32_bits(address);
523 cmd->data[3] = upper_32_bits(address);
524 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
525 if (s) /* size bit - we flush more than one 4kb page */
526 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
527 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
528 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
529}
530
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200531static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
532 u64 address, size_t size)
533{
534 u64 pages;
535 int s;
536
537 pages = iommu_num_pages(address, size, PAGE_SIZE);
538 s = 0;
539
540 if (pages > 1) {
541 /*
542 * If we have to flush more than one page, flush all
543 * TLB entries for this domain
544 */
545 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
546 s = 1;
547 }
548
549 address &= PAGE_MASK;
550
551 memset(cmd, 0, sizeof(*cmd));
552 cmd->data[0] = devid;
553 cmd->data[0] |= (qdep & 0xff) << 24;
554 cmd->data[1] = devid;
555 cmd->data[2] = lower_32_bits(address);
556 cmd->data[3] = upper_32_bits(address);
557 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
558 if (s)
559 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
560}
561
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200562static void build_inv_all(struct iommu_cmd *cmd)
563{
564 memset(cmd, 0, sizeof(*cmd));
565 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200566}
567
Joerg Roedel431b2a22008-07-11 17:14:22 +0200568/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200569 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200570 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200571 */
Joerg Roedel815b33f2011-04-06 17:26:49 +0200572static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200573{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200574 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200575 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200576
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200577 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100578
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200579again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200580 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200581
582 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
583 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
584 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
585 left = (head - next_tail) % iommu->cmd_buf_size;
586
587 if (left <= 2) {
588 struct iommu_cmd sync_cmd;
589 volatile u64 sem = 0;
590 int ret;
591
592 build_completion_wait(&sync_cmd, (u64)&sem);
593 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
594
595 spin_unlock_irqrestore(&iommu->lock, flags);
596
597 if ((ret = wait_on_sem(&sem)) != 0)
598 return ret;
599
600 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200601 }
602
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200603 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200604
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200605 /* We need to sync now to make sure all commands are processed */
Joerg Roedel815b33f2011-04-06 17:26:49 +0200606 iommu->need_sync = true;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200607
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200608 spin_unlock_irqrestore(&iommu->lock, flags);
609
Joerg Roedel815b33f2011-04-06 17:26:49 +0200610 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100611}
612
613/*
614 * This function queues a completion wait command into the command
615 * buffer of an IOMMU
616 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100617static int iommu_completion_wait(struct amd_iommu *iommu)
618{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200619 struct iommu_cmd cmd;
620 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200621 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100622
623 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200624 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100625
Joerg Roedel815b33f2011-04-06 17:26:49 +0200626 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100627
Joerg Roedel815b33f2011-04-06 17:26:49 +0200628 ret = iommu_queue_command(iommu, &cmd);
Joerg Roedel8d201962008-12-02 20:34:41 +0100629 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200630 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100631
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200632 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200633}
634
Joerg Roedeld8c13082011-04-06 18:51:26 +0200635static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200636{
637 struct iommu_cmd cmd;
638
Joerg Roedeld8c13082011-04-06 18:51:26 +0200639 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200640
Joerg Roedeld8c13082011-04-06 18:51:26 +0200641 return iommu_queue_command(iommu, &cmd);
642}
643
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200644static void iommu_flush_dte_all(struct amd_iommu *iommu)
645{
646 u32 devid;
647
648 for (devid = 0; devid <= 0xffff; ++devid)
649 iommu_flush_dte(iommu, devid);
650
651 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200652}
653
654/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200655 * This function uses heavy locking and may disable irqs for some time. But
656 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200657 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200658static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200659{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200660 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200661
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200662 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
663 struct iommu_cmd cmd;
664 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
665 dom_id, 1);
666 iommu_queue_command(iommu, &cmd);
667 }
Joerg Roedel431b2a22008-07-11 17:14:22 +0200668
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200669 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200670}
671
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200672static void iommu_flush_all(struct amd_iommu *iommu)
673{
674 struct iommu_cmd cmd;
675
676 build_inv_all(&cmd);
677
678 iommu_queue_command(iommu, &cmd);
679 iommu_completion_wait(iommu);
680}
681
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200682void iommu_flush_all_caches(struct amd_iommu *iommu)
683{
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200684 if (iommu_feature(iommu, FEATURE_IA)) {
685 iommu_flush_all(iommu);
686 } else {
687 iommu_flush_dte_all(iommu);
688 iommu_flush_tlb_all(iommu);
689 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200690}
691
Joerg Roedel431b2a22008-07-11 17:14:22 +0200692/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200693 * Command send function for flushing on-device TLB
694 */
695static int device_flush_iotlb(struct device *dev, u64 address, size_t size)
696{
697 struct pci_dev *pdev = to_pci_dev(dev);
698 struct amd_iommu *iommu;
699 struct iommu_cmd cmd;
700 u16 devid;
701 int qdep;
702
703 qdep = pci_ats_queue_depth(pdev);
704 devid = get_device_id(dev);
705 iommu = amd_iommu_rlookup_table[devid];
706
707 build_inv_iotlb_pages(&cmd, devid, qdep, address, size);
708
709 return iommu_queue_command(iommu, &cmd);
710}
711
712/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200713 * Command send function for invalidating a device table entry
714 */
Joerg Roedeld8c13082011-04-06 18:51:26 +0200715static int device_flush_dte(struct device *dev)
Joerg Roedel3fa43652009-11-26 15:04:38 +0100716{
717 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200718 struct pci_dev *pdev;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100719 u16 devid;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200720 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100721
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200722 pdev = to_pci_dev(dev);
Joerg Roedel3fa43652009-11-26 15:04:38 +0100723 devid = get_device_id(dev);
724 iommu = amd_iommu_rlookup_table[devid];
725
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200726 ret = iommu_flush_dte(iommu, devid);
727 if (ret)
728 return ret;
729
730 if (pci_ats_enabled(pdev))
731 ret = device_flush_iotlb(dev, 0, ~0UL);
732
733 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100734}
735
Joerg Roedel431b2a22008-07-11 17:14:22 +0200736/*
737 * TLB invalidation function which is called from the mapping functions.
738 * It invalidates a single PTE if the range to flush is within a single
739 * page. Otherwise it flushes the whole TLB of the IOMMU.
740 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200741static void __domain_flush_pages(struct protection_domain *domain,
742 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200743{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200744 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +0200745 struct iommu_cmd cmd;
746 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200747
Joerg Roedel11b64022011-04-06 11:49:28 +0200748 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +0200749
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100750 for (i = 0; i < amd_iommus_present; ++i) {
751 if (!domain->dev_iommu[i])
752 continue;
753
754 /*
755 * Devices of this domain are behind this IOMMU
756 * We need a TLB flush
757 */
Joerg Roedel11b64022011-04-06 11:49:28 +0200758 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100759 }
760
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200761 list_for_each_entry(dev_data, &domain->dev_list, list) {
762 struct pci_dev *pdev = to_pci_dev(dev_data->dev);
763
764 if (!pci_ats_enabled(pdev))
765 continue;
766
767 ret |= device_flush_iotlb(dev_data->dev, address, size);
768 }
769
Joerg Roedel11b64022011-04-06 11:49:28 +0200770 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100771}
772
Joerg Roedel17b124b2011-04-06 18:01:35 +0200773static void domain_flush_pages(struct protection_domain *domain,
774 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100775{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200776 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200777}
Joerg Roedelb6c02712008-06-26 21:27:53 +0200778
Joerg Roedel1c655772008-09-04 18:40:05 +0200779/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200780static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +0200781{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200782 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +0200783}
784
Chris Wright42a49f92009-06-15 15:42:00 +0200785/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200786static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +0200787{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200788 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
789}
790
791static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +0200792{
793 int i;
794
795 for (i = 0; i < amd_iommus_present; ++i) {
796 if (!domain->dev_iommu[i])
797 continue;
798
799 /*
800 * Devices of this domain are behind this IOMMU
801 * We need to wait for completion of all commands.
802 */
803 iommu_completion_wait(amd_iommus[i]);
804 }
805}
806
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100807
Joerg Roedel43f49602008-12-02 21:01:12 +0100808/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100809 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +0100810 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200811static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200812{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100813 struct iommu_dev_data *dev_data;
814 unsigned long flags;
815
816 spin_lock_irqsave(&domain->lock, flags);
817
818 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedeld8c13082011-04-06 18:51:26 +0200819 device_flush_dte(dev_data->dev);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100820
821 spin_unlock_irqrestore(&domain->lock, flags);
822}
823
Joerg Roedel431b2a22008-07-11 17:14:22 +0200824/****************************************************************************
825 *
826 * The functions below are used the create the page table mappings for
827 * unity mapped regions.
828 *
829 ****************************************************************************/
830
831/*
Joerg Roedel308973d2009-11-24 17:43:32 +0100832 * This function is used to add another level to an IO page table. Adding
833 * another level increases the size of the address space by 9 bits to a size up
834 * to 64 bits.
835 */
836static bool increase_address_space(struct protection_domain *domain,
837 gfp_t gfp)
838{
839 u64 *pte;
840
841 if (domain->mode == PAGE_MODE_6_LEVEL)
842 /* address space already 64 bit large */
843 return false;
844
845 pte = (void *)get_zeroed_page(gfp);
846 if (!pte)
847 return false;
848
849 *pte = PM_LEVEL_PDE(domain->mode,
850 virt_to_phys(domain->pt_root));
851 domain->pt_root = pte;
852 domain->mode += 1;
853 domain->updated = true;
854
855 return true;
856}
857
858static u64 *alloc_pte(struct protection_domain *domain,
859 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100860 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +0100861 u64 **pte_page,
862 gfp_t gfp)
863{
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100864 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +0100865 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100866
867 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +0100868
869 while (address > PM_LEVEL_SIZE(domain->mode))
870 increase_address_space(domain, gfp);
871
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100872 level = domain->mode - 1;
873 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
874 address = PAGE_SIZE_ALIGN(address, page_size);
875 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +0100876
877 while (level > end_lvl) {
878 if (!IOMMU_PTE_PRESENT(*pte)) {
879 page = (u64 *)get_zeroed_page(gfp);
880 if (!page)
881 return NULL;
882 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
883 }
884
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100885 /* No level skipping support yet */
886 if (PM_PTE_LEVEL(*pte) != level)
887 return NULL;
888
Joerg Roedel308973d2009-11-24 17:43:32 +0100889 level -= 1;
890
891 pte = IOMMU_PTE_PAGE(*pte);
892
893 if (pte_page && level == end_lvl)
894 *pte_page = pte;
895
896 pte = &pte[PM_LEVEL_INDEX(level, address)];
897 }
898
899 return pte;
900}
901
902/*
903 * This function checks if there is a PTE for a given dma address. If
904 * there is one, it returns the pointer to it.
905 */
Joerg Roedel24cd7722010-01-19 17:27:39 +0100906static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +0100907{
908 int level;
909 u64 *pte;
910
Joerg Roedel24cd7722010-01-19 17:27:39 +0100911 if (address > PM_LEVEL_SIZE(domain->mode))
912 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +0100913
Joerg Roedel24cd7722010-01-19 17:27:39 +0100914 level = domain->mode - 1;
915 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
916
917 while (level > 0) {
918
919 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +0100920 if (!IOMMU_PTE_PRESENT(*pte))
921 return NULL;
922
Joerg Roedel24cd7722010-01-19 17:27:39 +0100923 /* Large PTE */
924 if (PM_PTE_LEVEL(*pte) == 0x07) {
925 unsigned long pte_mask, __pte;
926
927 /*
928 * If we have a series of large PTEs, make
929 * sure to return a pointer to the first one.
930 */
931 pte_mask = PTE_PAGE_SIZE(*pte);
932 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
933 __pte = ((unsigned long)pte) & pte_mask;
934
935 return (u64 *)__pte;
936 }
937
938 /* No level skipping support yet */
939 if (PM_PTE_LEVEL(*pte) != level)
940 return NULL;
941
Joerg Roedel308973d2009-11-24 17:43:32 +0100942 level -= 1;
943
Joerg Roedel24cd7722010-01-19 17:27:39 +0100944 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +0100945 pte = IOMMU_PTE_PAGE(*pte);
946 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +0100947 }
948
949 return pte;
950}
951
952/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200953 * Generic mapping functions. It maps a physical address into a DMA
954 * address space. It allocates the page table pages if necessary.
955 * In the future it can be extended to a generic mapping function
956 * supporting all features of AMD IOMMU page tables like level skipping
957 * and full 64 bit address spaces.
958 */
Joerg Roedel38e817f2008-12-02 17:27:52 +0100959static int iommu_map_page(struct protection_domain *dom,
960 unsigned long bus_addr,
961 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200962 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100963 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200964{
Joerg Roedel8bda3092009-05-12 12:02:46 +0200965 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100966 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200967
Joerg Roedelbad1cac2009-09-02 16:52:23 +0200968 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200969 return -EINVAL;
970
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100971 bus_addr = PAGE_ALIGN(bus_addr);
972 phys_addr = PAGE_ALIGN(phys_addr);
973 count = PAGE_SIZE_PTE_COUNT(page_size);
974 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200975
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100976 for (i = 0; i < count; ++i)
977 if (IOMMU_PTE_PRESENT(pte[i]))
978 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200979
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100980 if (page_size > PAGE_SIZE) {
981 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
982 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
983 } else
984 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
985
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200986 if (prot & IOMMU_PROT_IR)
987 __pte |= IOMMU_PTE_IR;
988 if (prot & IOMMU_PROT_IW)
989 __pte |= IOMMU_PTE_IW;
990
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100991 for (i = 0; i < count; ++i)
992 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200993
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200994 update_domain(dom);
995
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200996 return 0;
997}
998
Joerg Roedel24cd7722010-01-19 17:27:39 +0100999static unsigned long iommu_unmap_page(struct protection_domain *dom,
1000 unsigned long bus_addr,
1001 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001002{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001003 unsigned long long unmap_size, unmapped;
1004 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001005
Joerg Roedel24cd7722010-01-19 17:27:39 +01001006 BUG_ON(!is_power_of_2(page_size));
1007
1008 unmapped = 0;
1009
1010 while (unmapped < page_size) {
1011
1012 pte = fetch_pte(dom, bus_addr);
1013
1014 if (!pte) {
1015 /*
1016 * No PTE for this address
1017 * move forward in 4kb steps
1018 */
1019 unmap_size = PAGE_SIZE;
1020 } else if (PM_PTE_LEVEL(*pte) == 0) {
1021 /* 4kb PTE found for this address */
1022 unmap_size = PAGE_SIZE;
1023 *pte = 0ULL;
1024 } else {
1025 int count, i;
1026
1027 /* Large PTE found which maps this address */
1028 unmap_size = PTE_PAGE_SIZE(*pte);
1029 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1030 for (i = 0; i < count; i++)
1031 pte[i] = 0ULL;
1032 }
1033
1034 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1035 unmapped += unmap_size;
1036 }
1037
1038 BUG_ON(!is_power_of_2(unmapped));
1039
1040 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001041}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001042
Joerg Roedel431b2a22008-07-11 17:14:22 +02001043/*
1044 * This function checks if a specific unity mapping entry is needed for
1045 * this specific IOMMU.
1046 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001047static int iommu_for_unity_map(struct amd_iommu *iommu,
1048 struct unity_map_entry *entry)
1049{
1050 u16 bdf, i;
1051
1052 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1053 bdf = amd_iommu_alias_table[i];
1054 if (amd_iommu_rlookup_table[bdf] == iommu)
1055 return 1;
1056 }
1057
1058 return 0;
1059}
1060
Joerg Roedel431b2a22008-07-11 17:14:22 +02001061/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001062 * This function actually applies the mapping to the page table of the
1063 * dma_ops domain.
1064 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001065static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1066 struct unity_map_entry *e)
1067{
1068 u64 addr;
1069 int ret;
1070
1071 for (addr = e->address_start; addr < e->address_end;
1072 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001073 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001074 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001075 if (ret)
1076 return ret;
1077 /*
1078 * if unity mapping is in aperture range mark the page
1079 * as allocated in the aperture
1080 */
1081 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001082 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001083 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001084 }
1085
1086 return 0;
1087}
1088
Joerg Roedel431b2a22008-07-11 17:14:22 +02001089/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001090 * Init the unity mappings for a specific IOMMU in the system
1091 *
1092 * Basically iterates over all unity mapping entries and applies them to
1093 * the default domain DMA of that IOMMU if necessary.
1094 */
1095static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1096{
1097 struct unity_map_entry *entry;
1098 int ret;
1099
1100 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1101 if (!iommu_for_unity_map(iommu, entry))
1102 continue;
1103 ret = dma_ops_unity_map(iommu->default_dom, entry);
1104 if (ret)
1105 return ret;
1106 }
1107
1108 return 0;
1109}
1110
1111/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001112 * Inits the unity mappings required for a specific device
1113 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001114static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1115 u16 devid)
1116{
1117 struct unity_map_entry *e;
1118 int ret;
1119
1120 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1121 if (!(devid >= e->devid_start && devid <= e->devid_end))
1122 continue;
1123 ret = dma_ops_unity_map(dma_dom, e);
1124 if (ret)
1125 return ret;
1126 }
1127
1128 return 0;
1129}
1130
Joerg Roedel431b2a22008-07-11 17:14:22 +02001131/****************************************************************************
1132 *
1133 * The next functions belong to the address allocator for the dma_ops
1134 * interface functions. They work like the allocators in the other IOMMU
1135 * drivers. Its basically a bitmap which marks the allocated pages in
1136 * the aperture. Maybe it could be enhanced in the future to a more
1137 * efficient allocator.
1138 *
1139 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001140
Joerg Roedel431b2a22008-07-11 17:14:22 +02001141/*
Joerg Roedel384de722009-05-15 12:30:05 +02001142 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001143 *
1144 * called with domain->lock held
1145 */
Joerg Roedel384de722009-05-15 12:30:05 +02001146
Joerg Roedel9cabe892009-05-18 16:38:55 +02001147/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001148 * Used to reserve address ranges in the aperture (e.g. for exclusion
1149 * ranges.
1150 */
1151static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1152 unsigned long start_page,
1153 unsigned int pages)
1154{
1155 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1156
1157 if (start_page + pages > last_page)
1158 pages = last_page - start_page;
1159
1160 for (i = start_page; i < start_page + pages; ++i) {
1161 int index = i / APERTURE_RANGE_PAGES;
1162 int page = i % APERTURE_RANGE_PAGES;
1163 __set_bit(page, dom->aperture[index]->bitmap);
1164 }
1165}
1166
1167/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001168 * This function is used to add a new aperture range to an existing
1169 * aperture in case of dma_ops domain allocation or address allocation
1170 * failure.
1171 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001172static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001173 bool populate, gfp_t gfp)
1174{
1175 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001176 struct amd_iommu *iommu;
Joerg Roedeld91afd12010-01-22 16:40:20 +01001177 unsigned long i;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001178
Joerg Roedelf5e97052009-05-22 12:31:53 +02001179#ifdef CONFIG_IOMMU_STRESS
1180 populate = false;
1181#endif
1182
Joerg Roedel9cabe892009-05-18 16:38:55 +02001183 if (index >= APERTURE_MAX_RANGES)
1184 return -ENOMEM;
1185
1186 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1187 if (!dma_dom->aperture[index])
1188 return -ENOMEM;
1189
1190 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1191 if (!dma_dom->aperture[index]->bitmap)
1192 goto out_free;
1193
1194 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1195
1196 if (populate) {
1197 unsigned long address = dma_dom->aperture_size;
1198 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1199 u64 *pte, *pte_page;
1200
1201 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001202 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001203 &pte_page, gfp);
1204 if (!pte)
1205 goto out_free;
1206
1207 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1208
1209 address += APERTURE_RANGE_SIZE / 64;
1210 }
1211 }
1212
1213 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1214
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001215 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001216 for_each_iommu(iommu) {
1217 if (iommu->exclusion_start &&
1218 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1219 && iommu->exclusion_start < dma_dom->aperture_size) {
1220 unsigned long startpage;
1221 int pages = iommu_num_pages(iommu->exclusion_start,
1222 iommu->exclusion_length,
1223 PAGE_SIZE);
1224 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1225 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1226 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001227 }
1228
1229 /*
1230 * Check for areas already mapped as present in the new aperture
1231 * range and mark those pages as reserved in the allocator. Such
1232 * mappings may already exist as a result of requested unity
1233 * mappings for devices.
1234 */
1235 for (i = dma_dom->aperture[index]->offset;
1236 i < dma_dom->aperture_size;
1237 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001238 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001239 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1240 continue;
1241
1242 dma_ops_reserve_addresses(dma_dom, i << PAGE_SHIFT, 1);
1243 }
1244
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001245 update_domain(&dma_dom->domain);
1246
Joerg Roedel9cabe892009-05-18 16:38:55 +02001247 return 0;
1248
1249out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001250 update_domain(&dma_dom->domain);
1251
Joerg Roedel9cabe892009-05-18 16:38:55 +02001252 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1253
1254 kfree(dma_dom->aperture[index]);
1255 dma_dom->aperture[index] = NULL;
1256
1257 return -ENOMEM;
1258}
1259
Joerg Roedel384de722009-05-15 12:30:05 +02001260static unsigned long dma_ops_area_alloc(struct device *dev,
1261 struct dma_ops_domain *dom,
1262 unsigned int pages,
1263 unsigned long align_mask,
1264 u64 dma_mask,
1265 unsigned long start)
1266{
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001267 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001268 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1269 int i = start >> APERTURE_RANGE_SHIFT;
1270 unsigned long boundary_size;
1271 unsigned long address = -1;
1272 unsigned long limit;
1273
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001274 next_bit >>= PAGE_SHIFT;
1275
Joerg Roedel384de722009-05-15 12:30:05 +02001276 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1277 PAGE_SIZE) >> PAGE_SHIFT;
1278
1279 for (;i < max_index; ++i) {
1280 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1281
1282 if (dom->aperture[i]->offset >= dma_mask)
1283 break;
1284
1285 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1286 dma_mask >> PAGE_SHIFT);
1287
1288 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1289 limit, next_bit, pages, 0,
1290 boundary_size, align_mask);
1291 if (address != -1) {
1292 address = dom->aperture[i]->offset +
1293 (address << PAGE_SHIFT);
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001294 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001295 break;
1296 }
1297
1298 next_bit = 0;
1299 }
1300
1301 return address;
1302}
1303
Joerg Roedeld3086442008-06-26 21:27:57 +02001304static unsigned long dma_ops_alloc_addresses(struct device *dev,
1305 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001306 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001307 unsigned long align_mask,
1308 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001309{
Joerg Roedeld3086442008-06-26 21:27:57 +02001310 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001311
Joerg Roedelfe16f082009-05-22 12:27:53 +02001312#ifdef CONFIG_IOMMU_STRESS
1313 dom->next_address = 0;
1314 dom->need_flush = true;
1315#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001316
Joerg Roedel384de722009-05-15 12:30:05 +02001317 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001318 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001319
Joerg Roedel1c655772008-09-04 18:40:05 +02001320 if (address == -1) {
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001321 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001322 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1323 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001324 dom->need_flush = true;
1325 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001326
Joerg Roedel384de722009-05-15 12:30:05 +02001327 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001328 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001329
1330 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1331
1332 return address;
1333}
1334
Joerg Roedel431b2a22008-07-11 17:14:22 +02001335/*
1336 * The address free function.
1337 *
1338 * called with domain->lock held
1339 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001340static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1341 unsigned long address,
1342 unsigned int pages)
1343{
Joerg Roedel384de722009-05-15 12:30:05 +02001344 unsigned i = address >> APERTURE_RANGE_SHIFT;
1345 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001346
Joerg Roedel384de722009-05-15 12:30:05 +02001347 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1348
Joerg Roedel47bccd62009-05-22 12:40:54 +02001349#ifdef CONFIG_IOMMU_STRESS
1350 if (i < 4)
1351 return;
1352#endif
1353
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001354 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001355 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001356
1357 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001358
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001359 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001360
Joerg Roedeld3086442008-06-26 21:27:57 +02001361}
1362
Joerg Roedel431b2a22008-07-11 17:14:22 +02001363/****************************************************************************
1364 *
1365 * The next functions belong to the domain allocation. A domain is
1366 * allocated for every IOMMU as the default domain. If device isolation
1367 * is enabled, every device get its own domain. The most important thing
1368 * about domains is the page table mapping the DMA address space they
1369 * contain.
1370 *
1371 ****************************************************************************/
1372
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001373/*
1374 * This function adds a protection domain to the global protection domain list
1375 */
1376static void add_domain_to_list(struct protection_domain *domain)
1377{
1378 unsigned long flags;
1379
1380 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1381 list_add(&domain->list, &amd_iommu_pd_list);
1382 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1383}
1384
1385/*
1386 * This function removes a protection domain to the global
1387 * protection domain list
1388 */
1389static void del_domain_from_list(struct protection_domain *domain)
1390{
1391 unsigned long flags;
1392
1393 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1394 list_del(&domain->list);
1395 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1396}
1397
Joerg Roedelec487d12008-06-26 21:27:58 +02001398static u16 domain_id_alloc(void)
1399{
1400 unsigned long flags;
1401 int id;
1402
1403 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1404 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1405 BUG_ON(id == 0);
1406 if (id > 0 && id < MAX_DOMAIN_ID)
1407 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1408 else
1409 id = 0;
1410 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1411
1412 return id;
1413}
1414
Joerg Roedela2acfb72008-12-02 18:28:53 +01001415static void domain_id_free(int id)
1416{
1417 unsigned long flags;
1418
1419 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1420 if (id > 0 && id < MAX_DOMAIN_ID)
1421 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1422 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1423}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001424
Joerg Roedel86db2e52008-12-02 18:20:21 +01001425static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001426{
1427 int i, j;
1428 u64 *p1, *p2, *p3;
1429
Joerg Roedel86db2e52008-12-02 18:20:21 +01001430 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001431
1432 if (!p1)
1433 return;
1434
1435 for (i = 0; i < 512; ++i) {
1436 if (!IOMMU_PTE_PRESENT(p1[i]))
1437 continue;
1438
1439 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001440 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001441 if (!IOMMU_PTE_PRESENT(p2[j]))
1442 continue;
1443 p3 = IOMMU_PTE_PAGE(p2[j]);
1444 free_page((unsigned long)p3);
1445 }
1446
1447 free_page((unsigned long)p2);
1448 }
1449
1450 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001451
1452 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001453}
1454
Joerg Roedel431b2a22008-07-11 17:14:22 +02001455/*
1456 * Free a domain, only used if something went wrong in the
1457 * allocation path and we need to free an already allocated page table
1458 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001459static void dma_ops_domain_free(struct dma_ops_domain *dom)
1460{
Joerg Roedel384de722009-05-15 12:30:05 +02001461 int i;
1462
Joerg Roedelec487d12008-06-26 21:27:58 +02001463 if (!dom)
1464 return;
1465
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001466 del_domain_from_list(&dom->domain);
1467
Joerg Roedel86db2e52008-12-02 18:20:21 +01001468 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001469
Joerg Roedel384de722009-05-15 12:30:05 +02001470 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1471 if (!dom->aperture[i])
1472 continue;
1473 free_page((unsigned long)dom->aperture[i]->bitmap);
1474 kfree(dom->aperture[i]);
1475 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001476
1477 kfree(dom);
1478}
1479
Joerg Roedel431b2a22008-07-11 17:14:22 +02001480/*
1481 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001482 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001483 * structures required for the dma_ops interface
1484 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001485static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001486{
1487 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001488
1489 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1490 if (!dma_dom)
1491 return NULL;
1492
1493 spin_lock_init(&dma_dom->domain.lock);
1494
1495 dma_dom->domain.id = domain_id_alloc();
1496 if (dma_dom->domain.id == 0)
1497 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001498 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001499 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001500 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001501 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001502 dma_dom->domain.priv = dma_dom;
1503 if (!dma_dom->domain.pt_root)
1504 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001505
Joerg Roedel1c655772008-09-04 18:40:05 +02001506 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001507 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001508
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001509 add_domain_to_list(&dma_dom->domain);
1510
Joerg Roedel576175c2009-11-23 19:08:46 +01001511 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001512 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001513
Joerg Roedel431b2a22008-07-11 17:14:22 +02001514 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001515 * mark the first page as allocated so we never return 0 as
1516 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001517 */
Joerg Roedel384de722009-05-15 12:30:05 +02001518 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001519 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001520
Joerg Roedelec487d12008-06-26 21:27:58 +02001521
1522 return dma_dom;
1523
1524free_dma_dom:
1525 dma_ops_domain_free(dma_dom);
1526
1527 return NULL;
1528}
1529
Joerg Roedel431b2a22008-07-11 17:14:22 +02001530/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001531 * little helper function to check whether a given protection domain is a
1532 * dma_ops domain
1533 */
1534static bool dma_ops_domain(struct protection_domain *domain)
1535{
1536 return domain->flags & PD_DMA_OPS_MASK;
1537}
1538
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001539static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001540{
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001541 u64 pte_root = virt_to_phys(domain->pt_root);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001542 u32 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001543
Joerg Roedel38ddf412008-09-11 10:38:32 +02001544 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1545 << DEV_ENTRY_MODE_SHIFT;
1546 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001547
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001548 if (ats)
1549 flags |= DTE_FLAG_IOTLB;
1550
1551 amd_iommu_dev_table[devid].data[3] |= flags;
1552 amd_iommu_dev_table[devid].data[2] = domain->id;
1553 amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
1554 amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001555}
1556
Joerg Roedel15898bb2009-11-24 15:39:42 +01001557static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001558{
Joerg Roedel355bf552008-12-08 12:02:41 +01001559 /* remove entry from the device table seen by the hardware */
1560 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1561 amd_iommu_dev_table[devid].data[1] = 0;
1562 amd_iommu_dev_table[devid].data[2] = 0;
1563
Joerg Roedelc5cca142009-10-09 18:31:20 +02001564 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001565}
1566
1567static void do_attach(struct device *dev, struct protection_domain *domain)
1568{
1569 struct iommu_dev_data *dev_data;
1570 struct amd_iommu *iommu;
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001571 struct pci_dev *pdev;
1572 bool ats = false;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001573 u16 devid;
1574
1575 devid = get_device_id(dev);
1576 iommu = amd_iommu_rlookup_table[devid];
1577 dev_data = get_dev_data(dev);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001578 pdev = to_pci_dev(dev);
1579
1580 if (amd_iommu_iotlb_sup)
1581 ats = pci_ats_enabled(pdev);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001582
1583 /* Update data structures */
1584 dev_data->domain = domain;
1585 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001586 set_dte_entry(devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001587
1588 /* Do reference counting */
1589 domain->dev_iommu[iommu->index] += 1;
1590 domain->dev_cnt += 1;
1591
1592 /* Flush the DTE entry */
Joerg Roedeld8c13082011-04-06 18:51:26 +02001593 device_flush_dte(dev);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001594}
1595
1596static void do_detach(struct device *dev)
1597{
1598 struct iommu_dev_data *dev_data;
1599 struct amd_iommu *iommu;
1600 u16 devid;
1601
1602 devid = get_device_id(dev);
1603 iommu = amd_iommu_rlookup_table[devid];
1604 dev_data = get_dev_data(dev);
Joerg Roedelc5cca142009-10-09 18:31:20 +02001605
Joerg Roedelc4596112009-11-20 14:57:32 +01001606 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001607 dev_data->domain->dev_iommu[iommu->index] -= 1;
1608 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001609
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001610 /* Update data structures */
1611 dev_data->domain = NULL;
1612 list_del(&dev_data->list);
1613 clear_dte_entry(devid);
1614
1615 /* Flush the DTE entry */
Joerg Roedeld8c13082011-04-06 18:51:26 +02001616 device_flush_dte(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001617}
1618
1619/*
1620 * If a device is not yet associated with a domain, this function does
1621 * assigns it visible for the hardware
1622 */
1623static int __attach_device(struct device *dev,
1624 struct protection_domain *domain)
1625{
Joerg Roedel657cbb62009-11-23 15:26:46 +01001626 struct iommu_dev_data *dev_data, *alias_data;
Julia Lawall84fe6c12010-05-27 12:31:51 +02001627 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001628
Joerg Roedel657cbb62009-11-23 15:26:46 +01001629 dev_data = get_dev_data(dev);
1630 alias_data = get_dev_data(dev_data->alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001631
Joerg Roedel657cbb62009-11-23 15:26:46 +01001632 if (!alias_data)
1633 return -EINVAL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001634
1635 /* lock domain */
1636 spin_lock(&domain->lock);
1637
1638 /* Some sanity checks */
Julia Lawall84fe6c12010-05-27 12:31:51 +02001639 ret = -EBUSY;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001640 if (alias_data->domain != NULL &&
1641 alias_data->domain != domain)
Julia Lawall84fe6c12010-05-27 12:31:51 +02001642 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001643
Joerg Roedel657cbb62009-11-23 15:26:46 +01001644 if (dev_data->domain != NULL &&
1645 dev_data->domain != domain)
Julia Lawall84fe6c12010-05-27 12:31:51 +02001646 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001647
1648 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001649 if (dev_data->alias != dev) {
1650 alias_data = get_dev_data(dev_data->alias);
1651 if (alias_data->domain == NULL)
1652 do_attach(dev_data->alias, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01001653
1654 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001655 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01001656
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001657 if (dev_data->domain == NULL)
1658 do_attach(dev, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001659
Joerg Roedel24100052009-11-25 15:59:57 +01001660 atomic_inc(&dev_data->bind);
1661
Julia Lawall84fe6c12010-05-27 12:31:51 +02001662 ret = 0;
1663
1664out_unlock:
1665
Joerg Roedel355bf552008-12-08 12:02:41 +01001666 /* ready */
1667 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001668
Julia Lawall84fe6c12010-05-27 12:31:51 +02001669 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001670}
1671
1672/*
1673 * If a device is not yet associated with a domain, this function does
1674 * assigns it visible for the hardware
1675 */
1676static int attach_device(struct device *dev,
1677 struct protection_domain *domain)
1678{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001679 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001680 unsigned long flags;
1681 int ret;
1682
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001683 if (amd_iommu_iotlb_sup)
1684 pci_enable_ats(pdev, PAGE_SHIFT);
1685
Joerg Roedel15898bb2009-11-24 15:39:42 +01001686 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1687 ret = __attach_device(dev, domain);
1688 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1689
1690 /*
1691 * We might boot into a crash-kernel here. The crashed kernel
1692 * left the caches in the IOMMU dirty. So we have to flush
1693 * here to evict all dirty stuff.
1694 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001695 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001696
1697 return ret;
1698}
1699
1700/*
1701 * Removes a device from a protection domain (unlocked)
1702 */
1703static void __detach_device(struct device *dev)
1704{
Joerg Roedel657cbb62009-11-23 15:26:46 +01001705 struct iommu_dev_data *dev_data = get_dev_data(dev);
Joerg Roedel24100052009-11-25 15:59:57 +01001706 struct iommu_dev_data *alias_data;
Joerg Roedel2ca76272010-01-22 16:45:31 +01001707 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001708 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001709
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001710 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001711
Joerg Roedel2ca76272010-01-22 16:45:31 +01001712 domain = dev_data->domain;
1713
1714 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01001715
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001716 if (dev_data->alias != dev) {
Joerg Roedel24100052009-11-25 15:59:57 +01001717 alias_data = get_dev_data(dev_data->alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001718 if (atomic_dec_and_test(&alias_data->bind))
1719 do_detach(dev_data->alias);
Joerg Roedel24100052009-11-25 15:59:57 +01001720 }
1721
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001722 if (atomic_dec_and_test(&dev_data->bind))
1723 do_detach(dev);
1724
Joerg Roedel2ca76272010-01-22 16:45:31 +01001725 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001726
Joerg Roedel21129f72009-09-01 11:59:42 +02001727 /*
1728 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001729 * passthrough domain if it is detached from any other domain.
1730 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02001731 */
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001732 if (iommu_pass_through &&
1733 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedel15898bb2009-11-24 15:39:42 +01001734 __attach_device(dev, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01001735}
1736
1737/*
1738 * Removes a device from a protection domain (with devtable_lock held)
1739 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01001740static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01001741{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001742 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedel355bf552008-12-08 12:02:41 +01001743 unsigned long flags;
1744
1745 /* lock device table */
1746 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001747 __detach_device(dev);
Joerg Roedel355bf552008-12-08 12:02:41 +01001748 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001749
1750 if (amd_iommu_iotlb_sup && pci_ats_enabled(pdev))
1751 pci_disable_ats(pdev);
Joerg Roedel355bf552008-12-08 12:02:41 +01001752}
Joerg Roedele275a2a2008-12-10 18:27:25 +01001753
Joerg Roedel15898bb2009-11-24 15:39:42 +01001754/*
1755 * Find out the protection domain structure for a given PCI device. This
1756 * will give us the pointer to the page table root for example.
1757 */
1758static struct protection_domain *domain_for_device(struct device *dev)
1759{
1760 struct protection_domain *dom;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001761 struct iommu_dev_data *dev_data, *alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001762 unsigned long flags;
Gustavo F. Padovan6ec5ff42011-05-20 16:13:00 -03001763 u16 devid;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001764
Joerg Roedel657cbb62009-11-23 15:26:46 +01001765 devid = get_device_id(dev);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001766 dev_data = get_dev_data(dev);
1767 alias_data = get_dev_data(dev_data->alias);
1768 if (!alias_data)
1769 return NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001770
1771 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001772 dom = dev_data->domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001773 if (dom == NULL &&
Joerg Roedel657cbb62009-11-23 15:26:46 +01001774 alias_data->domain != NULL) {
1775 __attach_device(dev, alias_data->domain);
1776 dom = alias_data->domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001777 }
1778
1779 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1780
1781 return dom;
1782}
1783
Joerg Roedele275a2a2008-12-10 18:27:25 +01001784static int device_change_notifier(struct notifier_block *nb,
1785 unsigned long action, void *data)
1786{
1787 struct device *dev = data;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001788 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001789 struct protection_domain *domain;
1790 struct dma_ops_domain *dma_domain;
1791 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001792 unsigned long flags;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001793
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001794 if (!check_device(dev))
1795 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001796
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001797 devid = get_device_id(dev);
1798 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01001799
1800 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07001801 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001802
1803 domain = domain_for_device(dev);
1804
Joerg Roedele275a2a2008-12-10 18:27:25 +01001805 if (!domain)
1806 goto out;
Joerg Roedela1ca3312009-09-01 12:22:22 +02001807 if (iommu_pass_through)
1808 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001809 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001810 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001811 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001812
1813 iommu_init_device(dev);
1814
1815 domain = domain_for_device(dev);
1816
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001817 /* allocate a protection domain if a device is added */
1818 dma_domain = find_protection_domain(devid);
1819 if (dma_domain)
1820 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01001821 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001822 if (!dma_domain)
1823 goto out;
1824 dma_domain->target_dev = devid;
1825
1826 spin_lock_irqsave(&iommu_pd_list_lock, flags);
1827 list_add_tail(&dma_domain->list, &iommu_pd_list);
1828 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
1829
1830 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001831 case BUS_NOTIFY_DEL_DEVICE:
1832
1833 iommu_uninit_device(dev);
1834
Joerg Roedele275a2a2008-12-10 18:27:25 +01001835 default:
1836 goto out;
1837 }
1838
Joerg Roedele275a2a2008-12-10 18:27:25 +01001839 iommu_completion_wait(iommu);
1840
1841out:
1842 return 0;
1843}
1844
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05301845static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01001846 .notifier_call = device_change_notifier,
1847};
Joerg Roedel355bf552008-12-08 12:02:41 +01001848
Joerg Roedel8638c492009-12-10 11:12:25 +01001849void amd_iommu_init_notifier(void)
1850{
1851 bus_register_notifier(&pci_bus_type, &device_nb);
1852}
1853
Joerg Roedel431b2a22008-07-11 17:14:22 +02001854/*****************************************************************************
1855 *
1856 * The next functions belong to the dma_ops mapping/unmapping code.
1857 *
1858 *****************************************************************************/
1859
1860/*
1861 * In the dma_ops path we only have the struct device. This function
1862 * finds the corresponding IOMMU, the protection domain and the
1863 * requestor id for a given device.
1864 * If the device is not yet associated with a domain this is also done
1865 * in this function.
1866 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001867static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001868{
Joerg Roedel94f6d192009-11-24 16:40:02 +01001869 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001870 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01001871 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001872
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001873 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01001874 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001875
Joerg Roedel94f6d192009-11-24 16:40:02 +01001876 domain = domain_for_device(dev);
1877 if (domain != NULL && !dma_ops_domain(domain))
1878 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001879
Joerg Roedel94f6d192009-11-24 16:40:02 +01001880 if (domain != NULL)
1881 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001882
Joerg Roedel15898bb2009-11-24 15:39:42 +01001883 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001884 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001885 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01001886 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
1887 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001888 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01001889 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01001890
Joerg Roedel94f6d192009-11-24 16:40:02 +01001891 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001892}
1893
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001894static void update_device_table(struct protection_domain *domain)
1895{
Joerg Roedel492667d2009-11-27 13:25:47 +01001896 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001897
Joerg Roedel492667d2009-11-27 13:25:47 +01001898 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001899 struct pci_dev *pdev = to_pci_dev(dev_data->dev);
Joerg Roedel492667d2009-11-27 13:25:47 +01001900 u16 devid = get_device_id(dev_data->dev);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001901 set_dte_entry(devid, domain, pci_ats_enabled(pdev));
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001902 }
1903}
1904
1905static void update_domain(struct protection_domain *domain)
1906{
1907 if (!domain->updated)
1908 return;
1909
1910 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02001911
1912 domain_flush_devices(domain);
1913 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001914
1915 domain->updated = false;
1916}
1917
Joerg Roedel431b2a22008-07-11 17:14:22 +02001918/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02001919 * This function fetches the PTE for a given address in the aperture
1920 */
1921static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
1922 unsigned long address)
1923{
Joerg Roedel384de722009-05-15 12:30:05 +02001924 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02001925 u64 *pte, *pte_page;
1926
Joerg Roedel384de722009-05-15 12:30:05 +02001927 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1928 if (!aperture)
1929 return NULL;
1930
1931 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02001932 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001933 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001934 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02001935 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
1936 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001937 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001938
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001939 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001940
1941 return pte;
1942}
1943
1944/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001945 * This is the generic map function. It maps one 4kb page at paddr to
1946 * the given address in the DMA address space for the domain.
1947 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001948static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001949 unsigned long address,
1950 phys_addr_t paddr,
1951 int direction)
1952{
1953 u64 *pte, __pte;
1954
1955 WARN_ON(address > dom->aperture_size);
1956
1957 paddr &= PAGE_MASK;
1958
Joerg Roedel8bda3092009-05-12 12:02:46 +02001959 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02001960 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001961 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001962
1963 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
1964
1965 if (direction == DMA_TO_DEVICE)
1966 __pte |= IOMMU_PTE_IR;
1967 else if (direction == DMA_FROM_DEVICE)
1968 __pte |= IOMMU_PTE_IW;
1969 else if (direction == DMA_BIDIRECTIONAL)
1970 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
1971
1972 WARN_ON(*pte);
1973
1974 *pte = __pte;
1975
1976 return (dma_addr_t)address;
1977}
1978
Joerg Roedel431b2a22008-07-11 17:14:22 +02001979/*
1980 * The generic unmapping function for on page in the DMA address space.
1981 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001982static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001983 unsigned long address)
1984{
Joerg Roedel384de722009-05-15 12:30:05 +02001985 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001986 u64 *pte;
1987
1988 if (address >= dom->aperture_size)
1989 return;
1990
Joerg Roedel384de722009-05-15 12:30:05 +02001991 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1992 if (!aperture)
1993 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001994
Joerg Roedel384de722009-05-15 12:30:05 +02001995 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
1996 if (!pte)
1997 return;
1998
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001999 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002000
2001 WARN_ON(!*pte);
2002
2003 *pte = 0ULL;
2004}
2005
Joerg Roedel431b2a22008-07-11 17:14:22 +02002006/*
2007 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002008 * contiguous memory region into DMA address space. It is used by all
2009 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002010 * Must be called with the domain lock held.
2011 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002012static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002013 struct dma_ops_domain *dma_dom,
2014 phys_addr_t paddr,
2015 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002016 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002017 bool align,
2018 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002019{
2020 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002021 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002022 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002023 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002024 int i;
2025
Joerg Roedele3c449f2008-10-15 22:02:11 -07002026 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002027 paddr &= PAGE_MASK;
2028
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002029 INC_STATS_COUNTER(total_map_requests);
2030
Joerg Roedelc1858972008-12-12 15:42:39 +01002031 if (pages > 1)
2032 INC_STATS_COUNTER(cross_page);
2033
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002034 if (align)
2035 align_mask = (1UL << get_order(size)) - 1;
2036
Joerg Roedel11b83882009-05-19 10:23:15 +02002037retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002038 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2039 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002040 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002041 /*
2042 * setting next_address here will let the address
2043 * allocator only scan the new allocated range in the
2044 * first run. This is a small optimization.
2045 */
2046 dma_dom->next_address = dma_dom->aperture_size;
2047
Joerg Roedel576175c2009-11-23 19:08:46 +01002048 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002049 goto out;
2050
2051 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002052 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002053 * allocation again
2054 */
2055 goto retry;
2056 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002057
2058 start = address;
2059 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002060 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002061 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002062 goto out_unmap;
2063
Joerg Roedelcb76c322008-06-26 21:28:00 +02002064 paddr += PAGE_SIZE;
2065 start += PAGE_SIZE;
2066 }
2067 address += offset;
2068
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002069 ADD_STATS_COUNTER(alloced_io_mem, size);
2070
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002071 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002072 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002073 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002074 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002075 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002076
Joerg Roedelcb76c322008-06-26 21:28:00 +02002077out:
2078 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002079
2080out_unmap:
2081
2082 for (--i; i >= 0; --i) {
2083 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002084 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002085 }
2086
2087 dma_ops_free_addresses(dma_dom, address, pages);
2088
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002089 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002090}
2091
Joerg Roedel431b2a22008-07-11 17:14:22 +02002092/*
2093 * Does the reverse of the __map_single function. Must be called with
2094 * the domain lock held too
2095 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002096static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002097 dma_addr_t dma_addr,
2098 size_t size,
2099 int dir)
2100{
Joerg Roedel04e04632010-09-23 16:12:48 +02002101 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002102 dma_addr_t i, start;
2103 unsigned int pages;
2104
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002105 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002106 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002107 return;
2108
Joerg Roedel04e04632010-09-23 16:12:48 +02002109 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002110 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002111 dma_addr &= PAGE_MASK;
2112 start = dma_addr;
2113
2114 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002115 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002116 start += PAGE_SIZE;
2117 }
2118
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002119 SUB_STATS_COUNTER(alloced_io_mem, size);
2120
Joerg Roedelcb76c322008-06-26 21:28:00 +02002121 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002122
Joerg Roedel80be3082008-11-06 14:59:05 +01002123 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002124 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002125 dma_dom->need_flush = false;
2126 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002127}
2128
Joerg Roedel431b2a22008-07-11 17:14:22 +02002129/*
2130 * The exported map_single function for dma_ops.
2131 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002132static dma_addr_t map_page(struct device *dev, struct page *page,
2133 unsigned long offset, size_t size,
2134 enum dma_data_direction dir,
2135 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002136{
2137 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002138 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002139 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002140 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002141 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002142
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002143 INC_STATS_COUNTER(cnt_map_single);
2144
Joerg Roedel94f6d192009-11-24 16:40:02 +01002145 domain = get_domain(dev);
2146 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002147 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002148 else if (IS_ERR(domain))
2149 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002150
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002151 dma_mask = *dev->dma_mask;
2152
Joerg Roedel4da70b92008-06-26 21:28:01 +02002153 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002154
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002155 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002156 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002157 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002158 goto out;
2159
Joerg Roedel17b124b2011-04-06 18:01:35 +02002160 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002161
2162out:
2163 spin_unlock_irqrestore(&domain->lock, flags);
2164
2165 return addr;
2166}
2167
Joerg Roedel431b2a22008-07-11 17:14:22 +02002168/*
2169 * The exported unmap_single function for dma_ops.
2170 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002171static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2172 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002173{
2174 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002175 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002176
Joerg Roedel146a6912008-12-12 15:07:12 +01002177 INC_STATS_COUNTER(cnt_unmap_single);
2178
Joerg Roedel94f6d192009-11-24 16:40:02 +01002179 domain = get_domain(dev);
2180 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002181 return;
2182
Joerg Roedel4da70b92008-06-26 21:28:01 +02002183 spin_lock_irqsave(&domain->lock, flags);
2184
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002185 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002186
Joerg Roedel17b124b2011-04-06 18:01:35 +02002187 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002188
2189 spin_unlock_irqrestore(&domain->lock, flags);
2190}
2191
Joerg Roedel431b2a22008-07-11 17:14:22 +02002192/*
2193 * This is a special map_sg function which is used if we should map a
2194 * device which is not handled by an AMD IOMMU in the system.
2195 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002196static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2197 int nelems, int dir)
2198{
2199 struct scatterlist *s;
2200 int i;
2201
2202 for_each_sg(sglist, s, nelems, i) {
2203 s->dma_address = (dma_addr_t)sg_phys(s);
2204 s->dma_length = s->length;
2205 }
2206
2207 return nelems;
2208}
2209
Joerg Roedel431b2a22008-07-11 17:14:22 +02002210/*
2211 * The exported map_sg function for dma_ops (handles scatter-gather
2212 * lists).
2213 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002214static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002215 int nelems, enum dma_data_direction dir,
2216 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002217{
2218 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002219 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002220 int i;
2221 struct scatterlist *s;
2222 phys_addr_t paddr;
2223 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002224 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002225
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002226 INC_STATS_COUNTER(cnt_map_sg);
2227
Joerg Roedel94f6d192009-11-24 16:40:02 +01002228 domain = get_domain(dev);
2229 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002230 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002231 else if (IS_ERR(domain))
2232 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002233
Joerg Roedel832a90c2008-09-18 15:54:23 +02002234 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002235
Joerg Roedel65b050a2008-06-26 21:28:02 +02002236 spin_lock_irqsave(&domain->lock, flags);
2237
2238 for_each_sg(sglist, s, nelems, i) {
2239 paddr = sg_phys(s);
2240
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002241 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002242 paddr, s->length, dir, false,
2243 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002244
2245 if (s->dma_address) {
2246 s->dma_length = s->length;
2247 mapped_elems++;
2248 } else
2249 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002250 }
2251
Joerg Roedel17b124b2011-04-06 18:01:35 +02002252 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002253
2254out:
2255 spin_unlock_irqrestore(&domain->lock, flags);
2256
2257 return mapped_elems;
2258unmap:
2259 for_each_sg(sglist, s, mapped_elems, i) {
2260 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002261 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002262 s->dma_length, dir);
2263 s->dma_address = s->dma_length = 0;
2264 }
2265
2266 mapped_elems = 0;
2267
2268 goto out;
2269}
2270
Joerg Roedel431b2a22008-07-11 17:14:22 +02002271/*
2272 * The exported map_sg function for dma_ops (handles scatter-gather
2273 * lists).
2274 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002275static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002276 int nelems, enum dma_data_direction dir,
2277 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002278{
2279 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002280 struct protection_domain *domain;
2281 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002282 int i;
2283
Joerg Roedel55877a62008-12-12 15:12:14 +01002284 INC_STATS_COUNTER(cnt_unmap_sg);
2285
Joerg Roedel94f6d192009-11-24 16:40:02 +01002286 domain = get_domain(dev);
2287 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002288 return;
2289
Joerg Roedel65b050a2008-06-26 21:28:02 +02002290 spin_lock_irqsave(&domain->lock, flags);
2291
2292 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002293 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002294 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002295 s->dma_address = s->dma_length = 0;
2296 }
2297
Joerg Roedel17b124b2011-04-06 18:01:35 +02002298 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002299
2300 spin_unlock_irqrestore(&domain->lock, flags);
2301}
2302
Joerg Roedel431b2a22008-07-11 17:14:22 +02002303/*
2304 * The exported alloc_coherent function for dma_ops.
2305 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002306static void *alloc_coherent(struct device *dev, size_t size,
2307 dma_addr_t *dma_addr, gfp_t flag)
2308{
2309 unsigned long flags;
2310 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002311 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002312 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002313 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002314
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002315 INC_STATS_COUNTER(cnt_alloc_coherent);
2316
Joerg Roedel94f6d192009-11-24 16:40:02 +01002317 domain = get_domain(dev);
2318 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002319 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2320 *dma_addr = __pa(virt_addr);
2321 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002322 } else if (IS_ERR(domain))
2323 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002324
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002325 dma_mask = dev->coherent_dma_mask;
2326 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2327 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002328
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002329 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2330 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302331 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002332
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002333 paddr = virt_to_phys(virt_addr);
2334
Joerg Roedel832a90c2008-09-18 15:54:23 +02002335 if (!dma_mask)
2336 dma_mask = *dev->dma_mask;
2337
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002338 spin_lock_irqsave(&domain->lock, flags);
2339
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002340 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002341 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002342
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002343 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002344 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002345 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002346 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002347
Joerg Roedel17b124b2011-04-06 18:01:35 +02002348 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002349
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002350 spin_unlock_irqrestore(&domain->lock, flags);
2351
2352 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002353
2354out_free:
2355
2356 free_pages((unsigned long)virt_addr, get_order(size));
2357
2358 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002359}
2360
Joerg Roedel431b2a22008-07-11 17:14:22 +02002361/*
2362 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002363 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002364static void free_coherent(struct device *dev, size_t size,
2365 void *virt_addr, dma_addr_t dma_addr)
2366{
2367 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002368 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002369
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002370 INC_STATS_COUNTER(cnt_free_coherent);
2371
Joerg Roedel94f6d192009-11-24 16:40:02 +01002372 domain = get_domain(dev);
2373 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002374 goto free_mem;
2375
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002376 spin_lock_irqsave(&domain->lock, flags);
2377
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002378 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002379
Joerg Roedel17b124b2011-04-06 18:01:35 +02002380 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002381
2382 spin_unlock_irqrestore(&domain->lock, flags);
2383
2384free_mem:
2385 free_pages((unsigned long)virt_addr, get_order(size));
2386}
2387
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002388/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002389 * This function is called by the DMA layer to find out if we can handle a
2390 * particular device. It is part of the dma_ops.
2391 */
2392static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2393{
Joerg Roedel420aef82009-11-23 16:14:57 +01002394 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002395}
2396
2397/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002398 * The function for pre-allocating protection domains.
2399 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002400 * If the driver core informs the DMA layer if a driver grabs a device
2401 * we don't need to preallocate the protection domains anymore.
2402 * For now we have to.
2403 */
Jaswinder Singh Rajput0e93dd82008-12-29 21:45:22 +05302404static void prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002405{
2406 struct pci_dev *dev = NULL;
2407 struct dma_ops_domain *dma_dom;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002408 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002409
Chris Wrightd18c69d2010-04-02 18:27:55 -07002410 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002411
2412 /* Do we handle this device? */
2413 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002414 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002415
2416 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002417 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002418 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002419
2420 devid = get_device_id(&dev->dev);
2421
Joerg Roedel87a64d52009-11-24 17:26:43 +01002422 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002423 if (!dma_dom)
2424 continue;
2425 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002426 dma_dom->target_dev = devid;
2427
Joerg Roedel15898bb2009-11-24 15:39:42 +01002428 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01002429
Joerg Roedelbd60b732008-09-11 10:24:48 +02002430 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002431 }
2432}
2433
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002434static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002435 .alloc_coherent = alloc_coherent,
2436 .free_coherent = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002437 .map_page = map_page,
2438 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002439 .map_sg = map_sg,
2440 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002441 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002442};
2443
Joerg Roedel27c21272011-05-30 15:56:24 +02002444static unsigned device_dma_ops_init(void)
2445{
2446 struct pci_dev *pdev = NULL;
2447 unsigned unhandled = 0;
2448
2449 for_each_pci_dev(pdev) {
2450 if (!check_device(&pdev->dev)) {
2451 unhandled += 1;
2452 continue;
2453 }
2454
2455 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
2456 }
2457
2458 return unhandled;
2459}
2460
Joerg Roedel431b2a22008-07-11 17:14:22 +02002461/*
2462 * The function which clues the AMD IOMMU driver into dma_ops.
2463 */
Joerg Roedelf5325092010-01-22 17:44:35 +01002464
2465void __init amd_iommu_init_api(void)
2466{
2467 register_iommu(&amd_iommu_ops);
2468}
2469
Joerg Roedel6631ee92008-06-26 21:28:05 +02002470int __init amd_iommu_init_dma_ops(void)
2471{
2472 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02002473 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002474
Joerg Roedel431b2a22008-07-11 17:14:22 +02002475 /*
2476 * first allocate a default protection domain for every IOMMU we
2477 * found in the system. Devices not assigned to any other
2478 * protection domain will be assigned to the default one.
2479 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02002480 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01002481 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002482 if (iommu->default_dom == NULL)
2483 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01002484 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002485 ret = iommu_init_unity_mappings(iommu);
2486 if (ret)
2487 goto free_domains;
2488 }
2489
Joerg Roedel431b2a22008-07-11 17:14:22 +02002490 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01002491 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002492 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01002493 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002494
2495 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002496 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002497
Joerg Roedel431b2a22008-07-11 17:14:22 +02002498 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02002499 unhandled = device_dma_ops_init();
2500 if (unhandled && max_pfn > MAX_DMA32_PFN) {
2501 /* There are unhandled devices - initialize swiotlb for them */
2502 swiotlb = 1;
2503 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02002504
Joerg Roedel7f265082008-12-12 13:50:21 +01002505 amd_iommu_stats_init();
2506
Joerg Roedel6631ee92008-06-26 21:28:05 +02002507 return 0;
2508
2509free_domains:
2510
Joerg Roedel3bd22172009-05-04 15:06:20 +02002511 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002512 if (iommu->default_dom)
2513 dma_ops_domain_free(iommu->default_dom);
2514 }
2515
2516 return ret;
2517}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002518
2519/*****************************************************************************
2520 *
2521 * The following functions belong to the exported interface of AMD IOMMU
2522 *
2523 * This interface allows access to lower level functions of the IOMMU
2524 * like protection domain handling and assignement of devices to domains
2525 * which is not possible with the dma_ops interface.
2526 *
2527 *****************************************************************************/
2528
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002529static void cleanup_domain(struct protection_domain *domain)
2530{
Joerg Roedel492667d2009-11-27 13:25:47 +01002531 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002532 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002533
2534 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2535
Joerg Roedel492667d2009-11-27 13:25:47 +01002536 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
2537 struct device *dev = dev_data->dev;
2538
Chris Wright04e856c2010-02-17 08:51:20 -08002539 __detach_device(dev);
Joerg Roedel492667d2009-11-27 13:25:47 +01002540 atomic_set(&dev_data->bind, 0);
2541 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002542
2543 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2544}
2545
Joerg Roedel26508152009-08-26 16:52:40 +02002546static void protection_domain_free(struct protection_domain *domain)
2547{
2548 if (!domain)
2549 return;
2550
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002551 del_domain_from_list(domain);
2552
Joerg Roedel26508152009-08-26 16:52:40 +02002553 if (domain->id)
2554 domain_id_free(domain->id);
2555
2556 kfree(domain);
2557}
2558
2559static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002560{
2561 struct protection_domain *domain;
2562
2563 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2564 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002565 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002566
2567 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002568 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01002569 domain->id = domain_id_alloc();
2570 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02002571 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002572 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02002573
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002574 add_domain_to_list(domain);
2575
Joerg Roedel26508152009-08-26 16:52:40 +02002576 return domain;
2577
2578out_err:
2579 kfree(domain);
2580
2581 return NULL;
2582}
2583
2584static int amd_iommu_domain_init(struct iommu_domain *dom)
2585{
2586 struct protection_domain *domain;
2587
2588 domain = protection_domain_alloc();
2589 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01002590 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02002591
2592 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002593 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2594 if (!domain->pt_root)
2595 goto out_free;
2596
2597 dom->priv = domain;
2598
2599 return 0;
2600
2601out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02002602 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01002603
2604 return -ENOMEM;
2605}
2606
Joerg Roedel98383fc2008-12-02 18:34:12 +01002607static void amd_iommu_domain_destroy(struct iommu_domain *dom)
2608{
2609 struct protection_domain *domain = dom->priv;
2610
2611 if (!domain)
2612 return;
2613
2614 if (domain->dev_cnt > 0)
2615 cleanup_domain(domain);
2616
2617 BUG_ON(domain->dev_cnt != 0);
2618
2619 free_pagetable(domain);
2620
Joerg Roedel8b408fe2010-03-08 14:20:07 +01002621 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002622
2623 dom->priv = NULL;
2624}
2625
Joerg Roedel684f2882008-12-08 12:07:44 +01002626static void amd_iommu_detach_device(struct iommu_domain *dom,
2627 struct device *dev)
2628{
Joerg Roedel657cbb62009-11-23 15:26:46 +01002629 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002630 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002631 u16 devid;
2632
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002633 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01002634 return;
2635
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002636 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002637
Joerg Roedel657cbb62009-11-23 15:26:46 +01002638 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002639 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002640
2641 iommu = amd_iommu_rlookup_table[devid];
2642 if (!iommu)
2643 return;
2644
Joerg Roedel684f2882008-12-08 12:07:44 +01002645 iommu_completion_wait(iommu);
2646}
2647
Joerg Roedel01106062008-12-02 19:34:11 +01002648static int amd_iommu_attach_device(struct iommu_domain *dom,
2649 struct device *dev)
2650{
2651 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002652 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01002653 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002654 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002655 u16 devid;
2656
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002657 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01002658 return -EINVAL;
2659
Joerg Roedel657cbb62009-11-23 15:26:46 +01002660 dev_data = dev->archdata.iommu;
2661
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002662 devid = get_device_id(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002663
2664 iommu = amd_iommu_rlookup_table[devid];
2665 if (!iommu)
2666 return -EINVAL;
2667
Joerg Roedel657cbb62009-11-23 15:26:46 +01002668 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002669 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002670
Joerg Roedel15898bb2009-11-24 15:39:42 +01002671 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01002672
2673 iommu_completion_wait(iommu);
2674
Joerg Roedel15898bb2009-11-24 15:39:42 +01002675 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002676}
2677
Joerg Roedel468e2362010-01-21 16:37:36 +01002678static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
2679 phys_addr_t paddr, int gfp_order, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002680{
Joerg Roedel468e2362010-01-21 16:37:36 +01002681 unsigned long page_size = 0x1000UL << gfp_order;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002682 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002683 int prot = 0;
2684 int ret;
2685
2686 if (iommu_prot & IOMMU_READ)
2687 prot |= IOMMU_PROT_IR;
2688 if (iommu_prot & IOMMU_WRITE)
2689 prot |= IOMMU_PROT_IW;
2690
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002691 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002692 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002693 mutex_unlock(&domain->api_lock);
2694
Joerg Roedel795e74f72010-05-11 17:40:57 +02002695 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002696}
2697
Joerg Roedel468e2362010-01-21 16:37:36 +01002698static int amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2699 int gfp_order)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002700{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002701 struct protection_domain *domain = dom->priv;
Joerg Roedel468e2362010-01-21 16:37:36 +01002702 unsigned long page_size, unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002703
Joerg Roedel468e2362010-01-21 16:37:36 +01002704 page_size = 0x1000UL << gfp_order;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002705
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002706 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01002707 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002708 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002709
Joerg Roedel17b124b2011-04-06 18:01:35 +02002710 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002711
Joerg Roedel468e2362010-01-21 16:37:36 +01002712 return get_order(unmap_size);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002713}
2714
Joerg Roedel645c4c82008-12-02 20:05:50 +01002715static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2716 unsigned long iova)
2717{
2718 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002719 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002720 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002721 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002722
Joerg Roedel24cd7722010-01-19 17:27:39 +01002723 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002724
Joerg Roedela6d41a42009-09-02 17:08:55 +02002725 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01002726 return 0;
2727
Joerg Roedelf03152b2010-01-21 16:15:24 +01002728 if (PM_PTE_LEVEL(*pte) == 0)
2729 offset_mask = PAGE_SIZE - 1;
2730 else
2731 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
2732
2733 __pte = *pte & PM_ADDR_MASK;
2734 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002735
2736 return paddr;
2737}
2738
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002739static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
2740 unsigned long cap)
2741{
Joerg Roedel80a506b2010-07-27 17:14:24 +02002742 switch (cap) {
2743 case IOMMU_CAP_CACHE_COHERENCY:
2744 return 1;
2745 }
2746
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002747 return 0;
2748}
2749
Joerg Roedel26961ef2008-12-03 17:00:17 +01002750static struct iommu_ops amd_iommu_ops = {
2751 .domain_init = amd_iommu_domain_init,
2752 .domain_destroy = amd_iommu_domain_destroy,
2753 .attach_dev = amd_iommu_attach_device,
2754 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01002755 .map = amd_iommu_map,
2756 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002757 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002758 .domain_has_cap = amd_iommu_domain_has_cap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002759};
2760
Joerg Roedel0feae532009-08-26 15:26:30 +02002761/*****************************************************************************
2762 *
2763 * The next functions do a basic initialization of IOMMU for pass through
2764 * mode
2765 *
2766 * In passthrough mode the IOMMU is initialized and enabled but not used for
2767 * DMA-API translation.
2768 *
2769 *****************************************************************************/
2770
2771int __init amd_iommu_init_passthrough(void)
2772{
Joerg Roedel15898bb2009-11-24 15:39:42 +01002773 struct amd_iommu *iommu;
Joerg Roedel0feae532009-08-26 15:26:30 +02002774 struct pci_dev *dev = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002775 u16 devid;
Joerg Roedel0feae532009-08-26 15:26:30 +02002776
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002777 /* allocate passthrough domain */
Joerg Roedel0feae532009-08-26 15:26:30 +02002778 pt_domain = protection_domain_alloc();
2779 if (!pt_domain)
2780 return -ENOMEM;
2781
2782 pt_domain->mode |= PAGE_MODE_NONE;
2783
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04002784 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002785 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02002786 continue;
2787
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002788 devid = get_device_id(&dev->dev);
2789
Joerg Roedel15898bb2009-11-24 15:39:42 +01002790 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02002791 if (!iommu)
2792 continue;
2793
Joerg Roedel15898bb2009-11-24 15:39:42 +01002794 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02002795 }
2796
2797 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
2798
2799 return 0;
2800}