blob: f42793d1574d452df331463cc96baee0522382c5 [file] [log] [blame]
Joerg Roedelb6c02712008-06-26 21:27:53 +02001/*
Joerg Roedel5d0d7152010-10-13 11:13:21 +02002 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
Joerg Roedelb6c02712008-06-26 21:27:53 +02003 * Author: Joerg Roedel <joerg.roedel@amd.com>
4 * Leo Duran <leo.duran@amd.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010020#include <linux/ratelimit.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020021#include <linux/pci.h>
Joerg Roedelcb41ed82011-04-05 11:00:53 +020022#include <linux/pci-ats.h>
Akinobu Mitaa66022c2009-12-15 16:48:28 -080023#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Joerg Roedel7f265082008-12-12 13:50:21 +010025#include <linux/debugfs.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020026#include <linux/scatterlist.h>
FUJITA Tomonori51491362009-01-05 23:47:25 +090027#include <linux/dma-mapping.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020028#include <linux/iommu-helper.h>
Joerg Roedelc156e342008-12-02 18:13:27 +010029#include <linux/iommu.h>
Joerg Roedel815b33f2011-04-06 17:26:49 +020030#include <linux/delay.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020031#include <linux/amd-iommu.h>
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010032#include <linux/notifier.h>
33#include <linux/export.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020034#include <linux/irq.h>
35#include <linux/msi.h>
36#include <asm/irq_remapping.h>
37#include <asm/io_apic.h>
38#include <asm/apic.h>
39#include <asm/hw_irq.h>
Joerg Roedel17f5b562011-07-06 17:14:44 +020040#include <asm/msidef.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020041#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090042#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010043#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020044#include <asm/dma.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020045
46#include "amd_iommu_proto.h"
47#include "amd_iommu_types.h"
Joerg Roedel6b474b82012-06-26 16:46:04 +020048#include "irq_remapping.h"
Joerg Roedelb6c02712008-06-26 21:27:53 +020049
50#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
51
Joerg Roedel815b33f2011-04-06 17:26:49 +020052#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020053
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020054/*
55 * This bitmap is used to advertise the page sizes our hardware support
56 * to the IOMMU core, which will then use this information to split
57 * physically contiguous memory regions it is mapping into page sizes
58 * that we support.
59 *
Joerg Roedel954e3dd2012-12-02 15:35:37 +010060 * 512GB Pages are not supported due to a hardware bug
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020061 */
Joerg Roedel954e3dd2012-12-02 15:35:37 +010062#define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020063
Joerg Roedelb6c02712008-06-26 21:27:53 +020064static DEFINE_RWLOCK(amd_iommu_devtable_lock);
65
Joerg Roedelbd60b732008-09-11 10:24:48 +020066/* A list of preallocated protection domains */
67static LIST_HEAD(iommu_pd_list);
68static DEFINE_SPINLOCK(iommu_pd_list_lock);
69
Joerg Roedel8fa5f802011-06-09 12:24:45 +020070/* List of all available dev_data structures */
71static LIST_HEAD(dev_data_list);
72static DEFINE_SPINLOCK(dev_data_list_lock);
73
Joerg Roedel6efed632012-06-14 15:52:58 +020074LIST_HEAD(ioapic_map);
75LIST_HEAD(hpet_map);
76
Joerg Roedel0feae532009-08-26 15:26:30 +020077/*
78 * Domain for untranslated devices - only allocated
79 * if iommu=pt passed on kernel cmd line.
80 */
81static struct protection_domain *pt_domain;
82
Joerg Roedel26961ef2008-12-03 17:00:17 +010083static struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010084
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010085static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +010086int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010087
Joerg Roedelac1534a2012-06-21 14:52:40 +020088static struct dma_map_ops amd_iommu_dma_ops;
89
Joerg Roedel431b2a22008-07-11 17:14:22 +020090/*
91 * general struct to manage commands send to an IOMMU
92 */
Joerg Roedeld6449532008-07-11 17:14:28 +020093struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +020094 u32 data[4];
95};
96
Joerg Roedel05152a02012-06-15 16:53:51 +020097struct kmem_cache *amd_iommu_irq_cache;
98
Joerg Roedel04bfdd82009-09-02 16:00:23 +020099static void update_domain(struct protection_domain *domain);
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100100static int __init alloc_passthrough_domain(void);
Chris Wrightc1eee672009-05-21 00:56:58 -0700101
Joerg Roedel15898bb2009-11-24 15:39:42 +0100102/****************************************************************************
103 *
104 * Helper functions
105 *
106 ****************************************************************************/
107
Joerg Roedelf62dda62011-06-09 12:55:35 +0200108static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200109{
110 struct iommu_dev_data *dev_data;
111 unsigned long flags;
112
113 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
114 if (!dev_data)
115 return NULL;
116
Joerg Roedelf62dda62011-06-09 12:55:35 +0200117 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200118 atomic_set(&dev_data->bind, 0);
119
120 spin_lock_irqsave(&dev_data_list_lock, flags);
121 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
122 spin_unlock_irqrestore(&dev_data_list_lock, flags);
123
124 return dev_data;
125}
126
127static void free_dev_data(struct iommu_dev_data *dev_data)
128{
129 unsigned long flags;
130
131 spin_lock_irqsave(&dev_data_list_lock, flags);
132 list_del(&dev_data->dev_data_list);
133 spin_unlock_irqrestore(&dev_data_list_lock, flags);
134
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600135 if (dev_data->group)
136 iommu_group_put(dev_data->group);
137
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200138 kfree(dev_data);
139}
140
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200141static struct iommu_dev_data *search_dev_data(u16 devid)
142{
143 struct iommu_dev_data *dev_data;
144 unsigned long flags;
145
146 spin_lock_irqsave(&dev_data_list_lock, flags);
147 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
148 if (dev_data->devid == devid)
149 goto out_unlock;
150 }
151
152 dev_data = NULL;
153
154out_unlock:
155 spin_unlock_irqrestore(&dev_data_list_lock, flags);
156
157 return dev_data;
158}
159
160static struct iommu_dev_data *find_dev_data(u16 devid)
161{
162 struct iommu_dev_data *dev_data;
163
164 dev_data = search_dev_data(devid);
165
166 if (dev_data == NULL)
167 dev_data = alloc_dev_data(devid);
168
169 return dev_data;
170}
171
Joerg Roedel15898bb2009-11-24 15:39:42 +0100172static inline u16 get_device_id(struct device *dev)
173{
174 struct pci_dev *pdev = to_pci_dev(dev);
175
176 return calc_devid(pdev->bus->number, pdev->devfn);
177}
178
Joerg Roedel657cbb62009-11-23 15:26:46 +0100179static struct iommu_dev_data *get_dev_data(struct device *dev)
180{
181 return dev->archdata.iommu;
182}
183
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100184static bool pci_iommuv2_capable(struct pci_dev *pdev)
185{
186 static const int caps[] = {
187 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100188 PCI_EXT_CAP_ID_PRI,
189 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100190 };
191 int i, pos;
192
193 for (i = 0; i < 3; ++i) {
194 pos = pci_find_ext_capability(pdev, caps[i]);
195 if (pos == 0)
196 return false;
197 }
198
199 return true;
200}
201
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100202static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
203{
204 struct iommu_dev_data *dev_data;
205
206 dev_data = get_dev_data(&pdev->dev);
207
208 return dev_data->errata & (1 << erratum) ? true : false;
209}
210
Joerg Roedel71c70982009-11-24 16:43:06 +0100211/*
212 * In this function the list of preallocated protection domains is traversed to
213 * find the domain for a specific device
214 */
215static struct dma_ops_domain *find_protection_domain(u16 devid)
216{
217 struct dma_ops_domain *entry, *ret = NULL;
218 unsigned long flags;
219 u16 alias = amd_iommu_alias_table[devid];
220
221 if (list_empty(&iommu_pd_list))
222 return NULL;
223
224 spin_lock_irqsave(&iommu_pd_list_lock, flags);
225
226 list_for_each_entry(entry, &iommu_pd_list, list) {
227 if (entry->target_dev == devid ||
228 entry->target_dev == alias) {
229 ret = entry;
230 break;
231 }
232 }
233
234 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
235
236 return ret;
237}
238
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100239/*
240 * This function checks if the driver got a valid device from the caller to
241 * avoid dereferencing invalid pointers.
242 */
243static bool check_device(struct device *dev)
244{
245 u16 devid;
246
247 if (!dev || !dev->dma_mask)
248 return false;
249
250 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100251 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100252 return false;
253
254 devid = get_device_id(dev);
255
256 /* Out of our scope? */
257 if (devid > amd_iommu_last_bdf)
258 return false;
259
260 if (amd_iommu_rlookup_table[devid] == NULL)
261 return false;
262
263 return true;
264}
265
Alex Williamson664b6002012-05-30 14:19:31 -0600266static void swap_pci_ref(struct pci_dev **from, struct pci_dev *to)
267{
268 pci_dev_put(*from);
269 *from = to;
270}
271
Alex Williamson2bff6a52012-10-08 22:49:48 -0600272static struct pci_bus *find_hosted_bus(struct pci_bus *bus)
273{
274 while (!bus->self) {
275 if (!pci_is_root_bus(bus))
276 bus = bus->parent;
277 else
278 return ERR_PTR(-ENODEV);
279 }
280
281 return bus;
282}
283
Alex Williamson664b6002012-05-30 14:19:31 -0600284#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
285
Alex Williamson2851db22012-10-08 22:49:41 -0600286static struct pci_dev *get_isolation_root(struct pci_dev *pdev)
Joerg Roedel657cbb62009-11-23 15:26:46 +0100287{
Alex Williamson2851db22012-10-08 22:49:41 -0600288 struct pci_dev *dma_pdev = pdev;
Alex Williamson9dcd6132012-05-30 14:19:07 -0600289
Alex Williamson31fe9432012-08-04 12:09:03 -0600290 /* Account for quirked devices */
Alex Williamson664b6002012-05-30 14:19:31 -0600291 swap_pci_ref(&dma_pdev, pci_get_dma_source(dma_pdev));
292
Alex Williamson31fe9432012-08-04 12:09:03 -0600293 /*
294 * If it's a multifunction device that does not support our
295 * required ACS flags, add to the same group as function 0.
296 */
Alex Williamson664b6002012-05-30 14:19:31 -0600297 if (dma_pdev->multifunction &&
298 !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS))
299 swap_pci_ref(&dma_pdev,
300 pci_get_slot(dma_pdev->bus,
301 PCI_DEVFN(PCI_SLOT(dma_pdev->devfn),
302 0)));
303
Alex Williamson31fe9432012-08-04 12:09:03 -0600304 /*
305 * Devices on the root bus go through the iommu. If that's not us,
306 * find the next upstream device and test ACS up to the root bus.
307 * Finding the next device may require skipping virtual buses.
308 */
Alex Williamson664b6002012-05-30 14:19:31 -0600309 while (!pci_is_root_bus(dma_pdev->bus)) {
Alex Williamson2bff6a52012-10-08 22:49:48 -0600310 struct pci_bus *bus = find_hosted_bus(dma_pdev->bus);
311 if (IS_ERR(bus))
312 break;
Alex Williamson31fe9432012-08-04 12:09:03 -0600313
314 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
Alex Williamson664b6002012-05-30 14:19:31 -0600315 break;
316
Alex Williamson31fe9432012-08-04 12:09:03 -0600317 swap_pci_ref(&dma_pdev, pci_dev_get(bus->self));
Joerg Roedel26018872011-06-06 16:50:14 +0200318 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100319
Alex Williamson2851db22012-10-08 22:49:41 -0600320 return dma_pdev;
321}
322
Alex Williamsonce7ac4a2012-10-08 22:49:54 -0600323static int use_pdev_iommu_group(struct pci_dev *pdev, struct device *dev)
324{
325 struct iommu_group *group = iommu_group_get(&pdev->dev);
326 int ret;
327
328 if (!group) {
329 group = iommu_group_alloc();
330 if (IS_ERR(group))
331 return PTR_ERR(group);
332
333 WARN_ON(&pdev->dev != dev);
334 }
335
336 ret = iommu_group_add_device(group, dev);
337 iommu_group_put(group);
338 return ret;
339}
340
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600341static int use_dev_data_iommu_group(struct iommu_dev_data *dev_data,
342 struct device *dev)
343{
344 if (!dev_data->group) {
345 struct iommu_group *group = iommu_group_alloc();
346 if (IS_ERR(group))
347 return PTR_ERR(group);
348
349 dev_data->group = group;
350 }
351
352 return iommu_group_add_device(dev_data->group, dev);
353}
354
Alex Williamson2851db22012-10-08 22:49:41 -0600355static int init_iommu_group(struct device *dev)
356{
357 struct iommu_dev_data *dev_data;
358 struct iommu_group *group;
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600359 struct pci_dev *dma_pdev;
Alex Williamson2851db22012-10-08 22:49:41 -0600360 int ret;
361
362 group = iommu_group_get(dev);
363 if (group) {
364 iommu_group_put(group);
365 return 0;
366 }
367
368 dev_data = find_dev_data(get_device_id(dev));
369 if (!dev_data)
370 return -ENOMEM;
371
372 if (dev_data->alias_data) {
373 u16 alias;
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600374 struct pci_bus *bus;
Alex Williamson2851db22012-10-08 22:49:41 -0600375
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600376 if (dev_data->alias_data->group)
377 goto use_group;
378
379 /*
380 * If the alias device exists, it's effectively just a first
381 * level quirk for finding the DMA source.
382 */
Alex Williamson2851db22012-10-08 22:49:41 -0600383 alias = amd_iommu_alias_table[dev_data->devid];
384 dma_pdev = pci_get_bus_and_slot(alias >> 8, alias & 0xff);
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600385 if (dma_pdev) {
386 dma_pdev = get_isolation_root(dma_pdev);
387 goto use_pdev;
388 }
389
390 /*
391 * If the alias is virtual, try to find a parent device
392 * and test whether the IOMMU group is actualy rooted above
393 * the alias. Be careful to also test the parent device if
394 * we think the alias is the root of the group.
395 */
396 bus = pci_find_bus(0, alias >> 8);
397 if (!bus)
398 goto use_group;
399
400 bus = find_hosted_bus(bus);
401 if (IS_ERR(bus) || !bus->self)
402 goto use_group;
403
404 dma_pdev = get_isolation_root(pci_dev_get(bus->self));
405 if (dma_pdev != bus->self || (dma_pdev->multifunction &&
406 !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS)))
407 goto use_pdev;
408
409 pci_dev_put(dma_pdev);
410 goto use_group;
Alex Williamson2851db22012-10-08 22:49:41 -0600411 }
412
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600413 dma_pdev = get_isolation_root(pci_dev_get(to_pci_dev(dev)));
414use_pdev:
Alex Williamsonce7ac4a2012-10-08 22:49:54 -0600415 ret = use_pdev_iommu_group(dma_pdev, dev);
Alex Williamson9dcd6132012-05-30 14:19:07 -0600416 pci_dev_put(dma_pdev);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600417 return ret;
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600418use_group:
419 return use_dev_data_iommu_group(dev_data->alias_data, dev);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600420}
421
422static int iommu_init_device(struct device *dev)
423{
424 struct pci_dev *pdev = to_pci_dev(dev);
425 struct iommu_dev_data *dev_data;
426 u16 alias;
427 int ret;
428
429 if (dev->archdata.iommu)
430 return 0;
431
432 dev_data = find_dev_data(get_device_id(dev));
433 if (!dev_data)
434 return -ENOMEM;
435
436 alias = amd_iommu_alias_table[dev_data->devid];
437 if (alias != dev_data->devid) {
438 struct iommu_dev_data *alias_data;
439
440 alias_data = find_dev_data(alias);
441 if (alias_data == NULL) {
442 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
443 dev_name(dev));
444 free_dev_data(dev_data);
445 return -ENOTSUPP;
446 }
447 dev_data->alias_data = alias_data;
448 }
449
450 ret = init_iommu_group(dev);
Alex Williamson9dcd6132012-05-30 14:19:07 -0600451 if (ret)
452 return ret;
453
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100454 if (pci_iommuv2_capable(pdev)) {
455 struct amd_iommu *iommu;
456
457 iommu = amd_iommu_rlookup_table[dev_data->devid];
458 dev_data->iommu_v2 = iommu->is_iommu_v2;
459 }
460
Joerg Roedel657cbb62009-11-23 15:26:46 +0100461 dev->archdata.iommu = dev_data;
462
Joerg Roedel657cbb62009-11-23 15:26:46 +0100463 return 0;
464}
465
Joerg Roedel26018872011-06-06 16:50:14 +0200466static void iommu_ignore_device(struct device *dev)
467{
468 u16 devid, alias;
469
470 devid = get_device_id(dev);
471 alias = amd_iommu_alias_table[devid];
472
473 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
474 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
475
476 amd_iommu_rlookup_table[devid] = NULL;
477 amd_iommu_rlookup_table[alias] = NULL;
478}
479
Joerg Roedel657cbb62009-11-23 15:26:46 +0100480static void iommu_uninit_device(struct device *dev)
481{
Alex Williamson9dcd6132012-05-30 14:19:07 -0600482 iommu_group_remove_device(dev);
483
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200484 /*
485 * Nothing to do here - we keep dev_data around for unplugged devices
486 * and reuse it when the device is re-plugged - not doing so would
487 * introduce a ton of races.
488 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100489}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100490
491void __init amd_iommu_uninit_devices(void)
492{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200493 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100494 struct pci_dev *pdev = NULL;
495
496 for_each_pci_dev(pdev) {
497
498 if (!check_device(&pdev->dev))
499 continue;
500
501 iommu_uninit_device(&pdev->dev);
502 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200503
504 /* Free all of our dev_data structures */
505 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
506 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100507}
508
509int __init amd_iommu_init_devices(void)
510{
511 struct pci_dev *pdev = NULL;
512 int ret = 0;
513
514 for_each_pci_dev(pdev) {
515
516 if (!check_device(&pdev->dev))
517 continue;
518
519 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200520 if (ret == -ENOTSUPP)
521 iommu_ignore_device(&pdev->dev);
522 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100523 goto out_free;
524 }
525
526 return 0;
527
528out_free:
529
530 amd_iommu_uninit_devices();
531
532 return ret;
533}
Joerg Roedel7f265082008-12-12 13:50:21 +0100534#ifdef CONFIG_AMD_IOMMU_STATS
535
536/*
537 * Initialization code for statistics collection
538 */
539
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100540DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100541DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100542DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100543DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100544DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100545DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100546DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100547DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100548DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100549DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100550DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100551DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100552DECLARE_STATS_COUNTER(complete_ppr);
553DECLARE_STATS_COUNTER(invalidate_iotlb);
554DECLARE_STATS_COUNTER(invalidate_iotlb_all);
555DECLARE_STATS_COUNTER(pri_requests);
556
Joerg Roedel7f265082008-12-12 13:50:21 +0100557static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100558static struct dentry *de_fflush;
559
560static void amd_iommu_stats_add(struct __iommu_counter *cnt)
561{
562 if (stats_dir == NULL)
563 return;
564
565 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
566 &cnt->value);
567}
568
569static void amd_iommu_stats_init(void)
570{
571 stats_dir = debugfs_create_dir("amd-iommu", NULL);
572 if (stats_dir == NULL)
573 return;
574
Joerg Roedel7f265082008-12-12 13:50:21 +0100575 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
Dan Carpenter3775d482012-06-27 12:09:18 +0300576 &amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100577
578 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100579 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100580 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100581 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100582 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100583 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100584 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100585 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100586 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100587 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100588 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100589 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100590 amd_iommu_stats_add(&complete_ppr);
591 amd_iommu_stats_add(&invalidate_iotlb);
592 amd_iommu_stats_add(&invalidate_iotlb_all);
593 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100594}
595
596#endif
597
Joerg Roedel431b2a22008-07-11 17:14:22 +0200598/****************************************************************************
599 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200600 * Interrupt handling functions
601 *
602 ****************************************************************************/
603
Joerg Roedele3e59872009-09-03 14:02:10 +0200604static void dump_dte_entry(u16 devid)
605{
606 int i;
607
Joerg Roedelee6c2862011-11-09 12:06:03 +0100608 for (i = 0; i < 4; ++i)
609 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200610 amd_iommu_dev_table[devid].data[i]);
611}
612
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200613static void dump_command(unsigned long phys_addr)
614{
615 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
616 int i;
617
618 for (i = 0; i < 4; ++i)
619 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
620}
621
Joerg Roedela345b232009-09-03 15:01:43 +0200622static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200623{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200624 int type, devid, domid, flags;
625 volatile u32 *event = __evt;
626 int count = 0;
627 u64 address;
628
629retry:
630 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
631 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
632 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
633 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
634 address = (u64)(((u64)event[3]) << 32) | event[2];
635
636 if (type == 0) {
637 /* Did we hit the erratum? */
638 if (++count == LOOP_TIMEOUT) {
639 pr_err("AMD-Vi: No event written to event log\n");
640 return;
641 }
642 udelay(1);
643 goto retry;
644 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200645
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200646 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200647
648 switch (type) {
649 case EVENT_TYPE_ILL_DEV:
650 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
651 "address=0x%016llx flags=0x%04x]\n",
652 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
653 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200654 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200655 break;
656 case EVENT_TYPE_IO_FAULT:
657 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
658 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
659 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
660 domid, address, flags);
661 break;
662 case EVENT_TYPE_DEV_TAB_ERR:
663 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
664 "address=0x%016llx flags=0x%04x]\n",
665 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
666 address, flags);
667 break;
668 case EVENT_TYPE_PAGE_TAB_ERR:
669 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
670 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
671 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
672 domid, address, flags);
673 break;
674 case EVENT_TYPE_ILL_CMD:
675 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200676 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200677 break;
678 case EVENT_TYPE_CMD_HARD_ERR:
679 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
680 "flags=0x%04x]\n", address, flags);
681 break;
682 case EVENT_TYPE_IOTLB_INV_TO:
683 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
684 "address=0x%016llx]\n",
685 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
686 address);
687 break;
688 case EVENT_TYPE_INV_DEV_REQ:
689 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
690 "address=0x%016llx flags=0x%04x]\n",
691 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
692 address, flags);
693 break;
694 default:
695 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
696 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200697
698 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200699}
700
701static void iommu_poll_events(struct amd_iommu *iommu)
702{
703 u32 head, tail;
704 unsigned long flags;
705
Suravee Suthikulpanit925fe082013-03-27 18:51:52 -0500706 /* enable event interrupts again */
707 writel(MMIO_STATUS_EVT_INT_MASK, iommu->mmio_base + MMIO_STATUS_OFFSET);
708
Joerg Roedel90008ee2008-09-09 16:41:05 +0200709 spin_lock_irqsave(&iommu->lock, flags);
710
711 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
712 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
713
714 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200715 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200716 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
717 }
718
719 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
720
721 spin_unlock_irqrestore(&iommu->lock, flags);
722}
723
Joerg Roedeleee53532012-06-01 15:20:23 +0200724static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100725{
726 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100727
Joerg Roedel399be2f2011-12-01 16:53:47 +0100728 INC_STATS_COUNTER(pri_requests);
729
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100730 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
731 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
732 return;
733 }
734
735 fault.address = raw[1];
736 fault.pasid = PPR_PASID(raw[0]);
737 fault.device_id = PPR_DEVID(raw[0]);
738 fault.tag = PPR_TAG(raw[0]);
739 fault.flags = PPR_FLAGS(raw[0]);
740
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100741 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
742}
743
744static void iommu_poll_ppr_log(struct amd_iommu *iommu)
745{
746 unsigned long flags;
747 u32 head, tail;
748
749 if (iommu->ppr_log == NULL)
750 return;
751
Joerg Roedeleee53532012-06-01 15:20:23 +0200752 /* enable ppr interrupts again */
753 writel(MMIO_STATUS_PPR_INT_MASK, iommu->mmio_base + MMIO_STATUS_OFFSET);
754
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100755 spin_lock_irqsave(&iommu->lock, flags);
756
757 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
758 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
759
760 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200761 volatile u64 *raw;
762 u64 entry[2];
763 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100764
Joerg Roedeleee53532012-06-01 15:20:23 +0200765 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100766
Joerg Roedeleee53532012-06-01 15:20:23 +0200767 /*
768 * Hardware bug: Interrupt may arrive before the entry is
769 * written to memory. If this happens we need to wait for the
770 * entry to arrive.
771 */
772 for (i = 0; i < LOOP_TIMEOUT; ++i) {
773 if (PPR_REQ_TYPE(raw[0]) != 0)
774 break;
775 udelay(1);
776 }
777
778 /* Avoid memcpy function-call overhead */
779 entry[0] = raw[0];
780 entry[1] = raw[1];
781
782 /*
783 * To detect the hardware bug we need to clear the entry
784 * back to zero.
785 */
786 raw[0] = raw[1] = 0UL;
787
788 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100789 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
790 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200791
792 /*
793 * Release iommu->lock because ppr-handling might need to
Frank Arnolddf805ab2012-08-27 19:21:04 +0200794 * re-acquire it
Joerg Roedeleee53532012-06-01 15:20:23 +0200795 */
796 spin_unlock_irqrestore(&iommu->lock, flags);
797
798 /* Handle PPR entry */
799 iommu_handle_ppr_entry(iommu, entry);
800
801 spin_lock_irqsave(&iommu->lock, flags);
802
803 /* Refresh ring-buffer information */
804 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100805 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
806 }
807
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100808 spin_unlock_irqrestore(&iommu->lock, flags);
809}
810
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200811irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200812{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200813 struct amd_iommu *iommu;
814
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100815 for_each_iommu(iommu) {
Joerg Roedel90008ee2008-09-09 16:41:05 +0200816 iommu_poll_events(iommu);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100817 iommu_poll_ppr_log(iommu);
818 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200819
820 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200821}
822
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200823irqreturn_t amd_iommu_int_handler(int irq, void *data)
824{
825 return IRQ_WAKE_THREAD;
826}
827
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200828/****************************************************************************
829 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200830 * IOMMU command queuing functions
831 *
832 ****************************************************************************/
833
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200834static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200835{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200836 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200837
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200838 while (*sem == 0 && i < LOOP_TIMEOUT) {
839 udelay(1);
840 i += 1;
841 }
842
843 if (i == LOOP_TIMEOUT) {
844 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
845 return -EIO;
846 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200847
848 return 0;
849}
850
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200851static void copy_cmd_to_buffer(struct amd_iommu *iommu,
852 struct iommu_cmd *cmd,
853 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200854{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200855 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200856
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200857 target = iommu->cmd_buf + tail;
858 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200859
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200860 /* Copy command to buffer */
861 memcpy(target, cmd, sizeof(*cmd));
862
863 /* Tell the IOMMU about it */
864 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
865}
866
Joerg Roedel815b33f2011-04-06 17:26:49 +0200867static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200868{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200869 WARN_ON(address & 0x7ULL);
870
Joerg Roedelded46732011-04-06 10:53:48 +0200871 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200872 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
873 cmd->data[1] = upper_32_bits(__pa(address));
874 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200875 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
876}
877
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200878static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
879{
880 memset(cmd, 0, sizeof(*cmd));
881 cmd->data[0] = devid;
882 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
883}
884
Joerg Roedel11b64022011-04-06 11:49:28 +0200885static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
886 size_t size, u16 domid, int pde)
887{
888 u64 pages;
889 int s;
890
891 pages = iommu_num_pages(address, size, PAGE_SIZE);
892 s = 0;
893
894 if (pages > 1) {
895 /*
896 * If we have to flush more than one page, flush all
897 * TLB entries for this domain
898 */
899 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
900 s = 1;
901 }
902
903 address &= PAGE_MASK;
904
905 memset(cmd, 0, sizeof(*cmd));
906 cmd->data[1] |= domid;
907 cmd->data[2] = lower_32_bits(address);
908 cmd->data[3] = upper_32_bits(address);
909 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
910 if (s) /* size bit - we flush more than one 4kb page */
911 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200912 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200913 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
914}
915
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200916static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
917 u64 address, size_t size)
918{
919 u64 pages;
920 int s;
921
922 pages = iommu_num_pages(address, size, PAGE_SIZE);
923 s = 0;
924
925 if (pages > 1) {
926 /*
927 * If we have to flush more than one page, flush all
928 * TLB entries for this domain
929 */
930 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
931 s = 1;
932 }
933
934 address &= PAGE_MASK;
935
936 memset(cmd, 0, sizeof(*cmd));
937 cmd->data[0] = devid;
938 cmd->data[0] |= (qdep & 0xff) << 24;
939 cmd->data[1] = devid;
940 cmd->data[2] = lower_32_bits(address);
941 cmd->data[3] = upper_32_bits(address);
942 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
943 if (s)
944 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
945}
946
Joerg Roedel22e266c2011-11-21 15:59:08 +0100947static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
948 u64 address, bool size)
949{
950 memset(cmd, 0, sizeof(*cmd));
951
952 address &= ~(0xfffULL);
953
954 cmd->data[0] = pasid & PASID_MASK;
955 cmd->data[1] = domid;
956 cmd->data[2] = lower_32_bits(address);
957 cmd->data[3] = upper_32_bits(address);
958 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
959 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
960 if (size)
961 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
962 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
963}
964
965static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
966 int qdep, u64 address, bool size)
967{
968 memset(cmd, 0, sizeof(*cmd));
969
970 address &= ~(0xfffULL);
971
972 cmd->data[0] = devid;
973 cmd->data[0] |= (pasid & 0xff) << 16;
974 cmd->data[0] |= (qdep & 0xff) << 24;
975 cmd->data[1] = devid;
976 cmd->data[1] |= ((pasid >> 8) & 0xfff) << 16;
977 cmd->data[2] = lower_32_bits(address);
978 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
979 cmd->data[3] = upper_32_bits(address);
980 if (size)
981 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
982 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
983}
984
Joerg Roedelc99afa22011-11-21 18:19:25 +0100985static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
986 int status, int tag, bool gn)
987{
988 memset(cmd, 0, sizeof(*cmd));
989
990 cmd->data[0] = devid;
991 if (gn) {
992 cmd->data[1] = pasid & PASID_MASK;
993 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
994 }
995 cmd->data[3] = tag & 0x1ff;
996 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
997
998 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
999}
1000
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001001static void build_inv_all(struct iommu_cmd *cmd)
1002{
1003 memset(cmd, 0, sizeof(*cmd));
1004 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001005}
1006
Joerg Roedel7ef27982012-06-21 16:46:04 +02001007static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
1008{
1009 memset(cmd, 0, sizeof(*cmd));
1010 cmd->data[0] = devid;
1011 CMD_SET_TYPE(cmd, CMD_INV_IRT);
1012}
1013
Joerg Roedel431b2a22008-07-11 17:14:22 +02001014/*
Joerg Roedelb6c02712008-06-26 21:27:53 +02001015 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001016 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001017 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001018static int iommu_queue_command_sync(struct amd_iommu *iommu,
1019 struct iommu_cmd *cmd,
1020 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001021{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001022 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +02001023 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001024
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001025 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +01001026
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001027again:
Joerg Roedel815b33f2011-04-06 17:26:49 +02001028 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001029
1030 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
1031 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
1032 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
1033 left = (head - next_tail) % iommu->cmd_buf_size;
1034
1035 if (left <= 2) {
1036 struct iommu_cmd sync_cmd;
1037 volatile u64 sem = 0;
1038 int ret;
1039
1040 build_completion_wait(&sync_cmd, (u64)&sem);
1041 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
1042
1043 spin_unlock_irqrestore(&iommu->lock, flags);
1044
1045 if ((ret = wait_on_sem(&sem)) != 0)
1046 return ret;
1047
1048 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +02001049 }
1050
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001051 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +02001052
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001053 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001054 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001055
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001056 spin_unlock_irqrestore(&iommu->lock, flags);
1057
Joerg Roedel815b33f2011-04-06 17:26:49 +02001058 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001059}
1060
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001061static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
1062{
1063 return iommu_queue_command_sync(iommu, cmd, true);
1064}
1065
Joerg Roedel8d201962008-12-02 20:34:41 +01001066/*
1067 * This function queues a completion wait command into the command
1068 * buffer of an IOMMU
1069 */
Joerg Roedel8d201962008-12-02 20:34:41 +01001070static int iommu_completion_wait(struct amd_iommu *iommu)
1071{
Joerg Roedel815b33f2011-04-06 17:26:49 +02001072 struct iommu_cmd cmd;
1073 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001074 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001075
1076 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001077 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001078
Joerg Roedel815b33f2011-04-06 17:26:49 +02001079 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +01001080
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001081 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +01001082 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001083 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001084
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001085 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001086}
1087
Joerg Roedeld8c13082011-04-06 18:51:26 +02001088static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001089{
1090 struct iommu_cmd cmd;
1091
Joerg Roedeld8c13082011-04-06 18:51:26 +02001092 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001093
Joerg Roedeld8c13082011-04-06 18:51:26 +02001094 return iommu_queue_command(iommu, &cmd);
1095}
1096
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001097static void iommu_flush_dte_all(struct amd_iommu *iommu)
1098{
1099 u32 devid;
1100
1101 for (devid = 0; devid <= 0xffff; ++devid)
1102 iommu_flush_dte(iommu, devid);
1103
1104 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001105}
1106
1107/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001108 * This function uses heavy locking and may disable irqs for some time. But
1109 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001110 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001111static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001112{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001113 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001114
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001115 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1116 struct iommu_cmd cmd;
1117 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1118 dom_id, 1);
1119 iommu_queue_command(iommu, &cmd);
1120 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001121
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001122 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001123}
1124
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001125static void iommu_flush_all(struct amd_iommu *iommu)
1126{
1127 struct iommu_cmd cmd;
1128
1129 build_inv_all(&cmd);
1130
1131 iommu_queue_command(iommu, &cmd);
1132 iommu_completion_wait(iommu);
1133}
1134
Joerg Roedel7ef27982012-06-21 16:46:04 +02001135static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1136{
1137 struct iommu_cmd cmd;
1138
1139 build_inv_irt(&cmd, devid);
1140
1141 iommu_queue_command(iommu, &cmd);
1142}
1143
1144static void iommu_flush_irt_all(struct amd_iommu *iommu)
1145{
1146 u32 devid;
1147
1148 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1149 iommu_flush_irt(iommu, devid);
1150
1151 iommu_completion_wait(iommu);
1152}
1153
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001154void iommu_flush_all_caches(struct amd_iommu *iommu)
1155{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001156 if (iommu_feature(iommu, FEATURE_IA)) {
1157 iommu_flush_all(iommu);
1158 } else {
1159 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001160 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001161 iommu_flush_tlb_all(iommu);
1162 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001163}
1164
Joerg Roedel431b2a22008-07-11 17:14:22 +02001165/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001166 * Command send function for flushing on-device TLB
1167 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001168static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1169 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001170{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001171 struct amd_iommu *iommu;
1172 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001173 int qdep;
1174
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001175 qdep = dev_data->ats.qdep;
1176 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001177
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001178 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001179
1180 return iommu_queue_command(iommu, &cmd);
1181}
1182
1183/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001184 * Command send function for invalidating a device table entry
1185 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001186static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001187{
1188 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001189 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001190
Joerg Roedel6c542042011-06-09 17:07:31 +02001191 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001192
Joerg Roedelf62dda62011-06-09 12:55:35 +02001193 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001194 if (ret)
1195 return ret;
1196
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001197 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001198 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001199
1200 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001201}
1202
Joerg Roedel431b2a22008-07-11 17:14:22 +02001203/*
1204 * TLB invalidation function which is called from the mapping functions.
1205 * It invalidates a single PTE if the range to flush is within a single
1206 * page. Otherwise it flushes the whole TLB of the IOMMU.
1207 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001208static void __domain_flush_pages(struct protection_domain *domain,
1209 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001210{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001211 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001212 struct iommu_cmd cmd;
1213 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001214
Joerg Roedel11b64022011-04-06 11:49:28 +02001215 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001216
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001217 for (i = 0; i < amd_iommus_present; ++i) {
1218 if (!domain->dev_iommu[i])
1219 continue;
1220
1221 /*
1222 * Devices of this domain are behind this IOMMU
1223 * We need a TLB flush
1224 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001225 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001226 }
1227
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001228 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001229
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001230 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001231 continue;
1232
Joerg Roedel6c542042011-06-09 17:07:31 +02001233 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001234 }
1235
Joerg Roedel11b64022011-04-06 11:49:28 +02001236 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001237}
1238
Joerg Roedel17b124b2011-04-06 18:01:35 +02001239static void domain_flush_pages(struct protection_domain *domain,
1240 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001241{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001242 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001243}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001244
Joerg Roedel1c655772008-09-04 18:40:05 +02001245/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001246static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001247{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001248 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001249}
1250
Chris Wright42a49f92009-06-15 15:42:00 +02001251/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001252static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001253{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001254 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1255}
1256
1257static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001258{
1259 int i;
1260
1261 for (i = 0; i < amd_iommus_present; ++i) {
1262 if (!domain->dev_iommu[i])
1263 continue;
1264
1265 /*
1266 * Devices of this domain are behind this IOMMU
1267 * We need to wait for completion of all commands.
1268 */
1269 iommu_completion_wait(amd_iommus[i]);
1270 }
1271}
1272
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001273
Joerg Roedel43f49602008-12-02 21:01:12 +01001274/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001275 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001276 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001277static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001278{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001279 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001280
1281 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001282 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001283}
1284
Joerg Roedel431b2a22008-07-11 17:14:22 +02001285/****************************************************************************
1286 *
1287 * The functions below are used the create the page table mappings for
1288 * unity mapped regions.
1289 *
1290 ****************************************************************************/
1291
1292/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001293 * This function is used to add another level to an IO page table. Adding
1294 * another level increases the size of the address space by 9 bits to a size up
1295 * to 64 bits.
1296 */
1297static bool increase_address_space(struct protection_domain *domain,
1298 gfp_t gfp)
1299{
1300 u64 *pte;
1301
1302 if (domain->mode == PAGE_MODE_6_LEVEL)
1303 /* address space already 64 bit large */
1304 return false;
1305
1306 pte = (void *)get_zeroed_page(gfp);
1307 if (!pte)
1308 return false;
1309
1310 *pte = PM_LEVEL_PDE(domain->mode,
1311 virt_to_phys(domain->pt_root));
1312 domain->pt_root = pte;
1313 domain->mode += 1;
1314 domain->updated = true;
1315
1316 return true;
1317}
1318
1319static u64 *alloc_pte(struct protection_domain *domain,
1320 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001321 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001322 u64 **pte_page,
1323 gfp_t gfp)
1324{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001325 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001326 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001327
1328 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001329
1330 while (address > PM_LEVEL_SIZE(domain->mode))
1331 increase_address_space(domain, gfp);
1332
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001333 level = domain->mode - 1;
1334 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1335 address = PAGE_SIZE_ALIGN(address, page_size);
1336 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001337
1338 while (level > end_lvl) {
1339 if (!IOMMU_PTE_PRESENT(*pte)) {
1340 page = (u64 *)get_zeroed_page(gfp);
1341 if (!page)
1342 return NULL;
1343 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1344 }
1345
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001346 /* No level skipping support yet */
1347 if (PM_PTE_LEVEL(*pte) != level)
1348 return NULL;
1349
Joerg Roedel308973d2009-11-24 17:43:32 +01001350 level -= 1;
1351
1352 pte = IOMMU_PTE_PAGE(*pte);
1353
1354 if (pte_page && level == end_lvl)
1355 *pte_page = pte;
1356
1357 pte = &pte[PM_LEVEL_INDEX(level, address)];
1358 }
1359
1360 return pte;
1361}
1362
1363/*
1364 * This function checks if there is a PTE for a given dma address. If
1365 * there is one, it returns the pointer to it.
1366 */
Joerg Roedel24cd7722010-01-19 17:27:39 +01001367static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +01001368{
1369 int level;
1370 u64 *pte;
1371
Joerg Roedel24cd7722010-01-19 17:27:39 +01001372 if (address > PM_LEVEL_SIZE(domain->mode))
1373 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001374
Joerg Roedel24cd7722010-01-19 17:27:39 +01001375 level = domain->mode - 1;
1376 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1377
1378 while (level > 0) {
1379
1380 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001381 if (!IOMMU_PTE_PRESENT(*pte))
1382 return NULL;
1383
Joerg Roedel24cd7722010-01-19 17:27:39 +01001384 /* Large PTE */
1385 if (PM_PTE_LEVEL(*pte) == 0x07) {
1386 unsigned long pte_mask, __pte;
1387
1388 /*
1389 * If we have a series of large PTEs, make
1390 * sure to return a pointer to the first one.
1391 */
1392 pte_mask = PTE_PAGE_SIZE(*pte);
1393 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1394 __pte = ((unsigned long)pte) & pte_mask;
1395
1396 return (u64 *)__pte;
1397 }
1398
1399 /* No level skipping support yet */
1400 if (PM_PTE_LEVEL(*pte) != level)
1401 return NULL;
1402
Joerg Roedel308973d2009-11-24 17:43:32 +01001403 level -= 1;
1404
Joerg Roedel24cd7722010-01-19 17:27:39 +01001405 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +01001406 pte = IOMMU_PTE_PAGE(*pte);
1407 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +01001408 }
1409
1410 return pte;
1411}
1412
1413/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001414 * Generic mapping functions. It maps a physical address into a DMA
1415 * address space. It allocates the page table pages if necessary.
1416 * In the future it can be extended to a generic mapping function
1417 * supporting all features of AMD IOMMU page tables like level skipping
1418 * and full 64 bit address spaces.
1419 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001420static int iommu_map_page(struct protection_domain *dom,
1421 unsigned long bus_addr,
1422 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001423 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001424 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001425{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001426 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001427 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001428
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001429 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001430 return -EINVAL;
1431
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001432 bus_addr = PAGE_ALIGN(bus_addr);
1433 phys_addr = PAGE_ALIGN(phys_addr);
1434 count = PAGE_SIZE_PTE_COUNT(page_size);
1435 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001436
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001437 for (i = 0; i < count; ++i)
1438 if (IOMMU_PTE_PRESENT(pte[i]))
1439 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001440
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001441 if (page_size > PAGE_SIZE) {
1442 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1443 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1444 } else
1445 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1446
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001447 if (prot & IOMMU_PROT_IR)
1448 __pte |= IOMMU_PTE_IR;
1449 if (prot & IOMMU_PROT_IW)
1450 __pte |= IOMMU_PTE_IW;
1451
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001452 for (i = 0; i < count; ++i)
1453 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001454
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001455 update_domain(dom);
1456
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001457 return 0;
1458}
1459
Joerg Roedel24cd7722010-01-19 17:27:39 +01001460static unsigned long iommu_unmap_page(struct protection_domain *dom,
1461 unsigned long bus_addr,
1462 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001463{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001464 unsigned long long unmap_size, unmapped;
1465 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001466
Joerg Roedel24cd7722010-01-19 17:27:39 +01001467 BUG_ON(!is_power_of_2(page_size));
1468
1469 unmapped = 0;
1470
1471 while (unmapped < page_size) {
1472
1473 pte = fetch_pte(dom, bus_addr);
1474
1475 if (!pte) {
1476 /*
1477 * No PTE for this address
1478 * move forward in 4kb steps
1479 */
1480 unmap_size = PAGE_SIZE;
1481 } else if (PM_PTE_LEVEL(*pte) == 0) {
1482 /* 4kb PTE found for this address */
1483 unmap_size = PAGE_SIZE;
1484 *pte = 0ULL;
1485 } else {
1486 int count, i;
1487
1488 /* Large PTE found which maps this address */
1489 unmap_size = PTE_PAGE_SIZE(*pte);
1490 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1491 for (i = 0; i < count; i++)
1492 pte[i] = 0ULL;
1493 }
1494
1495 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1496 unmapped += unmap_size;
1497 }
1498
1499 BUG_ON(!is_power_of_2(unmapped));
1500
1501 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001502}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001503
Joerg Roedel431b2a22008-07-11 17:14:22 +02001504/*
1505 * This function checks if a specific unity mapping entry is needed for
1506 * this specific IOMMU.
1507 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001508static int iommu_for_unity_map(struct amd_iommu *iommu,
1509 struct unity_map_entry *entry)
1510{
1511 u16 bdf, i;
1512
1513 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1514 bdf = amd_iommu_alias_table[i];
1515 if (amd_iommu_rlookup_table[bdf] == iommu)
1516 return 1;
1517 }
1518
1519 return 0;
1520}
1521
Joerg Roedel431b2a22008-07-11 17:14:22 +02001522/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001523 * This function actually applies the mapping to the page table of the
1524 * dma_ops domain.
1525 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001526static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1527 struct unity_map_entry *e)
1528{
1529 u64 addr;
1530 int ret;
1531
1532 for (addr = e->address_start; addr < e->address_end;
1533 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001534 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001535 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001536 if (ret)
1537 return ret;
1538 /*
1539 * if unity mapping is in aperture range mark the page
1540 * as allocated in the aperture
1541 */
1542 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001543 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001544 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001545 }
1546
1547 return 0;
1548}
1549
Joerg Roedel431b2a22008-07-11 17:14:22 +02001550/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001551 * Init the unity mappings for a specific IOMMU in the system
1552 *
1553 * Basically iterates over all unity mapping entries and applies them to
1554 * the default domain DMA of that IOMMU if necessary.
1555 */
1556static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1557{
1558 struct unity_map_entry *entry;
1559 int ret;
1560
1561 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1562 if (!iommu_for_unity_map(iommu, entry))
1563 continue;
1564 ret = dma_ops_unity_map(iommu->default_dom, entry);
1565 if (ret)
1566 return ret;
1567 }
1568
1569 return 0;
1570}
1571
1572/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001573 * Inits the unity mappings required for a specific device
1574 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001575static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1576 u16 devid)
1577{
1578 struct unity_map_entry *e;
1579 int ret;
1580
1581 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1582 if (!(devid >= e->devid_start && devid <= e->devid_end))
1583 continue;
1584 ret = dma_ops_unity_map(dma_dom, e);
1585 if (ret)
1586 return ret;
1587 }
1588
1589 return 0;
1590}
1591
Joerg Roedel431b2a22008-07-11 17:14:22 +02001592/****************************************************************************
1593 *
1594 * The next functions belong to the address allocator for the dma_ops
1595 * interface functions. They work like the allocators in the other IOMMU
1596 * drivers. Its basically a bitmap which marks the allocated pages in
1597 * the aperture. Maybe it could be enhanced in the future to a more
1598 * efficient allocator.
1599 *
1600 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001601
Joerg Roedel431b2a22008-07-11 17:14:22 +02001602/*
Joerg Roedel384de722009-05-15 12:30:05 +02001603 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001604 *
1605 * called with domain->lock held
1606 */
Joerg Roedel384de722009-05-15 12:30:05 +02001607
Joerg Roedel9cabe892009-05-18 16:38:55 +02001608/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001609 * Used to reserve address ranges in the aperture (e.g. for exclusion
1610 * ranges.
1611 */
1612static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1613 unsigned long start_page,
1614 unsigned int pages)
1615{
1616 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1617
1618 if (start_page + pages > last_page)
1619 pages = last_page - start_page;
1620
1621 for (i = start_page; i < start_page + pages; ++i) {
1622 int index = i / APERTURE_RANGE_PAGES;
1623 int page = i % APERTURE_RANGE_PAGES;
1624 __set_bit(page, dom->aperture[index]->bitmap);
1625 }
1626}
1627
1628/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001629 * This function is used to add a new aperture range to an existing
1630 * aperture in case of dma_ops domain allocation or address allocation
1631 * failure.
1632 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001633static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001634 bool populate, gfp_t gfp)
1635{
1636 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001637 struct amd_iommu *iommu;
Joerg Roedel17f5b562011-07-06 17:14:44 +02001638 unsigned long i, old_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001639
Joerg Roedelf5e97052009-05-22 12:31:53 +02001640#ifdef CONFIG_IOMMU_STRESS
1641 populate = false;
1642#endif
1643
Joerg Roedel9cabe892009-05-18 16:38:55 +02001644 if (index >= APERTURE_MAX_RANGES)
1645 return -ENOMEM;
1646
1647 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1648 if (!dma_dom->aperture[index])
1649 return -ENOMEM;
1650
1651 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1652 if (!dma_dom->aperture[index]->bitmap)
1653 goto out_free;
1654
1655 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1656
1657 if (populate) {
1658 unsigned long address = dma_dom->aperture_size;
1659 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1660 u64 *pte, *pte_page;
1661
1662 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001663 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001664 &pte_page, gfp);
1665 if (!pte)
1666 goto out_free;
1667
1668 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1669
1670 address += APERTURE_RANGE_SIZE / 64;
1671 }
1672 }
1673
Joerg Roedel17f5b562011-07-06 17:14:44 +02001674 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001675 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1676
Joerg Roedel17f5b562011-07-06 17:14:44 +02001677 /* Reserve address range used for MSI messages */
1678 if (old_size < MSI_ADDR_BASE_LO &&
1679 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1680 unsigned long spage;
1681 int pages;
1682
1683 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1684 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1685
1686 dma_ops_reserve_addresses(dma_dom, spage, pages);
1687 }
1688
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001689 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001690 for_each_iommu(iommu) {
1691 if (iommu->exclusion_start &&
1692 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1693 && iommu->exclusion_start < dma_dom->aperture_size) {
1694 unsigned long startpage;
1695 int pages = iommu_num_pages(iommu->exclusion_start,
1696 iommu->exclusion_length,
1697 PAGE_SIZE);
1698 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1699 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1700 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001701 }
1702
1703 /*
1704 * Check for areas already mapped as present in the new aperture
1705 * range and mark those pages as reserved in the allocator. Such
1706 * mappings may already exist as a result of requested unity
1707 * mappings for devices.
1708 */
1709 for (i = dma_dom->aperture[index]->offset;
1710 i < dma_dom->aperture_size;
1711 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001712 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001713 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1714 continue;
1715
Joerg Roedelfcd08612011-10-11 17:41:32 +02001716 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001717 }
1718
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001719 update_domain(&dma_dom->domain);
1720
Joerg Roedel9cabe892009-05-18 16:38:55 +02001721 return 0;
1722
1723out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001724 update_domain(&dma_dom->domain);
1725
Joerg Roedel9cabe892009-05-18 16:38:55 +02001726 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1727
1728 kfree(dma_dom->aperture[index]);
1729 dma_dom->aperture[index] = NULL;
1730
1731 return -ENOMEM;
1732}
1733
Joerg Roedel384de722009-05-15 12:30:05 +02001734static unsigned long dma_ops_area_alloc(struct device *dev,
1735 struct dma_ops_domain *dom,
1736 unsigned int pages,
1737 unsigned long align_mask,
1738 u64 dma_mask,
1739 unsigned long start)
1740{
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001741 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001742 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1743 int i = start >> APERTURE_RANGE_SHIFT;
1744 unsigned long boundary_size;
1745 unsigned long address = -1;
1746 unsigned long limit;
1747
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001748 next_bit >>= PAGE_SHIFT;
1749
Joerg Roedel384de722009-05-15 12:30:05 +02001750 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1751 PAGE_SIZE) >> PAGE_SHIFT;
1752
1753 for (;i < max_index; ++i) {
1754 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1755
1756 if (dom->aperture[i]->offset >= dma_mask)
1757 break;
1758
1759 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1760 dma_mask >> PAGE_SHIFT);
1761
1762 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1763 limit, next_bit, pages, 0,
1764 boundary_size, align_mask);
1765 if (address != -1) {
1766 address = dom->aperture[i]->offset +
1767 (address << PAGE_SHIFT);
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001768 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001769 break;
1770 }
1771
1772 next_bit = 0;
1773 }
1774
1775 return address;
1776}
1777
Joerg Roedeld3086442008-06-26 21:27:57 +02001778static unsigned long dma_ops_alloc_addresses(struct device *dev,
1779 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001780 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001781 unsigned long align_mask,
1782 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001783{
Joerg Roedeld3086442008-06-26 21:27:57 +02001784 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001785
Joerg Roedelfe16f082009-05-22 12:27:53 +02001786#ifdef CONFIG_IOMMU_STRESS
1787 dom->next_address = 0;
1788 dom->need_flush = true;
1789#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001790
Joerg Roedel384de722009-05-15 12:30:05 +02001791 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001792 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001793
Joerg Roedel1c655772008-09-04 18:40:05 +02001794 if (address == -1) {
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001795 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001796 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1797 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001798 dom->need_flush = true;
1799 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001800
Joerg Roedel384de722009-05-15 12:30:05 +02001801 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001802 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001803
1804 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1805
1806 return address;
1807}
1808
Joerg Roedel431b2a22008-07-11 17:14:22 +02001809/*
1810 * The address free function.
1811 *
1812 * called with domain->lock held
1813 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001814static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1815 unsigned long address,
1816 unsigned int pages)
1817{
Joerg Roedel384de722009-05-15 12:30:05 +02001818 unsigned i = address >> APERTURE_RANGE_SHIFT;
1819 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001820
Joerg Roedel384de722009-05-15 12:30:05 +02001821 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1822
Joerg Roedel47bccd62009-05-22 12:40:54 +02001823#ifdef CONFIG_IOMMU_STRESS
1824 if (i < 4)
1825 return;
1826#endif
1827
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001828 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001829 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001830
1831 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001832
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001833 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001834
Joerg Roedeld3086442008-06-26 21:27:57 +02001835}
1836
Joerg Roedel431b2a22008-07-11 17:14:22 +02001837/****************************************************************************
1838 *
1839 * The next functions belong to the domain allocation. A domain is
1840 * allocated for every IOMMU as the default domain. If device isolation
1841 * is enabled, every device get its own domain. The most important thing
1842 * about domains is the page table mapping the DMA address space they
1843 * contain.
1844 *
1845 ****************************************************************************/
1846
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001847/*
1848 * This function adds a protection domain to the global protection domain list
1849 */
1850static void add_domain_to_list(struct protection_domain *domain)
1851{
1852 unsigned long flags;
1853
1854 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1855 list_add(&domain->list, &amd_iommu_pd_list);
1856 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1857}
1858
1859/*
1860 * This function removes a protection domain to the global
1861 * protection domain list
1862 */
1863static void del_domain_from_list(struct protection_domain *domain)
1864{
1865 unsigned long flags;
1866
1867 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1868 list_del(&domain->list);
1869 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1870}
1871
Joerg Roedelec487d12008-06-26 21:27:58 +02001872static u16 domain_id_alloc(void)
1873{
1874 unsigned long flags;
1875 int id;
1876
1877 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1878 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1879 BUG_ON(id == 0);
1880 if (id > 0 && id < MAX_DOMAIN_ID)
1881 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1882 else
1883 id = 0;
1884 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1885
1886 return id;
1887}
1888
Joerg Roedela2acfb72008-12-02 18:28:53 +01001889static void domain_id_free(int id)
1890{
1891 unsigned long flags;
1892
1893 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1894 if (id > 0 && id < MAX_DOMAIN_ID)
1895 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1896 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1897}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001898
Joerg Roedel86db2e52008-12-02 18:20:21 +01001899static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001900{
1901 int i, j;
1902 u64 *p1, *p2, *p3;
1903
Joerg Roedel86db2e52008-12-02 18:20:21 +01001904 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001905
1906 if (!p1)
1907 return;
1908
1909 for (i = 0; i < 512; ++i) {
1910 if (!IOMMU_PTE_PRESENT(p1[i]))
1911 continue;
1912
1913 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001914 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001915 if (!IOMMU_PTE_PRESENT(p2[j]))
1916 continue;
1917 p3 = IOMMU_PTE_PAGE(p2[j]);
1918 free_page((unsigned long)p3);
1919 }
1920
1921 free_page((unsigned long)p2);
1922 }
1923
1924 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001925
1926 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001927}
1928
Joerg Roedelb16137b2011-11-21 16:50:23 +01001929static void free_gcr3_tbl_level1(u64 *tbl)
1930{
1931 u64 *ptr;
1932 int i;
1933
1934 for (i = 0; i < 512; ++i) {
1935 if (!(tbl[i] & GCR3_VALID))
1936 continue;
1937
1938 ptr = __va(tbl[i] & PAGE_MASK);
1939
1940 free_page((unsigned long)ptr);
1941 }
1942}
1943
1944static void free_gcr3_tbl_level2(u64 *tbl)
1945{
1946 u64 *ptr;
1947 int i;
1948
1949 for (i = 0; i < 512; ++i) {
1950 if (!(tbl[i] & GCR3_VALID))
1951 continue;
1952
1953 ptr = __va(tbl[i] & PAGE_MASK);
1954
1955 free_gcr3_tbl_level1(ptr);
1956 }
1957}
1958
Joerg Roedel52815b72011-11-17 17:24:28 +01001959static void free_gcr3_table(struct protection_domain *domain)
1960{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001961 if (domain->glx == 2)
1962 free_gcr3_tbl_level2(domain->gcr3_tbl);
1963 else if (domain->glx == 1)
1964 free_gcr3_tbl_level1(domain->gcr3_tbl);
1965 else if (domain->glx != 0)
1966 BUG();
1967
Joerg Roedel52815b72011-11-17 17:24:28 +01001968 free_page((unsigned long)domain->gcr3_tbl);
1969}
1970
Joerg Roedel431b2a22008-07-11 17:14:22 +02001971/*
1972 * Free a domain, only used if something went wrong in the
1973 * allocation path and we need to free an already allocated page table
1974 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001975static void dma_ops_domain_free(struct dma_ops_domain *dom)
1976{
Joerg Roedel384de722009-05-15 12:30:05 +02001977 int i;
1978
Joerg Roedelec487d12008-06-26 21:27:58 +02001979 if (!dom)
1980 return;
1981
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001982 del_domain_from_list(&dom->domain);
1983
Joerg Roedel86db2e52008-12-02 18:20:21 +01001984 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001985
Joerg Roedel384de722009-05-15 12:30:05 +02001986 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1987 if (!dom->aperture[i])
1988 continue;
1989 free_page((unsigned long)dom->aperture[i]->bitmap);
1990 kfree(dom->aperture[i]);
1991 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001992
1993 kfree(dom);
1994}
1995
Joerg Roedel431b2a22008-07-11 17:14:22 +02001996/*
1997 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001998 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001999 * structures required for the dma_ops interface
2000 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01002001static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02002002{
2003 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002004
2005 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
2006 if (!dma_dom)
2007 return NULL;
2008
2009 spin_lock_init(&dma_dom->domain.lock);
2010
2011 dma_dom->domain.id = domain_id_alloc();
2012 if (dma_dom->domain.id == 0)
2013 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002014 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02002015 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02002016 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01002017 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02002018 dma_dom->domain.priv = dma_dom;
2019 if (!dma_dom->domain.pt_root)
2020 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002021
Joerg Roedel1c655772008-09-04 18:40:05 +02002022 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02002023 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02002024
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002025 add_domain_to_list(&dma_dom->domain);
2026
Joerg Roedel576175c2009-11-23 19:08:46 +01002027 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02002028 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002029
Joerg Roedel431b2a22008-07-11 17:14:22 +02002030 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02002031 * mark the first page as allocated so we never return 0 as
2032 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02002033 */
Joerg Roedel384de722009-05-15 12:30:05 +02002034 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02002035 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02002036
Joerg Roedelec487d12008-06-26 21:27:58 +02002037
2038 return dma_dom;
2039
2040free_dma_dom:
2041 dma_ops_domain_free(dma_dom);
2042
2043 return NULL;
2044}
2045
Joerg Roedel431b2a22008-07-11 17:14:22 +02002046/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01002047 * little helper function to check whether a given protection domain is a
2048 * dma_ops domain
2049 */
2050static bool dma_ops_domain(struct protection_domain *domain)
2051{
2052 return domain->flags & PD_DMA_OPS_MASK;
2053}
2054
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002055static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002056{
Joerg Roedel132bd682011-11-17 14:18:46 +01002057 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01002058 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01002059
Joerg Roedel132bd682011-11-17 14:18:46 +01002060 if (domain->mode != PAGE_MODE_NONE)
2061 pte_root = virt_to_phys(domain->pt_root);
2062
Joerg Roedel38ddf412008-09-11 10:38:32 +02002063 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
2064 << DEV_ENTRY_MODE_SHIFT;
2065 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002066
Joerg Roedelee6c2862011-11-09 12:06:03 +01002067 flags = amd_iommu_dev_table[devid].data[1];
2068
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002069 if (ats)
2070 flags |= DTE_FLAG_IOTLB;
2071
Joerg Roedel52815b72011-11-17 17:24:28 +01002072 if (domain->flags & PD_IOMMUV2_MASK) {
2073 u64 gcr3 = __pa(domain->gcr3_tbl);
2074 u64 glx = domain->glx;
2075 u64 tmp;
2076
2077 pte_root |= DTE_FLAG_GV;
2078 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
2079
2080 /* First mask out possible old values for GCR3 table */
2081 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
2082 flags &= ~tmp;
2083
2084 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
2085 flags &= ~tmp;
2086
2087 /* Encode GCR3 table into DTE */
2088 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
2089 pte_root |= tmp;
2090
2091 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
2092 flags |= tmp;
2093
2094 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
2095 flags |= tmp;
2096 }
2097
Joerg Roedelee6c2862011-11-09 12:06:03 +01002098 flags &= ~(0xffffUL);
2099 flags |= domain->id;
2100
2101 amd_iommu_dev_table[devid].data[1] = flags;
2102 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002103}
2104
Joerg Roedel15898bb2009-11-24 15:39:42 +01002105static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01002106{
Joerg Roedel355bf552008-12-08 12:02:41 +01002107 /* remove entry from the device table seen by the hardware */
2108 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
2109 amd_iommu_dev_table[devid].data[1] = 0;
Joerg Roedel355bf552008-12-08 12:02:41 +01002110
Joerg Roedelc5cca142009-10-09 18:31:20 +02002111 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002112}
2113
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002114static void do_attach(struct iommu_dev_data *dev_data,
2115 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002116{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002117 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002118 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002119
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002120 iommu = amd_iommu_rlookup_table[dev_data->devid];
2121 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002122
2123 /* Update data structures */
2124 dev_data->domain = domain;
2125 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002126 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002127
2128 /* Do reference counting */
2129 domain->dev_iommu[iommu->index] += 1;
2130 domain->dev_cnt += 1;
2131
2132 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002133 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002134}
2135
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002136static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002137{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002138 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002139
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002140 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02002141
Joerg Roedelc4596112009-11-20 14:57:32 +01002142 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002143 dev_data->domain->dev_iommu[iommu->index] -= 1;
2144 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01002145
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002146 /* Update data structures */
2147 dev_data->domain = NULL;
2148 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002149 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002150
2151 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002152 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002153}
2154
2155/*
2156 * If a device is not yet associated with a domain, this function does
2157 * assigns it visible for the hardware
2158 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002159static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002160 struct protection_domain *domain)
2161{
Julia Lawall84fe6c12010-05-27 12:31:51 +02002162 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002163
Joerg Roedel15898bb2009-11-24 15:39:42 +01002164 /* lock domain */
2165 spin_lock(&domain->lock);
2166
Joerg Roedel71f77582011-06-09 19:03:15 +02002167 if (dev_data->alias_data != NULL) {
2168 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002169
Joerg Roedel2b02b092011-06-09 17:48:39 +02002170 /* Some sanity checks */
2171 ret = -EBUSY;
2172 if (alias_data->domain != NULL &&
2173 alias_data->domain != domain)
2174 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002175
Joerg Roedel2b02b092011-06-09 17:48:39 +02002176 if (dev_data->domain != NULL &&
2177 dev_data->domain != domain)
2178 goto out_unlock;
2179
2180 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002181 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002182 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002183
2184 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01002185 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01002186
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002187 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002188 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002189
Joerg Roedel24100052009-11-25 15:59:57 +01002190 atomic_inc(&dev_data->bind);
2191
Julia Lawall84fe6c12010-05-27 12:31:51 +02002192 ret = 0;
2193
2194out_unlock:
2195
Joerg Roedel355bf552008-12-08 12:02:41 +01002196 /* ready */
2197 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002198
Julia Lawall84fe6c12010-05-27 12:31:51 +02002199 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002200}
2201
Joerg Roedel52815b72011-11-17 17:24:28 +01002202
2203static void pdev_iommuv2_disable(struct pci_dev *pdev)
2204{
2205 pci_disable_ats(pdev);
2206 pci_disable_pri(pdev);
2207 pci_disable_pasid(pdev);
2208}
2209
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002210/* FIXME: Change generic reset-function to do the same */
2211static int pri_reset_while_enabled(struct pci_dev *pdev)
2212{
2213 u16 control;
2214 int pos;
2215
Joerg Roedel46277b72011-12-07 14:34:02 +01002216 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002217 if (!pos)
2218 return -EINVAL;
2219
Joerg Roedel46277b72011-12-07 14:34:02 +01002220 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2221 control |= PCI_PRI_CTRL_RESET;
2222 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002223
2224 return 0;
2225}
2226
Joerg Roedel52815b72011-11-17 17:24:28 +01002227static int pdev_iommuv2_enable(struct pci_dev *pdev)
2228{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002229 bool reset_enable;
2230 int reqs, ret;
2231
2232 /* FIXME: Hardcode number of outstanding requests for now */
2233 reqs = 32;
2234 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2235 reqs = 1;
2236 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002237
2238 /* Only allow access to user-accessible pages */
2239 ret = pci_enable_pasid(pdev, 0);
2240 if (ret)
2241 goto out_err;
2242
2243 /* First reset the PRI state of the device */
2244 ret = pci_reset_pri(pdev);
2245 if (ret)
2246 goto out_err;
2247
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002248 /* Enable PRI */
2249 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002250 if (ret)
2251 goto out_err;
2252
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002253 if (reset_enable) {
2254 ret = pri_reset_while_enabled(pdev);
2255 if (ret)
2256 goto out_err;
2257 }
2258
Joerg Roedel52815b72011-11-17 17:24:28 +01002259 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2260 if (ret)
2261 goto out_err;
2262
2263 return 0;
2264
2265out_err:
2266 pci_disable_pri(pdev);
2267 pci_disable_pasid(pdev);
2268
2269 return ret;
2270}
2271
Joerg Roedelc99afa22011-11-21 18:19:25 +01002272/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002273#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002274
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002275static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002276{
Joerg Roedela3b93122012-04-12 12:49:26 +02002277 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002278 int pos;
2279
Joerg Roedel46277b72011-12-07 14:34:02 +01002280 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002281 if (!pos)
2282 return false;
2283
Joerg Roedela3b93122012-04-12 12:49:26 +02002284 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002285
Joerg Roedela3b93122012-04-12 12:49:26 +02002286 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002287}
2288
Joerg Roedel15898bb2009-11-24 15:39:42 +01002289/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02002290 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01002291 * assigns it visible for the hardware
2292 */
2293static int attach_device(struct device *dev,
2294 struct protection_domain *domain)
2295{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002296 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002297 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002298 unsigned long flags;
2299 int ret;
2300
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002301 dev_data = get_dev_data(dev);
2302
Joerg Roedel52815b72011-11-17 17:24:28 +01002303 if (domain->flags & PD_IOMMUV2_MASK) {
2304 if (!dev_data->iommu_v2 || !dev_data->passthrough)
2305 return -EINVAL;
2306
2307 if (pdev_iommuv2_enable(pdev) != 0)
2308 return -EINVAL;
2309
2310 dev_data->ats.enabled = true;
2311 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002312 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002313 } else if (amd_iommu_iotlb_sup &&
2314 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002315 dev_data->ats.enabled = true;
2316 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2317 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002318
Joerg Roedel15898bb2009-11-24 15:39:42 +01002319 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002320 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002321 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2322
2323 /*
2324 * We might boot into a crash-kernel here. The crashed kernel
2325 * left the caches in the IOMMU dirty. So we have to flush
2326 * here to evict all dirty stuff.
2327 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002328 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002329
2330 return ret;
2331}
2332
2333/*
2334 * Removes a device from a protection domain (unlocked)
2335 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002336static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002337{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002338 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002339 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002340
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002341 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002342
Joerg Roedel2ca76272010-01-22 16:45:31 +01002343 domain = dev_data->domain;
2344
2345 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01002346
Joerg Roedel71f77582011-06-09 19:03:15 +02002347 if (dev_data->alias_data != NULL) {
2348 struct iommu_dev_data *alias_data = dev_data->alias_data;
2349
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002350 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002351 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01002352 }
2353
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002354 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002355 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002356
Joerg Roedel2ca76272010-01-22 16:45:31 +01002357 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002358
Joerg Roedel21129f72009-09-01 11:59:42 +02002359 /*
2360 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002361 * passthrough domain if it is detached from any other domain.
2362 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02002363 */
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002364 if (dev_data->passthrough &&
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002365 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002366 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01002367}
2368
2369/*
2370 * Removes a device from a protection domain (with devtable_lock held)
2371 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002372static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002373{
Joerg Roedel52815b72011-11-17 17:24:28 +01002374 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002375 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002376 unsigned long flags;
2377
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002378 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002379 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002380
Joerg Roedel355bf552008-12-08 12:02:41 +01002381 /* lock device table */
2382 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002383 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002384 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002385
Joerg Roedel52815b72011-11-17 17:24:28 +01002386 if (domain->flags & PD_IOMMUV2_MASK)
2387 pdev_iommuv2_disable(to_pci_dev(dev));
2388 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002389 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002390
2391 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002392}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002393
Joerg Roedel15898bb2009-11-24 15:39:42 +01002394/*
2395 * Find out the protection domain structure for a given PCI device. This
2396 * will give us the pointer to the page table root for example.
2397 */
2398static struct protection_domain *domain_for_device(struct device *dev)
2399{
Joerg Roedel71f77582011-06-09 19:03:15 +02002400 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002401 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002402 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002403
Joerg Roedel657cbb62009-11-23 15:26:46 +01002404 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002405
Joerg Roedel2b02b092011-06-09 17:48:39 +02002406 if (dev_data->domain)
2407 return dev_data->domain;
2408
Joerg Roedel71f77582011-06-09 19:03:15 +02002409 if (dev_data->alias_data != NULL) {
2410 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002411
2412 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
2413 if (alias_data->domain != NULL) {
2414 __attach_device(dev_data, alias_data->domain);
2415 dom = alias_data->domain;
2416 }
2417 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002418 }
2419
Joerg Roedel15898bb2009-11-24 15:39:42 +01002420 return dom;
2421}
2422
Joerg Roedele275a2a2008-12-10 18:27:25 +01002423static int device_change_notifier(struct notifier_block *nb,
2424 unsigned long action, void *data)
2425{
Joerg Roedele275a2a2008-12-10 18:27:25 +01002426 struct dma_ops_domain *dma_domain;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002427 struct protection_domain *domain;
2428 struct iommu_dev_data *dev_data;
2429 struct device *dev = data;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002430 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002431 unsigned long flags;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002432 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002433
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002434 if (!check_device(dev))
2435 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002436
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002437 devid = get_device_id(dev);
2438 iommu = amd_iommu_rlookup_table[devid];
2439 dev_data = get_dev_data(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002440
2441 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07002442 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002443
2444 domain = domain_for_device(dev);
2445
Joerg Roedele275a2a2008-12-10 18:27:25 +01002446 if (!domain)
2447 goto out;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002448 if (dev_data->passthrough)
Joerg Roedela1ca3312009-09-01 12:22:22 +02002449 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002450 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002451 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002452 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002453
2454 iommu_init_device(dev);
2455
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002456 /*
2457 * dev_data is still NULL and
2458 * got initialized in iommu_init_device
2459 */
2460 dev_data = get_dev_data(dev);
2461
2462 if (iommu_pass_through || dev_data->iommu_v2) {
2463 dev_data->passthrough = true;
2464 attach_device(dev, pt_domain);
2465 break;
2466 }
2467
Joerg Roedel657cbb62009-11-23 15:26:46 +01002468 domain = domain_for_device(dev);
2469
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002470 /* allocate a protection domain if a device is added */
2471 dma_domain = find_protection_domain(devid);
Joerg Roedelc2a28762013-03-26 22:48:23 +01002472 if (!dma_domain) {
2473 dma_domain = dma_ops_domain_alloc();
2474 if (!dma_domain)
2475 goto out;
2476 dma_domain->target_dev = devid;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002477
Joerg Roedelc2a28762013-03-26 22:48:23 +01002478 spin_lock_irqsave(&iommu_pd_list_lock, flags);
2479 list_add_tail(&dma_domain->list, &iommu_pd_list);
2480 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
2481 }
Joerg Roedelac1534a2012-06-21 14:52:40 +02002482
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002483 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedelac1534a2012-06-21 14:52:40 +02002484
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002485 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002486 case BUS_NOTIFY_DEL_DEVICE:
2487
2488 iommu_uninit_device(dev);
2489
Joerg Roedele275a2a2008-12-10 18:27:25 +01002490 default:
2491 goto out;
2492 }
2493
Joerg Roedele275a2a2008-12-10 18:27:25 +01002494 iommu_completion_wait(iommu);
2495
2496out:
2497 return 0;
2498}
2499
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302500static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01002501 .notifier_call = device_change_notifier,
2502};
Joerg Roedel355bf552008-12-08 12:02:41 +01002503
Joerg Roedel8638c492009-12-10 11:12:25 +01002504void amd_iommu_init_notifier(void)
2505{
2506 bus_register_notifier(&pci_bus_type, &device_nb);
2507}
2508
Joerg Roedel431b2a22008-07-11 17:14:22 +02002509/*****************************************************************************
2510 *
2511 * The next functions belong to the dma_ops mapping/unmapping code.
2512 *
2513 *****************************************************************************/
2514
2515/*
2516 * In the dma_ops path we only have the struct device. This function
2517 * finds the corresponding IOMMU, the protection domain and the
2518 * requestor id for a given device.
2519 * If the device is not yet associated with a domain this is also done
2520 * in this function.
2521 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002522static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002523{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002524 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002525 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002526 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002527
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002528 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002529 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002530
Joerg Roedel94f6d192009-11-24 16:40:02 +01002531 domain = domain_for_device(dev);
2532 if (domain != NULL && !dma_ops_domain(domain))
2533 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002534
Joerg Roedel94f6d192009-11-24 16:40:02 +01002535 if (domain != NULL)
2536 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002537
Frank Arnolddf805ab2012-08-27 19:21:04 +02002538 /* Device not bound yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002539 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002540 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01002541 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2542 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002543 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01002544 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01002545
Joerg Roedel94f6d192009-11-24 16:40:02 +01002546 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002547}
2548
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002549static void update_device_table(struct protection_domain *domain)
2550{
Joerg Roedel492667d2009-11-27 13:25:47 +01002551 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002552
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002553 list_for_each_entry(dev_data, &domain->dev_list, list)
2554 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002555}
2556
2557static void update_domain(struct protection_domain *domain)
2558{
2559 if (!domain->updated)
2560 return;
2561
2562 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002563
2564 domain_flush_devices(domain);
2565 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002566
2567 domain->updated = false;
2568}
2569
Joerg Roedel431b2a22008-07-11 17:14:22 +02002570/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002571 * This function fetches the PTE for a given address in the aperture
2572 */
2573static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2574 unsigned long address)
2575{
Joerg Roedel384de722009-05-15 12:30:05 +02002576 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002577 u64 *pte, *pte_page;
2578
Joerg Roedel384de722009-05-15 12:30:05 +02002579 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2580 if (!aperture)
2581 return NULL;
2582
2583 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002584 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002585 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002586 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002587 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2588 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002589 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002590
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002591 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002592
2593 return pte;
2594}
2595
2596/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002597 * This is the generic map function. It maps one 4kb page at paddr to
2598 * the given address in the DMA address space for the domain.
2599 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002600static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002601 unsigned long address,
2602 phys_addr_t paddr,
2603 int direction)
2604{
2605 u64 *pte, __pte;
2606
2607 WARN_ON(address > dom->aperture_size);
2608
2609 paddr &= PAGE_MASK;
2610
Joerg Roedel8bda3092009-05-12 12:02:46 +02002611 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002612 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002613 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002614
2615 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2616
2617 if (direction == DMA_TO_DEVICE)
2618 __pte |= IOMMU_PTE_IR;
2619 else if (direction == DMA_FROM_DEVICE)
2620 __pte |= IOMMU_PTE_IW;
2621 else if (direction == DMA_BIDIRECTIONAL)
2622 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2623
2624 WARN_ON(*pte);
2625
2626 *pte = __pte;
2627
2628 return (dma_addr_t)address;
2629}
2630
Joerg Roedel431b2a22008-07-11 17:14:22 +02002631/*
2632 * The generic unmapping function for on page in the DMA address space.
2633 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002634static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002635 unsigned long address)
2636{
Joerg Roedel384de722009-05-15 12:30:05 +02002637 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002638 u64 *pte;
2639
2640 if (address >= dom->aperture_size)
2641 return;
2642
Joerg Roedel384de722009-05-15 12:30:05 +02002643 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2644 if (!aperture)
2645 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002646
Joerg Roedel384de722009-05-15 12:30:05 +02002647 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2648 if (!pte)
2649 return;
2650
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002651 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002652
2653 WARN_ON(!*pte);
2654
2655 *pte = 0ULL;
2656}
2657
Joerg Roedel431b2a22008-07-11 17:14:22 +02002658/*
2659 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002660 * contiguous memory region into DMA address space. It is used by all
2661 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002662 * Must be called with the domain lock held.
2663 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002664static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002665 struct dma_ops_domain *dma_dom,
2666 phys_addr_t paddr,
2667 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002668 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002669 bool align,
2670 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002671{
2672 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002673 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002674 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002675 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002676 int i;
2677
Joerg Roedele3c449f2008-10-15 22:02:11 -07002678 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002679 paddr &= PAGE_MASK;
2680
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002681 INC_STATS_COUNTER(total_map_requests);
2682
Joerg Roedelc1858972008-12-12 15:42:39 +01002683 if (pages > 1)
2684 INC_STATS_COUNTER(cross_page);
2685
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002686 if (align)
2687 align_mask = (1UL << get_order(size)) - 1;
2688
Joerg Roedel11b83882009-05-19 10:23:15 +02002689retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002690 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2691 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002692 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002693 /*
2694 * setting next_address here will let the address
2695 * allocator only scan the new allocated range in the
2696 * first run. This is a small optimization.
2697 */
2698 dma_dom->next_address = dma_dom->aperture_size;
2699
Joerg Roedel576175c2009-11-23 19:08:46 +01002700 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002701 goto out;
2702
2703 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002704 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002705 * allocation again
2706 */
2707 goto retry;
2708 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002709
2710 start = address;
2711 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002712 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002713 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002714 goto out_unmap;
2715
Joerg Roedelcb76c322008-06-26 21:28:00 +02002716 paddr += PAGE_SIZE;
2717 start += PAGE_SIZE;
2718 }
2719 address += offset;
2720
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002721 ADD_STATS_COUNTER(alloced_io_mem, size);
2722
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002723 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002724 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002725 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002726 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002727 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002728
Joerg Roedelcb76c322008-06-26 21:28:00 +02002729out:
2730 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002731
2732out_unmap:
2733
2734 for (--i; i >= 0; --i) {
2735 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002736 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002737 }
2738
2739 dma_ops_free_addresses(dma_dom, address, pages);
2740
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002741 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002742}
2743
Joerg Roedel431b2a22008-07-11 17:14:22 +02002744/*
2745 * Does the reverse of the __map_single function. Must be called with
2746 * the domain lock held too
2747 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002748static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002749 dma_addr_t dma_addr,
2750 size_t size,
2751 int dir)
2752{
Joerg Roedel04e04632010-09-23 16:12:48 +02002753 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002754 dma_addr_t i, start;
2755 unsigned int pages;
2756
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002757 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002758 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002759 return;
2760
Joerg Roedel04e04632010-09-23 16:12:48 +02002761 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002762 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002763 dma_addr &= PAGE_MASK;
2764 start = dma_addr;
2765
2766 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002767 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002768 start += PAGE_SIZE;
2769 }
2770
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002771 SUB_STATS_COUNTER(alloced_io_mem, size);
2772
Joerg Roedelcb76c322008-06-26 21:28:00 +02002773 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002774
Joerg Roedel80be3082008-11-06 14:59:05 +01002775 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002776 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002777 dma_dom->need_flush = false;
2778 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002779}
2780
Joerg Roedel431b2a22008-07-11 17:14:22 +02002781/*
2782 * The exported map_single function for dma_ops.
2783 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002784static dma_addr_t map_page(struct device *dev, struct page *page,
2785 unsigned long offset, size_t size,
2786 enum dma_data_direction dir,
2787 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002788{
2789 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002790 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002791 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002792 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002793 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002794
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002795 INC_STATS_COUNTER(cnt_map_single);
2796
Joerg Roedel94f6d192009-11-24 16:40:02 +01002797 domain = get_domain(dev);
2798 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002799 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002800 else if (IS_ERR(domain))
2801 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002802
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002803 dma_mask = *dev->dma_mask;
2804
Joerg Roedel4da70b92008-06-26 21:28:01 +02002805 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002806
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002807 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002808 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002809 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002810 goto out;
2811
Joerg Roedel17b124b2011-04-06 18:01:35 +02002812 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002813
2814out:
2815 spin_unlock_irqrestore(&domain->lock, flags);
2816
2817 return addr;
2818}
2819
Joerg Roedel431b2a22008-07-11 17:14:22 +02002820/*
2821 * The exported unmap_single function for dma_ops.
2822 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002823static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2824 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002825{
2826 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002827 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002828
Joerg Roedel146a6912008-12-12 15:07:12 +01002829 INC_STATS_COUNTER(cnt_unmap_single);
2830
Joerg Roedel94f6d192009-11-24 16:40:02 +01002831 domain = get_domain(dev);
2832 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002833 return;
2834
Joerg Roedel4da70b92008-06-26 21:28:01 +02002835 spin_lock_irqsave(&domain->lock, flags);
2836
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002837 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002838
Joerg Roedel17b124b2011-04-06 18:01:35 +02002839 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002840
2841 spin_unlock_irqrestore(&domain->lock, flags);
2842}
2843
Joerg Roedel431b2a22008-07-11 17:14:22 +02002844/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002845 * The exported map_sg function for dma_ops (handles scatter-gather
2846 * lists).
2847 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002848static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002849 int nelems, enum dma_data_direction dir,
2850 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002851{
2852 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002853 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002854 int i;
2855 struct scatterlist *s;
2856 phys_addr_t paddr;
2857 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002858 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002859
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002860 INC_STATS_COUNTER(cnt_map_sg);
2861
Joerg Roedel94f6d192009-11-24 16:40:02 +01002862 domain = get_domain(dev);
Joerg Roedela0e191b2013-04-09 15:04:36 +02002863 if (IS_ERR(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002864 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002865
Joerg Roedel832a90c2008-09-18 15:54:23 +02002866 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002867
Joerg Roedel65b050a2008-06-26 21:28:02 +02002868 spin_lock_irqsave(&domain->lock, flags);
2869
2870 for_each_sg(sglist, s, nelems, i) {
2871 paddr = sg_phys(s);
2872
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002873 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002874 paddr, s->length, dir, false,
2875 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002876
2877 if (s->dma_address) {
2878 s->dma_length = s->length;
2879 mapped_elems++;
2880 } else
2881 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002882 }
2883
Joerg Roedel17b124b2011-04-06 18:01:35 +02002884 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002885
2886out:
2887 spin_unlock_irqrestore(&domain->lock, flags);
2888
2889 return mapped_elems;
2890unmap:
2891 for_each_sg(sglist, s, mapped_elems, i) {
2892 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002893 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002894 s->dma_length, dir);
2895 s->dma_address = s->dma_length = 0;
2896 }
2897
2898 mapped_elems = 0;
2899
2900 goto out;
2901}
2902
Joerg Roedel431b2a22008-07-11 17:14:22 +02002903/*
2904 * The exported map_sg function for dma_ops (handles scatter-gather
2905 * lists).
2906 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002907static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002908 int nelems, enum dma_data_direction dir,
2909 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002910{
2911 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002912 struct protection_domain *domain;
2913 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002914 int i;
2915
Joerg Roedel55877a62008-12-12 15:12:14 +01002916 INC_STATS_COUNTER(cnt_unmap_sg);
2917
Joerg Roedel94f6d192009-11-24 16:40:02 +01002918 domain = get_domain(dev);
2919 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002920 return;
2921
Joerg Roedel65b050a2008-06-26 21:28:02 +02002922 spin_lock_irqsave(&domain->lock, flags);
2923
2924 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002925 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002926 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002927 s->dma_address = s->dma_length = 0;
2928 }
2929
Joerg Roedel17b124b2011-04-06 18:01:35 +02002930 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002931
2932 spin_unlock_irqrestore(&domain->lock, flags);
2933}
2934
Joerg Roedel431b2a22008-07-11 17:14:22 +02002935/*
2936 * The exported alloc_coherent function for dma_ops.
2937 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002938static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002939 dma_addr_t *dma_addr, gfp_t flag,
2940 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002941{
2942 unsigned long flags;
2943 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002944 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002945 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002946 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002947
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002948 INC_STATS_COUNTER(cnt_alloc_coherent);
2949
Joerg Roedel94f6d192009-11-24 16:40:02 +01002950 domain = get_domain(dev);
2951 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002952 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2953 *dma_addr = __pa(virt_addr);
2954 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002955 } else if (IS_ERR(domain))
2956 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002957
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002958 dma_mask = dev->coherent_dma_mask;
2959 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2960 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002961
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002962 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2963 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302964 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002965
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002966 paddr = virt_to_phys(virt_addr);
2967
Joerg Roedel832a90c2008-09-18 15:54:23 +02002968 if (!dma_mask)
2969 dma_mask = *dev->dma_mask;
2970
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002971 spin_lock_irqsave(&domain->lock, flags);
2972
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002973 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002974 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002975
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002976 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002977 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002978 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002979 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002980
Joerg Roedel17b124b2011-04-06 18:01:35 +02002981 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002982
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002983 spin_unlock_irqrestore(&domain->lock, flags);
2984
2985 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002986
2987out_free:
2988
2989 free_pages((unsigned long)virt_addr, get_order(size));
2990
2991 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002992}
2993
Joerg Roedel431b2a22008-07-11 17:14:22 +02002994/*
2995 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002996 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002997static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002998 void *virt_addr, dma_addr_t dma_addr,
2999 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003000{
3001 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003002 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003003
Joerg Roedel5d31ee72008-12-12 15:16:38 +01003004 INC_STATS_COUNTER(cnt_free_coherent);
3005
Joerg Roedel94f6d192009-11-24 16:40:02 +01003006 domain = get_domain(dev);
3007 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01003008 goto free_mem;
3009
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003010 spin_lock_irqsave(&domain->lock, flags);
3011
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01003012 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003013
Joerg Roedel17b124b2011-04-06 18:01:35 +02003014 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003015
3016 spin_unlock_irqrestore(&domain->lock, flags);
3017
3018free_mem:
3019 free_pages((unsigned long)virt_addr, get_order(size));
3020}
3021
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003022/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003023 * This function is called by the DMA layer to find out if we can handle a
3024 * particular device. It is part of the dma_ops.
3025 */
3026static int amd_iommu_dma_supported(struct device *dev, u64 mask)
3027{
Joerg Roedel420aef82009-11-23 16:14:57 +01003028 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003029}
3030
3031/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02003032 * The function for pre-allocating protection domains.
3033 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003034 * If the driver core informs the DMA layer if a driver grabs a device
3035 * we don't need to preallocate the protection domains anymore.
3036 * For now we have to.
3037 */
Steffen Persvold943bc7e2012-03-15 12:16:28 +01003038static void __init prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003039{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003040 struct iommu_dev_data *dev_data;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003041 struct dma_ops_domain *dma_dom;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003042 struct pci_dev *dev = NULL;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003043 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003044
Chris Wrightd18c69d2010-04-02 18:27:55 -07003045 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003046
3047 /* Do we handle this device? */
3048 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003049 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003050
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003051 dev_data = get_dev_data(&dev->dev);
3052 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
3053 /* Make sure passthrough domain is allocated */
3054 alloc_passthrough_domain();
3055 dev_data->passthrough = true;
3056 attach_device(&dev->dev, pt_domain);
Frank Arnolddf805ab2012-08-27 19:21:04 +02003057 pr_info("AMD-Vi: Using passthrough domain for device %s\n",
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003058 dev_name(&dev->dev));
3059 }
3060
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003061 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01003062 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003063 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003064
3065 devid = get_device_id(&dev->dev);
3066
Joerg Roedel87a64d52009-11-24 17:26:43 +01003067 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003068 if (!dma_dom)
3069 continue;
3070 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02003071 dma_dom->target_dev = devid;
3072
Joerg Roedel15898bb2009-11-24 15:39:42 +01003073 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01003074
Joerg Roedelbd60b732008-09-11 10:24:48 +02003075 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003076 }
3077}
3078
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003079static struct dma_map_ops amd_iommu_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003080 .alloc = alloc_coherent,
3081 .free = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09003082 .map_page = map_page,
3083 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003084 .map_sg = map_sg,
3085 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003086 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003087};
3088
Joerg Roedel27c21272011-05-30 15:56:24 +02003089static unsigned device_dma_ops_init(void)
3090{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003091 struct iommu_dev_data *dev_data;
Joerg Roedel27c21272011-05-30 15:56:24 +02003092 struct pci_dev *pdev = NULL;
3093 unsigned unhandled = 0;
3094
3095 for_each_pci_dev(pdev) {
3096 if (!check_device(&pdev->dev)) {
Joerg Roedelaf1be042012-01-18 14:03:11 +01003097
3098 iommu_ignore_device(&pdev->dev);
3099
Joerg Roedel27c21272011-05-30 15:56:24 +02003100 unhandled += 1;
3101 continue;
3102 }
3103
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003104 dev_data = get_dev_data(&pdev->dev);
3105
3106 if (!dev_data->passthrough)
3107 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
3108 else
3109 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
Joerg Roedel27c21272011-05-30 15:56:24 +02003110 }
3111
3112 return unhandled;
3113}
3114
Joerg Roedel431b2a22008-07-11 17:14:22 +02003115/*
3116 * The function which clues the AMD IOMMU driver into dma_ops.
3117 */
Joerg Roedelf5325092010-01-22 17:44:35 +01003118
3119void __init amd_iommu_init_api(void)
3120{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02003121 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01003122}
3123
Joerg Roedel6631ee92008-06-26 21:28:05 +02003124int __init amd_iommu_init_dma_ops(void)
3125{
3126 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02003127 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003128
Joerg Roedel431b2a22008-07-11 17:14:22 +02003129 /*
3130 * first allocate a default protection domain for every IOMMU we
3131 * found in the system. Devices not assigned to any other
3132 * protection domain will be assigned to the default one.
3133 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02003134 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01003135 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003136 if (iommu->default_dom == NULL)
3137 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01003138 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003139 ret = iommu_init_unity_mappings(iommu);
3140 if (ret)
3141 goto free_domains;
3142 }
3143
Joerg Roedel431b2a22008-07-11 17:14:22 +02003144 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01003145 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02003146 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01003147 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003148
3149 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09003150 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003151
Joerg Roedel431b2a22008-07-11 17:14:22 +02003152 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02003153 unhandled = device_dma_ops_init();
3154 if (unhandled && max_pfn > MAX_DMA32_PFN) {
3155 /* There are unhandled devices - initialize swiotlb for them */
3156 swiotlb = 1;
3157 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02003158
Joerg Roedel7f265082008-12-12 13:50:21 +01003159 amd_iommu_stats_init();
3160
Joerg Roedel62410ee2012-06-12 16:42:43 +02003161 if (amd_iommu_unmap_flush)
3162 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
3163 else
3164 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
3165
Joerg Roedel6631ee92008-06-26 21:28:05 +02003166 return 0;
3167
3168free_domains:
3169
Joerg Roedel3bd22172009-05-04 15:06:20 +02003170 for_each_iommu(iommu) {
Cyril Roelandt91457df2013-02-12 05:01:50 +01003171 dma_ops_domain_free(iommu->default_dom);
Joerg Roedel6631ee92008-06-26 21:28:05 +02003172 }
3173
3174 return ret;
3175}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003176
3177/*****************************************************************************
3178 *
3179 * The following functions belong to the exported interface of AMD IOMMU
3180 *
3181 * This interface allows access to lower level functions of the IOMMU
3182 * like protection domain handling and assignement of devices to domains
3183 * which is not possible with the dma_ops interface.
3184 *
3185 *****************************************************************************/
3186
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003187static void cleanup_domain(struct protection_domain *domain)
3188{
Joerg Roedel492667d2009-11-27 13:25:47 +01003189 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003190 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003191
3192 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3193
Joerg Roedel492667d2009-11-27 13:25:47 +01003194 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02003195 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01003196 atomic_set(&dev_data->bind, 0);
3197 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003198
3199 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3200}
3201
Joerg Roedel26508152009-08-26 16:52:40 +02003202static void protection_domain_free(struct protection_domain *domain)
3203{
3204 if (!domain)
3205 return;
3206
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003207 del_domain_from_list(domain);
3208
Joerg Roedel26508152009-08-26 16:52:40 +02003209 if (domain->id)
3210 domain_id_free(domain->id);
3211
3212 kfree(domain);
3213}
3214
3215static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01003216{
3217 struct protection_domain *domain;
3218
3219 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
3220 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02003221 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003222
3223 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003224 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01003225 domain->id = domain_id_alloc();
3226 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02003227 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01003228 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02003229
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003230 add_domain_to_list(domain);
3231
Joerg Roedel26508152009-08-26 16:52:40 +02003232 return domain;
3233
3234out_err:
3235 kfree(domain);
3236
3237 return NULL;
3238}
3239
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003240static int __init alloc_passthrough_domain(void)
3241{
3242 if (pt_domain != NULL)
3243 return 0;
3244
3245 /* allocate passthrough domain */
3246 pt_domain = protection_domain_alloc();
3247 if (!pt_domain)
3248 return -ENOMEM;
3249
3250 pt_domain->mode = PAGE_MODE_NONE;
3251
3252 return 0;
3253}
Joerg Roedel26508152009-08-26 16:52:40 +02003254static int amd_iommu_domain_init(struct iommu_domain *dom)
3255{
3256 struct protection_domain *domain;
3257
3258 domain = protection_domain_alloc();
3259 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01003260 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02003261
3262 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003263 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
3264 if (!domain->pt_root)
3265 goto out_free;
3266
Joerg Roedelf3572db2011-11-23 12:36:25 +01003267 domain->iommu_domain = dom;
3268
Joerg Roedelc156e342008-12-02 18:13:27 +01003269 dom->priv = domain;
3270
Joerg Roedel0ff64f82012-01-26 19:40:53 +01003271 dom->geometry.aperture_start = 0;
3272 dom->geometry.aperture_end = ~0ULL;
3273 dom->geometry.force_aperture = true;
3274
Joerg Roedelc156e342008-12-02 18:13:27 +01003275 return 0;
3276
3277out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02003278 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01003279
3280 return -ENOMEM;
3281}
3282
Joerg Roedel98383fc2008-12-02 18:34:12 +01003283static void amd_iommu_domain_destroy(struct iommu_domain *dom)
3284{
3285 struct protection_domain *domain = dom->priv;
3286
3287 if (!domain)
3288 return;
3289
3290 if (domain->dev_cnt > 0)
3291 cleanup_domain(domain);
3292
3293 BUG_ON(domain->dev_cnt != 0);
3294
Joerg Roedel132bd682011-11-17 14:18:46 +01003295 if (domain->mode != PAGE_MODE_NONE)
3296 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003297
Joerg Roedel52815b72011-11-17 17:24:28 +01003298 if (domain->flags & PD_IOMMUV2_MASK)
3299 free_gcr3_table(domain);
3300
Joerg Roedel8b408fe2010-03-08 14:20:07 +01003301 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003302
3303 dom->priv = NULL;
3304}
3305
Joerg Roedel684f2882008-12-08 12:07:44 +01003306static void amd_iommu_detach_device(struct iommu_domain *dom,
3307 struct device *dev)
3308{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003309 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003310 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003311 u16 devid;
3312
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003313 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003314 return;
3315
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003316 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003317
Joerg Roedel657cbb62009-11-23 15:26:46 +01003318 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003319 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003320
3321 iommu = amd_iommu_rlookup_table[devid];
3322 if (!iommu)
3323 return;
3324
Joerg Roedel684f2882008-12-08 12:07:44 +01003325 iommu_completion_wait(iommu);
3326}
3327
Joerg Roedel01106062008-12-02 19:34:11 +01003328static int amd_iommu_attach_device(struct iommu_domain *dom,
3329 struct device *dev)
3330{
3331 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01003332 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003333 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003334 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003335
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003336 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003337 return -EINVAL;
3338
Joerg Roedel657cbb62009-11-23 15:26:46 +01003339 dev_data = dev->archdata.iommu;
3340
Joerg Roedelf62dda62011-06-09 12:55:35 +02003341 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003342 if (!iommu)
3343 return -EINVAL;
3344
Joerg Roedel657cbb62009-11-23 15:26:46 +01003345 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003346 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003347
Joerg Roedel15898bb2009-11-24 15:39:42 +01003348 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003349
3350 iommu_completion_wait(iommu);
3351
Joerg Roedel15898bb2009-11-24 15:39:42 +01003352 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003353}
3354
Joerg Roedel468e2362010-01-21 16:37:36 +01003355static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003356 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003357{
3358 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003359 int prot = 0;
3360 int ret;
3361
Joerg Roedel132bd682011-11-17 14:18:46 +01003362 if (domain->mode == PAGE_MODE_NONE)
3363 return -EINVAL;
3364
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003365 if (iommu_prot & IOMMU_READ)
3366 prot |= IOMMU_PROT_IR;
3367 if (iommu_prot & IOMMU_WRITE)
3368 prot |= IOMMU_PROT_IW;
3369
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003370 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003371 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003372 mutex_unlock(&domain->api_lock);
3373
Joerg Roedel795e74f72010-05-11 17:40:57 +02003374 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003375}
3376
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003377static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3378 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003379{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003380 struct protection_domain *domain = dom->priv;
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003381 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003382
Joerg Roedel132bd682011-11-17 14:18:46 +01003383 if (domain->mode == PAGE_MODE_NONE)
3384 return -EINVAL;
3385
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003386 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003387 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003388 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003389
Joerg Roedel17b124b2011-04-06 18:01:35 +02003390 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003391
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003392 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003393}
3394
Joerg Roedel645c4c82008-12-02 20:05:50 +01003395static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
3396 unsigned long iova)
3397{
3398 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003399 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003400 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003401 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003402
Joerg Roedel132bd682011-11-17 14:18:46 +01003403 if (domain->mode == PAGE_MODE_NONE)
3404 return iova;
3405
Joerg Roedel24cd7722010-01-19 17:27:39 +01003406 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003407
Joerg Roedela6d41a42009-09-02 17:08:55 +02003408 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003409 return 0;
3410
Joerg Roedelf03152b2010-01-21 16:15:24 +01003411 if (PM_PTE_LEVEL(*pte) == 0)
3412 offset_mask = PAGE_SIZE - 1;
3413 else
3414 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
3415
3416 __pte = *pte & PM_ADDR_MASK;
3417 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003418
3419 return paddr;
3420}
3421
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003422static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
3423 unsigned long cap)
3424{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003425 switch (cap) {
3426 case IOMMU_CAP_CACHE_COHERENCY:
3427 return 1;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003428 case IOMMU_CAP_INTR_REMAP:
3429 return irq_remapping_enabled;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003430 }
3431
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003432 return 0;
3433}
3434
Joerg Roedel26961ef2008-12-03 17:00:17 +01003435static struct iommu_ops amd_iommu_ops = {
3436 .domain_init = amd_iommu_domain_init,
3437 .domain_destroy = amd_iommu_domain_destroy,
3438 .attach_dev = amd_iommu_attach_device,
3439 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003440 .map = amd_iommu_map,
3441 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003442 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003443 .domain_has_cap = amd_iommu_domain_has_cap,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003444 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003445};
3446
Joerg Roedel0feae532009-08-26 15:26:30 +02003447/*****************************************************************************
3448 *
3449 * The next functions do a basic initialization of IOMMU for pass through
3450 * mode
3451 *
3452 * In passthrough mode the IOMMU is initialized and enabled but not used for
3453 * DMA-API translation.
3454 *
3455 *****************************************************************************/
3456
3457int __init amd_iommu_init_passthrough(void)
3458{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003459 struct iommu_dev_data *dev_data;
Joerg Roedel0feae532009-08-26 15:26:30 +02003460 struct pci_dev *dev = NULL;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003461 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003462 u16 devid;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003463 int ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003464
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003465 ret = alloc_passthrough_domain();
3466 if (ret)
3467 return ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003468
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04003469 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003470 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02003471 continue;
3472
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003473 dev_data = get_dev_data(&dev->dev);
3474 dev_data->passthrough = true;
3475
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003476 devid = get_device_id(&dev->dev);
3477
Joerg Roedel15898bb2009-11-24 15:39:42 +01003478 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02003479 if (!iommu)
3480 continue;
3481
Joerg Roedel15898bb2009-11-24 15:39:42 +01003482 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02003483 }
3484
Joerg Roedel2655d7a2011-12-22 12:35:38 +01003485 amd_iommu_stats_init();
3486
Joerg Roedel0feae532009-08-26 15:26:30 +02003487 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
3488
3489 return 0;
3490}
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003491
3492/* IOMMUv2 specific functions */
3493int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3494{
3495 return atomic_notifier_chain_register(&ppr_notifier, nb);
3496}
3497EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3498
3499int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3500{
3501 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3502}
3503EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003504
3505void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3506{
3507 struct protection_domain *domain = dom->priv;
3508 unsigned long flags;
3509
3510 spin_lock_irqsave(&domain->lock, flags);
3511
3512 /* Update data structure */
3513 domain->mode = PAGE_MODE_NONE;
3514 domain->updated = true;
3515
3516 /* Make changes visible to IOMMUs */
3517 update_domain(domain);
3518
3519 /* Page-table is not visible to IOMMU anymore, so free it */
3520 free_pagetable(domain);
3521
3522 spin_unlock_irqrestore(&domain->lock, flags);
3523}
3524EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003525
3526int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3527{
3528 struct protection_domain *domain = dom->priv;
3529 unsigned long flags;
3530 int levels, ret;
3531
3532 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3533 return -EINVAL;
3534
3535 /* Number of GCR3 table levels required */
3536 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3537 levels += 1;
3538
3539 if (levels > amd_iommu_max_glx_val)
3540 return -EINVAL;
3541
3542 spin_lock_irqsave(&domain->lock, flags);
3543
3544 /*
3545 * Save us all sanity checks whether devices already in the
3546 * domain support IOMMUv2. Just force that the domain has no
3547 * devices attached when it is switched into IOMMUv2 mode.
3548 */
3549 ret = -EBUSY;
3550 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3551 goto out;
3552
3553 ret = -ENOMEM;
3554 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3555 if (domain->gcr3_tbl == NULL)
3556 goto out;
3557
3558 domain->glx = levels;
3559 domain->flags |= PD_IOMMUV2_MASK;
3560 domain->updated = true;
3561
3562 update_domain(domain);
3563
3564 ret = 0;
3565
3566out:
3567 spin_unlock_irqrestore(&domain->lock, flags);
3568
3569 return ret;
3570}
3571EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003572
3573static int __flush_pasid(struct protection_domain *domain, int pasid,
3574 u64 address, bool size)
3575{
3576 struct iommu_dev_data *dev_data;
3577 struct iommu_cmd cmd;
3578 int i, ret;
3579
3580 if (!(domain->flags & PD_IOMMUV2_MASK))
3581 return -EINVAL;
3582
3583 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3584
3585 /*
3586 * IOMMU TLB needs to be flushed before Device TLB to
3587 * prevent device TLB refill from IOMMU TLB
3588 */
3589 for (i = 0; i < amd_iommus_present; ++i) {
3590 if (domain->dev_iommu[i] == 0)
3591 continue;
3592
3593 ret = iommu_queue_command(amd_iommus[i], &cmd);
3594 if (ret != 0)
3595 goto out;
3596 }
3597
3598 /* Wait until IOMMU TLB flushes are complete */
3599 domain_flush_complete(domain);
3600
3601 /* Now flush device TLBs */
3602 list_for_each_entry(dev_data, &domain->dev_list, list) {
3603 struct amd_iommu *iommu;
3604 int qdep;
3605
3606 BUG_ON(!dev_data->ats.enabled);
3607
3608 qdep = dev_data->ats.qdep;
3609 iommu = amd_iommu_rlookup_table[dev_data->devid];
3610
3611 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3612 qdep, address, size);
3613
3614 ret = iommu_queue_command(iommu, &cmd);
3615 if (ret != 0)
3616 goto out;
3617 }
3618
3619 /* Wait until all device TLBs are flushed */
3620 domain_flush_complete(domain);
3621
3622 ret = 0;
3623
3624out:
3625
3626 return ret;
3627}
3628
3629static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3630 u64 address)
3631{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003632 INC_STATS_COUNTER(invalidate_iotlb);
3633
Joerg Roedel22e266c2011-11-21 15:59:08 +01003634 return __flush_pasid(domain, pasid, address, false);
3635}
3636
3637int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3638 u64 address)
3639{
3640 struct protection_domain *domain = dom->priv;
3641 unsigned long flags;
3642 int ret;
3643
3644 spin_lock_irqsave(&domain->lock, flags);
3645 ret = __amd_iommu_flush_page(domain, pasid, address);
3646 spin_unlock_irqrestore(&domain->lock, flags);
3647
3648 return ret;
3649}
3650EXPORT_SYMBOL(amd_iommu_flush_page);
3651
3652static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3653{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003654 INC_STATS_COUNTER(invalidate_iotlb_all);
3655
Joerg Roedel22e266c2011-11-21 15:59:08 +01003656 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3657 true);
3658}
3659
3660int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3661{
3662 struct protection_domain *domain = dom->priv;
3663 unsigned long flags;
3664 int ret;
3665
3666 spin_lock_irqsave(&domain->lock, flags);
3667 ret = __amd_iommu_flush_tlb(domain, pasid);
3668 spin_unlock_irqrestore(&domain->lock, flags);
3669
3670 return ret;
3671}
3672EXPORT_SYMBOL(amd_iommu_flush_tlb);
3673
Joerg Roedelb16137b2011-11-21 16:50:23 +01003674static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3675{
3676 int index;
3677 u64 *pte;
3678
3679 while (true) {
3680
3681 index = (pasid >> (9 * level)) & 0x1ff;
3682 pte = &root[index];
3683
3684 if (level == 0)
3685 break;
3686
3687 if (!(*pte & GCR3_VALID)) {
3688 if (!alloc)
3689 return NULL;
3690
3691 root = (void *)get_zeroed_page(GFP_ATOMIC);
3692 if (root == NULL)
3693 return NULL;
3694
3695 *pte = __pa(root) | GCR3_VALID;
3696 }
3697
3698 root = __va(*pte & PAGE_MASK);
3699
3700 level -= 1;
3701 }
3702
3703 return pte;
3704}
3705
3706static int __set_gcr3(struct protection_domain *domain, int pasid,
3707 unsigned long cr3)
3708{
3709 u64 *pte;
3710
3711 if (domain->mode != PAGE_MODE_NONE)
3712 return -EINVAL;
3713
3714 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3715 if (pte == NULL)
3716 return -ENOMEM;
3717
3718 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3719
3720 return __amd_iommu_flush_tlb(domain, pasid);
3721}
3722
3723static int __clear_gcr3(struct protection_domain *domain, int pasid)
3724{
3725 u64 *pte;
3726
3727 if (domain->mode != PAGE_MODE_NONE)
3728 return -EINVAL;
3729
3730 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3731 if (pte == NULL)
3732 return 0;
3733
3734 *pte = 0;
3735
3736 return __amd_iommu_flush_tlb(domain, pasid);
3737}
3738
3739int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3740 unsigned long cr3)
3741{
3742 struct protection_domain *domain = dom->priv;
3743 unsigned long flags;
3744 int ret;
3745
3746 spin_lock_irqsave(&domain->lock, flags);
3747 ret = __set_gcr3(domain, pasid, cr3);
3748 spin_unlock_irqrestore(&domain->lock, flags);
3749
3750 return ret;
3751}
3752EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3753
3754int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3755{
3756 struct protection_domain *domain = dom->priv;
3757 unsigned long flags;
3758 int ret;
3759
3760 spin_lock_irqsave(&domain->lock, flags);
3761 ret = __clear_gcr3(domain, pasid);
3762 spin_unlock_irqrestore(&domain->lock, flags);
3763
3764 return ret;
3765}
3766EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003767
3768int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3769 int status, int tag)
3770{
3771 struct iommu_dev_data *dev_data;
3772 struct amd_iommu *iommu;
3773 struct iommu_cmd cmd;
3774
Joerg Roedel399be2f2011-12-01 16:53:47 +01003775 INC_STATS_COUNTER(complete_ppr);
3776
Joerg Roedelc99afa22011-11-21 18:19:25 +01003777 dev_data = get_dev_data(&pdev->dev);
3778 iommu = amd_iommu_rlookup_table[dev_data->devid];
3779
3780 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3781 tag, dev_data->pri_tlp);
3782
3783 return iommu_queue_command(iommu, &cmd);
3784}
3785EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003786
3787struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3788{
3789 struct protection_domain *domain;
3790
3791 domain = get_domain(&pdev->dev);
3792 if (IS_ERR(domain))
3793 return NULL;
3794
3795 /* Only return IOMMUv2 domains */
3796 if (!(domain->flags & PD_IOMMUV2_MASK))
3797 return NULL;
3798
3799 return domain->iommu_domain;
3800}
3801EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003802
3803void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3804{
3805 struct iommu_dev_data *dev_data;
3806
3807 if (!amd_iommu_v2_supported())
3808 return;
3809
3810 dev_data = get_dev_data(&pdev->dev);
3811 dev_data->errata |= (1 << erratum);
3812}
3813EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003814
3815int amd_iommu_device_info(struct pci_dev *pdev,
3816 struct amd_iommu_device_info *info)
3817{
3818 int max_pasids;
3819 int pos;
3820
3821 if (pdev == NULL || info == NULL)
3822 return -EINVAL;
3823
3824 if (!amd_iommu_v2_supported())
3825 return -EINVAL;
3826
3827 memset(info, 0, sizeof(*info));
3828
3829 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3830 if (pos)
3831 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3832
3833 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3834 if (pos)
3835 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3836
3837 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3838 if (pos) {
3839 int features;
3840
3841 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3842 max_pasids = min(max_pasids, (1 << 20));
3843
3844 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3845 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3846
3847 features = pci_pasid_features(pdev);
3848 if (features & PCI_PASID_CAP_EXEC)
3849 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3850 if (features & PCI_PASID_CAP_PRIV)
3851 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3852 }
3853
3854 return 0;
3855}
3856EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003857
3858#ifdef CONFIG_IRQ_REMAP
3859
3860/*****************************************************************************
3861 *
3862 * Interrupt Remapping Implementation
3863 *
3864 *****************************************************************************/
3865
3866union irte {
3867 u32 val;
3868 struct {
3869 u32 valid : 1,
3870 no_fault : 1,
3871 int_type : 3,
3872 rq_eoi : 1,
3873 dm : 1,
3874 rsvd_1 : 1,
3875 destination : 8,
3876 vector : 8,
3877 rsvd_2 : 8;
3878 } fields;
3879};
3880
3881#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3882#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3883#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3884#define DTE_IRQ_REMAP_ENABLE 1ULL
3885
3886static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3887{
3888 u64 dte;
3889
3890 dte = amd_iommu_dev_table[devid].data[2];
3891 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3892 dte |= virt_to_phys(table->table);
3893 dte |= DTE_IRQ_REMAP_INTCTL;
3894 dte |= DTE_IRQ_TABLE_LEN;
3895 dte |= DTE_IRQ_REMAP_ENABLE;
3896
3897 amd_iommu_dev_table[devid].data[2] = dte;
3898}
3899
3900#define IRTE_ALLOCATED (~1U)
3901
3902static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3903{
3904 struct irq_remap_table *table = NULL;
3905 struct amd_iommu *iommu;
3906 unsigned long flags;
3907 u16 alias;
3908
3909 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3910
3911 iommu = amd_iommu_rlookup_table[devid];
3912 if (!iommu)
3913 goto out_unlock;
3914
3915 table = irq_lookup_table[devid];
3916 if (table)
3917 goto out;
3918
3919 alias = amd_iommu_alias_table[devid];
3920 table = irq_lookup_table[alias];
3921 if (table) {
3922 irq_lookup_table[devid] = table;
3923 set_dte_irq_entry(devid, table);
3924 iommu_flush_dte(iommu, devid);
3925 goto out;
3926 }
3927
3928 /* Nothing there yet, allocate new irq remapping table */
3929 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3930 if (!table)
3931 goto out;
3932
Joerg Roedel197887f2013-04-09 21:14:08 +02003933 /* Initialize table spin-lock */
3934 spin_lock_init(&table->lock);
3935
Joerg Roedel2b324502012-06-21 16:29:10 +02003936 if (ioapic)
3937 /* Keep the first 32 indexes free for IOAPIC interrupts */
3938 table->min_index = 32;
3939
3940 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3941 if (!table->table) {
3942 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003943 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003944 goto out;
3945 }
3946
3947 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3948
3949 if (ioapic) {
3950 int i;
3951
3952 for (i = 0; i < 32; ++i)
3953 table->table[i] = IRTE_ALLOCATED;
3954 }
3955
3956 irq_lookup_table[devid] = table;
3957 set_dte_irq_entry(devid, table);
3958 iommu_flush_dte(iommu, devid);
3959 if (devid != alias) {
3960 irq_lookup_table[alias] = table;
3961 set_dte_irq_entry(devid, table);
3962 iommu_flush_dte(iommu, alias);
3963 }
3964
3965out:
3966 iommu_completion_wait(iommu);
3967
3968out_unlock:
3969 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3970
3971 return table;
3972}
3973
3974static int alloc_irq_index(struct irq_cfg *cfg, u16 devid, int count)
3975{
3976 struct irq_remap_table *table;
3977 unsigned long flags;
3978 int index, c;
3979
3980 table = get_irq_table(devid, false);
3981 if (!table)
3982 return -ENODEV;
3983
3984 spin_lock_irqsave(&table->lock, flags);
3985
3986 /* Scan table for free entries */
3987 for (c = 0, index = table->min_index;
3988 index < MAX_IRQS_PER_TABLE;
3989 ++index) {
3990 if (table->table[index] == 0)
3991 c += 1;
3992 else
3993 c = 0;
3994
3995 if (c == count) {
Joerg Roedel0dfedd62013-04-09 15:39:16 +02003996 struct irq_2_irte *irte_info;
Joerg Roedel2b324502012-06-21 16:29:10 +02003997
3998 for (; c != 0; --c)
3999 table->table[index - c + 1] = IRTE_ALLOCATED;
4000
4001 index -= count - 1;
4002
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004003 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004004 irte_info = &cfg->irq_2_irte;
4005 irte_info->devid = devid;
4006 irte_info->index = index;
Joerg Roedel2b324502012-06-21 16:29:10 +02004007
4008 goto out;
4009 }
4010 }
4011
4012 index = -ENOSPC;
4013
4014out:
4015 spin_unlock_irqrestore(&table->lock, flags);
4016
4017 return index;
4018}
4019
4020static int get_irte(u16 devid, int index, union irte *irte)
4021{
4022 struct irq_remap_table *table;
4023 unsigned long flags;
4024
4025 table = get_irq_table(devid, false);
4026 if (!table)
4027 return -ENOMEM;
4028
4029 spin_lock_irqsave(&table->lock, flags);
4030 irte->val = table->table[index];
4031 spin_unlock_irqrestore(&table->lock, flags);
4032
4033 return 0;
4034}
4035
4036static int modify_irte(u16 devid, int index, union irte irte)
4037{
4038 struct irq_remap_table *table;
4039 struct amd_iommu *iommu;
4040 unsigned long flags;
4041
4042 iommu = amd_iommu_rlookup_table[devid];
4043 if (iommu == NULL)
4044 return -EINVAL;
4045
4046 table = get_irq_table(devid, false);
4047 if (!table)
4048 return -ENOMEM;
4049
4050 spin_lock_irqsave(&table->lock, flags);
4051 table->table[index] = irte.val;
4052 spin_unlock_irqrestore(&table->lock, flags);
4053
4054 iommu_flush_irt(iommu, devid);
4055 iommu_completion_wait(iommu);
4056
4057 return 0;
4058}
4059
4060static void free_irte(u16 devid, int index)
4061{
4062 struct irq_remap_table *table;
4063 struct amd_iommu *iommu;
4064 unsigned long flags;
4065
4066 iommu = amd_iommu_rlookup_table[devid];
4067 if (iommu == NULL)
4068 return;
4069
4070 table = get_irq_table(devid, false);
4071 if (!table)
4072 return;
4073
4074 spin_lock_irqsave(&table->lock, flags);
4075 table->table[index] = 0;
4076 spin_unlock_irqrestore(&table->lock, flags);
4077
4078 iommu_flush_irt(iommu, devid);
4079 iommu_completion_wait(iommu);
4080}
4081
Joerg Roedel5527de72012-06-26 11:17:32 +02004082static int setup_ioapic_entry(int irq, struct IO_APIC_route_entry *entry,
4083 unsigned int destination, int vector,
4084 struct io_apic_irq_attr *attr)
4085{
4086 struct irq_remap_table *table;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004087 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004088 struct irq_cfg *cfg;
4089 union irte irte;
4090 int ioapic_id;
4091 int index;
4092 int devid;
4093 int ret;
4094
4095 cfg = irq_get_chip_data(irq);
4096 if (!cfg)
4097 return -EINVAL;
4098
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004099 irte_info = &cfg->irq_2_irte;
Joerg Roedel5527de72012-06-26 11:17:32 +02004100 ioapic_id = mpc_ioapic_id(attr->ioapic);
4101 devid = get_ioapic_devid(ioapic_id);
4102
4103 if (devid < 0)
4104 return devid;
4105
4106 table = get_irq_table(devid, true);
4107 if (table == NULL)
4108 return -ENOMEM;
4109
4110 index = attr->ioapic_pin;
4111
4112 /* Setup IRQ remapping info */
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004113 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004114 irte_info->devid = devid;
4115 irte_info->index = index;
Joerg Roedel5527de72012-06-26 11:17:32 +02004116
4117 /* Setup IRTE for IOMMU */
4118 irte.val = 0;
4119 irte.fields.vector = vector;
4120 irte.fields.int_type = apic->irq_delivery_mode;
4121 irte.fields.destination = destination;
4122 irte.fields.dm = apic->irq_dest_mode;
4123 irte.fields.valid = 1;
4124
4125 ret = modify_irte(devid, index, irte);
4126 if (ret)
4127 return ret;
4128
4129 /* Setup IOAPIC entry */
4130 memset(entry, 0, sizeof(*entry));
4131
4132 entry->vector = index;
4133 entry->mask = 0;
4134 entry->trigger = attr->trigger;
4135 entry->polarity = attr->polarity;
4136
4137 /*
4138 * Mask level triggered irqs.
Joerg Roedel5527de72012-06-26 11:17:32 +02004139 */
4140 if (attr->trigger)
4141 entry->mask = 1;
4142
4143 return 0;
4144}
4145
4146static int set_affinity(struct irq_data *data, const struct cpumask *mask,
4147 bool force)
4148{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004149 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004150 unsigned int dest, irq;
4151 struct irq_cfg *cfg;
4152 union irte irte;
4153 int err;
4154
4155 if (!config_enabled(CONFIG_SMP))
4156 return -1;
4157
4158 cfg = data->chip_data;
4159 irq = data->irq;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004160 irte_info = &cfg->irq_2_irte;
Joerg Roedel5527de72012-06-26 11:17:32 +02004161
4162 if (!cpumask_intersects(mask, cpu_online_mask))
4163 return -EINVAL;
4164
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004165 if (get_irte(irte_info->devid, irte_info->index, &irte))
Joerg Roedel5527de72012-06-26 11:17:32 +02004166 return -EBUSY;
4167
4168 if (assign_irq_vector(irq, cfg, mask))
4169 return -EBUSY;
4170
4171 err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
4172 if (err) {
4173 if (assign_irq_vector(irq, cfg, data->affinity))
4174 pr_err("AMD-Vi: Failed to recover vector for irq %d\n", irq);
4175 return err;
4176 }
4177
4178 irte.fields.vector = cfg->vector;
4179 irte.fields.destination = dest;
4180
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004181 modify_irte(irte_info->devid, irte_info->index, irte);
Joerg Roedel5527de72012-06-26 11:17:32 +02004182
4183 if (cfg->move_in_progress)
4184 send_cleanup_vector(cfg);
4185
4186 cpumask_copy(data->affinity, mask);
4187
4188 return 0;
4189}
4190
4191static int free_irq(int irq)
4192{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004193 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004194 struct irq_cfg *cfg;
4195
4196 cfg = irq_get_chip_data(irq);
4197 if (!cfg)
4198 return -EINVAL;
4199
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004200 irte_info = &cfg->irq_2_irte;
Joerg Roedel5527de72012-06-26 11:17:32 +02004201
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004202 free_irte(irte_info->devid, irte_info->index);
Joerg Roedel5527de72012-06-26 11:17:32 +02004203
4204 return 0;
4205}
4206
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004207static void compose_msi_msg(struct pci_dev *pdev,
4208 unsigned int irq, unsigned int dest,
4209 struct msi_msg *msg, u8 hpet_id)
4210{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004211 struct irq_2_irte *irte_info;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004212 struct irq_cfg *cfg;
4213 union irte irte;
4214
4215 cfg = irq_get_chip_data(irq);
4216 if (!cfg)
4217 return;
4218
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004219 irte_info = &cfg->irq_2_irte;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004220
4221 irte.val = 0;
4222 irte.fields.vector = cfg->vector;
4223 irte.fields.int_type = apic->irq_delivery_mode;
4224 irte.fields.destination = dest;
4225 irte.fields.dm = apic->irq_dest_mode;
4226 irte.fields.valid = 1;
4227
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004228 modify_irte(irte_info->devid, irte_info->index, irte);
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004229
4230 msg->address_hi = MSI_ADDR_BASE_HI;
4231 msg->address_lo = MSI_ADDR_BASE_LO;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004232 msg->data = irte_info->index;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004233}
4234
4235static int msi_alloc_irq(struct pci_dev *pdev, int irq, int nvec)
4236{
4237 struct irq_cfg *cfg;
4238 int index;
4239 u16 devid;
4240
4241 if (!pdev)
4242 return -EINVAL;
4243
4244 cfg = irq_get_chip_data(irq);
4245 if (!cfg)
4246 return -EINVAL;
4247
4248 devid = get_device_id(&pdev->dev);
4249 index = alloc_irq_index(cfg, devid, nvec);
4250
4251 return index < 0 ? MAX_IRQS_PER_TABLE : index;
4252}
4253
4254static int msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
4255 int index, int offset)
4256{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004257 struct irq_2_irte *irte_info;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004258 struct irq_cfg *cfg;
4259 u16 devid;
4260
4261 if (!pdev)
4262 return -EINVAL;
4263
4264 cfg = irq_get_chip_data(irq);
4265 if (!cfg)
4266 return -EINVAL;
4267
4268 if (index >= MAX_IRQS_PER_TABLE)
4269 return 0;
4270
4271 devid = get_device_id(&pdev->dev);
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004272 irte_info = &cfg->irq_2_irte;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004273
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004274 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004275 irte_info->devid = devid;
4276 irte_info->index = index + offset;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004277
4278 return 0;
4279}
4280
Joerg Roedeld9761952012-06-26 16:00:08 +02004281static int setup_hpet_msi(unsigned int irq, unsigned int id)
4282{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004283 struct irq_2_irte *irte_info;
Joerg Roedeld9761952012-06-26 16:00:08 +02004284 struct irq_cfg *cfg;
4285 int index, devid;
4286
4287 cfg = irq_get_chip_data(irq);
4288 if (!cfg)
4289 return -EINVAL;
4290
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004291 irte_info = &cfg->irq_2_irte;
Joerg Roedeld9761952012-06-26 16:00:08 +02004292 devid = get_hpet_devid(id);
4293 if (devid < 0)
4294 return devid;
4295
4296 index = alloc_irq_index(cfg, devid, 1);
4297 if (index < 0)
4298 return index;
4299
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004300 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004301 irte_info->devid = devid;
4302 irte_info->index = index;
Joerg Roedeld9761952012-06-26 16:00:08 +02004303
4304 return 0;
4305}
4306
Joerg Roedel6b474b82012-06-26 16:46:04 +02004307struct irq_remap_ops amd_iommu_irq_ops = {
4308 .supported = amd_iommu_supported,
4309 .prepare = amd_iommu_prepare,
4310 .enable = amd_iommu_enable,
4311 .disable = amd_iommu_disable,
4312 .reenable = amd_iommu_reenable,
4313 .enable_faulting = amd_iommu_enable_faulting,
4314 .setup_ioapic_entry = setup_ioapic_entry,
4315 .set_affinity = set_affinity,
4316 .free_irq = free_irq,
4317 .compose_msi_msg = compose_msi_msg,
4318 .msi_alloc_irq = msi_alloc_irq,
4319 .msi_setup_irq = msi_setup_irq,
4320 .setup_hpet_msi = setup_hpet_msi,
4321};
Joerg Roedel2b324502012-06-21 16:29:10 +02004322#endif