blob: 405f8dad7c7737a9a4d1b5030119ef2f49d829be [file] [log] [blame]
Joerg Roedelb6c02712008-06-26 21:27:53 +02001/*
Joerg Roedelbf3118c2009-11-20 13:39:19 +01002 * Copyright (C) 2007-2009 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>
21#include <linux/gfp.h>
22#include <linux/bitops.h>
Joerg Roedel7f265082008-12-12 13:50:21 +010023#include <linux/debugfs.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020024#include <linux/scatterlist.h>
FUJITA Tomonori51491362009-01-05 23:47:25 +090025#include <linux/dma-mapping.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020026#include <linux/iommu-helper.h>
Joerg Roedelc156e342008-12-02 18:13:27 +010027#include <linux/iommu.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020028#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090029#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010030#include <asm/gart.h>
Joerg Roedel6a9401a2009-11-20 13:22:21 +010031#include <asm/amd_iommu_proto.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020032#include <asm/amd_iommu_types.h>
Joerg Roedelc6da9922008-06-26 21:28:06 +020033#include <asm/amd_iommu.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020034
35#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
36
Joerg Roedel136f78a2008-07-11 17:14:27 +020037#define EXIT_LOOP_COUNT 10000000
38
Joerg Roedelb6c02712008-06-26 21:27:53 +020039static DEFINE_RWLOCK(amd_iommu_devtable_lock);
40
Joerg Roedelbd60b732008-09-11 10:24:48 +020041/* A list of preallocated protection domains */
42static LIST_HEAD(iommu_pd_list);
43static DEFINE_SPINLOCK(iommu_pd_list_lock);
44
Joerg Roedel0feae532009-08-26 15:26:30 +020045/*
46 * Domain for untranslated devices - only allocated
47 * if iommu=pt passed on kernel cmd line.
48 */
49static struct protection_domain *pt_domain;
50
Joerg Roedel26961ef2008-12-03 17:00:17 +010051static struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010052
Joerg Roedel431b2a22008-07-11 17:14:22 +020053/*
54 * general struct to manage commands send to an IOMMU
55 */
Joerg Roedeld6449532008-07-11 17:14:28 +020056struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +020057 u32 data[4];
58};
59
Joerg Roedelbd0e5212008-06-26 21:27:56 +020060static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
61 struct unity_map_entry *e);
Joerg Roedel8bc3e122009-09-02 16:48:40 +020062static u64 *alloc_pte(struct protection_domain *domain,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +020063 unsigned long address, int end_lvl,
64 u64 **pte_page, gfp_t gfp);
Joerg Roedel00cd1222009-05-19 09:52:40 +020065static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
66 unsigned long start_page,
67 unsigned int pages);
Joerg Roedela345b232009-09-03 15:01:43 +020068static void reset_iommu_command_buffer(struct amd_iommu *iommu);
Joerg Roedel9355a082009-09-02 14:24:08 +020069static u64 *fetch_pte(struct protection_domain *domain,
Joerg Roedela6b256b2009-09-03 12:21:31 +020070 unsigned long address, int map_size);
Joerg Roedel04bfdd82009-09-02 16:00:23 +020071static void update_domain(struct protection_domain *domain);
Chris Wrightc1eee672009-05-21 00:56:58 -070072
Joerg Roedel15898bb2009-11-24 15:39:42 +010073/****************************************************************************
74 *
75 * Helper functions
76 *
77 ****************************************************************************/
78
79static inline u16 get_device_id(struct device *dev)
80{
81 struct pci_dev *pdev = to_pci_dev(dev);
82
83 return calc_devid(pdev->bus->number, pdev->devfn);
84}
85
Joerg Roedel71c70982009-11-24 16:43:06 +010086/*
87 * In this function the list of preallocated protection domains is traversed to
88 * find the domain for a specific device
89 */
90static struct dma_ops_domain *find_protection_domain(u16 devid)
91{
92 struct dma_ops_domain *entry, *ret = NULL;
93 unsigned long flags;
94 u16 alias = amd_iommu_alias_table[devid];
95
96 if (list_empty(&iommu_pd_list))
97 return NULL;
98
99 spin_lock_irqsave(&iommu_pd_list_lock, flags);
100
101 list_for_each_entry(entry, &iommu_pd_list, list) {
102 if (entry->target_dev == devid ||
103 entry->target_dev == alias) {
104 ret = entry;
105 break;
106 }
107 }
108
109 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
110
111 return ret;
112}
113
Joerg Roedel7f265082008-12-12 13:50:21 +0100114#ifdef CONFIG_AMD_IOMMU_STATS
115
116/*
117 * Initialization code for statistics collection
118 */
119
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100120DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100121DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100122DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100123DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100124DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100125DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100126DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100127DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100128DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100129DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100130DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100131DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100132
Joerg Roedel7f265082008-12-12 13:50:21 +0100133static struct dentry *stats_dir;
134static struct dentry *de_isolate;
135static struct dentry *de_fflush;
136
137static void amd_iommu_stats_add(struct __iommu_counter *cnt)
138{
139 if (stats_dir == NULL)
140 return;
141
142 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
143 &cnt->value);
144}
145
146static void amd_iommu_stats_init(void)
147{
148 stats_dir = debugfs_create_dir("amd-iommu", NULL);
149 if (stats_dir == NULL)
150 return;
151
152 de_isolate = debugfs_create_bool("isolation", 0444, stats_dir,
153 (u32 *)&amd_iommu_isolate);
154
155 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
156 (u32 *)&amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100157
158 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100159 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100160 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100161 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100162 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100163 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100164 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100165 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100166 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100167 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100168 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100169 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100170}
171
172#endif
173
Joerg Roedel431b2a22008-07-11 17:14:22 +0200174/****************************************************************************
175 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200176 * Interrupt handling functions
177 *
178 ****************************************************************************/
179
Joerg Roedele3e59872009-09-03 14:02:10 +0200180static void dump_dte_entry(u16 devid)
181{
182 int i;
183
184 for (i = 0; i < 8; ++i)
185 pr_err("AMD-Vi: DTE[%d]: %08x\n", i,
186 amd_iommu_dev_table[devid].data[i]);
187}
188
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200189static void dump_command(unsigned long phys_addr)
190{
191 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
192 int i;
193
194 for (i = 0; i < 4; ++i)
195 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
196}
197
Joerg Roedela345b232009-09-03 15:01:43 +0200198static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200199{
200 u32 *event = __evt;
201 int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
202 int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
203 int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
204 int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
205 u64 address = (u64)(((u64)event[3]) << 32) | event[2];
206
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200207 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200208
209 switch (type) {
210 case EVENT_TYPE_ILL_DEV:
211 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
212 "address=0x%016llx flags=0x%04x]\n",
213 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
214 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200215 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200216 break;
217 case EVENT_TYPE_IO_FAULT:
218 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
219 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
220 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
221 domid, address, flags);
222 break;
223 case EVENT_TYPE_DEV_TAB_ERR:
224 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
225 "address=0x%016llx flags=0x%04x]\n",
226 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
227 address, flags);
228 break;
229 case EVENT_TYPE_PAGE_TAB_ERR:
230 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
231 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
232 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
233 domid, address, flags);
234 break;
235 case EVENT_TYPE_ILL_CMD:
236 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedela345b232009-09-03 15:01:43 +0200237 reset_iommu_command_buffer(iommu);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200238 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200239 break;
240 case EVENT_TYPE_CMD_HARD_ERR:
241 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
242 "flags=0x%04x]\n", address, flags);
243 break;
244 case EVENT_TYPE_IOTLB_INV_TO:
245 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
246 "address=0x%016llx]\n",
247 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
248 address);
249 break;
250 case EVENT_TYPE_INV_DEV_REQ:
251 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
252 "address=0x%016llx flags=0x%04x]\n",
253 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
254 address, flags);
255 break;
256 default:
257 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
258 }
259}
260
261static void iommu_poll_events(struct amd_iommu *iommu)
262{
263 u32 head, tail;
264 unsigned long flags;
265
266 spin_lock_irqsave(&iommu->lock, flags);
267
268 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
269 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
270
271 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200272 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200273 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
274 }
275
276 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
277
278 spin_unlock_irqrestore(&iommu->lock, flags);
279}
280
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200281irqreturn_t amd_iommu_int_handler(int irq, void *data)
282{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200283 struct amd_iommu *iommu;
284
Joerg Roedel3bd22172009-05-04 15:06:20 +0200285 for_each_iommu(iommu)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200286 iommu_poll_events(iommu);
287
288 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200289}
290
291/****************************************************************************
292 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200293 * IOMMU command queuing functions
294 *
295 ****************************************************************************/
296
297/*
298 * Writes the command to the IOMMUs command buffer and informs the
299 * hardware about the new command. Must be called with iommu->lock held.
300 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200301static int __iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200302{
303 u32 tail, head;
304 u8 *target;
305
306 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Jiri Kosina8a7c5ef2008-08-19 02:13:55 +0200307 target = iommu->cmd_buf + tail;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200308 memcpy_toio(target, cmd, sizeof(*cmd));
309 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
310 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
311 if (tail == head)
312 return -ENOMEM;
313 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
314
315 return 0;
316}
317
Joerg Roedel431b2a22008-07-11 17:14:22 +0200318/*
319 * General queuing function for commands. Takes iommu->lock and calls
320 * __iommu_queue_command().
321 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200322static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200323{
324 unsigned long flags;
325 int ret;
326
327 spin_lock_irqsave(&iommu->lock, flags);
328 ret = __iommu_queue_command(iommu, cmd);
Joerg Roedel09ee17e2008-12-03 12:19:27 +0100329 if (!ret)
Joerg Roedel0cfd7aa2008-12-10 19:58:00 +0100330 iommu->need_sync = true;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200331 spin_unlock_irqrestore(&iommu->lock, flags);
332
333 return ret;
334}
335
Joerg Roedel431b2a22008-07-11 17:14:22 +0200336/*
Joerg Roedel8d201962008-12-02 20:34:41 +0100337 * This function waits until an IOMMU has completed a completion
338 * wait command
Joerg Roedel431b2a22008-07-11 17:14:22 +0200339 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100340static void __iommu_wait_for_completion(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200341{
Joerg Roedel8d201962008-12-02 20:34:41 +0100342 int ready = 0;
Joerg Roedel519c31b2008-08-14 19:55:15 +0200343 unsigned status = 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100344 unsigned long i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200345
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100346 INC_STATS_COUNTER(compl_wait);
347
Joerg Roedel136f78a2008-07-11 17:14:27 +0200348 while (!ready && (i < EXIT_LOOP_COUNT)) {
349 ++i;
Joerg Roedel519c31b2008-08-14 19:55:15 +0200350 /* wait for the bit to become one */
351 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
352 ready = status & MMIO_STATUS_COM_WAIT_INT_MASK;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200353 }
354
Joerg Roedel519c31b2008-08-14 19:55:15 +0200355 /* set bit back to zero */
356 status &= ~MMIO_STATUS_COM_WAIT_INT_MASK;
357 writel(status, iommu->mmio_base + MMIO_STATUS_OFFSET);
358
Joerg Roedel6a1eddd2009-09-03 15:15:10 +0200359 if (unlikely(i == EXIT_LOOP_COUNT)) {
360 spin_unlock(&iommu->lock);
361 reset_iommu_command_buffer(iommu);
362 spin_lock(&iommu->lock);
363 }
Joerg Roedel8d201962008-12-02 20:34:41 +0100364}
365
366/*
367 * This function queues a completion wait command into the command
368 * buffer of an IOMMU
369 */
370static int __iommu_completion_wait(struct amd_iommu *iommu)
371{
372 struct iommu_cmd cmd;
373
374 memset(&cmd, 0, sizeof(cmd));
375 cmd.data[0] = CMD_COMPL_WAIT_INT_MASK;
376 CMD_SET_TYPE(&cmd, CMD_COMPL_WAIT);
377
378 return __iommu_queue_command(iommu, &cmd);
379}
380
381/*
382 * This function is called whenever we need to ensure that the IOMMU has
383 * completed execution of all commands we sent. It sends a
384 * COMPLETION_WAIT command and waits for it to finish. The IOMMU informs
385 * us about that by writing a value to a physical address we pass with
386 * the command.
387 */
388static int iommu_completion_wait(struct amd_iommu *iommu)
389{
390 int ret = 0;
391 unsigned long flags;
392
393 spin_lock_irqsave(&iommu->lock, flags);
394
395 if (!iommu->need_sync)
396 goto out;
397
398 ret = __iommu_completion_wait(iommu);
399
Joerg Roedel0cfd7aa2008-12-10 19:58:00 +0100400 iommu->need_sync = false;
Joerg Roedel8d201962008-12-02 20:34:41 +0100401
402 if (ret)
403 goto out;
404
405 __iommu_wait_for_completion(iommu);
Joerg Roedel84df8172008-12-17 16:36:44 +0100406
Joerg Roedel7e4f88d2008-09-17 14:19:15 +0200407out:
408 spin_unlock_irqrestore(&iommu->lock, flags);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200409
410 return 0;
411}
412
Joerg Roedel0518a3a2009-11-20 16:00:05 +0100413static void iommu_flush_complete(struct protection_domain *domain)
414{
415 int i;
416
417 for (i = 0; i < amd_iommus_present; ++i) {
418 if (!domain->dev_iommu[i])
419 continue;
420
421 /*
422 * Devices of this domain are behind this IOMMU
423 * We need to wait for completion of all commands.
424 */
425 iommu_completion_wait(amd_iommus[i]);
426 }
427}
428
Joerg Roedel431b2a22008-07-11 17:14:22 +0200429/*
430 * Command send function for invalidating a device table entry
431 */
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200432static int iommu_queue_inv_dev_entry(struct amd_iommu *iommu, u16 devid)
433{
Joerg Roedeld6449532008-07-11 17:14:28 +0200434 struct iommu_cmd cmd;
Joerg Roedelee2fa742008-09-17 13:47:25 +0200435 int ret;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200436
437 BUG_ON(iommu == NULL);
438
439 memset(&cmd, 0, sizeof(cmd));
440 CMD_SET_TYPE(&cmd, CMD_INV_DEV_ENTRY);
441 cmd.data[0] = devid;
442
Joerg Roedelee2fa742008-09-17 13:47:25 +0200443 ret = iommu_queue_command(iommu, &cmd);
444
Joerg Roedelee2fa742008-09-17 13:47:25 +0200445 return ret;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200446}
447
Joerg Roedel237b6f32008-12-02 20:54:37 +0100448static void __iommu_build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
449 u16 domid, int pde, int s)
450{
451 memset(cmd, 0, sizeof(*cmd));
452 address &= PAGE_MASK;
453 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
454 cmd->data[1] |= domid;
455 cmd->data[2] = lower_32_bits(address);
456 cmd->data[3] = upper_32_bits(address);
457 if (s) /* size bit - we flush more than one 4kb page */
458 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
459 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
460 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
461}
462
Joerg Roedel431b2a22008-07-11 17:14:22 +0200463/*
464 * Generic command send function for invalidaing TLB entries
465 */
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200466static int iommu_queue_inv_iommu_pages(struct amd_iommu *iommu,
467 u64 address, u16 domid, int pde, int s)
468{
Joerg Roedeld6449532008-07-11 17:14:28 +0200469 struct iommu_cmd cmd;
Joerg Roedelee2fa742008-09-17 13:47:25 +0200470 int ret;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200471
Joerg Roedel237b6f32008-12-02 20:54:37 +0100472 __iommu_build_inv_iommu_pages(&cmd, address, domid, pde, s);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200473
Joerg Roedelee2fa742008-09-17 13:47:25 +0200474 ret = iommu_queue_command(iommu, &cmd);
475
Joerg Roedelee2fa742008-09-17 13:47:25 +0200476 return ret;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200477}
478
Joerg Roedel431b2a22008-07-11 17:14:22 +0200479/*
480 * TLB invalidation function which is called from the mapping functions.
481 * It invalidates a single PTE if the range to flush is within a single
482 * page. Otherwise it flushes the whole TLB of the IOMMU.
483 */
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100484static void __iommu_flush_pages(struct protection_domain *domain,
485 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200486{
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100487 int s = 0, i;
Joerg Roedeldcd1e922009-11-20 15:30:58 +0100488 unsigned long pages = iommu_num_pages(address, size, PAGE_SIZE);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200489
490 address &= PAGE_MASK;
491
Joerg Roedel999ba412008-07-03 19:35:08 +0200492 if (pages > 1) {
493 /*
494 * If we have to flush more than one page, flush all
495 * TLB entries for this domain
496 */
497 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
498 s = 1;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200499 }
500
Joerg Roedel999ba412008-07-03 19:35:08 +0200501
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100502 for (i = 0; i < amd_iommus_present; ++i) {
503 if (!domain->dev_iommu[i])
504 continue;
505
506 /*
507 * Devices of this domain are behind this IOMMU
508 * We need a TLB flush
509 */
510 iommu_queue_inv_iommu_pages(amd_iommus[i], address,
511 domain->id, pde, s);
512 }
513
514 return;
515}
516
517static void iommu_flush_pages(struct protection_domain *domain,
518 u64 address, size_t size)
519{
520 __iommu_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200521}
Joerg Roedelb6c02712008-06-26 21:27:53 +0200522
Joerg Roedel1c655772008-09-04 18:40:05 +0200523/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedeldcd1e922009-11-20 15:30:58 +0100524static void iommu_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +0200525{
Joerg Roedeldcd1e922009-11-20 15:30:58 +0100526 __iommu_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +0200527}
528
Chris Wright42a49f92009-06-15 15:42:00 +0200529/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedeldcd1e922009-11-20 15:30:58 +0100530static void iommu_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +0200531{
Joerg Roedeldcd1e922009-11-20 15:30:58 +0100532 __iommu_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
Chris Wright42a49f92009-06-15 15:42:00 +0200533}
534
Joerg Roedel43f49602008-12-02 21:01:12 +0100535/*
Joerg Roedel09b42802009-11-20 17:02:44 +0100536 * This function flushes all domains that have devices on the given IOMMU
Joerg Roedel43f49602008-12-02 21:01:12 +0100537 */
Joerg Roedele394d722009-09-03 15:28:33 +0200538static void flush_all_domains_on_iommu(struct amd_iommu *iommu)
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200539{
Joerg Roedel09b42802009-11-20 17:02:44 +0100540 u64 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
541 struct protection_domain *domain;
542 unsigned long flags;
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200543
Joerg Roedel09b42802009-11-20 17:02:44 +0100544 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
545
546 list_for_each_entry(domain, &amd_iommu_pd_list, list) {
547 if (domain->dev_iommu[iommu->index] == 0)
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200548 continue;
Joerg Roedel09b42802009-11-20 17:02:44 +0100549
550 spin_lock(&domain->lock);
551 iommu_queue_inv_iommu_pages(iommu, address, domain->id, 1, 1);
552 iommu_flush_complete(domain);
553 spin_unlock(&domain->lock);
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200554 }
Joerg Roedele394d722009-09-03 15:28:33 +0200555
Joerg Roedel09b42802009-11-20 17:02:44 +0100556 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
Joerg Roedele394d722009-09-03 15:28:33 +0200557}
558
Joerg Roedel09b42802009-11-20 17:02:44 +0100559/*
560 * This function uses heavy locking and may disable irqs for some time. But
561 * this is no issue because it is only called during resume.
562 */
Joerg Roedele394d722009-09-03 15:28:33 +0200563void amd_iommu_flush_all_domains(void)
564{
Joerg Roedele3306662009-11-20 16:48:58 +0100565 struct protection_domain *domain;
Joerg Roedel09b42802009-11-20 17:02:44 +0100566 unsigned long flags;
567
568 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
Joerg Roedele394d722009-09-03 15:28:33 +0200569
Joerg Roedele3306662009-11-20 16:48:58 +0100570 list_for_each_entry(domain, &amd_iommu_pd_list, list) {
Joerg Roedel09b42802009-11-20 17:02:44 +0100571 spin_lock(&domain->lock);
Joerg Roedele3306662009-11-20 16:48:58 +0100572 iommu_flush_tlb_pde(domain);
573 iommu_flush_complete(domain);
Joerg Roedel09b42802009-11-20 17:02:44 +0100574 spin_unlock(&domain->lock);
Joerg Roedele3306662009-11-20 16:48:58 +0100575 }
Joerg Roedel09b42802009-11-20 17:02:44 +0100576
577 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200578}
579
Joerg Roedeld586d782009-09-03 15:39:23 +0200580static void flush_all_devices_for_iommu(struct amd_iommu *iommu)
581{
582 int i;
583
584 for (i = 0; i <= amd_iommu_last_bdf; ++i) {
585 if (iommu != amd_iommu_rlookup_table[i])
586 continue;
587
588 iommu_queue_inv_dev_entry(iommu, i);
589 iommu_completion_wait(iommu);
Joerg Roedel431b2a22008-07-11 17:14:22 +0200590 }
591}
592
Joerg Roedel6a0dbcb2009-09-02 15:41:59 +0200593static void flush_devices_by_domain(struct protection_domain *domain)
Joerg Roedel7d7a1102009-05-05 15:48:10 +0200594{
595 struct amd_iommu *iommu;
596 int i;
597
598 for (i = 0; i <= amd_iommu_last_bdf; ++i) {
Joerg Roedel6a0dbcb2009-09-02 15:41:59 +0200599 if ((domain == NULL && amd_iommu_pd_table[i] == NULL) ||
600 (amd_iommu_pd_table[i] != domain))
Joerg Roedel7d7a1102009-05-05 15:48:10 +0200601 continue;
602
603 iommu = amd_iommu_rlookup_table[i];
604 if (!iommu)
605 continue;
606
607 iommu_queue_inv_dev_entry(iommu, i);
608 iommu_completion_wait(iommu);
609 }
610}
611
Joerg Roedela345b232009-09-03 15:01:43 +0200612static void reset_iommu_command_buffer(struct amd_iommu *iommu)
613{
614 pr_err("AMD-Vi: Resetting IOMMU command buffer\n");
615
Joerg Roedelb26e81b2009-09-03 15:08:09 +0200616 if (iommu->reset_in_progress)
617 panic("AMD-Vi: ILLEGAL_COMMAND_ERROR while resetting command buffer\n");
618
619 iommu->reset_in_progress = true;
620
Joerg Roedela345b232009-09-03 15:01:43 +0200621 amd_iommu_reset_cmd_buffer(iommu);
622 flush_all_devices_for_iommu(iommu);
623 flush_all_domains_on_iommu(iommu);
Joerg Roedelb26e81b2009-09-03 15:08:09 +0200624
625 iommu->reset_in_progress = false;
Joerg Roedela345b232009-09-03 15:01:43 +0200626}
627
Joerg Roedel6a0dbcb2009-09-02 15:41:59 +0200628void amd_iommu_flush_all_devices(void)
629{
630 flush_devices_by_domain(NULL);
631}
632
Joerg Roedel431b2a22008-07-11 17:14:22 +0200633/****************************************************************************
634 *
635 * The functions below are used the create the page table mappings for
636 * unity mapped regions.
637 *
638 ****************************************************************************/
639
640/*
641 * Generic mapping functions. It maps a physical address into a DMA
642 * address space. It allocates the page table pages if necessary.
643 * In the future it can be extended to a generic mapping function
644 * supporting all features of AMD IOMMU page tables like level skipping
645 * and full 64 bit address spaces.
646 */
Joerg Roedel38e817f2008-12-02 17:27:52 +0100647static int iommu_map_page(struct protection_domain *dom,
648 unsigned long bus_addr,
649 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200650 int prot,
651 int map_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200652{
Joerg Roedel8bda3092009-05-12 12:02:46 +0200653 u64 __pte, *pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200654
655 bus_addr = PAGE_ALIGN(bus_addr);
Joerg Roedelbb9d4ff2008-12-04 15:59:48 +0100656 phys_addr = PAGE_ALIGN(phys_addr);
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200657
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200658 BUG_ON(!PM_ALIGNED(map_size, bus_addr));
659 BUG_ON(!PM_ALIGNED(map_size, phys_addr));
660
Joerg Roedelbad1cac2009-09-02 16:52:23 +0200661 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200662 return -EINVAL;
663
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200664 pte = alloc_pte(dom, bus_addr, map_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200665
666 if (IOMMU_PTE_PRESENT(*pte))
667 return -EBUSY;
668
669 __pte = phys_addr | IOMMU_PTE_P;
670 if (prot & IOMMU_PROT_IR)
671 __pte |= IOMMU_PTE_IR;
672 if (prot & IOMMU_PROT_IW)
673 __pte |= IOMMU_PTE_IW;
674
675 *pte = __pte;
676
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200677 update_domain(dom);
678
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200679 return 0;
680}
681
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100682static void iommu_unmap_page(struct protection_domain *dom,
Joerg Roedela6b256b2009-09-03 12:21:31 +0200683 unsigned long bus_addr, int map_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100684{
Joerg Roedela6b256b2009-09-03 12:21:31 +0200685 u64 *pte = fetch_pte(dom, bus_addr, map_size);
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100686
Joerg Roedel38a76ee2009-09-02 17:02:47 +0200687 if (pte)
688 *pte = 0;
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100689}
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100690
Joerg Roedel431b2a22008-07-11 17:14:22 +0200691/*
692 * This function checks if a specific unity mapping entry is needed for
693 * this specific IOMMU.
694 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200695static int iommu_for_unity_map(struct amd_iommu *iommu,
696 struct unity_map_entry *entry)
697{
698 u16 bdf, i;
699
700 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
701 bdf = amd_iommu_alias_table[i];
702 if (amd_iommu_rlookup_table[bdf] == iommu)
703 return 1;
704 }
705
706 return 0;
707}
708
Joerg Roedel431b2a22008-07-11 17:14:22 +0200709/*
710 * Init the unity mappings for a specific IOMMU in the system
711 *
712 * Basically iterates over all unity mapping entries and applies them to
713 * the default domain DMA of that IOMMU if necessary.
714 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200715static int iommu_init_unity_mappings(struct amd_iommu *iommu)
716{
717 struct unity_map_entry *entry;
718 int ret;
719
720 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
721 if (!iommu_for_unity_map(iommu, entry))
722 continue;
723 ret = dma_ops_unity_map(iommu->default_dom, entry);
724 if (ret)
725 return ret;
726 }
727
728 return 0;
729}
730
Joerg Roedel431b2a22008-07-11 17:14:22 +0200731/*
732 * This function actually applies the mapping to the page table of the
733 * dma_ops domain.
734 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200735static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
736 struct unity_map_entry *e)
737{
738 u64 addr;
739 int ret;
740
741 for (addr = e->address_start; addr < e->address_end;
742 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200743 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
744 PM_MAP_4k);
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200745 if (ret)
746 return ret;
747 /*
748 * if unity mapping is in aperture range mark the page
749 * as allocated in the aperture
750 */
751 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +0200752 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +0200753 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200754 }
755
756 return 0;
757}
758
Joerg Roedel431b2a22008-07-11 17:14:22 +0200759/*
760 * Inits the unity mappings required for a specific device
761 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200762static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
763 u16 devid)
764{
765 struct unity_map_entry *e;
766 int ret;
767
768 list_for_each_entry(e, &amd_iommu_unity_map, list) {
769 if (!(devid >= e->devid_start && devid <= e->devid_end))
770 continue;
771 ret = dma_ops_unity_map(dma_dom, e);
772 if (ret)
773 return ret;
774 }
775
776 return 0;
777}
778
Joerg Roedel431b2a22008-07-11 17:14:22 +0200779/****************************************************************************
780 *
781 * The next functions belong to the address allocator for the dma_ops
782 * interface functions. They work like the allocators in the other IOMMU
783 * drivers. Its basically a bitmap which marks the allocated pages in
784 * the aperture. Maybe it could be enhanced in the future to a more
785 * efficient allocator.
786 *
787 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +0200788
Joerg Roedel431b2a22008-07-11 17:14:22 +0200789/*
Joerg Roedel384de722009-05-15 12:30:05 +0200790 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200791 *
792 * called with domain->lock held
793 */
Joerg Roedel384de722009-05-15 12:30:05 +0200794
Joerg Roedel9cabe892009-05-18 16:38:55 +0200795/*
Joerg Roedel00cd1222009-05-19 09:52:40 +0200796 * This function checks if there is a PTE for a given dma address. If
797 * there is one, it returns the pointer to it.
798 */
Joerg Roedel9355a082009-09-02 14:24:08 +0200799static u64 *fetch_pte(struct protection_domain *domain,
Joerg Roedela6b256b2009-09-03 12:21:31 +0200800 unsigned long address, int map_size)
Joerg Roedel00cd1222009-05-19 09:52:40 +0200801{
Joerg Roedel9355a082009-09-02 14:24:08 +0200802 int level;
Joerg Roedel00cd1222009-05-19 09:52:40 +0200803 u64 *pte;
804
Joerg Roedel9355a082009-09-02 14:24:08 +0200805 level = domain->mode - 1;
806 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
Joerg Roedel00cd1222009-05-19 09:52:40 +0200807
Joerg Roedela6b256b2009-09-03 12:21:31 +0200808 while (level > map_size) {
Joerg Roedel9355a082009-09-02 14:24:08 +0200809 if (!IOMMU_PTE_PRESENT(*pte))
810 return NULL;
Joerg Roedel00cd1222009-05-19 09:52:40 +0200811
Joerg Roedel9355a082009-09-02 14:24:08 +0200812 level -= 1;
Joerg Roedel00cd1222009-05-19 09:52:40 +0200813
Joerg Roedel9355a082009-09-02 14:24:08 +0200814 pte = IOMMU_PTE_PAGE(*pte);
815 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel00cd1222009-05-19 09:52:40 +0200816
Joerg Roedela6b256b2009-09-03 12:21:31 +0200817 if ((PM_PTE_LEVEL(*pte) == 0) && level != map_size) {
818 pte = NULL;
819 break;
820 }
Joerg Roedel9355a082009-09-02 14:24:08 +0200821 }
Joerg Roedel00cd1222009-05-19 09:52:40 +0200822
823 return pte;
824}
825
826/*
Joerg Roedel9cabe892009-05-18 16:38:55 +0200827 * This function is used to add a new aperture range to an existing
828 * aperture in case of dma_ops domain allocation or address allocation
829 * failure.
830 */
Joerg Roedel576175c2009-11-23 19:08:46 +0100831static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +0200832 bool populate, gfp_t gfp)
833{
834 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +0100835 struct amd_iommu *iommu;
Joerg Roedel00cd1222009-05-19 09:52:40 +0200836 int i;
Joerg Roedel9cabe892009-05-18 16:38:55 +0200837
Joerg Roedelf5e97052009-05-22 12:31:53 +0200838#ifdef CONFIG_IOMMU_STRESS
839 populate = false;
840#endif
841
Joerg Roedel9cabe892009-05-18 16:38:55 +0200842 if (index >= APERTURE_MAX_RANGES)
843 return -ENOMEM;
844
845 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
846 if (!dma_dom->aperture[index])
847 return -ENOMEM;
848
849 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
850 if (!dma_dom->aperture[index]->bitmap)
851 goto out_free;
852
853 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
854
855 if (populate) {
856 unsigned long address = dma_dom->aperture_size;
857 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
858 u64 *pte, *pte_page;
859
860 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200861 pte = alloc_pte(&dma_dom->domain, address, PM_MAP_4k,
Joerg Roedel9cabe892009-05-18 16:38:55 +0200862 &pte_page, gfp);
863 if (!pte)
864 goto out_free;
865
866 dma_dom->aperture[index]->pte_pages[i] = pte_page;
867
868 address += APERTURE_RANGE_SIZE / 64;
869 }
870 }
871
872 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
873
Joerg Roedel00cd1222009-05-19 09:52:40 +0200874 /* Intialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +0100875 for_each_iommu(iommu) {
876 if (iommu->exclusion_start &&
877 iommu->exclusion_start >= dma_dom->aperture[index]->offset
878 && iommu->exclusion_start < dma_dom->aperture_size) {
879 unsigned long startpage;
880 int pages = iommu_num_pages(iommu->exclusion_start,
881 iommu->exclusion_length,
882 PAGE_SIZE);
883 startpage = iommu->exclusion_start >> PAGE_SHIFT;
884 dma_ops_reserve_addresses(dma_dom, startpage, pages);
885 }
Joerg Roedel00cd1222009-05-19 09:52:40 +0200886 }
887
888 /*
889 * Check for areas already mapped as present in the new aperture
890 * range and mark those pages as reserved in the allocator. Such
891 * mappings may already exist as a result of requested unity
892 * mappings for devices.
893 */
894 for (i = dma_dom->aperture[index]->offset;
895 i < dma_dom->aperture_size;
896 i += PAGE_SIZE) {
Joerg Roedela6b256b2009-09-03 12:21:31 +0200897 u64 *pte = fetch_pte(&dma_dom->domain, i, PM_MAP_4k);
Joerg Roedel00cd1222009-05-19 09:52:40 +0200898 if (!pte || !IOMMU_PTE_PRESENT(*pte))
899 continue;
900
901 dma_ops_reserve_addresses(dma_dom, i << PAGE_SHIFT, 1);
902 }
903
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200904 update_domain(&dma_dom->domain);
905
Joerg Roedel9cabe892009-05-18 16:38:55 +0200906 return 0;
907
908out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200909 update_domain(&dma_dom->domain);
910
Joerg Roedel9cabe892009-05-18 16:38:55 +0200911 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
912
913 kfree(dma_dom->aperture[index]);
914 dma_dom->aperture[index] = NULL;
915
916 return -ENOMEM;
917}
918
Joerg Roedel384de722009-05-15 12:30:05 +0200919static unsigned long dma_ops_area_alloc(struct device *dev,
920 struct dma_ops_domain *dom,
921 unsigned int pages,
922 unsigned long align_mask,
923 u64 dma_mask,
924 unsigned long start)
925{
Joerg Roedel803b8cb42009-05-18 15:32:48 +0200926 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +0200927 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
928 int i = start >> APERTURE_RANGE_SHIFT;
929 unsigned long boundary_size;
930 unsigned long address = -1;
931 unsigned long limit;
932
Joerg Roedel803b8cb42009-05-18 15:32:48 +0200933 next_bit >>= PAGE_SHIFT;
934
Joerg Roedel384de722009-05-15 12:30:05 +0200935 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
936 PAGE_SIZE) >> PAGE_SHIFT;
937
938 for (;i < max_index; ++i) {
939 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
940
941 if (dom->aperture[i]->offset >= dma_mask)
942 break;
943
944 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
945 dma_mask >> PAGE_SHIFT);
946
947 address = iommu_area_alloc(dom->aperture[i]->bitmap,
948 limit, next_bit, pages, 0,
949 boundary_size, align_mask);
950 if (address != -1) {
951 address = dom->aperture[i]->offset +
952 (address << PAGE_SHIFT);
Joerg Roedel803b8cb42009-05-18 15:32:48 +0200953 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +0200954 break;
955 }
956
957 next_bit = 0;
958 }
959
960 return address;
961}
962
Joerg Roedeld3086442008-06-26 21:27:57 +0200963static unsigned long dma_ops_alloc_addresses(struct device *dev,
964 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +0200965 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +0200966 unsigned long align_mask,
967 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +0200968{
Joerg Roedeld3086442008-06-26 21:27:57 +0200969 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +0200970
Joerg Roedelfe16f082009-05-22 12:27:53 +0200971#ifdef CONFIG_IOMMU_STRESS
972 dom->next_address = 0;
973 dom->need_flush = true;
974#endif
Joerg Roedeld3086442008-06-26 21:27:57 +0200975
Joerg Roedel384de722009-05-15 12:30:05 +0200976 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb42009-05-18 15:32:48 +0200977 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +0200978
Joerg Roedel1c655772008-09-04 18:40:05 +0200979 if (address == -1) {
Joerg Roedel803b8cb42009-05-18 15:32:48 +0200980 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +0200981 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
982 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +0200983 dom->need_flush = true;
984 }
Joerg Roedeld3086442008-06-26 21:27:57 +0200985
Joerg Roedel384de722009-05-15 12:30:05 +0200986 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +0900987 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +0200988
989 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
990
991 return address;
992}
993
Joerg Roedel431b2a22008-07-11 17:14:22 +0200994/*
995 * The address free function.
996 *
997 * called with domain->lock held
998 */
Joerg Roedeld3086442008-06-26 21:27:57 +0200999static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1000 unsigned long address,
1001 unsigned int pages)
1002{
Joerg Roedel384de722009-05-15 12:30:05 +02001003 unsigned i = address >> APERTURE_RANGE_SHIFT;
1004 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001005
Joerg Roedel384de722009-05-15 12:30:05 +02001006 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1007
Joerg Roedel47bccd62009-05-22 12:40:54 +02001008#ifdef CONFIG_IOMMU_STRESS
1009 if (i < 4)
1010 return;
1011#endif
1012
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001013 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001014 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001015
1016 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001017
Joerg Roedel384de722009-05-15 12:30:05 +02001018 iommu_area_free(range->bitmap, address, pages);
1019
Joerg Roedeld3086442008-06-26 21:27:57 +02001020}
1021
Joerg Roedel431b2a22008-07-11 17:14:22 +02001022/****************************************************************************
1023 *
1024 * The next functions belong to the domain allocation. A domain is
1025 * allocated for every IOMMU as the default domain. If device isolation
1026 * is enabled, every device get its own domain. The most important thing
1027 * about domains is the page table mapping the DMA address space they
1028 * contain.
1029 *
1030 ****************************************************************************/
1031
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001032/*
1033 * This function adds a protection domain to the global protection domain list
1034 */
1035static void add_domain_to_list(struct protection_domain *domain)
1036{
1037 unsigned long flags;
1038
1039 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1040 list_add(&domain->list, &amd_iommu_pd_list);
1041 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1042}
1043
1044/*
1045 * This function removes a protection domain to the global
1046 * protection domain list
1047 */
1048static void del_domain_from_list(struct protection_domain *domain)
1049{
1050 unsigned long flags;
1051
1052 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1053 list_del(&domain->list);
1054 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1055}
1056
Joerg Roedelec487d12008-06-26 21:27:58 +02001057static u16 domain_id_alloc(void)
1058{
1059 unsigned long flags;
1060 int id;
1061
1062 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1063 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1064 BUG_ON(id == 0);
1065 if (id > 0 && id < MAX_DOMAIN_ID)
1066 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1067 else
1068 id = 0;
1069 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1070
1071 return id;
1072}
1073
Joerg Roedela2acfb72008-12-02 18:28:53 +01001074static void domain_id_free(int id)
1075{
1076 unsigned long flags;
1077
1078 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1079 if (id > 0 && id < MAX_DOMAIN_ID)
1080 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1081 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1082}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001083
Joerg Roedel431b2a22008-07-11 17:14:22 +02001084/*
1085 * Used to reserve address ranges in the aperture (e.g. for exclusion
1086 * ranges.
1087 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001088static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1089 unsigned long start_page,
1090 unsigned int pages)
1091{
Joerg Roedel384de722009-05-15 12:30:05 +02001092 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
Joerg Roedelec487d12008-06-26 21:27:58 +02001093
1094 if (start_page + pages > last_page)
1095 pages = last_page - start_page;
1096
Joerg Roedel384de722009-05-15 12:30:05 +02001097 for (i = start_page; i < start_page + pages; ++i) {
1098 int index = i / APERTURE_RANGE_PAGES;
1099 int page = i % APERTURE_RANGE_PAGES;
1100 __set_bit(page, dom->aperture[index]->bitmap);
1101 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001102}
1103
Joerg Roedel86db2e52008-12-02 18:20:21 +01001104static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001105{
1106 int i, j;
1107 u64 *p1, *p2, *p3;
1108
Joerg Roedel86db2e52008-12-02 18:20:21 +01001109 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001110
1111 if (!p1)
1112 return;
1113
1114 for (i = 0; i < 512; ++i) {
1115 if (!IOMMU_PTE_PRESENT(p1[i]))
1116 continue;
1117
1118 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001119 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001120 if (!IOMMU_PTE_PRESENT(p2[j]))
1121 continue;
1122 p3 = IOMMU_PTE_PAGE(p2[j]);
1123 free_page((unsigned long)p3);
1124 }
1125
1126 free_page((unsigned long)p2);
1127 }
1128
1129 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001130
1131 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001132}
1133
Joerg Roedel431b2a22008-07-11 17:14:22 +02001134/*
1135 * Free a domain, only used if something went wrong in the
1136 * allocation path and we need to free an already allocated page table
1137 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001138static void dma_ops_domain_free(struct dma_ops_domain *dom)
1139{
Joerg Roedel384de722009-05-15 12:30:05 +02001140 int i;
1141
Joerg Roedelec487d12008-06-26 21:27:58 +02001142 if (!dom)
1143 return;
1144
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001145 del_domain_from_list(&dom->domain);
1146
Joerg Roedel86db2e52008-12-02 18:20:21 +01001147 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001148
Joerg Roedel384de722009-05-15 12:30:05 +02001149 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1150 if (!dom->aperture[i])
1151 continue;
1152 free_page((unsigned long)dom->aperture[i]->bitmap);
1153 kfree(dom->aperture[i]);
1154 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001155
1156 kfree(dom);
1157}
1158
Joerg Roedel431b2a22008-07-11 17:14:22 +02001159/*
1160 * Allocates a new protection domain usable for the dma_ops functions.
1161 * It also intializes the page table and the address allocator data
1162 * structures required for the dma_ops interface
1163 */
Joerg Roedeld9cfed92009-05-19 12:16:29 +02001164static struct dma_ops_domain *dma_ops_domain_alloc(struct amd_iommu *iommu)
Joerg Roedelec487d12008-06-26 21:27:58 +02001165{
1166 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001167
1168 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1169 if (!dma_dom)
1170 return NULL;
1171
1172 spin_lock_init(&dma_dom->domain.lock);
1173
1174 dma_dom->domain.id = domain_id_alloc();
1175 if (dma_dom->domain.id == 0)
1176 goto free_dma_dom;
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001177 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001178 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001179 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001180 dma_dom->domain.priv = dma_dom;
1181 if (!dma_dom->domain.pt_root)
1182 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001183
Joerg Roedel1c655772008-09-04 18:40:05 +02001184 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001185 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001186
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001187 add_domain_to_list(&dma_dom->domain);
1188
Joerg Roedel576175c2009-11-23 19:08:46 +01001189 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001190 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001191
Joerg Roedel431b2a22008-07-11 17:14:22 +02001192 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001193 * mark the first page as allocated so we never return 0 as
1194 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001195 */
Joerg Roedel384de722009-05-15 12:30:05 +02001196 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001197 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001198
Joerg Roedelec487d12008-06-26 21:27:58 +02001199
1200 return dma_dom;
1201
1202free_dma_dom:
1203 dma_ops_domain_free(dma_dom);
1204
1205 return NULL;
1206}
1207
Joerg Roedel431b2a22008-07-11 17:14:22 +02001208/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001209 * little helper function to check whether a given protection domain is a
1210 * dma_ops domain
1211 */
1212static bool dma_ops_domain(struct protection_domain *domain)
1213{
1214 return domain->flags & PD_DMA_OPS_MASK;
1215}
1216
Joerg Roedel407d7332009-09-02 16:07:00 +02001217static void set_dte_entry(u16 devid, struct protection_domain *domain)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001218{
Joerg Roedel15898bb2009-11-24 15:39:42 +01001219 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001220 u64 pte_root = virt_to_phys(domain->pt_root);
Joerg Roedel863c74e2008-12-02 17:56:36 +01001221
Joerg Roedel15898bb2009-11-24 15:39:42 +01001222 BUG_ON(amd_iommu_pd_table[devid] != NULL);
1223
Joerg Roedel38ddf412008-09-11 10:38:32 +02001224 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1225 << DEV_ENTRY_MODE_SHIFT;
1226 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001227
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001228 amd_iommu_dev_table[devid].data[2] = domain->id;
Joerg Roedelaa879ff2009-08-31 16:01:48 +02001229 amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
1230 amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001231
1232 amd_iommu_pd_table[devid] = domain;
Joerg Roedeleba6ac62009-09-01 12:07:08 +02001233
Joerg Roedelc4596112009-11-20 14:57:32 +01001234 /* Do reference counting */
1235 domain->dev_iommu[iommu->index] += 1;
1236 domain->dev_cnt += 1;
Joerg Roedeleba6ac62009-09-01 12:07:08 +02001237
Joerg Roedel15898bb2009-11-24 15:39:42 +01001238 /* Flush the changes DTE entry */
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001239 iommu_queue_inv_dev_entry(iommu, devid);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001240}
1241
Joerg Roedel15898bb2009-11-24 15:39:42 +01001242static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001243{
Joerg Roedel15898bb2009-11-24 15:39:42 +01001244 struct protection_domain *domain = amd_iommu_pd_table[devid];
Joerg Roedelc4596112009-11-20 14:57:32 +01001245 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
1246
Joerg Roedel15898bb2009-11-24 15:39:42 +01001247 BUG_ON(domain == NULL);
Joerg Roedel355bf552008-12-08 12:02:41 +01001248
1249 /* remove domain from the lookup table */
1250 amd_iommu_pd_table[devid] = NULL;
1251
1252 /* remove entry from the device table seen by the hardware */
1253 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1254 amd_iommu_dev_table[devid].data[1] = 0;
1255 amd_iommu_dev_table[devid].data[2] = 0;
1256
Joerg Roedelc5cca142009-10-09 18:31:20 +02001257 amd_iommu_apply_erratum_63(devid);
1258
Joerg Roedelc4596112009-11-20 14:57:32 +01001259 /* decrease reference counters */
1260 domain->dev_iommu[iommu->index] -= 1;
1261 domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001262
Joerg Roedel15898bb2009-11-24 15:39:42 +01001263 iommu_queue_inv_dev_entry(iommu, devid);
1264}
1265
1266/*
1267 * If a device is not yet associated with a domain, this function does
1268 * assigns it visible for the hardware
1269 */
1270static int __attach_device(struct device *dev,
1271 struct protection_domain *domain)
1272{
1273 u16 devid = get_device_id(dev);
1274 u16 alias = amd_iommu_alias_table[devid];
1275
1276 /* lock domain */
1277 spin_lock(&domain->lock);
1278
1279 /* Some sanity checks */
1280 if (amd_iommu_pd_table[alias] != NULL &&
1281 amd_iommu_pd_table[alias] != domain)
1282 return -EBUSY;
1283
1284 if (amd_iommu_pd_table[devid] != NULL &&
1285 amd_iommu_pd_table[devid] != domain)
1286 return -EBUSY;
1287
1288 /* Do real assignment */
1289 if (alias != devid &&
1290 amd_iommu_pd_table[alias] == NULL)
1291 set_dte_entry(alias, domain);
1292
1293 if (amd_iommu_pd_table[devid] == NULL)
1294 set_dte_entry(devid, domain);
1295
Joerg Roedel355bf552008-12-08 12:02:41 +01001296 /* ready */
1297 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001298
Joerg Roedel15898bb2009-11-24 15:39:42 +01001299 return 0;
1300}
1301
1302/*
1303 * If a device is not yet associated with a domain, this function does
1304 * assigns it visible for the hardware
1305 */
1306static int attach_device(struct device *dev,
1307 struct protection_domain *domain)
1308{
1309 unsigned long flags;
1310 int ret;
1311
1312 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1313 ret = __attach_device(dev, domain);
1314 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1315
1316 /*
1317 * We might boot into a crash-kernel here. The crashed kernel
1318 * left the caches in the IOMMU dirty. So we have to flush
1319 * here to evict all dirty stuff.
1320 */
1321 iommu_flush_tlb_pde(domain);
1322
1323 return ret;
1324}
1325
1326/*
1327 * Removes a device from a protection domain (unlocked)
1328 */
1329static void __detach_device(struct device *dev)
1330{
1331 u16 devid = get_device_id(dev);
1332 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
1333
1334 BUG_ON(!iommu);
1335
1336 clear_dte_entry(devid);
1337
Joerg Roedel21129f72009-09-01 11:59:42 +02001338 /*
1339 * If we run in passthrough mode the device must be assigned to the
1340 * passthrough domain if it is detached from any other domain
1341 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01001342 if (iommu_pass_through)
1343 __attach_device(dev, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01001344}
1345
1346/*
1347 * Removes a device from a protection domain (with devtable_lock held)
1348 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01001349static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01001350{
1351 unsigned long flags;
1352
1353 /* lock device table */
1354 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001355 __detach_device(dev);
Joerg Roedel355bf552008-12-08 12:02:41 +01001356 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1357}
Joerg Roedele275a2a2008-12-10 18:27:25 +01001358
Joerg Roedel15898bb2009-11-24 15:39:42 +01001359/*
1360 * Find out the protection domain structure for a given PCI device. This
1361 * will give us the pointer to the page table root for example.
1362 */
1363static struct protection_domain *domain_for_device(struct device *dev)
1364{
1365 struct protection_domain *dom;
1366 unsigned long flags;
1367 u16 devid, alias;
1368
1369 devid = get_device_id(dev);
1370 alias = amd_iommu_alias_table[devid];
1371
1372 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
1373 dom = amd_iommu_pd_table[devid];
1374 if (dom == NULL &&
1375 amd_iommu_pd_table[alias] != NULL) {
1376 __attach_device(dev, amd_iommu_pd_table[alias]);
1377 dom = amd_iommu_pd_table[devid];
1378 }
1379
1380 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1381
1382 return dom;
1383}
1384
Joerg Roedele275a2a2008-12-10 18:27:25 +01001385static int device_change_notifier(struct notifier_block *nb,
1386 unsigned long action, void *data)
1387{
1388 struct device *dev = data;
1389 struct pci_dev *pdev = to_pci_dev(dev);
1390 u16 devid = calc_devid(pdev->bus->number, pdev->devfn);
1391 struct protection_domain *domain;
1392 struct dma_ops_domain *dma_domain;
1393 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001394 unsigned long flags;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001395
1396 if (devid > amd_iommu_last_bdf)
1397 goto out;
1398
1399 devid = amd_iommu_alias_table[devid];
1400
1401 iommu = amd_iommu_rlookup_table[devid];
1402 if (iommu == NULL)
1403 goto out;
1404
Joerg Roedel15898bb2009-11-24 15:39:42 +01001405 domain = domain_for_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001406
1407 if (domain && !dma_ops_domain(domain))
1408 WARN_ONCE(1, "AMD IOMMU WARNING: device %s already bound "
1409 "to a non-dma-ops domain\n", dev_name(dev));
1410
1411 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07001412 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedele275a2a2008-12-10 18:27:25 +01001413 if (!domain)
1414 goto out;
Joerg Roedela1ca3312009-09-01 12:22:22 +02001415 if (iommu_pass_through)
1416 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001417 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001418 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001419 case BUS_NOTIFY_ADD_DEVICE:
1420 /* allocate a protection domain if a device is added */
1421 dma_domain = find_protection_domain(devid);
1422 if (dma_domain)
1423 goto out;
Joerg Roedeld9cfed92009-05-19 12:16:29 +02001424 dma_domain = dma_ops_domain_alloc(iommu);
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001425 if (!dma_domain)
1426 goto out;
1427 dma_domain->target_dev = devid;
1428
1429 spin_lock_irqsave(&iommu_pd_list_lock, flags);
1430 list_add_tail(&dma_domain->list, &iommu_pd_list);
1431 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
1432
1433 break;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001434 default:
1435 goto out;
1436 }
1437
1438 iommu_queue_inv_dev_entry(iommu, devid);
1439 iommu_completion_wait(iommu);
1440
1441out:
1442 return 0;
1443}
1444
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05301445static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01001446 .notifier_call = device_change_notifier,
1447};
Joerg Roedel355bf552008-12-08 12:02:41 +01001448
Joerg Roedel431b2a22008-07-11 17:14:22 +02001449/*****************************************************************************
1450 *
1451 * The next functions belong to the dma_ops mapping/unmapping code.
1452 *
1453 *****************************************************************************/
1454
1455/*
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001456 * This function checks if the driver got a valid device from the caller to
1457 * avoid dereferencing invalid pointers.
1458 */
1459static bool check_device(struct device *dev)
1460{
Joerg Roedel420aef82009-11-23 16:14:57 +01001461 u16 bdf;
1462 struct pci_dev *pcidev;
1463
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001464 if (!dev || !dev->dma_mask)
1465 return false;
1466
Joerg Roedel420aef82009-11-23 16:14:57 +01001467 /* No device or no PCI device */
1468 if (!dev || dev->bus != &pci_bus_type)
1469 return false;
1470
1471 pcidev = to_pci_dev(dev);
1472
1473 bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
1474
1475 /* Out of our scope? */
1476 if (bdf > amd_iommu_last_bdf)
1477 return false;
1478
1479 if (amd_iommu_rlookup_table[bdf] == NULL)
1480 return false;
1481
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001482 return true;
1483}
1484
1485/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001486 * In the dma_ops path we only have the struct device. This function
1487 * finds the corresponding IOMMU, the protection domain and the
1488 * requestor id for a given device.
1489 * If the device is not yet associated with a domain this is also done
1490 * in this function.
1491 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001492static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001493{
Joerg Roedel94f6d192009-11-24 16:40:02 +01001494 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001495 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01001496 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001497
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001498 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01001499 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001500
Joerg Roedel94f6d192009-11-24 16:40:02 +01001501 domain = domain_for_device(dev);
1502 if (domain != NULL && !dma_ops_domain(domain))
1503 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001504
Joerg Roedel94f6d192009-11-24 16:40:02 +01001505 if (domain != NULL)
1506 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001507
Joerg Roedel15898bb2009-11-24 15:39:42 +01001508 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001509 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001510 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01001511 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
1512 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001513 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01001514 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01001515
Joerg Roedel94f6d192009-11-24 16:40:02 +01001516 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001517}
1518
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001519static void update_device_table(struct protection_domain *domain)
1520{
Joerg Roedel2b681fa2009-09-03 17:14:57 +02001521 unsigned long flags;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001522 int i;
1523
1524 for (i = 0; i <= amd_iommu_last_bdf; ++i) {
1525 if (amd_iommu_pd_table[i] != domain)
1526 continue;
Joerg Roedel2b681fa2009-09-03 17:14:57 +02001527 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001528 set_dte_entry(i, domain);
Joerg Roedel2b681fa2009-09-03 17:14:57 +02001529 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001530 }
1531}
1532
1533static void update_domain(struct protection_domain *domain)
1534{
1535 if (!domain->updated)
1536 return;
1537
1538 update_device_table(domain);
1539 flush_devices_by_domain(domain);
Joerg Roedel601367d2009-11-20 16:08:55 +01001540 iommu_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001541
1542 domain->updated = false;
1543}
1544
Joerg Roedel431b2a22008-07-11 17:14:22 +02001545/*
Joerg Roedel50020fb2009-09-02 15:38:40 +02001546 * This function is used to add another level to an IO page table. Adding
1547 * another level increases the size of the address space by 9 bits to a size up
1548 * to 64 bits.
Joerg Roedel8bda3092009-05-12 12:02:46 +02001549 */
Joerg Roedel50020fb2009-09-02 15:38:40 +02001550static bool increase_address_space(struct protection_domain *domain,
1551 gfp_t gfp)
1552{
1553 u64 *pte;
1554
1555 if (domain->mode == PAGE_MODE_6_LEVEL)
1556 /* address space already 64 bit large */
1557 return false;
1558
1559 pte = (void *)get_zeroed_page(gfp);
1560 if (!pte)
1561 return false;
1562
1563 *pte = PM_LEVEL_PDE(domain->mode,
1564 virt_to_phys(domain->pt_root));
1565 domain->pt_root = pte;
1566 domain->mode += 1;
1567 domain->updated = true;
1568
1569 return true;
1570}
1571
Joerg Roedel8bc3e122009-09-02 16:48:40 +02001572static u64 *alloc_pte(struct protection_domain *domain,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001573 unsigned long address,
1574 int end_lvl,
1575 u64 **pte_page,
1576 gfp_t gfp)
Joerg Roedel8bda3092009-05-12 12:02:46 +02001577{
1578 u64 *pte, *page;
Joerg Roedel8bc3e122009-09-02 16:48:40 +02001579 int level;
Joerg Roedel8bda3092009-05-12 12:02:46 +02001580
Joerg Roedel8bc3e122009-09-02 16:48:40 +02001581 while (address > PM_LEVEL_SIZE(domain->mode))
1582 increase_address_space(domain, gfp);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001583
Joerg Roedel8bc3e122009-09-02 16:48:40 +02001584 level = domain->mode - 1;
1585 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1586
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001587 while (level > end_lvl) {
Joerg Roedel8bc3e122009-09-02 16:48:40 +02001588 if (!IOMMU_PTE_PRESENT(*pte)) {
1589 page = (u64 *)get_zeroed_page(gfp);
1590 if (!page)
1591 return NULL;
1592 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1593 }
1594
1595 level -= 1;
1596
1597 pte = IOMMU_PTE_PAGE(*pte);
1598
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001599 if (pte_page && level == end_lvl)
Joerg Roedel8bc3e122009-09-02 16:48:40 +02001600 *pte_page = pte;
1601
1602 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02001603 }
1604
Joerg Roedel8bda3092009-05-12 12:02:46 +02001605 return pte;
1606}
1607
1608/*
1609 * This function fetches the PTE for a given address in the aperture
1610 */
1611static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
1612 unsigned long address)
1613{
Joerg Roedel384de722009-05-15 12:30:05 +02001614 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02001615 u64 *pte, *pte_page;
1616
Joerg Roedel384de722009-05-15 12:30:05 +02001617 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1618 if (!aperture)
1619 return NULL;
1620
1621 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02001622 if (!pte) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001623 pte = alloc_pte(&dom->domain, address, PM_MAP_4k, &pte_page,
1624 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02001625 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
1626 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001627 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001628
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001629 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001630
1631 return pte;
1632}
1633
1634/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001635 * This is the generic map function. It maps one 4kb page at paddr to
1636 * the given address in the DMA address space for the domain.
1637 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001638static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001639 unsigned long address,
1640 phys_addr_t paddr,
1641 int direction)
1642{
1643 u64 *pte, __pte;
1644
1645 WARN_ON(address > dom->aperture_size);
1646
1647 paddr &= PAGE_MASK;
1648
Joerg Roedel8bda3092009-05-12 12:02:46 +02001649 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02001650 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001651 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001652
1653 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
1654
1655 if (direction == DMA_TO_DEVICE)
1656 __pte |= IOMMU_PTE_IR;
1657 else if (direction == DMA_FROM_DEVICE)
1658 __pte |= IOMMU_PTE_IW;
1659 else if (direction == DMA_BIDIRECTIONAL)
1660 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
1661
1662 WARN_ON(*pte);
1663
1664 *pte = __pte;
1665
1666 return (dma_addr_t)address;
1667}
1668
Joerg Roedel431b2a22008-07-11 17:14:22 +02001669/*
1670 * The generic unmapping function for on page in the DMA address space.
1671 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001672static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001673 unsigned long address)
1674{
Joerg Roedel384de722009-05-15 12:30:05 +02001675 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001676 u64 *pte;
1677
1678 if (address >= dom->aperture_size)
1679 return;
1680
Joerg Roedel384de722009-05-15 12:30:05 +02001681 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1682 if (!aperture)
1683 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001684
Joerg Roedel384de722009-05-15 12:30:05 +02001685 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
1686 if (!pte)
1687 return;
1688
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001689 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001690
1691 WARN_ON(!*pte);
1692
1693 *pte = 0ULL;
1694}
1695
Joerg Roedel431b2a22008-07-11 17:14:22 +02001696/*
1697 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01001698 * contiguous memory region into DMA address space. It is used by all
1699 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001700 * Must be called with the domain lock held.
1701 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02001702static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001703 struct dma_ops_domain *dma_dom,
1704 phys_addr_t paddr,
1705 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001706 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001707 bool align,
1708 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02001709{
1710 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02001711 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001712 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001713 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001714 int i;
1715
Joerg Roedele3c449f2008-10-15 22:02:11 -07001716 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001717 paddr &= PAGE_MASK;
1718
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01001719 INC_STATS_COUNTER(total_map_requests);
1720
Joerg Roedelc1858972008-12-12 15:42:39 +01001721 if (pages > 1)
1722 INC_STATS_COUNTER(cross_page);
1723
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001724 if (align)
1725 align_mask = (1UL << get_order(size)) - 1;
1726
Joerg Roedel11b83882009-05-19 10:23:15 +02001727retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02001728 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
1729 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001730 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02001731 /*
1732 * setting next_address here will let the address
1733 * allocator only scan the new allocated range in the
1734 * first run. This is a small optimization.
1735 */
1736 dma_dom->next_address = dma_dom->aperture_size;
1737
Joerg Roedel576175c2009-11-23 19:08:46 +01001738 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02001739 goto out;
1740
1741 /*
1742 * aperture was sucessfully enlarged by 128 MB, try
1743 * allocation again
1744 */
1745 goto retry;
1746 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02001747
1748 start = address;
1749 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01001750 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001751 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02001752 goto out_unmap;
1753
Joerg Roedelcb76c322008-06-26 21:28:00 +02001754 paddr += PAGE_SIZE;
1755 start += PAGE_SIZE;
1756 }
1757 address += offset;
1758
Joerg Roedel5774f7c2008-12-12 15:57:30 +01001759 ADD_STATS_COUNTER(alloced_io_mem, size);
1760
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09001761 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedeldcd1e922009-11-20 15:30:58 +01001762 iommu_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02001763 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01001764 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001765 iommu_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02001766
Joerg Roedelcb76c322008-06-26 21:28:00 +02001767out:
1768 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02001769
1770out_unmap:
1771
1772 for (--i; i >= 0; --i) {
1773 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01001774 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02001775 }
1776
1777 dma_ops_free_addresses(dma_dom, address, pages);
1778
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001779 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001780}
1781
Joerg Roedel431b2a22008-07-11 17:14:22 +02001782/*
1783 * Does the reverse of the __map_single function. Must be called with
1784 * the domain lock held too
1785 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01001786static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001787 dma_addr_t dma_addr,
1788 size_t size,
1789 int dir)
1790{
1791 dma_addr_t i, start;
1792 unsigned int pages;
1793
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001794 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01001795 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02001796 return;
1797
Joerg Roedele3c449f2008-10-15 22:02:11 -07001798 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001799 dma_addr &= PAGE_MASK;
1800 start = dma_addr;
1801
1802 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01001803 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001804 start += PAGE_SIZE;
1805 }
1806
Joerg Roedel5774f7c2008-12-12 15:57:30 +01001807 SUB_STATS_COUNTER(alloced_io_mem, size);
1808
Joerg Roedelcb76c322008-06-26 21:28:00 +02001809 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02001810
Joerg Roedel80be3082008-11-06 14:59:05 +01001811 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001812 iommu_flush_pages(&dma_dom->domain, dma_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01001813 dma_dom->need_flush = false;
1814 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02001815}
1816
Joerg Roedel431b2a22008-07-11 17:14:22 +02001817/*
1818 * The exported map_single function for dma_ops.
1819 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09001820static dma_addr_t map_page(struct device *dev, struct page *page,
1821 unsigned long offset, size_t size,
1822 enum dma_data_direction dir,
1823 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02001824{
1825 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02001826 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02001827 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02001828 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09001829 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02001830
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01001831 INC_STATS_COUNTER(cnt_map_single);
1832
Joerg Roedel94f6d192009-11-24 16:40:02 +01001833 domain = get_domain(dev);
1834 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02001835 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01001836 else if (IS_ERR(domain))
1837 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02001838
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001839 dma_mask = *dev->dma_mask;
1840
Joerg Roedel4da70b92008-06-26 21:28:01 +02001841 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01001842
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01001843 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001844 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001845 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02001846 goto out;
1847
Joerg Roedel0518a3a2009-11-20 16:00:05 +01001848 iommu_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02001849
1850out:
1851 spin_unlock_irqrestore(&domain->lock, flags);
1852
1853 return addr;
1854}
1855
Joerg Roedel431b2a22008-07-11 17:14:22 +02001856/*
1857 * The exported unmap_single function for dma_ops.
1858 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09001859static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
1860 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02001861{
1862 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02001863 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02001864
Joerg Roedel146a6912008-12-12 15:07:12 +01001865 INC_STATS_COUNTER(cnt_unmap_single);
1866
Joerg Roedel94f6d192009-11-24 16:40:02 +01001867 domain = get_domain(dev);
1868 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01001869 return;
1870
Joerg Roedel4da70b92008-06-26 21:28:01 +02001871 spin_lock_irqsave(&domain->lock, flags);
1872
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01001873 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02001874
Joerg Roedel0518a3a2009-11-20 16:00:05 +01001875 iommu_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02001876
1877 spin_unlock_irqrestore(&domain->lock, flags);
1878}
1879
Joerg Roedel431b2a22008-07-11 17:14:22 +02001880/*
1881 * This is a special map_sg function which is used if we should map a
1882 * device which is not handled by an AMD IOMMU in the system.
1883 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02001884static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
1885 int nelems, int dir)
1886{
1887 struct scatterlist *s;
1888 int i;
1889
1890 for_each_sg(sglist, s, nelems, i) {
1891 s->dma_address = (dma_addr_t)sg_phys(s);
1892 s->dma_length = s->length;
1893 }
1894
1895 return nelems;
1896}
1897
Joerg Roedel431b2a22008-07-11 17:14:22 +02001898/*
1899 * The exported map_sg function for dma_ops (handles scatter-gather
1900 * lists).
1901 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02001902static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09001903 int nelems, enum dma_data_direction dir,
1904 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02001905{
1906 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02001907 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02001908 int i;
1909 struct scatterlist *s;
1910 phys_addr_t paddr;
1911 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02001912 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02001913
Joerg Roedeld03f067a2008-12-12 15:09:48 +01001914 INC_STATS_COUNTER(cnt_map_sg);
1915
Joerg Roedel94f6d192009-11-24 16:40:02 +01001916 domain = get_domain(dev);
1917 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001918 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01001919 else if (IS_ERR(domain))
1920 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001921
Joerg Roedel832a90c2008-09-18 15:54:23 +02001922 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02001923
Joerg Roedel65b050a2008-06-26 21:28:02 +02001924 spin_lock_irqsave(&domain->lock, flags);
1925
1926 for_each_sg(sglist, s, nelems, i) {
1927 paddr = sg_phys(s);
1928
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01001929 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001930 paddr, s->length, dir, false,
1931 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02001932
1933 if (s->dma_address) {
1934 s->dma_length = s->length;
1935 mapped_elems++;
1936 } else
1937 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02001938 }
1939
Joerg Roedel0518a3a2009-11-20 16:00:05 +01001940 iommu_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02001941
1942out:
1943 spin_unlock_irqrestore(&domain->lock, flags);
1944
1945 return mapped_elems;
1946unmap:
1947 for_each_sg(sglist, s, mapped_elems, i) {
1948 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01001949 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02001950 s->dma_length, dir);
1951 s->dma_address = s->dma_length = 0;
1952 }
1953
1954 mapped_elems = 0;
1955
1956 goto out;
1957}
1958
Joerg Roedel431b2a22008-07-11 17:14:22 +02001959/*
1960 * The exported map_sg function for dma_ops (handles scatter-gather
1961 * lists).
1962 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02001963static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09001964 int nelems, enum dma_data_direction dir,
1965 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02001966{
1967 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02001968 struct protection_domain *domain;
1969 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02001970 int i;
1971
Joerg Roedel55877a62008-12-12 15:12:14 +01001972 INC_STATS_COUNTER(cnt_unmap_sg);
1973
Joerg Roedel94f6d192009-11-24 16:40:02 +01001974 domain = get_domain(dev);
1975 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01001976 return;
1977
Joerg Roedel65b050a2008-06-26 21:28:02 +02001978 spin_lock_irqsave(&domain->lock, flags);
1979
1980 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01001981 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02001982 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02001983 s->dma_address = s->dma_length = 0;
1984 }
1985
Joerg Roedel0518a3a2009-11-20 16:00:05 +01001986 iommu_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02001987
1988 spin_unlock_irqrestore(&domain->lock, flags);
1989}
1990
Joerg Roedel431b2a22008-07-11 17:14:22 +02001991/*
1992 * The exported alloc_coherent function for dma_ops.
1993 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02001994static void *alloc_coherent(struct device *dev, size_t size,
1995 dma_addr_t *dma_addr, gfp_t flag)
1996{
1997 unsigned long flags;
1998 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02001999 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002000 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002001 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002002
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002003 INC_STATS_COUNTER(cnt_alloc_coherent);
2004
Joerg Roedel94f6d192009-11-24 16:40:02 +01002005 domain = get_domain(dev);
2006 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002007 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2008 *dma_addr = __pa(virt_addr);
2009 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002010 } else if (IS_ERR(domain))
2011 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002012
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002013 dma_mask = dev->coherent_dma_mask;
2014 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2015 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002016
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002017 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2018 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302019 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002020
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002021 paddr = virt_to_phys(virt_addr);
2022
Joerg Roedel832a90c2008-09-18 15:54:23 +02002023 if (!dma_mask)
2024 dma_mask = *dev->dma_mask;
2025
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002026 spin_lock_irqsave(&domain->lock, flags);
2027
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002028 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002029 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002030
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002031 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002032 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002033 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002034 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002035
Joerg Roedel0518a3a2009-11-20 16:00:05 +01002036 iommu_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002037
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002038 spin_unlock_irqrestore(&domain->lock, flags);
2039
2040 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002041
2042out_free:
2043
2044 free_pages((unsigned long)virt_addr, get_order(size));
2045
2046 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002047}
2048
Joerg Roedel431b2a22008-07-11 17:14:22 +02002049/*
2050 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002051 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002052static void free_coherent(struct device *dev, size_t size,
2053 void *virt_addr, dma_addr_t dma_addr)
2054{
2055 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002056 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002057
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002058 INC_STATS_COUNTER(cnt_free_coherent);
2059
Joerg Roedel94f6d192009-11-24 16:40:02 +01002060 domain = get_domain(dev);
2061 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002062 goto free_mem;
2063
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002064 spin_lock_irqsave(&domain->lock, flags);
2065
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002066 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002067
Joerg Roedel0518a3a2009-11-20 16:00:05 +01002068 iommu_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002069
2070 spin_unlock_irqrestore(&domain->lock, flags);
2071
2072free_mem:
2073 free_pages((unsigned long)virt_addr, get_order(size));
2074}
2075
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002076/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002077 * This function is called by the DMA layer to find out if we can handle a
2078 * particular device. It is part of the dma_ops.
2079 */
2080static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2081{
Joerg Roedel420aef82009-11-23 16:14:57 +01002082 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002083}
2084
2085/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002086 * The function for pre-allocating protection domains.
2087 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002088 * If the driver core informs the DMA layer if a driver grabs a device
2089 * we don't need to preallocate the protection domains anymore.
2090 * For now we have to.
2091 */
Jaswinder Singh Rajput0e93dd82008-12-29 21:45:22 +05302092static void prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002093{
2094 struct pci_dev *dev = NULL;
2095 struct dma_ops_domain *dma_dom;
2096 struct amd_iommu *iommu;
Joerg Roedelbe831292009-11-23 12:50:00 +01002097 u16 devid, __devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002098
2099 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
Joerg Roedelbe831292009-11-23 12:50:00 +01002100 __devid = devid = calc_devid(dev->bus->number, dev->devfn);
Joerg Roedel3a61ec32008-07-25 13:07:50 +02002101 if (devid > amd_iommu_last_bdf)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002102 continue;
2103 devid = amd_iommu_alias_table[devid];
Joerg Roedel15898bb2009-11-24 15:39:42 +01002104 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002105 continue;
2106 iommu = amd_iommu_rlookup_table[devid];
2107 if (!iommu)
2108 continue;
Joerg Roedeld9cfed92009-05-19 12:16:29 +02002109 dma_dom = dma_ops_domain_alloc(iommu);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002110 if (!dma_dom)
2111 continue;
2112 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002113 dma_dom->target_dev = devid;
2114
Joerg Roedel15898bb2009-11-24 15:39:42 +01002115 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01002116
Joerg Roedelbd60b732008-09-11 10:24:48 +02002117 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002118 }
2119}
2120
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002121static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002122 .alloc_coherent = alloc_coherent,
2123 .free_coherent = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002124 .map_page = map_page,
2125 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002126 .map_sg = map_sg,
2127 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002128 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002129};
2130
Joerg Roedel431b2a22008-07-11 17:14:22 +02002131/*
2132 * The function which clues the AMD IOMMU driver into dma_ops.
2133 */
Joerg Roedel6631ee92008-06-26 21:28:05 +02002134int __init amd_iommu_init_dma_ops(void)
2135{
2136 struct amd_iommu *iommu;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002137 int ret;
2138
Joerg Roedel431b2a22008-07-11 17:14:22 +02002139 /*
2140 * first allocate a default protection domain for every IOMMU we
2141 * found in the system. Devices not assigned to any other
2142 * protection domain will be assigned to the default one.
2143 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02002144 for_each_iommu(iommu) {
Joerg Roedeld9cfed92009-05-19 12:16:29 +02002145 iommu->default_dom = dma_ops_domain_alloc(iommu);
Joerg Roedel6631ee92008-06-26 21:28:05 +02002146 if (iommu->default_dom == NULL)
2147 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01002148 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002149 ret = iommu_init_unity_mappings(iommu);
2150 if (ret)
2151 goto free_domains;
2152 }
2153
Joerg Roedel431b2a22008-07-11 17:14:22 +02002154 /*
2155 * If device isolation is enabled, pre-allocate the protection
2156 * domains for each device.
2157 */
Joerg Roedel6631ee92008-06-26 21:28:05 +02002158 if (amd_iommu_isolate)
2159 prealloc_protection_domains();
2160
2161 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002162 swiotlb = 0;
Ingo Molnar92af4e22008-06-27 10:48:16 +02002163#ifdef CONFIG_GART_IOMMU
Joerg Roedel6631ee92008-06-26 21:28:05 +02002164 gart_iommu_aperture_disabled = 1;
2165 gart_iommu_aperture = 0;
Ingo Molnar92af4e22008-06-27 10:48:16 +02002166#endif
Joerg Roedel6631ee92008-06-26 21:28:05 +02002167
Joerg Roedel431b2a22008-07-11 17:14:22 +02002168 /* Make the driver finally visible to the drivers */
Joerg Roedel6631ee92008-06-26 21:28:05 +02002169 dma_ops = &amd_iommu_dma_ops;
2170
Joerg Roedel26961ef2008-12-03 17:00:17 +01002171 register_iommu(&amd_iommu_ops);
Joerg Roedel26961ef2008-12-03 17:00:17 +01002172
Joerg Roedele275a2a2008-12-10 18:27:25 +01002173 bus_register_notifier(&pci_bus_type, &device_nb);
2174
Joerg Roedel7f265082008-12-12 13:50:21 +01002175 amd_iommu_stats_init();
2176
Joerg Roedel6631ee92008-06-26 21:28:05 +02002177 return 0;
2178
2179free_domains:
2180
Joerg Roedel3bd22172009-05-04 15:06:20 +02002181 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002182 if (iommu->default_dom)
2183 dma_ops_domain_free(iommu->default_dom);
2184 }
2185
2186 return ret;
2187}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002188
2189/*****************************************************************************
2190 *
2191 * The following functions belong to the exported interface of AMD IOMMU
2192 *
2193 * This interface allows access to lower level functions of the IOMMU
2194 * like protection domain handling and assignement of devices to domains
2195 * which is not possible with the dma_ops interface.
2196 *
2197 *****************************************************************************/
2198
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002199static void cleanup_domain(struct protection_domain *domain)
2200{
2201 unsigned long flags;
2202 u16 devid;
2203
2204 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2205
2206 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
2207 if (amd_iommu_pd_table[devid] == domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002208 clear_dte_entry(devid);
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002209
2210 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2211}
2212
Joerg Roedel26508152009-08-26 16:52:40 +02002213static void protection_domain_free(struct protection_domain *domain)
2214{
2215 if (!domain)
2216 return;
2217
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002218 del_domain_from_list(domain);
2219
Joerg Roedel26508152009-08-26 16:52:40 +02002220 if (domain->id)
2221 domain_id_free(domain->id);
2222
2223 kfree(domain);
2224}
2225
2226static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002227{
2228 struct protection_domain *domain;
2229
2230 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2231 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002232 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002233
2234 spin_lock_init(&domain->lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01002235 domain->id = domain_id_alloc();
2236 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02002237 goto out_err;
2238
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002239 add_domain_to_list(domain);
2240
Joerg Roedel26508152009-08-26 16:52:40 +02002241 return domain;
2242
2243out_err:
2244 kfree(domain);
2245
2246 return NULL;
2247}
2248
2249static int amd_iommu_domain_init(struct iommu_domain *dom)
2250{
2251 struct protection_domain *domain;
2252
2253 domain = protection_domain_alloc();
2254 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01002255 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02002256
2257 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002258 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2259 if (!domain->pt_root)
2260 goto out_free;
2261
2262 dom->priv = domain;
2263
2264 return 0;
2265
2266out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02002267 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01002268
2269 return -ENOMEM;
2270}
2271
Joerg Roedel98383fc2008-12-02 18:34:12 +01002272static void amd_iommu_domain_destroy(struct iommu_domain *dom)
2273{
2274 struct protection_domain *domain = dom->priv;
2275
2276 if (!domain)
2277 return;
2278
2279 if (domain->dev_cnt > 0)
2280 cleanup_domain(domain);
2281
2282 BUG_ON(domain->dev_cnt != 0);
2283
2284 free_pagetable(domain);
2285
2286 domain_id_free(domain->id);
2287
2288 kfree(domain);
2289
2290 dom->priv = NULL;
2291}
2292
Joerg Roedel684f2882008-12-08 12:07:44 +01002293static void amd_iommu_detach_device(struct iommu_domain *dom,
2294 struct device *dev)
2295{
Joerg Roedel684f2882008-12-08 12:07:44 +01002296 struct amd_iommu *iommu;
2297 struct pci_dev *pdev;
2298 u16 devid;
2299
2300 if (dev->bus != &pci_bus_type)
2301 return;
2302
2303 pdev = to_pci_dev(dev);
2304
2305 devid = calc_devid(pdev->bus->number, pdev->devfn);
2306
2307 if (devid > 0)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002308 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002309
2310 iommu = amd_iommu_rlookup_table[devid];
2311 if (!iommu)
2312 return;
2313
2314 iommu_queue_inv_dev_entry(iommu, devid);
2315 iommu_completion_wait(iommu);
2316}
2317
Joerg Roedel01106062008-12-02 19:34:11 +01002318static int amd_iommu_attach_device(struct iommu_domain *dom,
2319 struct device *dev)
2320{
2321 struct protection_domain *domain = dom->priv;
2322 struct protection_domain *old_domain;
2323 struct amd_iommu *iommu;
2324 struct pci_dev *pdev;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002325 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002326 u16 devid;
2327
2328 if (dev->bus != &pci_bus_type)
2329 return -EINVAL;
2330
2331 pdev = to_pci_dev(dev);
2332
2333 devid = calc_devid(pdev->bus->number, pdev->devfn);
2334
2335 if (devid >= amd_iommu_last_bdf ||
2336 devid != amd_iommu_alias_table[devid])
2337 return -EINVAL;
2338
2339 iommu = amd_iommu_rlookup_table[devid];
2340 if (!iommu)
2341 return -EINVAL;
2342
Joerg Roedel15898bb2009-11-24 15:39:42 +01002343 old_domain = amd_iommu_pd_table[devid];
Joerg Roedel01106062008-12-02 19:34:11 +01002344 if (old_domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002345 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002346
Joerg Roedel15898bb2009-11-24 15:39:42 +01002347 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01002348
2349 iommu_completion_wait(iommu);
2350
Joerg Roedel15898bb2009-11-24 15:39:42 +01002351 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002352}
2353
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002354static int amd_iommu_map_range(struct iommu_domain *dom,
2355 unsigned long iova, phys_addr_t paddr,
2356 size_t size, int iommu_prot)
2357{
2358 struct protection_domain *domain = dom->priv;
2359 unsigned long i, npages = iommu_num_pages(paddr, size, PAGE_SIZE);
2360 int prot = 0;
2361 int ret;
2362
2363 if (iommu_prot & IOMMU_READ)
2364 prot |= IOMMU_PROT_IR;
2365 if (iommu_prot & IOMMU_WRITE)
2366 prot |= IOMMU_PROT_IW;
2367
2368 iova &= PAGE_MASK;
2369 paddr &= PAGE_MASK;
2370
2371 for (i = 0; i < npages; ++i) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002372 ret = iommu_map_page(domain, iova, paddr, prot, PM_MAP_4k);
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002373 if (ret)
2374 return ret;
2375
2376 iova += PAGE_SIZE;
2377 paddr += PAGE_SIZE;
2378 }
2379
2380 return 0;
2381}
2382
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002383static void amd_iommu_unmap_range(struct iommu_domain *dom,
2384 unsigned long iova, size_t size)
2385{
2386
2387 struct protection_domain *domain = dom->priv;
2388 unsigned long i, npages = iommu_num_pages(iova, size, PAGE_SIZE);
2389
2390 iova &= PAGE_MASK;
2391
2392 for (i = 0; i < npages; ++i) {
Joerg Roedela6b256b2009-09-03 12:21:31 +02002393 iommu_unmap_page(domain, iova, PM_MAP_4k);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002394 iova += PAGE_SIZE;
2395 }
2396
Joerg Roedel601367d2009-11-20 16:08:55 +01002397 iommu_flush_tlb_pde(domain);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002398}
2399
Joerg Roedel645c4c82008-12-02 20:05:50 +01002400static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2401 unsigned long iova)
2402{
2403 struct protection_domain *domain = dom->priv;
2404 unsigned long offset = iova & ~PAGE_MASK;
2405 phys_addr_t paddr;
2406 u64 *pte;
2407
Joerg Roedela6b256b2009-09-03 12:21:31 +02002408 pte = fetch_pte(domain, iova, PM_MAP_4k);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002409
Joerg Roedela6d41a42009-09-02 17:08:55 +02002410 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01002411 return 0;
2412
2413 paddr = *pte & IOMMU_PAGE_MASK;
2414 paddr |= offset;
2415
2416 return paddr;
2417}
2418
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002419static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
2420 unsigned long cap)
2421{
2422 return 0;
2423}
2424
Joerg Roedel26961ef2008-12-03 17:00:17 +01002425static struct iommu_ops amd_iommu_ops = {
2426 .domain_init = amd_iommu_domain_init,
2427 .domain_destroy = amd_iommu_domain_destroy,
2428 .attach_dev = amd_iommu_attach_device,
2429 .detach_dev = amd_iommu_detach_device,
2430 .map = amd_iommu_map_range,
2431 .unmap = amd_iommu_unmap_range,
2432 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002433 .domain_has_cap = amd_iommu_domain_has_cap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002434};
2435
Joerg Roedel0feae532009-08-26 15:26:30 +02002436/*****************************************************************************
2437 *
2438 * The next functions do a basic initialization of IOMMU for pass through
2439 * mode
2440 *
2441 * In passthrough mode the IOMMU is initialized and enabled but not used for
2442 * DMA-API translation.
2443 *
2444 *****************************************************************************/
2445
2446int __init amd_iommu_init_passthrough(void)
2447{
Joerg Roedel15898bb2009-11-24 15:39:42 +01002448 struct amd_iommu *iommu;
Joerg Roedel0feae532009-08-26 15:26:30 +02002449 struct pci_dev *dev = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002450 u16 devid;
Joerg Roedel0feae532009-08-26 15:26:30 +02002451
2452 /* allocate passthroug domain */
2453 pt_domain = protection_domain_alloc();
2454 if (!pt_domain)
2455 return -ENOMEM;
2456
2457 pt_domain->mode |= PAGE_MODE_NONE;
2458
2459 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
Joerg Roedel0feae532009-08-26 15:26:30 +02002460
2461 devid = calc_devid(dev->bus->number, dev->devfn);
2462 if (devid > amd_iommu_last_bdf)
2463 continue;
2464
Joerg Roedel15898bb2009-11-24 15:39:42 +01002465 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02002466 if (!iommu)
2467 continue;
2468
Joerg Roedel15898bb2009-11-24 15:39:42 +01002469 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02002470 }
2471
2472 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
2473
2474 return 0;
2475}