blob: a15a172319099a056da0a9096a6965668a01fb8c [file] [log] [blame]
Joerg Roedelb6c02712008-06-26 21:27:53 +02001/*
Joerg Roedel5d0d7152010-10-13 11:13:21 +02002 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
Joerg Roedelb6c02712008-06-26 21:27:53 +02003 * Author: Joerg Roedel <joerg.roedel@amd.com>
4 * Leo Duran <leo.duran@amd.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/pci.h>
Joerg Roedelcb41ed82011-04-05 11:00:53 +020021#include <linux/pci-ats.h>
Akinobu Mitaa66022c2009-12-15 16:48:28 -080022#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Joerg Roedel7f265082008-12-12 13:50:21 +010024#include <linux/debugfs.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020025#include <linux/scatterlist.h>
FUJITA Tomonori51491362009-01-05 23:47:25 +090026#include <linux/dma-mapping.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020027#include <linux/iommu-helper.h>
Joerg Roedelc156e342008-12-02 18:13:27 +010028#include <linux/iommu.h>
Joerg Roedel815b33f2011-04-06 17:26:49 +020029#include <linux/delay.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020030#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090031#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010032#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020033#include <asm/dma.h>
Joerg Roedel6a9401a2009-11-20 13:22:21 +010034#include <asm/amd_iommu_proto.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020035#include <asm/amd_iommu_types.h>
Joerg Roedelc6da9922008-06-26 21:28:06 +020036#include <asm/amd_iommu.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020037
38#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
39
Joerg Roedel815b33f2011-04-06 17:26:49 +020040#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020041
Joerg Roedelb6c02712008-06-26 21:27:53 +020042static DEFINE_RWLOCK(amd_iommu_devtable_lock);
43
Joerg Roedelbd60b732008-09-11 10:24:48 +020044/* A list of preallocated protection domains */
45static LIST_HEAD(iommu_pd_list);
46static DEFINE_SPINLOCK(iommu_pd_list_lock);
47
Joerg Roedel8fa5f802011-06-09 12:24:45 +020048/* List of all available dev_data structures */
49static LIST_HEAD(dev_data_list);
50static DEFINE_SPINLOCK(dev_data_list_lock);
51
Joerg Roedel0feae532009-08-26 15:26:30 +020052/*
53 * Domain for untranslated devices - only allocated
54 * if iommu=pt passed on kernel cmd line.
55 */
56static struct protection_domain *pt_domain;
57
Joerg Roedel26961ef2008-12-03 17:00:17 +010058static struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010059
Joerg Roedel431b2a22008-07-11 17:14:22 +020060/*
61 * general struct to manage commands send to an IOMMU
62 */
Joerg Roedeld6449532008-07-11 17:14:28 +020063struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +020064 u32 data[4];
65};
66
Joerg Roedel04bfdd82009-09-02 16:00:23 +020067static void update_domain(struct protection_domain *domain);
Chris Wrightc1eee672009-05-21 00:56:58 -070068
Joerg Roedel15898bb2009-11-24 15:39:42 +010069/****************************************************************************
70 *
71 * Helper functions
72 *
73 ****************************************************************************/
74
Joerg Roedelf62dda62011-06-09 12:55:35 +020075static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +020076{
77 struct iommu_dev_data *dev_data;
78 unsigned long flags;
79
80 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
81 if (!dev_data)
82 return NULL;
83
Joerg Roedelf62dda62011-06-09 12:55:35 +020084 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +020085 atomic_set(&dev_data->bind, 0);
86
87 spin_lock_irqsave(&dev_data_list_lock, flags);
88 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
89 spin_unlock_irqrestore(&dev_data_list_lock, flags);
90
91 return dev_data;
92}
93
94static void free_dev_data(struct iommu_dev_data *dev_data)
95{
96 unsigned long flags;
97
98 spin_lock_irqsave(&dev_data_list_lock, flags);
99 list_del(&dev_data->dev_data_list);
100 spin_unlock_irqrestore(&dev_data_list_lock, flags);
101
102 kfree(dev_data);
103}
104
Joerg Roedel15898bb2009-11-24 15:39:42 +0100105static inline u16 get_device_id(struct device *dev)
106{
107 struct pci_dev *pdev = to_pci_dev(dev);
108
109 return calc_devid(pdev->bus->number, pdev->devfn);
110}
111
Joerg Roedel657cbb62009-11-23 15:26:46 +0100112static struct iommu_dev_data *get_dev_data(struct device *dev)
113{
114 return dev->archdata.iommu;
115}
116
Joerg Roedel71c70982009-11-24 16:43:06 +0100117/*
118 * In this function the list of preallocated protection domains is traversed to
119 * find the domain for a specific device
120 */
121static struct dma_ops_domain *find_protection_domain(u16 devid)
122{
123 struct dma_ops_domain *entry, *ret = NULL;
124 unsigned long flags;
125 u16 alias = amd_iommu_alias_table[devid];
126
127 if (list_empty(&iommu_pd_list))
128 return NULL;
129
130 spin_lock_irqsave(&iommu_pd_list_lock, flags);
131
132 list_for_each_entry(entry, &iommu_pd_list, list) {
133 if (entry->target_dev == devid ||
134 entry->target_dev == alias) {
135 ret = entry;
136 break;
137 }
138 }
139
140 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
141
142 return ret;
143}
144
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100145/*
146 * This function checks if the driver got a valid device from the caller to
147 * avoid dereferencing invalid pointers.
148 */
149static bool check_device(struct device *dev)
150{
151 u16 devid;
152
153 if (!dev || !dev->dma_mask)
154 return false;
155
156 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100157 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100158 return false;
159
160 devid = get_device_id(dev);
161
162 /* Out of our scope? */
163 if (devid > amd_iommu_last_bdf)
164 return false;
165
166 if (amd_iommu_rlookup_table[devid] == NULL)
167 return false;
168
169 return true;
170}
171
Joerg Roedel657cbb62009-11-23 15:26:46 +0100172static int iommu_init_device(struct device *dev)
173{
174 struct iommu_dev_data *dev_data;
175 struct pci_dev *pdev;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200176 u16 alias;
Joerg Roedel657cbb62009-11-23 15:26:46 +0100177
178 if (dev->archdata.iommu)
179 return 0;
180
Joerg Roedelf62dda62011-06-09 12:55:35 +0200181 dev_data = alloc_dev_data(get_device_id(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +0100182 if (!dev_data)
183 return -ENOMEM;
184
Joerg Roedelf62dda62011-06-09 12:55:35 +0200185 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedel657cbb62009-11-23 15:26:46 +0100186 pdev = pci_get_bus_and_slot(PCI_BUS(alias), alias & 0xff);
187 if (pdev)
188 dev_data->alias = &pdev->dev;
Joerg Roedel26018872011-06-06 16:50:14 +0200189 else {
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200190 free_dev_data(dev_data);
Joerg Roedel26018872011-06-06 16:50:14 +0200191 return -ENOTSUPP;
192 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100193
194 dev->archdata.iommu = dev_data;
195
Joerg Roedel657cbb62009-11-23 15:26:46 +0100196 return 0;
197}
198
Joerg Roedel26018872011-06-06 16:50:14 +0200199static void iommu_ignore_device(struct device *dev)
200{
201 u16 devid, alias;
202
203 devid = get_device_id(dev);
204 alias = amd_iommu_alias_table[devid];
205
206 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
207 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
208
209 amd_iommu_rlookup_table[devid] = NULL;
210 amd_iommu_rlookup_table[alias] = NULL;
211}
212
Joerg Roedel657cbb62009-11-23 15:26:46 +0100213static void iommu_uninit_device(struct device *dev)
214{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200215 /*
216 * Nothing to do here - we keep dev_data around for unplugged devices
217 * and reuse it when the device is re-plugged - not doing so would
218 * introduce a ton of races.
219 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100220}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100221
222void __init amd_iommu_uninit_devices(void)
223{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200224 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100225 struct pci_dev *pdev = NULL;
226
227 for_each_pci_dev(pdev) {
228
229 if (!check_device(&pdev->dev))
230 continue;
231
232 iommu_uninit_device(&pdev->dev);
233 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200234
235 /* Free all of our dev_data structures */
236 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
237 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100238}
239
240int __init amd_iommu_init_devices(void)
241{
242 struct pci_dev *pdev = NULL;
243 int ret = 0;
244
245 for_each_pci_dev(pdev) {
246
247 if (!check_device(&pdev->dev))
248 continue;
249
250 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200251 if (ret == -ENOTSUPP)
252 iommu_ignore_device(&pdev->dev);
253 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100254 goto out_free;
255 }
256
257 return 0;
258
259out_free:
260
261 amd_iommu_uninit_devices();
262
263 return ret;
264}
Joerg Roedel7f265082008-12-12 13:50:21 +0100265#ifdef CONFIG_AMD_IOMMU_STATS
266
267/*
268 * Initialization code for statistics collection
269 */
270
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100271DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100272DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100273DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100274DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100275DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100276DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100277DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100278DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100279DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100280DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100281DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100282DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100283
Joerg Roedel7f265082008-12-12 13:50:21 +0100284static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100285static struct dentry *de_fflush;
286
287static void amd_iommu_stats_add(struct __iommu_counter *cnt)
288{
289 if (stats_dir == NULL)
290 return;
291
292 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
293 &cnt->value);
294}
295
296static void amd_iommu_stats_init(void)
297{
298 stats_dir = debugfs_create_dir("amd-iommu", NULL);
299 if (stats_dir == NULL)
300 return;
301
Joerg Roedel7f265082008-12-12 13:50:21 +0100302 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
303 (u32 *)&amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100304
305 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100306 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100307 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100308 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100309 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100310 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100311 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100312 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100313 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100314 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100315 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100316 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100317}
318
319#endif
320
Joerg Roedel431b2a22008-07-11 17:14:22 +0200321/****************************************************************************
322 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200323 * Interrupt handling functions
324 *
325 ****************************************************************************/
326
Joerg Roedele3e59872009-09-03 14:02:10 +0200327static void dump_dte_entry(u16 devid)
328{
329 int i;
330
331 for (i = 0; i < 8; ++i)
332 pr_err("AMD-Vi: DTE[%d]: %08x\n", i,
333 amd_iommu_dev_table[devid].data[i]);
334}
335
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200336static void dump_command(unsigned long phys_addr)
337{
338 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
339 int i;
340
341 for (i = 0; i < 4; ++i)
342 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
343}
344
Joerg Roedela345b232009-09-03 15:01:43 +0200345static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200346{
347 u32 *event = __evt;
348 int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
349 int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
350 int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
351 int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
352 u64 address = (u64)(((u64)event[3]) << 32) | event[2];
353
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200354 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200355
356 switch (type) {
357 case EVENT_TYPE_ILL_DEV:
358 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
359 "address=0x%016llx flags=0x%04x]\n",
360 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
361 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200362 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200363 break;
364 case EVENT_TYPE_IO_FAULT:
365 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
366 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
367 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
368 domid, address, flags);
369 break;
370 case EVENT_TYPE_DEV_TAB_ERR:
371 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
372 "address=0x%016llx flags=0x%04x]\n",
373 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
374 address, flags);
375 break;
376 case EVENT_TYPE_PAGE_TAB_ERR:
377 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
378 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
379 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
380 domid, address, flags);
381 break;
382 case EVENT_TYPE_ILL_CMD:
383 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200384 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200385 break;
386 case EVENT_TYPE_CMD_HARD_ERR:
387 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
388 "flags=0x%04x]\n", address, flags);
389 break;
390 case EVENT_TYPE_IOTLB_INV_TO:
391 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
392 "address=0x%016llx]\n",
393 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
394 address);
395 break;
396 case EVENT_TYPE_INV_DEV_REQ:
397 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
398 "address=0x%016llx flags=0x%04x]\n",
399 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
400 address, flags);
401 break;
402 default:
403 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
404 }
405}
406
407static void iommu_poll_events(struct amd_iommu *iommu)
408{
409 u32 head, tail;
410 unsigned long flags;
411
412 spin_lock_irqsave(&iommu->lock, flags);
413
414 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
415 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
416
417 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200418 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200419 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
420 }
421
422 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
423
424 spin_unlock_irqrestore(&iommu->lock, flags);
425}
426
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200427irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200428{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200429 struct amd_iommu *iommu;
430
Joerg Roedel3bd22172009-05-04 15:06:20 +0200431 for_each_iommu(iommu)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200432 iommu_poll_events(iommu);
433
434 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200435}
436
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200437irqreturn_t amd_iommu_int_handler(int irq, void *data)
438{
439 return IRQ_WAKE_THREAD;
440}
441
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200442/****************************************************************************
443 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200444 * IOMMU command queuing functions
445 *
446 ****************************************************************************/
447
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200448static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200449{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200450 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200451
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200452 while (*sem == 0 && i < LOOP_TIMEOUT) {
453 udelay(1);
454 i += 1;
455 }
456
457 if (i == LOOP_TIMEOUT) {
458 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
459 return -EIO;
460 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200461
462 return 0;
463}
464
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200465static void copy_cmd_to_buffer(struct amd_iommu *iommu,
466 struct iommu_cmd *cmd,
467 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200468{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200469 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200470
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200471 target = iommu->cmd_buf + tail;
472 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200473
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200474 /* Copy command to buffer */
475 memcpy(target, cmd, sizeof(*cmd));
476
477 /* Tell the IOMMU about it */
478 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
479}
480
Joerg Roedel815b33f2011-04-06 17:26:49 +0200481static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200482{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200483 WARN_ON(address & 0x7ULL);
484
Joerg Roedelded46732011-04-06 10:53:48 +0200485 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200486 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
487 cmd->data[1] = upper_32_bits(__pa(address));
488 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200489 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
490}
491
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200492static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
493{
494 memset(cmd, 0, sizeof(*cmd));
495 cmd->data[0] = devid;
496 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
497}
498
Joerg Roedel11b64022011-04-06 11:49:28 +0200499static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
500 size_t size, u16 domid, int pde)
501{
502 u64 pages;
503 int s;
504
505 pages = iommu_num_pages(address, size, PAGE_SIZE);
506 s = 0;
507
508 if (pages > 1) {
509 /*
510 * If we have to flush more than one page, flush all
511 * TLB entries for this domain
512 */
513 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
514 s = 1;
515 }
516
517 address &= PAGE_MASK;
518
519 memset(cmd, 0, sizeof(*cmd));
520 cmd->data[1] |= domid;
521 cmd->data[2] = lower_32_bits(address);
522 cmd->data[3] = upper_32_bits(address);
523 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
524 if (s) /* size bit - we flush more than one 4kb page */
525 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
526 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
527 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
528}
529
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200530static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
531 u64 address, size_t size)
532{
533 u64 pages;
534 int s;
535
536 pages = iommu_num_pages(address, size, PAGE_SIZE);
537 s = 0;
538
539 if (pages > 1) {
540 /*
541 * If we have to flush more than one page, flush all
542 * TLB entries for this domain
543 */
544 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
545 s = 1;
546 }
547
548 address &= PAGE_MASK;
549
550 memset(cmd, 0, sizeof(*cmd));
551 cmd->data[0] = devid;
552 cmd->data[0] |= (qdep & 0xff) << 24;
553 cmd->data[1] = devid;
554 cmd->data[2] = lower_32_bits(address);
555 cmd->data[3] = upper_32_bits(address);
556 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
557 if (s)
558 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
559}
560
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200561static void build_inv_all(struct iommu_cmd *cmd)
562{
563 memset(cmd, 0, sizeof(*cmd));
564 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200565}
566
Joerg Roedel431b2a22008-07-11 17:14:22 +0200567/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200568 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200569 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200570 */
Joerg Roedel815b33f2011-04-06 17:26:49 +0200571static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200572{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200573 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200574 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200575
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200576 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100577
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200578again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200579 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200580
581 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
582 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
583 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
584 left = (head - next_tail) % iommu->cmd_buf_size;
585
586 if (left <= 2) {
587 struct iommu_cmd sync_cmd;
588 volatile u64 sem = 0;
589 int ret;
590
591 build_completion_wait(&sync_cmd, (u64)&sem);
592 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
593
594 spin_unlock_irqrestore(&iommu->lock, flags);
595
596 if ((ret = wait_on_sem(&sem)) != 0)
597 return ret;
598
599 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200600 }
601
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200602 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200603
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200604 /* We need to sync now to make sure all commands are processed */
Joerg Roedel815b33f2011-04-06 17:26:49 +0200605 iommu->need_sync = true;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200606
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200607 spin_unlock_irqrestore(&iommu->lock, flags);
608
Joerg Roedel815b33f2011-04-06 17:26:49 +0200609 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100610}
611
612/*
613 * This function queues a completion wait command into the command
614 * buffer of an IOMMU
615 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100616static int iommu_completion_wait(struct amd_iommu *iommu)
617{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200618 struct iommu_cmd cmd;
619 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200620 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100621
622 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200623 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100624
Joerg Roedel815b33f2011-04-06 17:26:49 +0200625 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100626
Joerg Roedel815b33f2011-04-06 17:26:49 +0200627 ret = iommu_queue_command(iommu, &cmd);
Joerg Roedel8d201962008-12-02 20:34:41 +0100628 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200629 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100630
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200631 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200632}
633
Joerg Roedeld8c13082011-04-06 18:51:26 +0200634static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200635{
636 struct iommu_cmd cmd;
637
Joerg Roedeld8c13082011-04-06 18:51:26 +0200638 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200639
Joerg Roedeld8c13082011-04-06 18:51:26 +0200640 return iommu_queue_command(iommu, &cmd);
641}
642
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200643static void iommu_flush_dte_all(struct amd_iommu *iommu)
644{
645 u32 devid;
646
647 for (devid = 0; devid <= 0xffff; ++devid)
648 iommu_flush_dte(iommu, devid);
649
650 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200651}
652
653/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200654 * This function uses heavy locking and may disable irqs for some time. But
655 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200656 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200657static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200658{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200659 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200660
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200661 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
662 struct iommu_cmd cmd;
663 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
664 dom_id, 1);
665 iommu_queue_command(iommu, &cmd);
666 }
Joerg Roedel431b2a22008-07-11 17:14:22 +0200667
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200668 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200669}
670
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200671static void iommu_flush_all(struct amd_iommu *iommu)
672{
673 struct iommu_cmd cmd;
674
675 build_inv_all(&cmd);
676
677 iommu_queue_command(iommu, &cmd);
678 iommu_completion_wait(iommu);
679}
680
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200681void iommu_flush_all_caches(struct amd_iommu *iommu)
682{
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200683 if (iommu_feature(iommu, FEATURE_IA)) {
684 iommu_flush_all(iommu);
685 } else {
686 iommu_flush_dte_all(iommu);
687 iommu_flush_tlb_all(iommu);
688 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200689}
690
Joerg Roedel431b2a22008-07-11 17:14:22 +0200691/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200692 * Command send function for flushing on-device TLB
693 */
Joerg Roedel6c542042011-06-09 17:07:31 +0200694static int device_flush_iotlb(struct iommu_dev_data *dev_data,
695 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200696{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200697 struct amd_iommu *iommu;
698 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200699 int qdep;
700
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200701 qdep = dev_data->ats.qdep;
702 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200703
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200704 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200705
706 return iommu_queue_command(iommu, &cmd);
707}
708
709/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200710 * Command send function for invalidating a device table entry
711 */
Joerg Roedel6c542042011-06-09 17:07:31 +0200712static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +0100713{
714 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200715 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100716
Joerg Roedel6c542042011-06-09 17:07:31 +0200717 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +0100718
Joerg Roedelf62dda62011-06-09 12:55:35 +0200719 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200720 if (ret)
721 return ret;
722
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200723 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +0200724 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200725
726 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100727}
728
Joerg Roedel431b2a22008-07-11 17:14:22 +0200729/*
730 * TLB invalidation function which is called from the mapping functions.
731 * It invalidates a single PTE if the range to flush is within a single
732 * page. Otherwise it flushes the whole TLB of the IOMMU.
733 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200734static void __domain_flush_pages(struct protection_domain *domain,
735 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200736{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200737 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +0200738 struct iommu_cmd cmd;
739 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200740
Joerg Roedel11b64022011-04-06 11:49:28 +0200741 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +0200742
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100743 for (i = 0; i < amd_iommus_present; ++i) {
744 if (!domain->dev_iommu[i])
745 continue;
746
747 /*
748 * Devices of this domain are behind this IOMMU
749 * We need a TLB flush
750 */
Joerg Roedel11b64022011-04-06 11:49:28 +0200751 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100752 }
753
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200754 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200755
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200756 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200757 continue;
758
Joerg Roedel6c542042011-06-09 17:07:31 +0200759 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200760 }
761
Joerg Roedel11b64022011-04-06 11:49:28 +0200762 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100763}
764
Joerg Roedel17b124b2011-04-06 18:01:35 +0200765static void domain_flush_pages(struct protection_domain *domain,
766 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100767{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200768 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200769}
Joerg Roedelb6c02712008-06-26 21:27:53 +0200770
Joerg Roedel1c655772008-09-04 18:40:05 +0200771/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200772static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +0200773{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200774 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +0200775}
776
Chris Wright42a49f92009-06-15 15:42:00 +0200777/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200778static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +0200779{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200780 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
781}
782
783static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +0200784{
785 int i;
786
787 for (i = 0; i < amd_iommus_present; ++i) {
788 if (!domain->dev_iommu[i])
789 continue;
790
791 /*
792 * Devices of this domain are behind this IOMMU
793 * We need to wait for completion of all commands.
794 */
795 iommu_completion_wait(amd_iommus[i]);
796 }
797}
798
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100799
Joerg Roedel43f49602008-12-02 21:01:12 +0100800/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100801 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +0100802 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200803static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200804{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100805 struct iommu_dev_data *dev_data;
806 unsigned long flags;
807
808 spin_lock_irqsave(&domain->lock, flags);
809
810 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +0200811 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100812
813 spin_unlock_irqrestore(&domain->lock, flags);
814}
815
Joerg Roedel431b2a22008-07-11 17:14:22 +0200816/****************************************************************************
817 *
818 * The functions below are used the create the page table mappings for
819 * unity mapped regions.
820 *
821 ****************************************************************************/
822
823/*
Joerg Roedel308973d2009-11-24 17:43:32 +0100824 * This function is used to add another level to an IO page table. Adding
825 * another level increases the size of the address space by 9 bits to a size up
826 * to 64 bits.
827 */
828static bool increase_address_space(struct protection_domain *domain,
829 gfp_t gfp)
830{
831 u64 *pte;
832
833 if (domain->mode == PAGE_MODE_6_LEVEL)
834 /* address space already 64 bit large */
835 return false;
836
837 pte = (void *)get_zeroed_page(gfp);
838 if (!pte)
839 return false;
840
841 *pte = PM_LEVEL_PDE(domain->mode,
842 virt_to_phys(domain->pt_root));
843 domain->pt_root = pte;
844 domain->mode += 1;
845 domain->updated = true;
846
847 return true;
848}
849
850static u64 *alloc_pte(struct protection_domain *domain,
851 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100852 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +0100853 u64 **pte_page,
854 gfp_t gfp)
855{
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100856 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +0100857 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100858
859 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +0100860
861 while (address > PM_LEVEL_SIZE(domain->mode))
862 increase_address_space(domain, gfp);
863
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100864 level = domain->mode - 1;
865 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
866 address = PAGE_SIZE_ALIGN(address, page_size);
867 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +0100868
869 while (level > end_lvl) {
870 if (!IOMMU_PTE_PRESENT(*pte)) {
871 page = (u64 *)get_zeroed_page(gfp);
872 if (!page)
873 return NULL;
874 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
875 }
876
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100877 /* No level skipping support yet */
878 if (PM_PTE_LEVEL(*pte) != level)
879 return NULL;
880
Joerg Roedel308973d2009-11-24 17:43:32 +0100881 level -= 1;
882
883 pte = IOMMU_PTE_PAGE(*pte);
884
885 if (pte_page && level == end_lvl)
886 *pte_page = pte;
887
888 pte = &pte[PM_LEVEL_INDEX(level, address)];
889 }
890
891 return pte;
892}
893
894/*
895 * This function checks if there is a PTE for a given dma address. If
896 * there is one, it returns the pointer to it.
897 */
Joerg Roedel24cd7722010-01-19 17:27:39 +0100898static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +0100899{
900 int level;
901 u64 *pte;
902
Joerg Roedel24cd7722010-01-19 17:27:39 +0100903 if (address > PM_LEVEL_SIZE(domain->mode))
904 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +0100905
Joerg Roedel24cd7722010-01-19 17:27:39 +0100906 level = domain->mode - 1;
907 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
908
909 while (level > 0) {
910
911 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +0100912 if (!IOMMU_PTE_PRESENT(*pte))
913 return NULL;
914
Joerg Roedel24cd7722010-01-19 17:27:39 +0100915 /* Large PTE */
916 if (PM_PTE_LEVEL(*pte) == 0x07) {
917 unsigned long pte_mask, __pte;
918
919 /*
920 * If we have a series of large PTEs, make
921 * sure to return a pointer to the first one.
922 */
923 pte_mask = PTE_PAGE_SIZE(*pte);
924 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
925 __pte = ((unsigned long)pte) & pte_mask;
926
927 return (u64 *)__pte;
928 }
929
930 /* No level skipping support yet */
931 if (PM_PTE_LEVEL(*pte) != level)
932 return NULL;
933
Joerg Roedel308973d2009-11-24 17:43:32 +0100934 level -= 1;
935
Joerg Roedel24cd7722010-01-19 17:27:39 +0100936 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +0100937 pte = IOMMU_PTE_PAGE(*pte);
938 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +0100939 }
940
941 return pte;
942}
943
944/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200945 * Generic mapping functions. It maps a physical address into a DMA
946 * address space. It allocates the page table pages if necessary.
947 * In the future it can be extended to a generic mapping function
948 * supporting all features of AMD IOMMU page tables like level skipping
949 * and full 64 bit address spaces.
950 */
Joerg Roedel38e817f2008-12-02 17:27:52 +0100951static int iommu_map_page(struct protection_domain *dom,
952 unsigned long bus_addr,
953 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200954 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100955 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200956{
Joerg Roedel8bda3092009-05-12 12:02:46 +0200957 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100958 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200959
Joerg Roedelbad1cac2009-09-02 16:52:23 +0200960 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200961 return -EINVAL;
962
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100963 bus_addr = PAGE_ALIGN(bus_addr);
964 phys_addr = PAGE_ALIGN(phys_addr);
965 count = PAGE_SIZE_PTE_COUNT(page_size);
966 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200967
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100968 for (i = 0; i < count; ++i)
969 if (IOMMU_PTE_PRESENT(pte[i]))
970 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200971
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100972 if (page_size > PAGE_SIZE) {
973 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
974 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
975 } else
976 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
977
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200978 if (prot & IOMMU_PROT_IR)
979 __pte |= IOMMU_PTE_IR;
980 if (prot & IOMMU_PROT_IW)
981 __pte |= IOMMU_PTE_IW;
982
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100983 for (i = 0; i < count; ++i)
984 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200985
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200986 update_domain(dom);
987
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200988 return 0;
989}
990
Joerg Roedel24cd7722010-01-19 17:27:39 +0100991static unsigned long iommu_unmap_page(struct protection_domain *dom,
992 unsigned long bus_addr,
993 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100994{
Joerg Roedel24cd7722010-01-19 17:27:39 +0100995 unsigned long long unmap_size, unmapped;
996 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100997
Joerg Roedel24cd7722010-01-19 17:27:39 +0100998 BUG_ON(!is_power_of_2(page_size));
999
1000 unmapped = 0;
1001
1002 while (unmapped < page_size) {
1003
1004 pte = fetch_pte(dom, bus_addr);
1005
1006 if (!pte) {
1007 /*
1008 * No PTE for this address
1009 * move forward in 4kb steps
1010 */
1011 unmap_size = PAGE_SIZE;
1012 } else if (PM_PTE_LEVEL(*pte) == 0) {
1013 /* 4kb PTE found for this address */
1014 unmap_size = PAGE_SIZE;
1015 *pte = 0ULL;
1016 } else {
1017 int count, i;
1018
1019 /* Large PTE found which maps this address */
1020 unmap_size = PTE_PAGE_SIZE(*pte);
1021 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1022 for (i = 0; i < count; i++)
1023 pte[i] = 0ULL;
1024 }
1025
1026 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1027 unmapped += unmap_size;
1028 }
1029
1030 BUG_ON(!is_power_of_2(unmapped));
1031
1032 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001033}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001034
Joerg Roedel431b2a22008-07-11 17:14:22 +02001035/*
1036 * This function checks if a specific unity mapping entry is needed for
1037 * this specific IOMMU.
1038 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001039static int iommu_for_unity_map(struct amd_iommu *iommu,
1040 struct unity_map_entry *entry)
1041{
1042 u16 bdf, i;
1043
1044 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1045 bdf = amd_iommu_alias_table[i];
1046 if (amd_iommu_rlookup_table[bdf] == iommu)
1047 return 1;
1048 }
1049
1050 return 0;
1051}
1052
Joerg Roedel431b2a22008-07-11 17:14:22 +02001053/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001054 * This function actually applies the mapping to the page table of the
1055 * dma_ops domain.
1056 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001057static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1058 struct unity_map_entry *e)
1059{
1060 u64 addr;
1061 int ret;
1062
1063 for (addr = e->address_start; addr < e->address_end;
1064 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001065 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001066 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001067 if (ret)
1068 return ret;
1069 /*
1070 * if unity mapping is in aperture range mark the page
1071 * as allocated in the aperture
1072 */
1073 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001074 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001075 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001076 }
1077
1078 return 0;
1079}
1080
Joerg Roedel431b2a22008-07-11 17:14:22 +02001081/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001082 * Init the unity mappings for a specific IOMMU in the system
1083 *
1084 * Basically iterates over all unity mapping entries and applies them to
1085 * the default domain DMA of that IOMMU if necessary.
1086 */
1087static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1088{
1089 struct unity_map_entry *entry;
1090 int ret;
1091
1092 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1093 if (!iommu_for_unity_map(iommu, entry))
1094 continue;
1095 ret = dma_ops_unity_map(iommu->default_dom, entry);
1096 if (ret)
1097 return ret;
1098 }
1099
1100 return 0;
1101}
1102
1103/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001104 * Inits the unity mappings required for a specific device
1105 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001106static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1107 u16 devid)
1108{
1109 struct unity_map_entry *e;
1110 int ret;
1111
1112 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1113 if (!(devid >= e->devid_start && devid <= e->devid_end))
1114 continue;
1115 ret = dma_ops_unity_map(dma_dom, e);
1116 if (ret)
1117 return ret;
1118 }
1119
1120 return 0;
1121}
1122
Joerg Roedel431b2a22008-07-11 17:14:22 +02001123/****************************************************************************
1124 *
1125 * The next functions belong to the address allocator for the dma_ops
1126 * interface functions. They work like the allocators in the other IOMMU
1127 * drivers. Its basically a bitmap which marks the allocated pages in
1128 * the aperture. Maybe it could be enhanced in the future to a more
1129 * efficient allocator.
1130 *
1131 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001132
Joerg Roedel431b2a22008-07-11 17:14:22 +02001133/*
Joerg Roedel384de722009-05-15 12:30:05 +02001134 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001135 *
1136 * called with domain->lock held
1137 */
Joerg Roedel384de722009-05-15 12:30:05 +02001138
Joerg Roedel9cabe892009-05-18 16:38:55 +02001139/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001140 * Used to reserve address ranges in the aperture (e.g. for exclusion
1141 * ranges.
1142 */
1143static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1144 unsigned long start_page,
1145 unsigned int pages)
1146{
1147 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1148
1149 if (start_page + pages > last_page)
1150 pages = last_page - start_page;
1151
1152 for (i = start_page; i < start_page + pages; ++i) {
1153 int index = i / APERTURE_RANGE_PAGES;
1154 int page = i % APERTURE_RANGE_PAGES;
1155 __set_bit(page, dom->aperture[index]->bitmap);
1156 }
1157}
1158
1159/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001160 * This function is used to add a new aperture range to an existing
1161 * aperture in case of dma_ops domain allocation or address allocation
1162 * failure.
1163 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001164static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001165 bool populate, gfp_t gfp)
1166{
1167 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001168 struct amd_iommu *iommu;
Joerg Roedeld91afd12010-01-22 16:40:20 +01001169 unsigned long i;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001170
Joerg Roedelf5e97052009-05-22 12:31:53 +02001171#ifdef CONFIG_IOMMU_STRESS
1172 populate = false;
1173#endif
1174
Joerg Roedel9cabe892009-05-18 16:38:55 +02001175 if (index >= APERTURE_MAX_RANGES)
1176 return -ENOMEM;
1177
1178 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1179 if (!dma_dom->aperture[index])
1180 return -ENOMEM;
1181
1182 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1183 if (!dma_dom->aperture[index]->bitmap)
1184 goto out_free;
1185
1186 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1187
1188 if (populate) {
1189 unsigned long address = dma_dom->aperture_size;
1190 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1191 u64 *pte, *pte_page;
1192
1193 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001194 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001195 &pte_page, gfp);
1196 if (!pte)
1197 goto out_free;
1198
1199 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1200
1201 address += APERTURE_RANGE_SIZE / 64;
1202 }
1203 }
1204
1205 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1206
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001207 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001208 for_each_iommu(iommu) {
1209 if (iommu->exclusion_start &&
1210 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1211 && iommu->exclusion_start < dma_dom->aperture_size) {
1212 unsigned long startpage;
1213 int pages = iommu_num_pages(iommu->exclusion_start,
1214 iommu->exclusion_length,
1215 PAGE_SIZE);
1216 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1217 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1218 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001219 }
1220
1221 /*
1222 * Check for areas already mapped as present in the new aperture
1223 * range and mark those pages as reserved in the allocator. Such
1224 * mappings may already exist as a result of requested unity
1225 * mappings for devices.
1226 */
1227 for (i = dma_dom->aperture[index]->offset;
1228 i < dma_dom->aperture_size;
1229 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001230 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001231 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1232 continue;
1233
1234 dma_ops_reserve_addresses(dma_dom, i << PAGE_SHIFT, 1);
1235 }
1236
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001237 update_domain(&dma_dom->domain);
1238
Joerg Roedel9cabe892009-05-18 16:38:55 +02001239 return 0;
1240
1241out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001242 update_domain(&dma_dom->domain);
1243
Joerg Roedel9cabe892009-05-18 16:38:55 +02001244 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1245
1246 kfree(dma_dom->aperture[index]);
1247 dma_dom->aperture[index] = NULL;
1248
1249 return -ENOMEM;
1250}
1251
Joerg Roedel384de722009-05-15 12:30:05 +02001252static unsigned long dma_ops_area_alloc(struct device *dev,
1253 struct dma_ops_domain *dom,
1254 unsigned int pages,
1255 unsigned long align_mask,
1256 u64 dma_mask,
1257 unsigned long start)
1258{
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001259 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001260 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1261 int i = start >> APERTURE_RANGE_SHIFT;
1262 unsigned long boundary_size;
1263 unsigned long address = -1;
1264 unsigned long limit;
1265
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001266 next_bit >>= PAGE_SHIFT;
1267
Joerg Roedel384de722009-05-15 12:30:05 +02001268 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1269 PAGE_SIZE) >> PAGE_SHIFT;
1270
1271 for (;i < max_index; ++i) {
1272 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1273
1274 if (dom->aperture[i]->offset >= dma_mask)
1275 break;
1276
1277 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1278 dma_mask >> PAGE_SHIFT);
1279
1280 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1281 limit, next_bit, pages, 0,
1282 boundary_size, align_mask);
1283 if (address != -1) {
1284 address = dom->aperture[i]->offset +
1285 (address << PAGE_SHIFT);
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001286 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001287 break;
1288 }
1289
1290 next_bit = 0;
1291 }
1292
1293 return address;
1294}
1295
Joerg Roedeld3086442008-06-26 21:27:57 +02001296static unsigned long dma_ops_alloc_addresses(struct device *dev,
1297 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001298 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001299 unsigned long align_mask,
1300 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001301{
Joerg Roedeld3086442008-06-26 21:27:57 +02001302 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001303
Joerg Roedelfe16f082009-05-22 12:27:53 +02001304#ifdef CONFIG_IOMMU_STRESS
1305 dom->next_address = 0;
1306 dom->need_flush = true;
1307#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001308
Joerg Roedel384de722009-05-15 12:30:05 +02001309 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001310 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001311
Joerg Roedel1c655772008-09-04 18:40:05 +02001312 if (address == -1) {
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001313 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001314 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1315 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001316 dom->need_flush = true;
1317 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001318
Joerg Roedel384de722009-05-15 12:30:05 +02001319 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001320 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001321
1322 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1323
1324 return address;
1325}
1326
Joerg Roedel431b2a22008-07-11 17:14:22 +02001327/*
1328 * The address free function.
1329 *
1330 * called with domain->lock held
1331 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001332static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1333 unsigned long address,
1334 unsigned int pages)
1335{
Joerg Roedel384de722009-05-15 12:30:05 +02001336 unsigned i = address >> APERTURE_RANGE_SHIFT;
1337 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001338
Joerg Roedel384de722009-05-15 12:30:05 +02001339 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1340
Joerg Roedel47bccd62009-05-22 12:40:54 +02001341#ifdef CONFIG_IOMMU_STRESS
1342 if (i < 4)
1343 return;
1344#endif
1345
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001346 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001347 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001348
1349 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001350
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001351 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001352
Joerg Roedeld3086442008-06-26 21:27:57 +02001353}
1354
Joerg Roedel431b2a22008-07-11 17:14:22 +02001355/****************************************************************************
1356 *
1357 * The next functions belong to the domain allocation. A domain is
1358 * allocated for every IOMMU as the default domain. If device isolation
1359 * is enabled, every device get its own domain. The most important thing
1360 * about domains is the page table mapping the DMA address space they
1361 * contain.
1362 *
1363 ****************************************************************************/
1364
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001365/*
1366 * This function adds a protection domain to the global protection domain list
1367 */
1368static void add_domain_to_list(struct protection_domain *domain)
1369{
1370 unsigned long flags;
1371
1372 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1373 list_add(&domain->list, &amd_iommu_pd_list);
1374 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1375}
1376
1377/*
1378 * This function removes a protection domain to the global
1379 * protection domain list
1380 */
1381static void del_domain_from_list(struct protection_domain *domain)
1382{
1383 unsigned long flags;
1384
1385 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1386 list_del(&domain->list);
1387 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1388}
1389
Joerg Roedelec487d12008-06-26 21:27:58 +02001390static u16 domain_id_alloc(void)
1391{
1392 unsigned long flags;
1393 int id;
1394
1395 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1396 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1397 BUG_ON(id == 0);
1398 if (id > 0 && id < MAX_DOMAIN_ID)
1399 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1400 else
1401 id = 0;
1402 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1403
1404 return id;
1405}
1406
Joerg Roedela2acfb72008-12-02 18:28:53 +01001407static void domain_id_free(int id)
1408{
1409 unsigned long flags;
1410
1411 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1412 if (id > 0 && id < MAX_DOMAIN_ID)
1413 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1414 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1415}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001416
Joerg Roedel86db2e52008-12-02 18:20:21 +01001417static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001418{
1419 int i, j;
1420 u64 *p1, *p2, *p3;
1421
Joerg Roedel86db2e52008-12-02 18:20:21 +01001422 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001423
1424 if (!p1)
1425 return;
1426
1427 for (i = 0; i < 512; ++i) {
1428 if (!IOMMU_PTE_PRESENT(p1[i]))
1429 continue;
1430
1431 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001432 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001433 if (!IOMMU_PTE_PRESENT(p2[j]))
1434 continue;
1435 p3 = IOMMU_PTE_PAGE(p2[j]);
1436 free_page((unsigned long)p3);
1437 }
1438
1439 free_page((unsigned long)p2);
1440 }
1441
1442 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001443
1444 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001445}
1446
Joerg Roedel431b2a22008-07-11 17:14:22 +02001447/*
1448 * Free a domain, only used if something went wrong in the
1449 * allocation path and we need to free an already allocated page table
1450 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001451static void dma_ops_domain_free(struct dma_ops_domain *dom)
1452{
Joerg Roedel384de722009-05-15 12:30:05 +02001453 int i;
1454
Joerg Roedelec487d12008-06-26 21:27:58 +02001455 if (!dom)
1456 return;
1457
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001458 del_domain_from_list(&dom->domain);
1459
Joerg Roedel86db2e52008-12-02 18:20:21 +01001460 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001461
Joerg Roedel384de722009-05-15 12:30:05 +02001462 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1463 if (!dom->aperture[i])
1464 continue;
1465 free_page((unsigned long)dom->aperture[i]->bitmap);
1466 kfree(dom->aperture[i]);
1467 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001468
1469 kfree(dom);
1470}
1471
Joerg Roedel431b2a22008-07-11 17:14:22 +02001472/*
1473 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001474 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001475 * structures required for the dma_ops interface
1476 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001477static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001478{
1479 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001480
1481 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1482 if (!dma_dom)
1483 return NULL;
1484
1485 spin_lock_init(&dma_dom->domain.lock);
1486
1487 dma_dom->domain.id = domain_id_alloc();
1488 if (dma_dom->domain.id == 0)
1489 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001490 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001491 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001492 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001493 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001494 dma_dom->domain.priv = dma_dom;
1495 if (!dma_dom->domain.pt_root)
1496 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001497
Joerg Roedel1c655772008-09-04 18:40:05 +02001498 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001499 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001500
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001501 add_domain_to_list(&dma_dom->domain);
1502
Joerg Roedel576175c2009-11-23 19:08:46 +01001503 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001504 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001505
Joerg Roedel431b2a22008-07-11 17:14:22 +02001506 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001507 * mark the first page as allocated so we never return 0 as
1508 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001509 */
Joerg Roedel384de722009-05-15 12:30:05 +02001510 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001511 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001512
Joerg Roedelec487d12008-06-26 21:27:58 +02001513
1514 return dma_dom;
1515
1516free_dma_dom:
1517 dma_ops_domain_free(dma_dom);
1518
1519 return NULL;
1520}
1521
Joerg Roedel431b2a22008-07-11 17:14:22 +02001522/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001523 * little helper function to check whether a given protection domain is a
1524 * dma_ops domain
1525 */
1526static bool dma_ops_domain(struct protection_domain *domain)
1527{
1528 return domain->flags & PD_DMA_OPS_MASK;
1529}
1530
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001531static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001532{
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001533 u64 pte_root = virt_to_phys(domain->pt_root);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001534 u32 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001535
Joerg Roedel38ddf412008-09-11 10:38:32 +02001536 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1537 << DEV_ENTRY_MODE_SHIFT;
1538 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001539
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001540 if (ats)
1541 flags |= DTE_FLAG_IOTLB;
1542
1543 amd_iommu_dev_table[devid].data[3] |= flags;
1544 amd_iommu_dev_table[devid].data[2] = domain->id;
1545 amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
1546 amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001547}
1548
Joerg Roedel15898bb2009-11-24 15:39:42 +01001549static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001550{
Joerg Roedel355bf552008-12-08 12:02:41 +01001551 /* remove entry from the device table seen by the hardware */
1552 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1553 amd_iommu_dev_table[devid].data[1] = 0;
1554 amd_iommu_dev_table[devid].data[2] = 0;
1555
Joerg Roedelc5cca142009-10-09 18:31:20 +02001556 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001557}
1558
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001559static void do_attach(struct iommu_dev_data *dev_data,
1560 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001561{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001562 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001563 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001564
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001565 iommu = amd_iommu_rlookup_table[dev_data->devid];
1566 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001567
1568 /* Update data structures */
1569 dev_data->domain = domain;
1570 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001571 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001572
1573 /* Do reference counting */
1574 domain->dev_iommu[iommu->index] += 1;
1575 domain->dev_cnt += 1;
1576
1577 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001578 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001579}
1580
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001581static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001582{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001583 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001584
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001585 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02001586
Joerg Roedelc4596112009-11-20 14:57:32 +01001587 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001588 dev_data->domain->dev_iommu[iommu->index] -= 1;
1589 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001590
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001591 /* Update data structures */
1592 dev_data->domain = NULL;
1593 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001594 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001595
1596 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001597 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001598}
1599
1600/*
1601 * If a device is not yet associated with a domain, this function does
1602 * assigns it visible for the hardware
1603 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001604static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01001605 struct protection_domain *domain)
1606{
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001607 struct iommu_dev_data *alias_data;
Julia Lawall84fe6c12010-05-27 12:31:51 +02001608 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001609
Joerg Roedel657cbb62009-11-23 15:26:46 +01001610 alias_data = get_dev_data(dev_data->alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001611
Joerg Roedel657cbb62009-11-23 15:26:46 +01001612 if (!alias_data)
1613 return -EINVAL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001614
1615 /* lock domain */
1616 spin_lock(&domain->lock);
1617
1618 /* Some sanity checks */
Julia Lawall84fe6c12010-05-27 12:31:51 +02001619 ret = -EBUSY;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001620 if (alias_data->domain != NULL &&
1621 alias_data->domain != domain)
Julia Lawall84fe6c12010-05-27 12:31:51 +02001622 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001623
Joerg Roedel657cbb62009-11-23 15:26:46 +01001624 if (dev_data->domain != NULL &&
1625 dev_data->domain != domain)
Julia Lawall84fe6c12010-05-27 12:31:51 +02001626 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001627
1628 /* Do real assignment */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001629 if (dev_data->devid != alias_data->devid) {
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001630 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001631 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01001632
1633 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001634 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01001635
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001636 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001637 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001638
Joerg Roedel24100052009-11-25 15:59:57 +01001639 atomic_inc(&dev_data->bind);
1640
Julia Lawall84fe6c12010-05-27 12:31:51 +02001641 ret = 0;
1642
1643out_unlock:
1644
Joerg Roedel355bf552008-12-08 12:02:41 +01001645 /* ready */
1646 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001647
Julia Lawall84fe6c12010-05-27 12:31:51 +02001648 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001649}
1650
1651/*
1652 * If a device is not yet associated with a domain, this function does
1653 * assigns it visible for the hardware
1654 */
1655static int attach_device(struct device *dev,
1656 struct protection_domain *domain)
1657{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001658 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001659 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001660 unsigned long flags;
1661 int ret;
1662
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001663 dev_data = get_dev_data(dev);
1664
1665 if (amd_iommu_iotlb_sup && pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
1666 dev_data->ats.enabled = true;
1667 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1668 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001669
Joerg Roedel15898bb2009-11-24 15:39:42 +01001670 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001671 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001672 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1673
1674 /*
1675 * We might boot into a crash-kernel here. The crashed kernel
1676 * left the caches in the IOMMU dirty. So we have to flush
1677 * here to evict all dirty stuff.
1678 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001679 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001680
1681 return ret;
1682}
1683
1684/*
1685 * Removes a device from a protection domain (unlocked)
1686 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001687static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01001688{
Joerg Roedel24100052009-11-25 15:59:57 +01001689 struct iommu_dev_data *alias_data;
Joerg Roedel2ca76272010-01-22 16:45:31 +01001690 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001691 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001692
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001693 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001694
Joerg Roedel2ca76272010-01-22 16:45:31 +01001695 domain = dev_data->domain;
1696
1697 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01001698
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001699 alias_data = get_dev_data(dev_data->alias);
1700 if (dev_data->devid != alias_data->devid) {
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001701 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001702 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01001703 }
1704
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001705 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001706 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001707
Joerg Roedel2ca76272010-01-22 16:45:31 +01001708 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001709
Joerg Roedel21129f72009-09-01 11:59:42 +02001710 /*
1711 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001712 * passthrough domain if it is detached from any other domain.
1713 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02001714 */
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001715 if (iommu_pass_through &&
1716 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001717 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01001718}
1719
1720/*
1721 * Removes a device from a protection domain (with devtable_lock held)
1722 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01001723static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01001724{
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001725 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01001726 unsigned long flags;
1727
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001728 dev_data = get_dev_data(dev);
1729
Joerg Roedel355bf552008-12-08 12:02:41 +01001730 /* lock device table */
1731 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001732 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01001733 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001734
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001735 if (dev_data->ats.enabled) {
1736 pci_disable_ats(to_pci_dev(dev));
1737 dev_data->ats.enabled = false;
1738 }
Joerg Roedel355bf552008-12-08 12:02:41 +01001739}
Joerg Roedele275a2a2008-12-10 18:27:25 +01001740
Joerg Roedel15898bb2009-11-24 15:39:42 +01001741/*
1742 * Find out the protection domain structure for a given PCI device. This
1743 * will give us the pointer to the page table root for example.
1744 */
1745static struct protection_domain *domain_for_device(struct device *dev)
1746{
1747 struct protection_domain *dom;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001748 struct iommu_dev_data *dev_data, *alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001749 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001750
Joerg Roedel657cbb62009-11-23 15:26:46 +01001751 dev_data = get_dev_data(dev);
1752 alias_data = get_dev_data(dev_data->alias);
1753 if (!alias_data)
1754 return NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001755
1756 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001757 dom = dev_data->domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001758 if (dom == NULL &&
Joerg Roedel657cbb62009-11-23 15:26:46 +01001759 alias_data->domain != NULL) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001760 __attach_device(dev_data, alias_data->domain);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001761 dom = alias_data->domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001762 }
1763
1764 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1765
1766 return dom;
1767}
1768
Joerg Roedele275a2a2008-12-10 18:27:25 +01001769static int device_change_notifier(struct notifier_block *nb,
1770 unsigned long action, void *data)
1771{
1772 struct device *dev = data;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001773 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001774 struct protection_domain *domain;
1775 struct dma_ops_domain *dma_domain;
1776 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001777 unsigned long flags;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001778
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001779 if (!check_device(dev))
1780 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001781
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001782 devid = get_device_id(dev);
1783 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01001784
1785 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07001786 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001787
1788 domain = domain_for_device(dev);
1789
Joerg Roedele275a2a2008-12-10 18:27:25 +01001790 if (!domain)
1791 goto out;
Joerg Roedela1ca3312009-09-01 12:22:22 +02001792 if (iommu_pass_through)
1793 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001794 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001795 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001796 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001797
1798 iommu_init_device(dev);
1799
1800 domain = domain_for_device(dev);
1801
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001802 /* allocate a protection domain if a device is added */
1803 dma_domain = find_protection_domain(devid);
1804 if (dma_domain)
1805 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01001806 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001807 if (!dma_domain)
1808 goto out;
1809 dma_domain->target_dev = devid;
1810
1811 spin_lock_irqsave(&iommu_pd_list_lock, flags);
1812 list_add_tail(&dma_domain->list, &iommu_pd_list);
1813 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
1814
1815 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001816 case BUS_NOTIFY_DEL_DEVICE:
1817
1818 iommu_uninit_device(dev);
1819
Joerg Roedele275a2a2008-12-10 18:27:25 +01001820 default:
1821 goto out;
1822 }
1823
Joerg Roedele275a2a2008-12-10 18:27:25 +01001824 iommu_completion_wait(iommu);
1825
1826out:
1827 return 0;
1828}
1829
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05301830static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01001831 .notifier_call = device_change_notifier,
1832};
Joerg Roedel355bf552008-12-08 12:02:41 +01001833
Joerg Roedel8638c492009-12-10 11:12:25 +01001834void amd_iommu_init_notifier(void)
1835{
1836 bus_register_notifier(&pci_bus_type, &device_nb);
1837}
1838
Joerg Roedel431b2a22008-07-11 17:14:22 +02001839/*****************************************************************************
1840 *
1841 * The next functions belong to the dma_ops mapping/unmapping code.
1842 *
1843 *****************************************************************************/
1844
1845/*
1846 * In the dma_ops path we only have the struct device. This function
1847 * finds the corresponding IOMMU, the protection domain and the
1848 * requestor id for a given device.
1849 * If the device is not yet associated with a domain this is also done
1850 * in this function.
1851 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001852static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001853{
Joerg Roedel94f6d192009-11-24 16:40:02 +01001854 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001855 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01001856 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001857
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001858 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01001859 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001860
Joerg Roedel94f6d192009-11-24 16:40:02 +01001861 domain = domain_for_device(dev);
1862 if (domain != NULL && !dma_ops_domain(domain))
1863 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001864
Joerg Roedel94f6d192009-11-24 16:40:02 +01001865 if (domain != NULL)
1866 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001867
Joerg Roedel15898bb2009-11-24 15:39:42 +01001868 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001869 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001870 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01001871 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
1872 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001873 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01001874 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01001875
Joerg Roedel94f6d192009-11-24 16:40:02 +01001876 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001877}
1878
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001879static void update_device_table(struct protection_domain *domain)
1880{
Joerg Roedel492667d2009-11-27 13:25:47 +01001881 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001882
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001883 list_for_each_entry(dev_data, &domain->dev_list, list)
1884 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001885}
1886
1887static void update_domain(struct protection_domain *domain)
1888{
1889 if (!domain->updated)
1890 return;
1891
1892 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02001893
1894 domain_flush_devices(domain);
1895 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001896
1897 domain->updated = false;
1898}
1899
Joerg Roedel431b2a22008-07-11 17:14:22 +02001900/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02001901 * This function fetches the PTE for a given address in the aperture
1902 */
1903static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
1904 unsigned long address)
1905{
Joerg Roedel384de722009-05-15 12:30:05 +02001906 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02001907 u64 *pte, *pte_page;
1908
Joerg Roedel384de722009-05-15 12:30:05 +02001909 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1910 if (!aperture)
1911 return NULL;
1912
1913 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02001914 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001915 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001916 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02001917 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
1918 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001919 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001920
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001921 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001922
1923 return pte;
1924}
1925
1926/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001927 * This is the generic map function. It maps one 4kb page at paddr to
1928 * the given address in the DMA address space for the domain.
1929 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001930static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001931 unsigned long address,
1932 phys_addr_t paddr,
1933 int direction)
1934{
1935 u64 *pte, __pte;
1936
1937 WARN_ON(address > dom->aperture_size);
1938
1939 paddr &= PAGE_MASK;
1940
Joerg Roedel8bda3092009-05-12 12:02:46 +02001941 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02001942 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001943 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001944
1945 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
1946
1947 if (direction == DMA_TO_DEVICE)
1948 __pte |= IOMMU_PTE_IR;
1949 else if (direction == DMA_FROM_DEVICE)
1950 __pte |= IOMMU_PTE_IW;
1951 else if (direction == DMA_BIDIRECTIONAL)
1952 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
1953
1954 WARN_ON(*pte);
1955
1956 *pte = __pte;
1957
1958 return (dma_addr_t)address;
1959}
1960
Joerg Roedel431b2a22008-07-11 17:14:22 +02001961/*
1962 * The generic unmapping function for on page in the DMA address space.
1963 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001964static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001965 unsigned long address)
1966{
Joerg Roedel384de722009-05-15 12:30:05 +02001967 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001968 u64 *pte;
1969
1970 if (address >= dom->aperture_size)
1971 return;
1972
Joerg Roedel384de722009-05-15 12:30:05 +02001973 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1974 if (!aperture)
1975 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001976
Joerg Roedel384de722009-05-15 12:30:05 +02001977 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
1978 if (!pte)
1979 return;
1980
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001981 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001982
1983 WARN_ON(!*pte);
1984
1985 *pte = 0ULL;
1986}
1987
Joerg Roedel431b2a22008-07-11 17:14:22 +02001988/*
1989 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01001990 * contiguous memory region into DMA address space. It is used by all
1991 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001992 * Must be called with the domain lock held.
1993 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02001994static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001995 struct dma_ops_domain *dma_dom,
1996 phys_addr_t paddr,
1997 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001998 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001999 bool align,
2000 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002001{
2002 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002003 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002004 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002005 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002006 int i;
2007
Joerg Roedele3c449f2008-10-15 22:02:11 -07002008 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002009 paddr &= PAGE_MASK;
2010
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002011 INC_STATS_COUNTER(total_map_requests);
2012
Joerg Roedelc1858972008-12-12 15:42:39 +01002013 if (pages > 1)
2014 INC_STATS_COUNTER(cross_page);
2015
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002016 if (align)
2017 align_mask = (1UL << get_order(size)) - 1;
2018
Joerg Roedel11b83882009-05-19 10:23:15 +02002019retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002020 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2021 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002022 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002023 /*
2024 * setting next_address here will let the address
2025 * allocator only scan the new allocated range in the
2026 * first run. This is a small optimization.
2027 */
2028 dma_dom->next_address = dma_dom->aperture_size;
2029
Joerg Roedel576175c2009-11-23 19:08:46 +01002030 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002031 goto out;
2032
2033 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002034 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002035 * allocation again
2036 */
2037 goto retry;
2038 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002039
2040 start = address;
2041 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002042 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002043 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002044 goto out_unmap;
2045
Joerg Roedelcb76c322008-06-26 21:28:00 +02002046 paddr += PAGE_SIZE;
2047 start += PAGE_SIZE;
2048 }
2049 address += offset;
2050
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002051 ADD_STATS_COUNTER(alloced_io_mem, size);
2052
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002053 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002054 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002055 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002056 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002057 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002058
Joerg Roedelcb76c322008-06-26 21:28:00 +02002059out:
2060 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002061
2062out_unmap:
2063
2064 for (--i; i >= 0; --i) {
2065 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002066 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002067 }
2068
2069 dma_ops_free_addresses(dma_dom, address, pages);
2070
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002071 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002072}
2073
Joerg Roedel431b2a22008-07-11 17:14:22 +02002074/*
2075 * Does the reverse of the __map_single function. Must be called with
2076 * the domain lock held too
2077 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002078static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002079 dma_addr_t dma_addr,
2080 size_t size,
2081 int dir)
2082{
Joerg Roedel04e04632010-09-23 16:12:48 +02002083 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002084 dma_addr_t i, start;
2085 unsigned int pages;
2086
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002087 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002088 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002089 return;
2090
Joerg Roedel04e04632010-09-23 16:12:48 +02002091 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002092 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002093 dma_addr &= PAGE_MASK;
2094 start = dma_addr;
2095
2096 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002097 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002098 start += PAGE_SIZE;
2099 }
2100
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002101 SUB_STATS_COUNTER(alloced_io_mem, size);
2102
Joerg Roedelcb76c322008-06-26 21:28:00 +02002103 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002104
Joerg Roedel80be3082008-11-06 14:59:05 +01002105 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002106 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002107 dma_dom->need_flush = false;
2108 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002109}
2110
Joerg Roedel431b2a22008-07-11 17:14:22 +02002111/*
2112 * The exported map_single function for dma_ops.
2113 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002114static dma_addr_t map_page(struct device *dev, struct page *page,
2115 unsigned long offset, size_t size,
2116 enum dma_data_direction dir,
2117 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002118{
2119 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002120 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002121 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002122 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002123 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002124
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002125 INC_STATS_COUNTER(cnt_map_single);
2126
Joerg Roedel94f6d192009-11-24 16:40:02 +01002127 domain = get_domain(dev);
2128 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002129 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002130 else if (IS_ERR(domain))
2131 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002132
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002133 dma_mask = *dev->dma_mask;
2134
Joerg Roedel4da70b92008-06-26 21:28:01 +02002135 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002136
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002137 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002138 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002139 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002140 goto out;
2141
Joerg Roedel17b124b2011-04-06 18:01:35 +02002142 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002143
2144out:
2145 spin_unlock_irqrestore(&domain->lock, flags);
2146
2147 return addr;
2148}
2149
Joerg Roedel431b2a22008-07-11 17:14:22 +02002150/*
2151 * The exported unmap_single function for dma_ops.
2152 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002153static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2154 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002155{
2156 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002157 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002158
Joerg Roedel146a6912008-12-12 15:07:12 +01002159 INC_STATS_COUNTER(cnt_unmap_single);
2160
Joerg Roedel94f6d192009-11-24 16:40:02 +01002161 domain = get_domain(dev);
2162 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002163 return;
2164
Joerg Roedel4da70b92008-06-26 21:28:01 +02002165 spin_lock_irqsave(&domain->lock, flags);
2166
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002167 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002168
Joerg Roedel17b124b2011-04-06 18:01:35 +02002169 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002170
2171 spin_unlock_irqrestore(&domain->lock, flags);
2172}
2173
Joerg Roedel431b2a22008-07-11 17:14:22 +02002174/*
2175 * This is a special map_sg function which is used if we should map a
2176 * device which is not handled by an AMD IOMMU in the system.
2177 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002178static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2179 int nelems, int dir)
2180{
2181 struct scatterlist *s;
2182 int i;
2183
2184 for_each_sg(sglist, s, nelems, i) {
2185 s->dma_address = (dma_addr_t)sg_phys(s);
2186 s->dma_length = s->length;
2187 }
2188
2189 return nelems;
2190}
2191
Joerg Roedel431b2a22008-07-11 17:14:22 +02002192/*
2193 * The exported map_sg function for dma_ops (handles scatter-gather
2194 * lists).
2195 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002196static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002197 int nelems, enum dma_data_direction dir,
2198 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002199{
2200 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002201 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002202 int i;
2203 struct scatterlist *s;
2204 phys_addr_t paddr;
2205 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002206 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002207
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002208 INC_STATS_COUNTER(cnt_map_sg);
2209
Joerg Roedel94f6d192009-11-24 16:40:02 +01002210 domain = get_domain(dev);
2211 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002212 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002213 else if (IS_ERR(domain))
2214 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002215
Joerg Roedel832a90c2008-09-18 15:54:23 +02002216 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002217
Joerg Roedel65b050a2008-06-26 21:28:02 +02002218 spin_lock_irqsave(&domain->lock, flags);
2219
2220 for_each_sg(sglist, s, nelems, i) {
2221 paddr = sg_phys(s);
2222
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002223 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002224 paddr, s->length, dir, false,
2225 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002226
2227 if (s->dma_address) {
2228 s->dma_length = s->length;
2229 mapped_elems++;
2230 } else
2231 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002232 }
2233
Joerg Roedel17b124b2011-04-06 18:01:35 +02002234 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002235
2236out:
2237 spin_unlock_irqrestore(&domain->lock, flags);
2238
2239 return mapped_elems;
2240unmap:
2241 for_each_sg(sglist, s, mapped_elems, i) {
2242 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002243 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002244 s->dma_length, dir);
2245 s->dma_address = s->dma_length = 0;
2246 }
2247
2248 mapped_elems = 0;
2249
2250 goto out;
2251}
2252
Joerg Roedel431b2a22008-07-11 17:14:22 +02002253/*
2254 * The exported map_sg function for dma_ops (handles scatter-gather
2255 * lists).
2256 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002257static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002258 int nelems, enum dma_data_direction dir,
2259 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002260{
2261 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002262 struct protection_domain *domain;
2263 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002264 int i;
2265
Joerg Roedel55877a62008-12-12 15:12:14 +01002266 INC_STATS_COUNTER(cnt_unmap_sg);
2267
Joerg Roedel94f6d192009-11-24 16:40:02 +01002268 domain = get_domain(dev);
2269 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002270 return;
2271
Joerg Roedel65b050a2008-06-26 21:28:02 +02002272 spin_lock_irqsave(&domain->lock, flags);
2273
2274 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002275 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002276 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002277 s->dma_address = s->dma_length = 0;
2278 }
2279
Joerg Roedel17b124b2011-04-06 18:01:35 +02002280 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002281
2282 spin_unlock_irqrestore(&domain->lock, flags);
2283}
2284
Joerg Roedel431b2a22008-07-11 17:14:22 +02002285/*
2286 * The exported alloc_coherent function for dma_ops.
2287 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002288static void *alloc_coherent(struct device *dev, size_t size,
2289 dma_addr_t *dma_addr, gfp_t flag)
2290{
2291 unsigned long flags;
2292 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002293 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002294 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002295 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002296
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002297 INC_STATS_COUNTER(cnt_alloc_coherent);
2298
Joerg Roedel94f6d192009-11-24 16:40:02 +01002299 domain = get_domain(dev);
2300 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002301 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2302 *dma_addr = __pa(virt_addr);
2303 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002304 } else if (IS_ERR(domain))
2305 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002306
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002307 dma_mask = dev->coherent_dma_mask;
2308 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2309 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002310
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002311 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2312 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302313 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002314
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002315 paddr = virt_to_phys(virt_addr);
2316
Joerg Roedel832a90c2008-09-18 15:54:23 +02002317 if (!dma_mask)
2318 dma_mask = *dev->dma_mask;
2319
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002320 spin_lock_irqsave(&domain->lock, flags);
2321
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002322 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002323 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002324
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002325 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002326 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002327 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002328 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002329
Joerg Roedel17b124b2011-04-06 18:01:35 +02002330 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002331
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002332 spin_unlock_irqrestore(&domain->lock, flags);
2333
2334 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002335
2336out_free:
2337
2338 free_pages((unsigned long)virt_addr, get_order(size));
2339
2340 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002341}
2342
Joerg Roedel431b2a22008-07-11 17:14:22 +02002343/*
2344 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002345 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002346static void free_coherent(struct device *dev, size_t size,
2347 void *virt_addr, dma_addr_t dma_addr)
2348{
2349 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002350 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002351
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002352 INC_STATS_COUNTER(cnt_free_coherent);
2353
Joerg Roedel94f6d192009-11-24 16:40:02 +01002354 domain = get_domain(dev);
2355 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002356 goto free_mem;
2357
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002358 spin_lock_irqsave(&domain->lock, flags);
2359
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002360 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002361
Joerg Roedel17b124b2011-04-06 18:01:35 +02002362 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002363
2364 spin_unlock_irqrestore(&domain->lock, flags);
2365
2366free_mem:
2367 free_pages((unsigned long)virt_addr, get_order(size));
2368}
2369
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002370/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002371 * This function is called by the DMA layer to find out if we can handle a
2372 * particular device. It is part of the dma_ops.
2373 */
2374static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2375{
Joerg Roedel420aef82009-11-23 16:14:57 +01002376 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002377}
2378
2379/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002380 * The function for pre-allocating protection domains.
2381 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002382 * If the driver core informs the DMA layer if a driver grabs a device
2383 * we don't need to preallocate the protection domains anymore.
2384 * For now we have to.
2385 */
Jaswinder Singh Rajput0e93dd82008-12-29 21:45:22 +05302386static void prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002387{
2388 struct pci_dev *dev = NULL;
2389 struct dma_ops_domain *dma_dom;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002390 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002391
Chris Wrightd18c69d2010-04-02 18:27:55 -07002392 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002393
2394 /* Do we handle this device? */
2395 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002396 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002397
2398 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002399 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002400 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002401
2402 devid = get_device_id(&dev->dev);
2403
Joerg Roedel87a64d52009-11-24 17:26:43 +01002404 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002405 if (!dma_dom)
2406 continue;
2407 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002408 dma_dom->target_dev = devid;
2409
Joerg Roedel15898bb2009-11-24 15:39:42 +01002410 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01002411
Joerg Roedelbd60b732008-09-11 10:24:48 +02002412 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002413 }
2414}
2415
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002416static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002417 .alloc_coherent = alloc_coherent,
2418 .free_coherent = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002419 .map_page = map_page,
2420 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002421 .map_sg = map_sg,
2422 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002423 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002424};
2425
Joerg Roedel27c21272011-05-30 15:56:24 +02002426static unsigned device_dma_ops_init(void)
2427{
2428 struct pci_dev *pdev = NULL;
2429 unsigned unhandled = 0;
2430
2431 for_each_pci_dev(pdev) {
2432 if (!check_device(&pdev->dev)) {
2433 unhandled += 1;
2434 continue;
2435 }
2436
2437 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
2438 }
2439
2440 return unhandled;
2441}
2442
Joerg Roedel431b2a22008-07-11 17:14:22 +02002443/*
2444 * The function which clues the AMD IOMMU driver into dma_ops.
2445 */
Joerg Roedelf5325092010-01-22 17:44:35 +01002446
2447void __init amd_iommu_init_api(void)
2448{
2449 register_iommu(&amd_iommu_ops);
2450}
2451
Joerg Roedel6631ee92008-06-26 21:28:05 +02002452int __init amd_iommu_init_dma_ops(void)
2453{
2454 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02002455 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002456
Joerg Roedel431b2a22008-07-11 17:14:22 +02002457 /*
2458 * first allocate a default protection domain for every IOMMU we
2459 * found in the system. Devices not assigned to any other
2460 * protection domain will be assigned to the default one.
2461 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02002462 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01002463 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002464 if (iommu->default_dom == NULL)
2465 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01002466 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002467 ret = iommu_init_unity_mappings(iommu);
2468 if (ret)
2469 goto free_domains;
2470 }
2471
Joerg Roedel431b2a22008-07-11 17:14:22 +02002472 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01002473 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002474 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01002475 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002476
2477 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002478 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002479
Joerg Roedel431b2a22008-07-11 17:14:22 +02002480 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02002481 unhandled = device_dma_ops_init();
2482 if (unhandled && max_pfn > MAX_DMA32_PFN) {
2483 /* There are unhandled devices - initialize swiotlb for them */
2484 swiotlb = 1;
2485 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02002486
Joerg Roedel7f265082008-12-12 13:50:21 +01002487 amd_iommu_stats_init();
2488
Joerg Roedel6631ee92008-06-26 21:28:05 +02002489 return 0;
2490
2491free_domains:
2492
Joerg Roedel3bd22172009-05-04 15:06:20 +02002493 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002494 if (iommu->default_dom)
2495 dma_ops_domain_free(iommu->default_dom);
2496 }
2497
2498 return ret;
2499}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002500
2501/*****************************************************************************
2502 *
2503 * The following functions belong to the exported interface of AMD IOMMU
2504 *
2505 * This interface allows access to lower level functions of the IOMMU
2506 * like protection domain handling and assignement of devices to domains
2507 * which is not possible with the dma_ops interface.
2508 *
2509 *****************************************************************************/
2510
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002511static void cleanup_domain(struct protection_domain *domain)
2512{
Joerg Roedel492667d2009-11-27 13:25:47 +01002513 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002514 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002515
2516 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2517
Joerg Roedel492667d2009-11-27 13:25:47 +01002518 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002519 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01002520 atomic_set(&dev_data->bind, 0);
2521 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002522
2523 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2524}
2525
Joerg Roedel26508152009-08-26 16:52:40 +02002526static void protection_domain_free(struct protection_domain *domain)
2527{
2528 if (!domain)
2529 return;
2530
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002531 del_domain_from_list(domain);
2532
Joerg Roedel26508152009-08-26 16:52:40 +02002533 if (domain->id)
2534 domain_id_free(domain->id);
2535
2536 kfree(domain);
2537}
2538
2539static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002540{
2541 struct protection_domain *domain;
2542
2543 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2544 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002545 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002546
2547 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002548 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01002549 domain->id = domain_id_alloc();
2550 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02002551 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002552 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02002553
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002554 add_domain_to_list(domain);
2555
Joerg Roedel26508152009-08-26 16:52:40 +02002556 return domain;
2557
2558out_err:
2559 kfree(domain);
2560
2561 return NULL;
2562}
2563
2564static int amd_iommu_domain_init(struct iommu_domain *dom)
2565{
2566 struct protection_domain *domain;
2567
2568 domain = protection_domain_alloc();
2569 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01002570 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02002571
2572 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002573 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2574 if (!domain->pt_root)
2575 goto out_free;
2576
2577 dom->priv = domain;
2578
2579 return 0;
2580
2581out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02002582 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01002583
2584 return -ENOMEM;
2585}
2586
Joerg Roedel98383fc2008-12-02 18:34:12 +01002587static void amd_iommu_domain_destroy(struct iommu_domain *dom)
2588{
2589 struct protection_domain *domain = dom->priv;
2590
2591 if (!domain)
2592 return;
2593
2594 if (domain->dev_cnt > 0)
2595 cleanup_domain(domain);
2596
2597 BUG_ON(domain->dev_cnt != 0);
2598
2599 free_pagetable(domain);
2600
Joerg Roedel8b408fe2010-03-08 14:20:07 +01002601 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002602
2603 dom->priv = NULL;
2604}
2605
Joerg Roedel684f2882008-12-08 12:07:44 +01002606static void amd_iommu_detach_device(struct iommu_domain *dom,
2607 struct device *dev)
2608{
Joerg Roedel657cbb62009-11-23 15:26:46 +01002609 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002610 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002611 u16 devid;
2612
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002613 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01002614 return;
2615
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002616 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002617
Joerg Roedel657cbb62009-11-23 15:26:46 +01002618 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002619 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002620
2621 iommu = amd_iommu_rlookup_table[devid];
2622 if (!iommu)
2623 return;
2624
Joerg Roedel684f2882008-12-08 12:07:44 +01002625 iommu_completion_wait(iommu);
2626}
2627
Joerg Roedel01106062008-12-02 19:34:11 +01002628static int amd_iommu_attach_device(struct iommu_domain *dom,
2629 struct device *dev)
2630{
2631 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002632 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01002633 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002634 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002635
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002636 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01002637 return -EINVAL;
2638
Joerg Roedel657cbb62009-11-23 15:26:46 +01002639 dev_data = dev->archdata.iommu;
2640
Joerg Roedelf62dda62011-06-09 12:55:35 +02002641 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01002642 if (!iommu)
2643 return -EINVAL;
2644
Joerg Roedel657cbb62009-11-23 15:26:46 +01002645 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002646 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002647
Joerg Roedel15898bb2009-11-24 15:39:42 +01002648 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01002649
2650 iommu_completion_wait(iommu);
2651
Joerg Roedel15898bb2009-11-24 15:39:42 +01002652 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002653}
2654
Joerg Roedel468e2362010-01-21 16:37:36 +01002655static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
2656 phys_addr_t paddr, int gfp_order, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002657{
Joerg Roedel468e2362010-01-21 16:37:36 +01002658 unsigned long page_size = 0x1000UL << gfp_order;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002659 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002660 int prot = 0;
2661 int ret;
2662
2663 if (iommu_prot & IOMMU_READ)
2664 prot |= IOMMU_PROT_IR;
2665 if (iommu_prot & IOMMU_WRITE)
2666 prot |= IOMMU_PROT_IW;
2667
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002668 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002669 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002670 mutex_unlock(&domain->api_lock);
2671
Joerg Roedel795e74f72010-05-11 17:40:57 +02002672 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002673}
2674
Joerg Roedel468e2362010-01-21 16:37:36 +01002675static int amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2676 int gfp_order)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002677{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002678 struct protection_domain *domain = dom->priv;
Joerg Roedel468e2362010-01-21 16:37:36 +01002679 unsigned long page_size, unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002680
Joerg Roedel468e2362010-01-21 16:37:36 +01002681 page_size = 0x1000UL << gfp_order;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002682
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002683 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01002684 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002685 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002686
Joerg Roedel17b124b2011-04-06 18:01:35 +02002687 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002688
Joerg Roedel468e2362010-01-21 16:37:36 +01002689 return get_order(unmap_size);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002690}
2691
Joerg Roedel645c4c82008-12-02 20:05:50 +01002692static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2693 unsigned long iova)
2694{
2695 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002696 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002697 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002698 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002699
Joerg Roedel24cd7722010-01-19 17:27:39 +01002700 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002701
Joerg Roedela6d41a42009-09-02 17:08:55 +02002702 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01002703 return 0;
2704
Joerg Roedelf03152b2010-01-21 16:15:24 +01002705 if (PM_PTE_LEVEL(*pte) == 0)
2706 offset_mask = PAGE_SIZE - 1;
2707 else
2708 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
2709
2710 __pte = *pte & PM_ADDR_MASK;
2711 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002712
2713 return paddr;
2714}
2715
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002716static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
2717 unsigned long cap)
2718{
Joerg Roedel80a506b2010-07-27 17:14:24 +02002719 switch (cap) {
2720 case IOMMU_CAP_CACHE_COHERENCY:
2721 return 1;
2722 }
2723
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002724 return 0;
2725}
2726
Joerg Roedel26961ef2008-12-03 17:00:17 +01002727static struct iommu_ops amd_iommu_ops = {
2728 .domain_init = amd_iommu_domain_init,
2729 .domain_destroy = amd_iommu_domain_destroy,
2730 .attach_dev = amd_iommu_attach_device,
2731 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01002732 .map = amd_iommu_map,
2733 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002734 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002735 .domain_has_cap = amd_iommu_domain_has_cap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002736};
2737
Joerg Roedel0feae532009-08-26 15:26:30 +02002738/*****************************************************************************
2739 *
2740 * The next functions do a basic initialization of IOMMU for pass through
2741 * mode
2742 *
2743 * In passthrough mode the IOMMU is initialized and enabled but not used for
2744 * DMA-API translation.
2745 *
2746 *****************************************************************************/
2747
2748int __init amd_iommu_init_passthrough(void)
2749{
Joerg Roedel15898bb2009-11-24 15:39:42 +01002750 struct amd_iommu *iommu;
Joerg Roedel0feae532009-08-26 15:26:30 +02002751 struct pci_dev *dev = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002752 u16 devid;
Joerg Roedel0feae532009-08-26 15:26:30 +02002753
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002754 /* allocate passthrough domain */
Joerg Roedel0feae532009-08-26 15:26:30 +02002755 pt_domain = protection_domain_alloc();
2756 if (!pt_domain)
2757 return -ENOMEM;
2758
2759 pt_domain->mode |= PAGE_MODE_NONE;
2760
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04002761 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002762 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02002763 continue;
2764
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002765 devid = get_device_id(&dev->dev);
2766
Joerg Roedel15898bb2009-11-24 15:39:42 +01002767 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02002768 if (!iommu)
2769 continue;
2770
Joerg Roedel15898bb2009-11-24 15:39:42 +01002771 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02002772 }
2773
2774 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
2775
2776 return 0;
2777}