blob: 0f8776940bf562c7543af7fe61b9e7f0bf6234dd [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 Roedel63ce3ae2015-02-04 16:12:55 +01003 * Author: Joerg Roedel <jroedel@suse.de>
Joerg Roedelb6c02712008-06-26 21:27:53 +02004 * 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>
Joerg Roedel3b839a52015-04-01 14:58:47 +020036#include <linux/dma-contiguous.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020037#include <asm/irq_remapping.h>
38#include <asm/io_apic.h>
39#include <asm/apic.h>
40#include <asm/hw_irq.h>
Joerg Roedel17f5b562011-07-06 17:14:44 +020041#include <asm/msidef.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020042#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090043#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010044#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020045#include <asm/dma.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020046
47#include "amd_iommu_proto.h"
48#include "amd_iommu_types.h"
Joerg Roedel6b474b82012-06-26 16:46:04 +020049#include "irq_remapping.h"
Joerg Roedelb6c02712008-06-26 21:27:53 +020050
51#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
52
Joerg Roedel815b33f2011-04-06 17:26:49 +020053#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020054
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020055/*
56 * This bitmap is used to advertise the page sizes our hardware support
57 * to the IOMMU core, which will then use this information to split
58 * physically contiguous memory regions it is mapping into page sizes
59 * that we support.
60 *
Joerg Roedel954e3dd2012-12-02 15:35:37 +010061 * 512GB Pages are not supported due to a hardware bug
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020062 */
Joerg Roedel954e3dd2012-12-02 15:35:37 +010063#define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020064
Joerg Roedelb6c02712008-06-26 21:27:53 +020065static DEFINE_RWLOCK(amd_iommu_devtable_lock);
66
Joerg Roedelbd60b732008-09-11 10:24:48 +020067/* A list of preallocated protection domains */
68static LIST_HEAD(iommu_pd_list);
69static DEFINE_SPINLOCK(iommu_pd_list_lock);
70
Joerg Roedel8fa5f802011-06-09 12:24:45 +020071/* List of all available dev_data structures */
72static LIST_HEAD(dev_data_list);
73static DEFINE_SPINLOCK(dev_data_list_lock);
74
Joerg Roedel6efed632012-06-14 15:52:58 +020075LIST_HEAD(ioapic_map);
76LIST_HEAD(hpet_map);
77
Joerg Roedel0feae532009-08-26 15:26:30 +020078/*
79 * Domain for untranslated devices - only allocated
80 * if iommu=pt passed on kernel cmd line.
81 */
82static struct protection_domain *pt_domain;
83
Thierry Redingb22f6432014-06-27 09:03:12 +020084static const struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010085
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010086static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +010087int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010088
Joerg Roedelac1534a2012-06-21 14:52:40 +020089static struct dma_map_ops amd_iommu_dma_ops;
90
Joerg Roedel431b2a22008-07-11 17:14:22 +020091/*
Joerg Roedel50917e22014-08-05 16:38:38 +020092 * This struct contains device specific data for the IOMMU
93 */
94struct iommu_dev_data {
95 struct list_head list; /* For domain->dev_list */
96 struct list_head dev_data_list; /* For global dev_data_list */
Joerg Roedelf251e182014-08-05 16:48:10 +020097 struct list_head alias_list; /* Link alias-groups together */
Joerg Roedel50917e22014-08-05 16:38:38 +020098 struct iommu_dev_data *alias_data;/* The alias dev_data */
99 struct protection_domain *domain; /* Domain the device is bound to */
Joerg Roedel50917e22014-08-05 16:38:38 +0200100 u16 devid; /* PCI Device ID */
101 bool iommu_v2; /* Device can make use of IOMMUv2 */
102 bool passthrough; /* Default for device is pt_domain */
103 struct {
104 bool enabled;
105 int qdep;
106 } ats; /* ATS state */
107 bool pri_tlp; /* PASID TLB required for
108 PPR completions */
109 u32 errata; /* Bitmap for errata to apply */
110};
111
112/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200113 * general struct to manage commands send to an IOMMU
114 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200115struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +0200116 u32 data[4];
117};
118
Joerg Roedel05152a02012-06-15 16:53:51 +0200119struct kmem_cache *amd_iommu_irq_cache;
120
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200121static void update_domain(struct protection_domain *domain);
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200122static int alloc_passthrough_domain(void);
Chris Wrightc1eee672009-05-21 00:56:58 -0700123
Joerg Roedel15898bb2009-11-24 15:39:42 +0100124/****************************************************************************
125 *
126 * Helper functions
127 *
128 ****************************************************************************/
129
Joerg Roedel3f4b87b2015-03-26 13:43:07 +0100130static struct protection_domain *to_pdomain(struct iommu_domain *dom)
131{
132 return container_of(dom, struct protection_domain, domain);
133}
134
Joerg Roedelf62dda62011-06-09 12:55:35 +0200135static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200136{
137 struct iommu_dev_data *dev_data;
138 unsigned long flags;
139
140 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
141 if (!dev_data)
142 return NULL;
143
Joerg Roedelf251e182014-08-05 16:48:10 +0200144 INIT_LIST_HEAD(&dev_data->alias_list);
145
Joerg Roedelf62dda62011-06-09 12:55:35 +0200146 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200147
148 spin_lock_irqsave(&dev_data_list_lock, flags);
149 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
150 spin_unlock_irqrestore(&dev_data_list_lock, flags);
151
152 return dev_data;
153}
154
155static void free_dev_data(struct iommu_dev_data *dev_data)
156{
157 unsigned long flags;
158
159 spin_lock_irqsave(&dev_data_list_lock, flags);
160 list_del(&dev_data->dev_data_list);
161 spin_unlock_irqrestore(&dev_data_list_lock, flags);
162
163 kfree(dev_data);
164}
165
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200166static struct iommu_dev_data *search_dev_data(u16 devid)
167{
168 struct iommu_dev_data *dev_data;
169 unsigned long flags;
170
171 spin_lock_irqsave(&dev_data_list_lock, flags);
172 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
173 if (dev_data->devid == devid)
174 goto out_unlock;
175 }
176
177 dev_data = NULL;
178
179out_unlock:
180 spin_unlock_irqrestore(&dev_data_list_lock, flags);
181
182 return dev_data;
183}
184
185static struct iommu_dev_data *find_dev_data(u16 devid)
186{
187 struct iommu_dev_data *dev_data;
188
189 dev_data = search_dev_data(devid);
190
191 if (dev_data == NULL)
192 dev_data = alloc_dev_data(devid);
193
194 return dev_data;
195}
196
Joerg Roedel15898bb2009-11-24 15:39:42 +0100197static inline u16 get_device_id(struct device *dev)
198{
199 struct pci_dev *pdev = to_pci_dev(dev);
200
Shuah Khan6f2729b2013-02-27 17:07:30 -0700201 return PCI_DEVID(pdev->bus->number, pdev->devfn);
Joerg Roedel15898bb2009-11-24 15:39:42 +0100202}
203
Joerg Roedel657cbb62009-11-23 15:26:46 +0100204static struct iommu_dev_data *get_dev_data(struct device *dev)
205{
206 return dev->archdata.iommu;
207}
208
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100209static bool pci_iommuv2_capable(struct pci_dev *pdev)
210{
211 static const int caps[] = {
212 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100213 PCI_EXT_CAP_ID_PRI,
214 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100215 };
216 int i, pos;
217
218 for (i = 0; i < 3; ++i) {
219 pos = pci_find_ext_capability(pdev, caps[i]);
220 if (pos == 0)
221 return false;
222 }
223
224 return true;
225}
226
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100227static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
228{
229 struct iommu_dev_data *dev_data;
230
231 dev_data = get_dev_data(&pdev->dev);
232
233 return dev_data->errata & (1 << erratum) ? true : false;
234}
235
Joerg Roedel71c70982009-11-24 16:43:06 +0100236/*
237 * In this function the list of preallocated protection domains is traversed to
238 * find the domain for a specific device
239 */
240static struct dma_ops_domain *find_protection_domain(u16 devid)
241{
242 struct dma_ops_domain *entry, *ret = NULL;
243 unsigned long flags;
244 u16 alias = amd_iommu_alias_table[devid];
245
246 if (list_empty(&iommu_pd_list))
247 return NULL;
248
249 spin_lock_irqsave(&iommu_pd_list_lock, flags);
250
251 list_for_each_entry(entry, &iommu_pd_list, list) {
252 if (entry->target_dev == devid ||
253 entry->target_dev == alias) {
254 ret = entry;
255 break;
256 }
257 }
258
259 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
260
261 return ret;
262}
263
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100264/*
265 * This function checks if the driver got a valid device from the caller to
266 * avoid dereferencing invalid pointers.
267 */
268static bool check_device(struct device *dev)
269{
270 u16 devid;
271
272 if (!dev || !dev->dma_mask)
273 return false;
274
Yijing Wangb82a2272013-12-05 19:42:41 +0800275 /* No PCI device */
276 if (!dev_is_pci(dev))
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100277 return false;
278
279 devid = get_device_id(dev);
280
281 /* Out of our scope? */
282 if (devid > amd_iommu_last_bdf)
283 return false;
284
285 if (amd_iommu_rlookup_table[devid] == NULL)
286 return false;
287
288 return true;
289}
290
Alex Williamson25b11ce2014-09-19 10:03:13 -0600291static void init_iommu_group(struct device *dev)
Alex Williamson2851db22012-10-08 22:49:41 -0600292{
Alex Williamson2851db22012-10-08 22:49:41 -0600293 struct iommu_group *group;
Alex Williamson2851db22012-10-08 22:49:41 -0600294
Alex Williamson65d53522014-07-03 09:51:30 -0600295 group = iommu_group_get_for_dev(dev);
Alex Williamson25b11ce2014-09-19 10:03:13 -0600296 if (!IS_ERR(group))
297 iommu_group_put(group);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600298}
299
Alex Williamsonc1931092014-07-03 09:51:24 -0600300static int __last_alias(struct pci_dev *pdev, u16 alias, void *data)
301{
302 *(u16 *)data = alias;
303 return 0;
304}
305
306static u16 get_alias(struct device *dev)
307{
308 struct pci_dev *pdev = to_pci_dev(dev);
309 u16 devid, ivrs_alias, pci_alias;
310
311 devid = get_device_id(dev);
312 ivrs_alias = amd_iommu_alias_table[devid];
313 pci_for_each_dma_alias(pdev, __last_alias, &pci_alias);
314
315 if (ivrs_alias == pci_alias)
316 return ivrs_alias;
317
318 /*
319 * DMA alias showdown
320 *
321 * The IVRS is fairly reliable in telling us about aliases, but it
322 * can't know about every screwy device. If we don't have an IVRS
323 * reported alias, use the PCI reported alias. In that case we may
324 * still need to initialize the rlookup and dev_table entries if the
325 * alias is to a non-existent device.
326 */
327 if (ivrs_alias == devid) {
328 if (!amd_iommu_rlookup_table[pci_alias]) {
329 amd_iommu_rlookup_table[pci_alias] =
330 amd_iommu_rlookup_table[devid];
331 memcpy(amd_iommu_dev_table[pci_alias].data,
332 amd_iommu_dev_table[devid].data,
333 sizeof(amd_iommu_dev_table[pci_alias].data));
334 }
335
336 return pci_alias;
337 }
338
339 pr_info("AMD-Vi: Using IVRS reported alias %02x:%02x.%d "
340 "for device %s[%04x:%04x], kernel reported alias "
341 "%02x:%02x.%d\n", PCI_BUS_NUM(ivrs_alias), PCI_SLOT(ivrs_alias),
342 PCI_FUNC(ivrs_alias), dev_name(dev), pdev->vendor, pdev->device,
343 PCI_BUS_NUM(pci_alias), PCI_SLOT(pci_alias),
344 PCI_FUNC(pci_alias));
345
346 /*
347 * If we don't have a PCI DMA alias and the IVRS alias is on the same
348 * bus, then the IVRS table may know about a quirk that we don't.
349 */
350 if (pci_alias == devid &&
351 PCI_BUS_NUM(ivrs_alias) == pdev->bus->number) {
352 pdev->dev_flags |= PCI_DEV_FLAGS_DMA_ALIAS_DEVFN;
353 pdev->dma_alias_devfn = ivrs_alias & 0xff;
354 pr_info("AMD-Vi: Added PCI DMA alias %02x.%d for %s\n",
355 PCI_SLOT(ivrs_alias), PCI_FUNC(ivrs_alias),
356 dev_name(dev));
357 }
358
359 return ivrs_alias;
360}
361
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600362static int iommu_init_device(struct device *dev)
363{
364 struct pci_dev *pdev = to_pci_dev(dev);
365 struct iommu_dev_data *dev_data;
366 u16 alias;
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600367
368 if (dev->archdata.iommu)
369 return 0;
370
371 dev_data = find_dev_data(get_device_id(dev));
372 if (!dev_data)
373 return -ENOMEM;
374
Alex Williamsonc1931092014-07-03 09:51:24 -0600375 alias = get_alias(dev);
376
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600377 if (alias != dev_data->devid) {
378 struct iommu_dev_data *alias_data;
379
380 alias_data = find_dev_data(alias);
381 if (alias_data == NULL) {
382 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
383 dev_name(dev));
384 free_dev_data(dev_data);
385 return -ENOTSUPP;
386 }
387 dev_data->alias_data = alias_data;
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600388
Joerg Roedelf251e182014-08-05 16:48:10 +0200389 /* Add device to the alias_list */
390 list_add(&dev_data->alias_list, &alias_data->alias_list);
Radmila Kompováe644a012013-05-02 17:24:25 +0200391 }
Alex Williamson9dcd6132012-05-30 14:19:07 -0600392
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100393 if (pci_iommuv2_capable(pdev)) {
394 struct amd_iommu *iommu;
395
396 iommu = amd_iommu_rlookup_table[dev_data->devid];
397 dev_data->iommu_v2 = iommu->is_iommu_v2;
398 }
399
Joerg Roedel657cbb62009-11-23 15:26:46 +0100400 dev->archdata.iommu = dev_data;
401
Alex Williamson066f2e92014-06-12 16:12:37 -0600402 iommu_device_link(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
403 dev);
404
Joerg Roedel657cbb62009-11-23 15:26:46 +0100405 return 0;
406}
407
Joerg Roedel26018872011-06-06 16:50:14 +0200408static void iommu_ignore_device(struct device *dev)
409{
410 u16 devid, alias;
411
412 devid = get_device_id(dev);
413 alias = amd_iommu_alias_table[devid];
414
415 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
416 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
417
418 amd_iommu_rlookup_table[devid] = NULL;
419 amd_iommu_rlookup_table[alias] = NULL;
420}
421
Joerg Roedel657cbb62009-11-23 15:26:46 +0100422static void iommu_uninit_device(struct device *dev)
423{
Alex Williamsonc1931092014-07-03 09:51:24 -0600424 struct iommu_dev_data *dev_data = search_dev_data(get_device_id(dev));
425
426 if (!dev_data)
427 return;
428
Alex Williamson066f2e92014-06-12 16:12:37 -0600429 iommu_device_unlink(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
430 dev);
431
Alex Williamson9dcd6132012-05-30 14:19:07 -0600432 iommu_group_remove_device(dev);
433
Alex Williamsonc1931092014-07-03 09:51:24 -0600434 /* Unlink from alias, it may change if another device is re-plugged */
435 dev_data->alias_data = NULL;
436
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200437 /* Remove dma-ops */
438 dev->archdata.dma_ops = NULL;
439
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200440 /*
Alex Williamsonc1931092014-07-03 09:51:24 -0600441 * We keep dev_data around for unplugged devices and reuse it when the
442 * device is re-plugged - not doing so would introduce a ton of races.
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200443 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100444}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100445
Joerg Roedel7f265082008-12-12 13:50:21 +0100446#ifdef CONFIG_AMD_IOMMU_STATS
447
448/*
449 * Initialization code for statistics collection
450 */
451
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100452DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100453DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100454DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100455DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100456DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100457DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100458DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100459DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100460DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100461DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100462DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100463DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100464DECLARE_STATS_COUNTER(complete_ppr);
465DECLARE_STATS_COUNTER(invalidate_iotlb);
466DECLARE_STATS_COUNTER(invalidate_iotlb_all);
467DECLARE_STATS_COUNTER(pri_requests);
468
Joerg Roedel7f265082008-12-12 13:50:21 +0100469static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100470static struct dentry *de_fflush;
471
472static void amd_iommu_stats_add(struct __iommu_counter *cnt)
473{
474 if (stats_dir == NULL)
475 return;
476
477 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
478 &cnt->value);
479}
480
481static void amd_iommu_stats_init(void)
482{
483 stats_dir = debugfs_create_dir("amd-iommu", NULL);
484 if (stats_dir == NULL)
485 return;
486
Joerg Roedel7f265082008-12-12 13:50:21 +0100487 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
Dan Carpenter3775d482012-06-27 12:09:18 +0300488 &amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100489
490 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100491 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100492 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100493 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100494 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100495 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100496 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100497 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100498 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100499 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100500 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100501 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100502 amd_iommu_stats_add(&complete_ppr);
503 amd_iommu_stats_add(&invalidate_iotlb);
504 amd_iommu_stats_add(&invalidate_iotlb_all);
505 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100506}
507
508#endif
509
Joerg Roedel431b2a22008-07-11 17:14:22 +0200510/****************************************************************************
511 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200512 * Interrupt handling functions
513 *
514 ****************************************************************************/
515
Joerg Roedele3e59872009-09-03 14:02:10 +0200516static void dump_dte_entry(u16 devid)
517{
518 int i;
519
Joerg Roedelee6c2862011-11-09 12:06:03 +0100520 for (i = 0; i < 4; ++i)
521 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200522 amd_iommu_dev_table[devid].data[i]);
523}
524
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200525static void dump_command(unsigned long phys_addr)
526{
527 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
528 int i;
529
530 for (i = 0; i < 4; ++i)
531 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
532}
533
Joerg Roedela345b232009-09-03 15:01:43 +0200534static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200535{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200536 int type, devid, domid, flags;
537 volatile u32 *event = __evt;
538 int count = 0;
539 u64 address;
540
541retry:
542 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
543 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
544 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
545 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
546 address = (u64)(((u64)event[3]) << 32) | event[2];
547
548 if (type == 0) {
549 /* Did we hit the erratum? */
550 if (++count == LOOP_TIMEOUT) {
551 pr_err("AMD-Vi: No event written to event log\n");
552 return;
553 }
554 udelay(1);
555 goto retry;
556 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200557
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200558 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200559
560 switch (type) {
561 case EVENT_TYPE_ILL_DEV:
562 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
563 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700564 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200565 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200566 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200567 break;
568 case EVENT_TYPE_IO_FAULT:
569 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
570 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700571 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200572 domid, address, flags);
573 break;
574 case EVENT_TYPE_DEV_TAB_ERR:
575 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
576 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700577 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200578 address, flags);
579 break;
580 case EVENT_TYPE_PAGE_TAB_ERR:
581 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
582 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700583 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200584 domid, address, flags);
585 break;
586 case EVENT_TYPE_ILL_CMD:
587 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200588 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200589 break;
590 case EVENT_TYPE_CMD_HARD_ERR:
591 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
592 "flags=0x%04x]\n", address, flags);
593 break;
594 case EVENT_TYPE_IOTLB_INV_TO:
595 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
596 "address=0x%016llx]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700597 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200598 address);
599 break;
600 case EVENT_TYPE_INV_DEV_REQ:
601 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
602 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700603 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200604 address, flags);
605 break;
606 default:
607 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
608 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200609
610 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200611}
612
613static void iommu_poll_events(struct amd_iommu *iommu)
614{
615 u32 head, tail;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200616
617 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
618 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
619
620 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200621 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200622 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
623 }
624
625 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200626}
627
Joerg Roedeleee53532012-06-01 15:20:23 +0200628static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100629{
630 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100631
Joerg Roedel399be2f2011-12-01 16:53:47 +0100632 INC_STATS_COUNTER(pri_requests);
633
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100634 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
635 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
636 return;
637 }
638
639 fault.address = raw[1];
640 fault.pasid = PPR_PASID(raw[0]);
641 fault.device_id = PPR_DEVID(raw[0]);
642 fault.tag = PPR_TAG(raw[0]);
643 fault.flags = PPR_FLAGS(raw[0]);
644
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100645 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
646}
647
648static void iommu_poll_ppr_log(struct amd_iommu *iommu)
649{
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100650 u32 head, tail;
651
652 if (iommu->ppr_log == NULL)
653 return;
654
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100655 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
656 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
657
658 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200659 volatile u64 *raw;
660 u64 entry[2];
661 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100662
Joerg Roedeleee53532012-06-01 15:20:23 +0200663 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100664
Joerg Roedeleee53532012-06-01 15:20:23 +0200665 /*
666 * Hardware bug: Interrupt may arrive before the entry is
667 * written to memory. If this happens we need to wait for the
668 * entry to arrive.
669 */
670 for (i = 0; i < LOOP_TIMEOUT; ++i) {
671 if (PPR_REQ_TYPE(raw[0]) != 0)
672 break;
673 udelay(1);
674 }
675
676 /* Avoid memcpy function-call overhead */
677 entry[0] = raw[0];
678 entry[1] = raw[1];
679
680 /*
681 * To detect the hardware bug we need to clear the entry
682 * back to zero.
683 */
684 raw[0] = raw[1] = 0UL;
685
686 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100687 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
688 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200689
Joerg Roedeleee53532012-06-01 15:20:23 +0200690 /* Handle PPR entry */
691 iommu_handle_ppr_entry(iommu, entry);
692
Joerg Roedeleee53532012-06-01 15:20:23 +0200693 /* Refresh ring-buffer information */
694 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100695 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
696 }
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100697}
698
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200699irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200700{
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500701 struct amd_iommu *iommu = (struct amd_iommu *) data;
702 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200703
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500704 while (status & (MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK)) {
705 /* Enable EVT and PPR interrupts again */
706 writel((MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK),
707 iommu->mmio_base + MMIO_STATUS_OFFSET);
708
709 if (status & MMIO_STATUS_EVT_INT_MASK) {
710 pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
711 iommu_poll_events(iommu);
712 }
713
714 if (status & MMIO_STATUS_PPR_INT_MASK) {
715 pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
716 iommu_poll_ppr_log(iommu);
717 }
718
719 /*
720 * Hardware bug: ERBT1312
721 * When re-enabling interrupt (by writing 1
722 * to clear the bit), the hardware might also try to set
723 * the interrupt bit in the event status register.
724 * In this scenario, the bit will be set, and disable
725 * subsequent interrupts.
726 *
727 * Workaround: The IOMMU driver should read back the
728 * status register and check if the interrupt bits are cleared.
729 * If not, driver will need to go through the interrupt handler
730 * again and re-clear the bits
731 */
732 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100733 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200734 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200735}
736
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200737irqreturn_t amd_iommu_int_handler(int irq, void *data)
738{
739 return IRQ_WAKE_THREAD;
740}
741
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200742/****************************************************************************
743 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200744 * IOMMU command queuing functions
745 *
746 ****************************************************************************/
747
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200748static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200749{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200750 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200751
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200752 while (*sem == 0 && i < LOOP_TIMEOUT) {
753 udelay(1);
754 i += 1;
755 }
756
757 if (i == LOOP_TIMEOUT) {
758 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
759 return -EIO;
760 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200761
762 return 0;
763}
764
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200765static void copy_cmd_to_buffer(struct amd_iommu *iommu,
766 struct iommu_cmd *cmd,
767 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200768{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200769 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200770
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200771 target = iommu->cmd_buf + tail;
772 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200773
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200774 /* Copy command to buffer */
775 memcpy(target, cmd, sizeof(*cmd));
776
777 /* Tell the IOMMU about it */
778 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
779}
780
Joerg Roedel815b33f2011-04-06 17:26:49 +0200781static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200782{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200783 WARN_ON(address & 0x7ULL);
784
Joerg Roedelded46732011-04-06 10:53:48 +0200785 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200786 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
787 cmd->data[1] = upper_32_bits(__pa(address));
788 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200789 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
790}
791
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200792static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
793{
794 memset(cmd, 0, sizeof(*cmd));
795 cmd->data[0] = devid;
796 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
797}
798
Joerg Roedel11b64022011-04-06 11:49:28 +0200799static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
800 size_t size, u16 domid, int pde)
801{
802 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100803 bool s;
Joerg Roedel11b64022011-04-06 11:49:28 +0200804
805 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100806 s = false;
Joerg Roedel11b64022011-04-06 11:49:28 +0200807
808 if (pages > 1) {
809 /*
810 * If we have to flush more than one page, flush all
811 * TLB entries for this domain
812 */
813 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100814 s = true;
Joerg Roedel11b64022011-04-06 11:49:28 +0200815 }
816
817 address &= PAGE_MASK;
818
819 memset(cmd, 0, sizeof(*cmd));
820 cmd->data[1] |= domid;
821 cmd->data[2] = lower_32_bits(address);
822 cmd->data[3] = upper_32_bits(address);
823 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
824 if (s) /* size bit - we flush more than one 4kb page */
825 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200826 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200827 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
828}
829
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200830static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
831 u64 address, size_t size)
832{
833 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100834 bool s;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200835
836 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100837 s = false;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200838
839 if (pages > 1) {
840 /*
841 * If we have to flush more than one page, flush all
842 * TLB entries for this domain
843 */
844 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100845 s = true;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200846 }
847
848 address &= PAGE_MASK;
849
850 memset(cmd, 0, sizeof(*cmd));
851 cmd->data[0] = devid;
852 cmd->data[0] |= (qdep & 0xff) << 24;
853 cmd->data[1] = devid;
854 cmd->data[2] = lower_32_bits(address);
855 cmd->data[3] = upper_32_bits(address);
856 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
857 if (s)
858 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
859}
860
Joerg Roedel22e266c2011-11-21 15:59:08 +0100861static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
862 u64 address, bool size)
863{
864 memset(cmd, 0, sizeof(*cmd));
865
866 address &= ~(0xfffULL);
867
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600868 cmd->data[0] = pasid;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100869 cmd->data[1] = domid;
870 cmd->data[2] = lower_32_bits(address);
871 cmd->data[3] = upper_32_bits(address);
872 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
873 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
874 if (size)
875 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
876 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
877}
878
879static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
880 int qdep, u64 address, bool size)
881{
882 memset(cmd, 0, sizeof(*cmd));
883
884 address &= ~(0xfffULL);
885
886 cmd->data[0] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600887 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100888 cmd->data[0] |= (qdep & 0xff) << 24;
889 cmd->data[1] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600890 cmd->data[1] |= (pasid & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100891 cmd->data[2] = lower_32_bits(address);
892 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
893 cmd->data[3] = upper_32_bits(address);
894 if (size)
895 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
896 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
897}
898
Joerg Roedelc99afa22011-11-21 18:19:25 +0100899static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
900 int status, int tag, bool gn)
901{
902 memset(cmd, 0, sizeof(*cmd));
903
904 cmd->data[0] = devid;
905 if (gn) {
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600906 cmd->data[1] = pasid;
Joerg Roedelc99afa22011-11-21 18:19:25 +0100907 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
908 }
909 cmd->data[3] = tag & 0x1ff;
910 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
911
912 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
913}
914
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200915static void build_inv_all(struct iommu_cmd *cmd)
916{
917 memset(cmd, 0, sizeof(*cmd));
918 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200919}
920
Joerg Roedel7ef27982012-06-21 16:46:04 +0200921static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
922{
923 memset(cmd, 0, sizeof(*cmd));
924 cmd->data[0] = devid;
925 CMD_SET_TYPE(cmd, CMD_INV_IRT);
926}
927
Joerg Roedel431b2a22008-07-11 17:14:22 +0200928/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200929 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200930 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200931 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200932static int iommu_queue_command_sync(struct amd_iommu *iommu,
933 struct iommu_cmd *cmd,
934 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200935{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200936 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200937 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200938
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200939 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100940
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200941again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200942 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200943
944 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
945 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
946 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
947 left = (head - next_tail) % iommu->cmd_buf_size;
948
949 if (left <= 2) {
950 struct iommu_cmd sync_cmd;
951 volatile u64 sem = 0;
952 int ret;
953
954 build_completion_wait(&sync_cmd, (u64)&sem);
955 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
956
957 spin_unlock_irqrestore(&iommu->lock, flags);
958
959 if ((ret = wait_on_sem(&sem)) != 0)
960 return ret;
961
962 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200963 }
964
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200965 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200966
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200967 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200968 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200969
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200970 spin_unlock_irqrestore(&iommu->lock, flags);
971
Joerg Roedel815b33f2011-04-06 17:26:49 +0200972 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100973}
974
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200975static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
976{
977 return iommu_queue_command_sync(iommu, cmd, true);
978}
979
Joerg Roedel8d201962008-12-02 20:34:41 +0100980/*
981 * This function queues a completion wait command into the command
982 * buffer of an IOMMU
983 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100984static int iommu_completion_wait(struct amd_iommu *iommu)
985{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200986 struct iommu_cmd cmd;
987 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200988 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100989
990 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200991 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100992
Joerg Roedel815b33f2011-04-06 17:26:49 +0200993 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100994
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200995 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +0100996 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200997 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100998
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200999 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001000}
1001
Joerg Roedeld8c13082011-04-06 18:51:26 +02001002static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001003{
1004 struct iommu_cmd cmd;
1005
Joerg Roedeld8c13082011-04-06 18:51:26 +02001006 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001007
Joerg Roedeld8c13082011-04-06 18:51:26 +02001008 return iommu_queue_command(iommu, &cmd);
1009}
1010
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001011static void iommu_flush_dte_all(struct amd_iommu *iommu)
1012{
1013 u32 devid;
1014
1015 for (devid = 0; devid <= 0xffff; ++devid)
1016 iommu_flush_dte(iommu, devid);
1017
1018 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001019}
1020
1021/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001022 * This function uses heavy locking and may disable irqs for some time. But
1023 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001024 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001025static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001026{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001027 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001028
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001029 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1030 struct iommu_cmd cmd;
1031 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1032 dom_id, 1);
1033 iommu_queue_command(iommu, &cmd);
1034 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001035
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001036 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001037}
1038
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001039static void iommu_flush_all(struct amd_iommu *iommu)
1040{
1041 struct iommu_cmd cmd;
1042
1043 build_inv_all(&cmd);
1044
1045 iommu_queue_command(iommu, &cmd);
1046 iommu_completion_wait(iommu);
1047}
1048
Joerg Roedel7ef27982012-06-21 16:46:04 +02001049static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1050{
1051 struct iommu_cmd cmd;
1052
1053 build_inv_irt(&cmd, devid);
1054
1055 iommu_queue_command(iommu, &cmd);
1056}
1057
1058static void iommu_flush_irt_all(struct amd_iommu *iommu)
1059{
1060 u32 devid;
1061
1062 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1063 iommu_flush_irt(iommu, devid);
1064
1065 iommu_completion_wait(iommu);
1066}
1067
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001068void iommu_flush_all_caches(struct amd_iommu *iommu)
1069{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001070 if (iommu_feature(iommu, FEATURE_IA)) {
1071 iommu_flush_all(iommu);
1072 } else {
1073 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001074 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001075 iommu_flush_tlb_all(iommu);
1076 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001077}
1078
Joerg Roedel431b2a22008-07-11 17:14:22 +02001079/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001080 * Command send function for flushing on-device TLB
1081 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001082static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1083 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001084{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001085 struct amd_iommu *iommu;
1086 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001087 int qdep;
1088
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001089 qdep = dev_data->ats.qdep;
1090 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001091
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001092 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001093
1094 return iommu_queue_command(iommu, &cmd);
1095}
1096
1097/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001098 * Command send function for invalidating a device table entry
1099 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001100static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001101{
1102 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001103 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001104
Joerg Roedel6c542042011-06-09 17:07:31 +02001105 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001106
Joerg Roedelf62dda62011-06-09 12:55:35 +02001107 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001108 if (ret)
1109 return ret;
1110
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001111 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001112 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001113
1114 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001115}
1116
Joerg Roedel431b2a22008-07-11 17:14:22 +02001117/*
1118 * TLB invalidation function which is called from the mapping functions.
1119 * It invalidates a single PTE if the range to flush is within a single
1120 * page. Otherwise it flushes the whole TLB of the IOMMU.
1121 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001122static void __domain_flush_pages(struct protection_domain *domain,
1123 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001124{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001125 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001126 struct iommu_cmd cmd;
1127 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001128
Joerg Roedel11b64022011-04-06 11:49:28 +02001129 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001130
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001131 for (i = 0; i < amd_iommus_present; ++i) {
1132 if (!domain->dev_iommu[i])
1133 continue;
1134
1135 /*
1136 * Devices of this domain are behind this IOMMU
1137 * We need a TLB flush
1138 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001139 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001140 }
1141
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001142 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001143
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001144 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001145 continue;
1146
Joerg Roedel6c542042011-06-09 17:07:31 +02001147 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001148 }
1149
Joerg Roedel11b64022011-04-06 11:49:28 +02001150 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001151}
1152
Joerg Roedel17b124b2011-04-06 18:01:35 +02001153static void domain_flush_pages(struct protection_domain *domain,
1154 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001155{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001156 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001157}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001158
Joerg Roedel1c655772008-09-04 18:40:05 +02001159/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001160static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001161{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001162 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001163}
1164
Chris Wright42a49f92009-06-15 15:42:00 +02001165/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001166static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001167{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001168 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1169}
1170
1171static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001172{
1173 int i;
1174
1175 for (i = 0; i < amd_iommus_present; ++i) {
1176 if (!domain->dev_iommu[i])
1177 continue;
1178
1179 /*
1180 * Devices of this domain are behind this IOMMU
1181 * We need to wait for completion of all commands.
1182 */
1183 iommu_completion_wait(amd_iommus[i]);
1184 }
1185}
1186
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001187
Joerg Roedel43f49602008-12-02 21:01:12 +01001188/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001189 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001190 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001191static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001192{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001193 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001194
1195 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001196 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001197}
1198
Joerg Roedel431b2a22008-07-11 17:14:22 +02001199/****************************************************************************
1200 *
1201 * The functions below are used the create the page table mappings for
1202 * unity mapped regions.
1203 *
1204 ****************************************************************************/
1205
1206/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001207 * This function is used to add another level to an IO page table. Adding
1208 * another level increases the size of the address space by 9 bits to a size up
1209 * to 64 bits.
1210 */
1211static bool increase_address_space(struct protection_domain *domain,
1212 gfp_t gfp)
1213{
1214 u64 *pte;
1215
1216 if (domain->mode == PAGE_MODE_6_LEVEL)
1217 /* address space already 64 bit large */
1218 return false;
1219
1220 pte = (void *)get_zeroed_page(gfp);
1221 if (!pte)
1222 return false;
1223
1224 *pte = PM_LEVEL_PDE(domain->mode,
1225 virt_to_phys(domain->pt_root));
1226 domain->pt_root = pte;
1227 domain->mode += 1;
1228 domain->updated = true;
1229
1230 return true;
1231}
1232
1233static u64 *alloc_pte(struct protection_domain *domain,
1234 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001235 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001236 u64 **pte_page,
1237 gfp_t gfp)
1238{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001239 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001240 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001241
1242 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001243
1244 while (address > PM_LEVEL_SIZE(domain->mode))
1245 increase_address_space(domain, gfp);
1246
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001247 level = domain->mode - 1;
1248 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1249 address = PAGE_SIZE_ALIGN(address, page_size);
1250 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001251
1252 while (level > end_lvl) {
1253 if (!IOMMU_PTE_PRESENT(*pte)) {
1254 page = (u64 *)get_zeroed_page(gfp);
1255 if (!page)
1256 return NULL;
1257 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1258 }
1259
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001260 /* No level skipping support yet */
1261 if (PM_PTE_LEVEL(*pte) != level)
1262 return NULL;
1263
Joerg Roedel308973d2009-11-24 17:43:32 +01001264 level -= 1;
1265
1266 pte = IOMMU_PTE_PAGE(*pte);
1267
1268 if (pte_page && level == end_lvl)
1269 *pte_page = pte;
1270
1271 pte = &pte[PM_LEVEL_INDEX(level, address)];
1272 }
1273
1274 return pte;
1275}
1276
1277/*
1278 * This function checks if there is a PTE for a given dma address. If
1279 * there is one, it returns the pointer to it.
1280 */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001281static u64 *fetch_pte(struct protection_domain *domain,
1282 unsigned long address,
1283 unsigned long *page_size)
Joerg Roedel308973d2009-11-24 17:43:32 +01001284{
1285 int level;
1286 u64 *pte;
1287
Joerg Roedel24cd7722010-01-19 17:27:39 +01001288 if (address > PM_LEVEL_SIZE(domain->mode))
1289 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001290
Joerg Roedel3039ca12015-04-01 14:58:48 +02001291 level = domain->mode - 1;
1292 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1293 *page_size = PTE_LEVEL_PAGE_SIZE(level);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001294
1295 while (level > 0) {
1296
1297 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001298 if (!IOMMU_PTE_PRESENT(*pte))
1299 return NULL;
1300
Joerg Roedel24cd7722010-01-19 17:27:39 +01001301 /* Large PTE */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001302 if (PM_PTE_LEVEL(*pte) == 7 ||
1303 PM_PTE_LEVEL(*pte) == 0)
1304 break;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001305
1306 /* No level skipping support yet */
1307 if (PM_PTE_LEVEL(*pte) != level)
1308 return NULL;
1309
Joerg Roedel308973d2009-11-24 17:43:32 +01001310 level -= 1;
1311
Joerg Roedel24cd7722010-01-19 17:27:39 +01001312 /* Walk to the next level */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001313 pte = IOMMU_PTE_PAGE(*pte);
1314 pte = &pte[PM_LEVEL_INDEX(level, address)];
1315 *page_size = PTE_LEVEL_PAGE_SIZE(level);
1316 }
1317
1318 if (PM_PTE_LEVEL(*pte) == 0x07) {
1319 unsigned long pte_mask;
1320
1321 /*
1322 * If we have a series of large PTEs, make
1323 * sure to return a pointer to the first one.
1324 */
1325 *page_size = pte_mask = PTE_PAGE_SIZE(*pte);
1326 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1327 pte = (u64 *)(((unsigned long)pte) & pte_mask);
Joerg Roedel308973d2009-11-24 17:43:32 +01001328 }
1329
1330 return pte;
1331}
1332
1333/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001334 * Generic mapping functions. It maps a physical address into a DMA
1335 * address space. It allocates the page table pages if necessary.
1336 * In the future it can be extended to a generic mapping function
1337 * supporting all features of AMD IOMMU page tables like level skipping
1338 * and full 64 bit address spaces.
1339 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001340static int iommu_map_page(struct protection_domain *dom,
1341 unsigned long bus_addr,
1342 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001343 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001344 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001345{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001346 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001347 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001348
Joerg Roedeld4b03662015-04-01 14:58:52 +02001349 BUG_ON(!IS_ALIGNED(bus_addr, page_size));
1350 BUG_ON(!IS_ALIGNED(phys_addr, page_size));
1351
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001352 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001353 return -EINVAL;
1354
Joerg Roedeld4b03662015-04-01 14:58:52 +02001355 count = PAGE_SIZE_PTE_COUNT(page_size);
1356 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001357
Maurizio Lombardi63eaa752014-09-11 12:28:03 +02001358 if (!pte)
1359 return -ENOMEM;
1360
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001361 for (i = 0; i < count; ++i)
1362 if (IOMMU_PTE_PRESENT(pte[i]))
1363 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001364
Joerg Roedeld4b03662015-04-01 14:58:52 +02001365 if (count > 1) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001366 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1367 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1368 } else
1369 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1370
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001371 if (prot & IOMMU_PROT_IR)
1372 __pte |= IOMMU_PTE_IR;
1373 if (prot & IOMMU_PROT_IW)
1374 __pte |= IOMMU_PTE_IW;
1375
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001376 for (i = 0; i < count; ++i)
1377 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001378
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001379 update_domain(dom);
1380
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001381 return 0;
1382}
1383
Joerg Roedel24cd7722010-01-19 17:27:39 +01001384static unsigned long iommu_unmap_page(struct protection_domain *dom,
1385 unsigned long bus_addr,
1386 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001387{
Joerg Roedel71b390e2015-04-01 14:58:49 +02001388 unsigned long long unmapped;
1389 unsigned long unmap_size;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001390 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001391
Joerg Roedel24cd7722010-01-19 17:27:39 +01001392 BUG_ON(!is_power_of_2(page_size));
1393
1394 unmapped = 0;
1395
1396 while (unmapped < page_size) {
1397
Joerg Roedel71b390e2015-04-01 14:58:49 +02001398 pte = fetch_pte(dom, bus_addr, &unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001399
Joerg Roedel71b390e2015-04-01 14:58:49 +02001400 if (pte) {
1401 int i, count;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001402
Joerg Roedel71b390e2015-04-01 14:58:49 +02001403 count = PAGE_SIZE_PTE_COUNT(unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001404 for (i = 0; i < count; i++)
1405 pte[i] = 0ULL;
1406 }
1407
1408 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1409 unmapped += unmap_size;
1410 }
1411
Alex Williamson60d0ca32013-06-21 14:33:19 -06001412 BUG_ON(unmapped && !is_power_of_2(unmapped));
Joerg Roedel24cd7722010-01-19 17:27:39 +01001413
1414 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001415}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001416
Joerg Roedel431b2a22008-07-11 17:14:22 +02001417/*
1418 * This function checks if a specific unity mapping entry is needed for
1419 * this specific IOMMU.
1420 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001421static int iommu_for_unity_map(struct amd_iommu *iommu,
1422 struct unity_map_entry *entry)
1423{
1424 u16 bdf, i;
1425
1426 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1427 bdf = amd_iommu_alias_table[i];
1428 if (amd_iommu_rlookup_table[bdf] == iommu)
1429 return 1;
1430 }
1431
1432 return 0;
1433}
1434
Joerg Roedel431b2a22008-07-11 17:14:22 +02001435/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001436 * This function actually applies the mapping to the page table of the
1437 * dma_ops domain.
1438 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001439static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1440 struct unity_map_entry *e)
1441{
1442 u64 addr;
1443 int ret;
1444
1445 for (addr = e->address_start; addr < e->address_end;
1446 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001447 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001448 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001449 if (ret)
1450 return ret;
1451 /*
1452 * if unity mapping is in aperture range mark the page
1453 * as allocated in the aperture
1454 */
1455 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001456 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001457 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001458 }
1459
1460 return 0;
1461}
1462
Joerg Roedel431b2a22008-07-11 17:14:22 +02001463/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001464 * Init the unity mappings for a specific IOMMU in the system
1465 *
1466 * Basically iterates over all unity mapping entries and applies them to
1467 * the default domain DMA of that IOMMU if necessary.
1468 */
1469static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1470{
1471 struct unity_map_entry *entry;
1472 int ret;
1473
1474 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1475 if (!iommu_for_unity_map(iommu, entry))
1476 continue;
1477 ret = dma_ops_unity_map(iommu->default_dom, entry);
1478 if (ret)
1479 return ret;
1480 }
1481
1482 return 0;
1483}
1484
1485/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001486 * Inits the unity mappings required for a specific device
1487 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001488static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1489 u16 devid)
1490{
1491 struct unity_map_entry *e;
1492 int ret;
1493
1494 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1495 if (!(devid >= e->devid_start && devid <= e->devid_end))
1496 continue;
1497 ret = dma_ops_unity_map(dma_dom, e);
1498 if (ret)
1499 return ret;
1500 }
1501
1502 return 0;
1503}
1504
Joerg Roedel431b2a22008-07-11 17:14:22 +02001505/****************************************************************************
1506 *
1507 * The next functions belong to the address allocator for the dma_ops
1508 * interface functions. They work like the allocators in the other IOMMU
1509 * drivers. Its basically a bitmap which marks the allocated pages in
1510 * the aperture. Maybe it could be enhanced in the future to a more
1511 * efficient allocator.
1512 *
1513 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001514
Joerg Roedel431b2a22008-07-11 17:14:22 +02001515/*
Joerg Roedel384de722009-05-15 12:30:05 +02001516 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001517 *
1518 * called with domain->lock held
1519 */
Joerg Roedel384de722009-05-15 12:30:05 +02001520
Joerg Roedel9cabe892009-05-18 16:38:55 +02001521/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001522 * Used to reserve address ranges in the aperture (e.g. for exclusion
1523 * ranges.
1524 */
1525static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1526 unsigned long start_page,
1527 unsigned int pages)
1528{
1529 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1530
1531 if (start_page + pages > last_page)
1532 pages = last_page - start_page;
1533
1534 for (i = start_page; i < start_page + pages; ++i) {
1535 int index = i / APERTURE_RANGE_PAGES;
1536 int page = i % APERTURE_RANGE_PAGES;
1537 __set_bit(page, dom->aperture[index]->bitmap);
1538 }
1539}
1540
1541/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001542 * This function is used to add a new aperture range to an existing
1543 * aperture in case of dma_ops domain allocation or address allocation
1544 * failure.
1545 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001546static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001547 bool populate, gfp_t gfp)
1548{
1549 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001550 struct amd_iommu *iommu;
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001551 unsigned long i, old_size, pte_pgsize;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001552
Joerg Roedelf5e97052009-05-22 12:31:53 +02001553#ifdef CONFIG_IOMMU_STRESS
1554 populate = false;
1555#endif
1556
Joerg Roedel9cabe892009-05-18 16:38:55 +02001557 if (index >= APERTURE_MAX_RANGES)
1558 return -ENOMEM;
1559
1560 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1561 if (!dma_dom->aperture[index])
1562 return -ENOMEM;
1563
1564 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1565 if (!dma_dom->aperture[index]->bitmap)
1566 goto out_free;
1567
1568 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1569
1570 if (populate) {
1571 unsigned long address = dma_dom->aperture_size;
1572 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1573 u64 *pte, *pte_page;
1574
1575 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001576 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001577 &pte_page, gfp);
1578 if (!pte)
1579 goto out_free;
1580
1581 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1582
1583 address += APERTURE_RANGE_SIZE / 64;
1584 }
1585 }
1586
Joerg Roedel17f5b562011-07-06 17:14:44 +02001587 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001588 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1589
Joerg Roedel17f5b562011-07-06 17:14:44 +02001590 /* Reserve address range used for MSI messages */
1591 if (old_size < MSI_ADDR_BASE_LO &&
1592 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1593 unsigned long spage;
1594 int pages;
1595
1596 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1597 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1598
1599 dma_ops_reserve_addresses(dma_dom, spage, pages);
1600 }
1601
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001602 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001603 for_each_iommu(iommu) {
1604 if (iommu->exclusion_start &&
1605 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1606 && iommu->exclusion_start < dma_dom->aperture_size) {
1607 unsigned long startpage;
1608 int pages = iommu_num_pages(iommu->exclusion_start,
1609 iommu->exclusion_length,
1610 PAGE_SIZE);
1611 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1612 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1613 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001614 }
1615
1616 /*
1617 * Check for areas already mapped as present in the new aperture
1618 * range and mark those pages as reserved in the allocator. Such
1619 * mappings may already exist as a result of requested unity
1620 * mappings for devices.
1621 */
1622 for (i = dma_dom->aperture[index]->offset;
1623 i < dma_dom->aperture_size;
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001624 i += pte_pgsize) {
Joerg Roedel3039ca12015-04-01 14:58:48 +02001625 u64 *pte = fetch_pte(&dma_dom->domain, i, &pte_pgsize);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001626 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1627 continue;
1628
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001629 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT,
1630 pte_pgsize >> 12);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001631 }
1632
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001633 update_domain(&dma_dom->domain);
1634
Joerg Roedel9cabe892009-05-18 16:38:55 +02001635 return 0;
1636
1637out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001638 update_domain(&dma_dom->domain);
1639
Joerg Roedel9cabe892009-05-18 16:38:55 +02001640 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1641
1642 kfree(dma_dom->aperture[index]);
1643 dma_dom->aperture[index] = NULL;
1644
1645 return -ENOMEM;
1646}
1647
Joerg Roedel384de722009-05-15 12:30:05 +02001648static unsigned long dma_ops_area_alloc(struct device *dev,
1649 struct dma_ops_domain *dom,
1650 unsigned int pages,
1651 unsigned long align_mask,
1652 u64 dma_mask,
1653 unsigned long start)
1654{
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001655 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001656 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1657 int i = start >> APERTURE_RANGE_SHIFT;
1658 unsigned long boundary_size;
1659 unsigned long address = -1;
1660 unsigned long limit;
1661
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001662 next_bit >>= PAGE_SHIFT;
1663
Joerg Roedel384de722009-05-15 12:30:05 +02001664 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1665 PAGE_SIZE) >> PAGE_SHIFT;
1666
1667 for (;i < max_index; ++i) {
1668 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1669
1670 if (dom->aperture[i]->offset >= dma_mask)
1671 break;
1672
1673 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1674 dma_mask >> PAGE_SHIFT);
1675
1676 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1677 limit, next_bit, pages, 0,
1678 boundary_size, align_mask);
1679 if (address != -1) {
1680 address = dom->aperture[i]->offset +
1681 (address << PAGE_SHIFT);
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001682 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001683 break;
1684 }
1685
1686 next_bit = 0;
1687 }
1688
1689 return address;
1690}
1691
Joerg Roedeld3086442008-06-26 21:27:57 +02001692static unsigned long dma_ops_alloc_addresses(struct device *dev,
1693 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001694 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001695 unsigned long align_mask,
1696 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001697{
Joerg Roedeld3086442008-06-26 21:27:57 +02001698 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001699
Joerg Roedelfe16f082009-05-22 12:27:53 +02001700#ifdef CONFIG_IOMMU_STRESS
1701 dom->next_address = 0;
1702 dom->need_flush = true;
1703#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001704
Joerg Roedel384de722009-05-15 12:30:05 +02001705 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001706 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001707
Joerg Roedel1c655772008-09-04 18:40:05 +02001708 if (address == -1) {
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001709 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001710 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1711 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001712 dom->need_flush = true;
1713 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001714
Joerg Roedel384de722009-05-15 12:30:05 +02001715 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001716 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001717
1718 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1719
1720 return address;
1721}
1722
Joerg Roedel431b2a22008-07-11 17:14:22 +02001723/*
1724 * The address free function.
1725 *
1726 * called with domain->lock held
1727 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001728static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1729 unsigned long address,
1730 unsigned int pages)
1731{
Joerg Roedel384de722009-05-15 12:30:05 +02001732 unsigned i = address >> APERTURE_RANGE_SHIFT;
1733 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001734
Joerg Roedel384de722009-05-15 12:30:05 +02001735 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1736
Joerg Roedel47bccd62009-05-22 12:40:54 +02001737#ifdef CONFIG_IOMMU_STRESS
1738 if (i < 4)
1739 return;
1740#endif
1741
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001742 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001743 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001744
1745 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001746
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001747 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001748
Joerg Roedeld3086442008-06-26 21:27:57 +02001749}
1750
Joerg Roedel431b2a22008-07-11 17:14:22 +02001751/****************************************************************************
1752 *
1753 * The next functions belong to the domain allocation. A domain is
1754 * allocated for every IOMMU as the default domain. If device isolation
1755 * is enabled, every device get its own domain. The most important thing
1756 * about domains is the page table mapping the DMA address space they
1757 * contain.
1758 *
1759 ****************************************************************************/
1760
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001761/*
1762 * This function adds a protection domain to the global protection domain list
1763 */
1764static void add_domain_to_list(struct protection_domain *domain)
1765{
1766 unsigned long flags;
1767
1768 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1769 list_add(&domain->list, &amd_iommu_pd_list);
1770 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1771}
1772
1773/*
1774 * This function removes a protection domain to the global
1775 * protection domain list
1776 */
1777static void del_domain_from_list(struct protection_domain *domain)
1778{
1779 unsigned long flags;
1780
1781 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1782 list_del(&domain->list);
1783 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1784}
1785
Joerg Roedelec487d12008-06-26 21:27:58 +02001786static u16 domain_id_alloc(void)
1787{
1788 unsigned long flags;
1789 int id;
1790
1791 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1792 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1793 BUG_ON(id == 0);
1794 if (id > 0 && id < MAX_DOMAIN_ID)
1795 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1796 else
1797 id = 0;
1798 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1799
1800 return id;
1801}
1802
Joerg Roedela2acfb72008-12-02 18:28:53 +01001803static void domain_id_free(int id)
1804{
1805 unsigned long flags;
1806
1807 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1808 if (id > 0 && id < MAX_DOMAIN_ID)
1809 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1810 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1811}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001812
Joerg Roedel5c34c402013-06-20 20:22:58 +02001813#define DEFINE_FREE_PT_FN(LVL, FN) \
1814static void free_pt_##LVL (unsigned long __pt) \
1815{ \
1816 unsigned long p; \
1817 u64 *pt; \
1818 int i; \
1819 \
1820 pt = (u64 *)__pt; \
1821 \
1822 for (i = 0; i < 512; ++i) { \
1823 if (!IOMMU_PTE_PRESENT(pt[i])) \
1824 continue; \
1825 \
1826 p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
1827 FN(p); \
1828 } \
1829 free_page((unsigned long)pt); \
1830}
1831
1832DEFINE_FREE_PT_FN(l2, free_page)
1833DEFINE_FREE_PT_FN(l3, free_pt_l2)
1834DEFINE_FREE_PT_FN(l4, free_pt_l3)
1835DEFINE_FREE_PT_FN(l5, free_pt_l4)
1836DEFINE_FREE_PT_FN(l6, free_pt_l5)
1837
Joerg Roedel86db2e52008-12-02 18:20:21 +01001838static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001839{
Joerg Roedel5c34c402013-06-20 20:22:58 +02001840 unsigned long root = (unsigned long)domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001841
Joerg Roedel5c34c402013-06-20 20:22:58 +02001842 switch (domain->mode) {
1843 case PAGE_MODE_NONE:
1844 break;
1845 case PAGE_MODE_1_LEVEL:
1846 free_page(root);
1847 break;
1848 case PAGE_MODE_2_LEVEL:
1849 free_pt_l2(root);
1850 break;
1851 case PAGE_MODE_3_LEVEL:
1852 free_pt_l3(root);
1853 break;
1854 case PAGE_MODE_4_LEVEL:
1855 free_pt_l4(root);
1856 break;
1857 case PAGE_MODE_5_LEVEL:
1858 free_pt_l5(root);
1859 break;
1860 case PAGE_MODE_6_LEVEL:
1861 free_pt_l6(root);
1862 break;
1863 default:
1864 BUG();
Joerg Roedelec487d12008-06-26 21:27:58 +02001865 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001866}
1867
Joerg Roedelb16137b2011-11-21 16:50:23 +01001868static void free_gcr3_tbl_level1(u64 *tbl)
1869{
1870 u64 *ptr;
1871 int i;
1872
1873 for (i = 0; i < 512; ++i) {
1874 if (!(tbl[i] & GCR3_VALID))
1875 continue;
1876
1877 ptr = __va(tbl[i] & PAGE_MASK);
1878
1879 free_page((unsigned long)ptr);
1880 }
1881}
1882
1883static void free_gcr3_tbl_level2(u64 *tbl)
1884{
1885 u64 *ptr;
1886 int i;
1887
1888 for (i = 0; i < 512; ++i) {
1889 if (!(tbl[i] & GCR3_VALID))
1890 continue;
1891
1892 ptr = __va(tbl[i] & PAGE_MASK);
1893
1894 free_gcr3_tbl_level1(ptr);
1895 }
1896}
1897
Joerg Roedel52815b72011-11-17 17:24:28 +01001898static void free_gcr3_table(struct protection_domain *domain)
1899{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001900 if (domain->glx == 2)
1901 free_gcr3_tbl_level2(domain->gcr3_tbl);
1902 else if (domain->glx == 1)
1903 free_gcr3_tbl_level1(domain->gcr3_tbl);
1904 else if (domain->glx != 0)
1905 BUG();
1906
Joerg Roedel52815b72011-11-17 17:24:28 +01001907 free_page((unsigned long)domain->gcr3_tbl);
1908}
1909
Joerg Roedel431b2a22008-07-11 17:14:22 +02001910/*
1911 * Free a domain, only used if something went wrong in the
1912 * allocation path and we need to free an already allocated page table
1913 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001914static void dma_ops_domain_free(struct dma_ops_domain *dom)
1915{
Joerg Roedel384de722009-05-15 12:30:05 +02001916 int i;
1917
Joerg Roedelec487d12008-06-26 21:27:58 +02001918 if (!dom)
1919 return;
1920
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001921 del_domain_from_list(&dom->domain);
1922
Joerg Roedel86db2e52008-12-02 18:20:21 +01001923 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001924
Joerg Roedel384de722009-05-15 12:30:05 +02001925 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1926 if (!dom->aperture[i])
1927 continue;
1928 free_page((unsigned long)dom->aperture[i]->bitmap);
1929 kfree(dom->aperture[i]);
1930 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001931
1932 kfree(dom);
1933}
1934
Joerg Roedel431b2a22008-07-11 17:14:22 +02001935/*
1936 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001937 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001938 * structures required for the dma_ops interface
1939 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001940static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001941{
1942 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001943
1944 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1945 if (!dma_dom)
1946 return NULL;
1947
1948 spin_lock_init(&dma_dom->domain.lock);
1949
1950 dma_dom->domain.id = domain_id_alloc();
1951 if (dma_dom->domain.id == 0)
1952 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001953 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001954 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001955 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001956 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001957 dma_dom->domain.priv = dma_dom;
1958 if (!dma_dom->domain.pt_root)
1959 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001960
Joerg Roedel1c655772008-09-04 18:40:05 +02001961 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001962 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001963
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001964 add_domain_to_list(&dma_dom->domain);
1965
Joerg Roedel576175c2009-11-23 19:08:46 +01001966 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001967 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001968
Joerg Roedel431b2a22008-07-11 17:14:22 +02001969 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001970 * mark the first page as allocated so we never return 0 as
1971 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001972 */
Joerg Roedel384de722009-05-15 12:30:05 +02001973 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001974 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001975
Joerg Roedelec487d12008-06-26 21:27:58 +02001976
1977 return dma_dom;
1978
1979free_dma_dom:
1980 dma_ops_domain_free(dma_dom);
1981
1982 return NULL;
1983}
1984
Joerg Roedel431b2a22008-07-11 17:14:22 +02001985/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001986 * little helper function to check whether a given protection domain is a
1987 * dma_ops domain
1988 */
1989static bool dma_ops_domain(struct protection_domain *domain)
1990{
1991 return domain->flags & PD_DMA_OPS_MASK;
1992}
1993
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001994static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001995{
Joerg Roedel132bd682011-11-17 14:18:46 +01001996 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001997 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001998
Joerg Roedel132bd682011-11-17 14:18:46 +01001999 if (domain->mode != PAGE_MODE_NONE)
2000 pte_root = virt_to_phys(domain->pt_root);
2001
Joerg Roedel38ddf412008-09-11 10:38:32 +02002002 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
2003 << DEV_ENTRY_MODE_SHIFT;
2004 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002005
Joerg Roedelee6c2862011-11-09 12:06:03 +01002006 flags = amd_iommu_dev_table[devid].data[1];
2007
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002008 if (ats)
2009 flags |= DTE_FLAG_IOTLB;
2010
Joerg Roedel52815b72011-11-17 17:24:28 +01002011 if (domain->flags & PD_IOMMUV2_MASK) {
2012 u64 gcr3 = __pa(domain->gcr3_tbl);
2013 u64 glx = domain->glx;
2014 u64 tmp;
2015
2016 pte_root |= DTE_FLAG_GV;
2017 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
2018
2019 /* First mask out possible old values for GCR3 table */
2020 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
2021 flags &= ~tmp;
2022
2023 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
2024 flags &= ~tmp;
2025
2026 /* Encode GCR3 table into DTE */
2027 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
2028 pte_root |= tmp;
2029
2030 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
2031 flags |= tmp;
2032
2033 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
2034 flags |= tmp;
2035 }
2036
Joerg Roedelee6c2862011-11-09 12:06:03 +01002037 flags &= ~(0xffffUL);
2038 flags |= domain->id;
2039
2040 amd_iommu_dev_table[devid].data[1] = flags;
2041 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002042}
2043
Joerg Roedel15898bb2009-11-24 15:39:42 +01002044static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01002045{
Joerg Roedel355bf552008-12-08 12:02:41 +01002046 /* remove entry from the device table seen by the hardware */
2047 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
2048 amd_iommu_dev_table[devid].data[1] = 0;
Joerg Roedel355bf552008-12-08 12:02:41 +01002049
Joerg Roedelc5cca142009-10-09 18:31:20 +02002050 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002051}
2052
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002053static void do_attach(struct iommu_dev_data *dev_data,
2054 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002055{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002056 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002057 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002058
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002059 iommu = amd_iommu_rlookup_table[dev_data->devid];
2060 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002061
2062 /* Update data structures */
2063 dev_data->domain = domain;
2064 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002065 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002066
2067 /* Do reference counting */
2068 domain->dev_iommu[iommu->index] += 1;
2069 domain->dev_cnt += 1;
2070
2071 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002072 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002073}
2074
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002075static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002076{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002077 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002078
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002079 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02002080
Joerg Roedelc4596112009-11-20 14:57:32 +01002081 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002082 dev_data->domain->dev_iommu[iommu->index] -= 1;
2083 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01002084
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002085 /* Update data structures */
2086 dev_data->domain = NULL;
2087 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002088 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002089
2090 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002091 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002092}
2093
2094/*
2095 * If a device is not yet associated with a domain, this function does
2096 * assigns it visible for the hardware
2097 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002098static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002099 struct protection_domain *domain)
2100{
Joerg Roedel397111a2014-08-05 17:31:51 +02002101 struct iommu_dev_data *head, *entry;
Julia Lawall84fe6c12010-05-27 12:31:51 +02002102 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002103
Joerg Roedel15898bb2009-11-24 15:39:42 +01002104 /* lock domain */
2105 spin_lock(&domain->lock);
2106
Joerg Roedel397111a2014-08-05 17:31:51 +02002107 head = dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002108
Joerg Roedel397111a2014-08-05 17:31:51 +02002109 if (head->alias_data != NULL)
2110 head = head->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002111
Joerg Roedel397111a2014-08-05 17:31:51 +02002112 /* Now we have the root of the alias group, if any */
Joerg Roedel2b02b092011-06-09 17:48:39 +02002113
Joerg Roedel397111a2014-08-05 17:31:51 +02002114 ret = -EBUSY;
2115 if (head->domain != NULL)
2116 goto out_unlock;
Joerg Roedel24100052009-11-25 15:59:57 +01002117
Joerg Roedel397111a2014-08-05 17:31:51 +02002118 /* Attach alias group root */
2119 do_attach(head, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002120
Joerg Roedel397111a2014-08-05 17:31:51 +02002121 /* Attach other devices in the alias group */
2122 list_for_each_entry(entry, &head->alias_list, alias_list)
2123 do_attach(entry, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002124
Julia Lawall84fe6c12010-05-27 12:31:51 +02002125 ret = 0;
2126
2127out_unlock:
2128
Joerg Roedel355bf552008-12-08 12:02:41 +01002129 /* ready */
2130 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002131
Julia Lawall84fe6c12010-05-27 12:31:51 +02002132 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002133}
2134
Joerg Roedel52815b72011-11-17 17:24:28 +01002135
2136static void pdev_iommuv2_disable(struct pci_dev *pdev)
2137{
2138 pci_disable_ats(pdev);
2139 pci_disable_pri(pdev);
2140 pci_disable_pasid(pdev);
2141}
2142
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002143/* FIXME: Change generic reset-function to do the same */
2144static int pri_reset_while_enabled(struct pci_dev *pdev)
2145{
2146 u16 control;
2147 int pos;
2148
Joerg Roedel46277b72011-12-07 14:34:02 +01002149 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002150 if (!pos)
2151 return -EINVAL;
2152
Joerg Roedel46277b72011-12-07 14:34:02 +01002153 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2154 control |= PCI_PRI_CTRL_RESET;
2155 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002156
2157 return 0;
2158}
2159
Joerg Roedel52815b72011-11-17 17:24:28 +01002160static int pdev_iommuv2_enable(struct pci_dev *pdev)
2161{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002162 bool reset_enable;
2163 int reqs, ret;
2164
2165 /* FIXME: Hardcode number of outstanding requests for now */
2166 reqs = 32;
2167 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2168 reqs = 1;
2169 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002170
2171 /* Only allow access to user-accessible pages */
2172 ret = pci_enable_pasid(pdev, 0);
2173 if (ret)
2174 goto out_err;
2175
2176 /* First reset the PRI state of the device */
2177 ret = pci_reset_pri(pdev);
2178 if (ret)
2179 goto out_err;
2180
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002181 /* Enable PRI */
2182 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002183 if (ret)
2184 goto out_err;
2185
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002186 if (reset_enable) {
2187 ret = pri_reset_while_enabled(pdev);
2188 if (ret)
2189 goto out_err;
2190 }
2191
Joerg Roedel52815b72011-11-17 17:24:28 +01002192 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2193 if (ret)
2194 goto out_err;
2195
2196 return 0;
2197
2198out_err:
2199 pci_disable_pri(pdev);
2200 pci_disable_pasid(pdev);
2201
2202 return ret;
2203}
2204
Joerg Roedelc99afa22011-11-21 18:19:25 +01002205/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002206#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002207
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002208static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002209{
Joerg Roedela3b93122012-04-12 12:49:26 +02002210 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002211 int pos;
2212
Joerg Roedel46277b72011-12-07 14:34:02 +01002213 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002214 if (!pos)
2215 return false;
2216
Joerg Roedela3b93122012-04-12 12:49:26 +02002217 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002218
Joerg Roedela3b93122012-04-12 12:49:26 +02002219 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002220}
2221
Joerg Roedel15898bb2009-11-24 15:39:42 +01002222/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02002223 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01002224 * assigns it visible for the hardware
2225 */
2226static int attach_device(struct device *dev,
2227 struct protection_domain *domain)
2228{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002229 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002230 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002231 unsigned long flags;
2232 int ret;
2233
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002234 dev_data = get_dev_data(dev);
2235
Joerg Roedel52815b72011-11-17 17:24:28 +01002236 if (domain->flags & PD_IOMMUV2_MASK) {
2237 if (!dev_data->iommu_v2 || !dev_data->passthrough)
2238 return -EINVAL;
2239
2240 if (pdev_iommuv2_enable(pdev) != 0)
2241 return -EINVAL;
2242
2243 dev_data->ats.enabled = true;
2244 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002245 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002246 } else if (amd_iommu_iotlb_sup &&
2247 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002248 dev_data->ats.enabled = true;
2249 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2250 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002251
Joerg Roedel15898bb2009-11-24 15:39:42 +01002252 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002253 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002254 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2255
2256 /*
2257 * We might boot into a crash-kernel here. The crashed kernel
2258 * left the caches in the IOMMU dirty. So we have to flush
2259 * here to evict all dirty stuff.
2260 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002261 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002262
2263 return ret;
2264}
2265
2266/*
2267 * Removes a device from a protection domain (unlocked)
2268 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002269static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002270{
Joerg Roedel397111a2014-08-05 17:31:51 +02002271 struct iommu_dev_data *head, *entry;
Joerg Roedel2ca76272010-01-22 16:45:31 +01002272 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002273 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002274
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002275 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002276
Joerg Roedel2ca76272010-01-22 16:45:31 +01002277 domain = dev_data->domain;
2278
2279 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01002280
Joerg Roedel397111a2014-08-05 17:31:51 +02002281 head = dev_data;
2282 if (head->alias_data != NULL)
2283 head = head->alias_data;
Joerg Roedel71f77582011-06-09 19:03:15 +02002284
Joerg Roedel397111a2014-08-05 17:31:51 +02002285 list_for_each_entry(entry, &head->alias_list, alias_list)
2286 do_detach(entry);
Joerg Roedel24100052009-11-25 15:59:57 +01002287
Joerg Roedel397111a2014-08-05 17:31:51 +02002288 do_detach(head);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002289
Joerg Roedel2ca76272010-01-22 16:45:31 +01002290 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002291
Joerg Roedel21129f72009-09-01 11:59:42 +02002292 /*
2293 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002294 * passthrough domain if it is detached from any other domain.
2295 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02002296 */
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002297 if (dev_data->passthrough &&
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002298 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002299 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01002300}
2301
2302/*
2303 * Removes a device from a protection domain (with devtable_lock held)
2304 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002305static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002306{
Joerg Roedel52815b72011-11-17 17:24:28 +01002307 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002308 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002309 unsigned long flags;
2310
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002311 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002312 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002313
Joerg Roedel355bf552008-12-08 12:02:41 +01002314 /* lock device table */
2315 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002316 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002317 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002318
Joerg Roedel52815b72011-11-17 17:24:28 +01002319 if (domain->flags & PD_IOMMUV2_MASK)
2320 pdev_iommuv2_disable(to_pci_dev(dev));
2321 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002322 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002323
2324 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002325}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002326
Joerg Roedel15898bb2009-11-24 15:39:42 +01002327/*
2328 * Find out the protection domain structure for a given PCI device. This
2329 * will give us the pointer to the page table root for example.
2330 */
2331static struct protection_domain *domain_for_device(struct device *dev)
2332{
Joerg Roedel71f77582011-06-09 19:03:15 +02002333 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002334 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002335 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002336
Joerg Roedel657cbb62009-11-23 15:26:46 +01002337 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002338
Joerg Roedel2b02b092011-06-09 17:48:39 +02002339 if (dev_data->domain)
2340 return dev_data->domain;
2341
Joerg Roedel71f77582011-06-09 19:03:15 +02002342 if (dev_data->alias_data != NULL) {
2343 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002344
2345 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
2346 if (alias_data->domain != NULL) {
2347 __attach_device(dev_data, alias_data->domain);
2348 dom = alias_data->domain;
2349 }
2350 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002351 }
2352
Joerg Roedel15898bb2009-11-24 15:39:42 +01002353 return dom;
2354}
2355
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002356static int amd_iommu_add_device(struct device *dev)
Joerg Roedele275a2a2008-12-10 18:27:25 +01002357{
Joerg Roedele275a2a2008-12-10 18:27:25 +01002358 struct dma_ops_domain *dma_domain;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002359 struct protection_domain *domain;
2360 struct iommu_dev_data *dev_data;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002361 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002362 unsigned long flags;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002363 u16 devid;
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002364 int ret;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002365
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002366 if (!check_device(dev) || get_dev_data(dev))
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002367 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002368
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002369 devid = get_device_id(dev);
2370 iommu = amd_iommu_rlookup_table[devid];
2371
2372 ret = iommu_init_device(dev);
2373 if (ret == -ENOTSUPP) {
2374 iommu_ignore_device(dev);
2375 goto out;
2376 }
2377 init_iommu_group(dev);
2378
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002379 dev_data = get_dev_data(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002380
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002381 if (iommu_pass_through || dev_data->iommu_v2) {
2382 /* Make sure passthrough domain is allocated */
2383 alloc_passthrough_domain();
2384 dev_data->passthrough = true;
2385 attach_device(dev, pt_domain);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002386 goto out;
2387 }
2388
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002389 domain = domain_for_device(dev);
2390
2391 /* allocate a protection domain if a device is added */
2392 dma_domain = find_protection_domain(devid);
2393 if (!dma_domain) {
2394 dma_domain = dma_ops_domain_alloc();
2395 if (!dma_domain)
2396 goto out;
2397 dma_domain->target_dev = devid;
2398
2399 init_unity_mappings_for_device(dma_domain, devid);
2400
2401 spin_lock_irqsave(&iommu_pd_list_lock, flags);
2402 list_add_tail(&dma_domain->list, &iommu_pd_list);
2403 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
2404 }
2405
2406 attach_device(dev, &dma_domain->domain);
2407
2408 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002409
2410out:
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002411 iommu_completion_wait(iommu);
2412
Joerg Roedele275a2a2008-12-10 18:27:25 +01002413 return 0;
2414}
2415
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002416static void amd_iommu_remove_device(struct device *dev)
Joerg Roedel8638c492009-12-10 11:12:25 +01002417{
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002418 struct amd_iommu *iommu;
2419 u16 devid;
2420
2421 if (!check_device(dev))
2422 return;
2423
2424 devid = get_device_id(dev);
2425 iommu = amd_iommu_rlookup_table[devid];
2426
2427 iommu_uninit_device(dev);
2428 iommu_completion_wait(iommu);
Joerg Roedel8638c492009-12-10 11:12:25 +01002429}
2430
Joerg Roedel431b2a22008-07-11 17:14:22 +02002431/*****************************************************************************
2432 *
2433 * The next functions belong to the dma_ops mapping/unmapping code.
2434 *
2435 *****************************************************************************/
2436
2437/*
2438 * In the dma_ops path we only have the struct device. This function
2439 * finds the corresponding IOMMU, the protection domain and the
2440 * requestor id for a given device.
2441 * If the device is not yet associated with a domain this is also done
2442 * in this function.
2443 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002444static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002445{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002446 struct protection_domain *domain;
Joerg Roedel063071d2015-05-28 18:41:38 +02002447 struct iommu_domain *io_domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002448 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002449 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002450
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002451 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002452 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002453
Joerg Roedel063071d2015-05-28 18:41:38 +02002454 io_domain = iommu_get_domain_for_dev(dev);
2455 if (io_domain) {
2456 domain = to_pdomain(io_domain);
2457 return domain;
2458 }
2459
Joerg Roedel94f6d192009-11-24 16:40:02 +01002460 domain = domain_for_device(dev);
2461 if (domain != NULL && !dma_ops_domain(domain))
2462 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002463
Joerg Roedel94f6d192009-11-24 16:40:02 +01002464 if (domain != NULL)
2465 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002466
Frank Arnolddf805ab2012-08-27 19:21:04 +02002467 /* Device not bound yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002468 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002469 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01002470 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2471 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002472 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01002473 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01002474
Joerg Roedel94f6d192009-11-24 16:40:02 +01002475 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002476}
2477
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002478static void update_device_table(struct protection_domain *domain)
2479{
Joerg Roedel492667d2009-11-27 13:25:47 +01002480 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002481
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002482 list_for_each_entry(dev_data, &domain->dev_list, list)
2483 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002484}
2485
2486static void update_domain(struct protection_domain *domain)
2487{
2488 if (!domain->updated)
2489 return;
2490
2491 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002492
2493 domain_flush_devices(domain);
2494 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002495
2496 domain->updated = false;
2497}
2498
Joerg Roedel431b2a22008-07-11 17:14:22 +02002499/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002500 * This function fetches the PTE for a given address in the aperture
2501 */
2502static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2503 unsigned long address)
2504{
Joerg Roedel384de722009-05-15 12:30:05 +02002505 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002506 u64 *pte, *pte_page;
2507
Joerg Roedel384de722009-05-15 12:30:05 +02002508 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2509 if (!aperture)
2510 return NULL;
2511
2512 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002513 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002514 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002515 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002516 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2517 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002518 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002519
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002520 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002521
2522 return pte;
2523}
2524
2525/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002526 * This is the generic map function. It maps one 4kb page at paddr to
2527 * the given address in the DMA address space for the domain.
2528 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002529static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002530 unsigned long address,
2531 phys_addr_t paddr,
2532 int direction)
2533{
2534 u64 *pte, __pte;
2535
2536 WARN_ON(address > dom->aperture_size);
2537
2538 paddr &= PAGE_MASK;
2539
Joerg Roedel8bda3092009-05-12 12:02:46 +02002540 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002541 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002542 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002543
2544 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2545
2546 if (direction == DMA_TO_DEVICE)
2547 __pte |= IOMMU_PTE_IR;
2548 else if (direction == DMA_FROM_DEVICE)
2549 __pte |= IOMMU_PTE_IW;
2550 else if (direction == DMA_BIDIRECTIONAL)
2551 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2552
2553 WARN_ON(*pte);
2554
2555 *pte = __pte;
2556
2557 return (dma_addr_t)address;
2558}
2559
Joerg Roedel431b2a22008-07-11 17:14:22 +02002560/*
2561 * The generic unmapping function for on page in the DMA address space.
2562 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002563static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002564 unsigned long address)
2565{
Joerg Roedel384de722009-05-15 12:30:05 +02002566 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002567 u64 *pte;
2568
2569 if (address >= dom->aperture_size)
2570 return;
2571
Joerg Roedel384de722009-05-15 12:30:05 +02002572 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2573 if (!aperture)
2574 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002575
Joerg Roedel384de722009-05-15 12:30:05 +02002576 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2577 if (!pte)
2578 return;
2579
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002580 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002581
2582 WARN_ON(!*pte);
2583
2584 *pte = 0ULL;
2585}
2586
Joerg Roedel431b2a22008-07-11 17:14:22 +02002587/*
2588 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002589 * contiguous memory region into DMA address space. It is used by all
2590 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002591 * Must be called with the domain lock held.
2592 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002593static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002594 struct dma_ops_domain *dma_dom,
2595 phys_addr_t paddr,
2596 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002597 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002598 bool align,
2599 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002600{
2601 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002602 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002603 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002604 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002605 int i;
2606
Joerg Roedele3c449f2008-10-15 22:02:11 -07002607 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002608 paddr &= PAGE_MASK;
2609
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002610 INC_STATS_COUNTER(total_map_requests);
2611
Joerg Roedelc1858972008-12-12 15:42:39 +01002612 if (pages > 1)
2613 INC_STATS_COUNTER(cross_page);
2614
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002615 if (align)
2616 align_mask = (1UL << get_order(size)) - 1;
2617
Joerg Roedel11b83882009-05-19 10:23:15 +02002618retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002619 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2620 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002621 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002622 /*
2623 * setting next_address here will let the address
2624 * allocator only scan the new allocated range in the
2625 * first run. This is a small optimization.
2626 */
2627 dma_dom->next_address = dma_dom->aperture_size;
2628
Joerg Roedel576175c2009-11-23 19:08:46 +01002629 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002630 goto out;
2631
2632 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002633 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002634 * allocation again
2635 */
2636 goto retry;
2637 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002638
2639 start = address;
2640 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002641 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002642 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002643 goto out_unmap;
2644
Joerg Roedelcb76c322008-06-26 21:28:00 +02002645 paddr += PAGE_SIZE;
2646 start += PAGE_SIZE;
2647 }
2648 address += offset;
2649
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002650 ADD_STATS_COUNTER(alloced_io_mem, size);
2651
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002652 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002653 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002654 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002655 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002656 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002657
Joerg Roedelcb76c322008-06-26 21:28:00 +02002658out:
2659 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002660
2661out_unmap:
2662
2663 for (--i; i >= 0; --i) {
2664 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002665 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002666 }
2667
2668 dma_ops_free_addresses(dma_dom, address, pages);
2669
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002670 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002671}
2672
Joerg Roedel431b2a22008-07-11 17:14:22 +02002673/*
2674 * Does the reverse of the __map_single function. Must be called with
2675 * the domain lock held too
2676 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002677static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002678 dma_addr_t dma_addr,
2679 size_t size,
2680 int dir)
2681{
Joerg Roedel04e04632010-09-23 16:12:48 +02002682 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002683 dma_addr_t i, start;
2684 unsigned int pages;
2685
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002686 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002687 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002688 return;
2689
Joerg Roedel04e04632010-09-23 16:12:48 +02002690 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002691 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002692 dma_addr &= PAGE_MASK;
2693 start = dma_addr;
2694
2695 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002696 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002697 start += PAGE_SIZE;
2698 }
2699
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002700 SUB_STATS_COUNTER(alloced_io_mem, size);
2701
Joerg Roedelcb76c322008-06-26 21:28:00 +02002702 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002703
Joerg Roedel80be3082008-11-06 14:59:05 +01002704 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002705 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002706 dma_dom->need_flush = false;
2707 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002708}
2709
Joerg Roedel431b2a22008-07-11 17:14:22 +02002710/*
2711 * The exported map_single function for dma_ops.
2712 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002713static dma_addr_t map_page(struct device *dev, struct page *page,
2714 unsigned long offset, size_t size,
2715 enum dma_data_direction dir,
2716 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002717{
2718 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002719 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002720 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002721 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002722 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002723
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002724 INC_STATS_COUNTER(cnt_map_single);
2725
Joerg Roedel94f6d192009-11-24 16:40:02 +01002726 domain = get_domain(dev);
2727 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002728 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002729 else if (IS_ERR(domain))
2730 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002731
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002732 dma_mask = *dev->dma_mask;
2733
Joerg Roedel4da70b92008-06-26 21:28:01 +02002734 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002735
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002736 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002737 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002738 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002739 goto out;
2740
Joerg Roedel17b124b2011-04-06 18:01:35 +02002741 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002742
2743out:
2744 spin_unlock_irqrestore(&domain->lock, flags);
2745
2746 return addr;
2747}
2748
Joerg Roedel431b2a22008-07-11 17:14:22 +02002749/*
2750 * The exported unmap_single function for dma_ops.
2751 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002752static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2753 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002754{
2755 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002756 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002757
Joerg Roedel146a6912008-12-12 15:07:12 +01002758 INC_STATS_COUNTER(cnt_unmap_single);
2759
Joerg Roedel94f6d192009-11-24 16:40:02 +01002760 domain = get_domain(dev);
2761 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002762 return;
2763
Joerg Roedel4da70b92008-06-26 21:28:01 +02002764 spin_lock_irqsave(&domain->lock, flags);
2765
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002766 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002767
Joerg Roedel17b124b2011-04-06 18:01:35 +02002768 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002769
2770 spin_unlock_irqrestore(&domain->lock, flags);
2771}
2772
Joerg Roedel431b2a22008-07-11 17:14:22 +02002773/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002774 * The exported map_sg function for dma_ops (handles scatter-gather
2775 * lists).
2776 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002777static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002778 int nelems, enum dma_data_direction dir,
2779 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002780{
2781 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002782 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002783 int i;
2784 struct scatterlist *s;
2785 phys_addr_t paddr;
2786 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002787 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002788
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002789 INC_STATS_COUNTER(cnt_map_sg);
2790
Joerg Roedel94f6d192009-11-24 16:40:02 +01002791 domain = get_domain(dev);
Joerg Roedela0e191b2013-04-09 15:04:36 +02002792 if (IS_ERR(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002793 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002794
Joerg Roedel832a90c2008-09-18 15:54:23 +02002795 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002796
Joerg Roedel65b050a2008-06-26 21:28:02 +02002797 spin_lock_irqsave(&domain->lock, flags);
2798
2799 for_each_sg(sglist, s, nelems, i) {
2800 paddr = sg_phys(s);
2801
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002802 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002803 paddr, s->length, dir, false,
2804 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002805
2806 if (s->dma_address) {
2807 s->dma_length = s->length;
2808 mapped_elems++;
2809 } else
2810 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002811 }
2812
Joerg Roedel17b124b2011-04-06 18:01:35 +02002813 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002814
2815out:
2816 spin_unlock_irqrestore(&domain->lock, flags);
2817
2818 return mapped_elems;
2819unmap:
2820 for_each_sg(sglist, s, mapped_elems, i) {
2821 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002822 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002823 s->dma_length, dir);
2824 s->dma_address = s->dma_length = 0;
2825 }
2826
2827 mapped_elems = 0;
2828
2829 goto out;
2830}
2831
Joerg Roedel431b2a22008-07-11 17:14:22 +02002832/*
2833 * The exported map_sg function for dma_ops (handles scatter-gather
2834 * lists).
2835 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002836static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002837 int nelems, enum dma_data_direction dir,
2838 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002839{
2840 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002841 struct protection_domain *domain;
2842 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002843 int i;
2844
Joerg Roedel55877a62008-12-12 15:12:14 +01002845 INC_STATS_COUNTER(cnt_unmap_sg);
2846
Joerg Roedel94f6d192009-11-24 16:40:02 +01002847 domain = get_domain(dev);
2848 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002849 return;
2850
Joerg Roedel65b050a2008-06-26 21:28:02 +02002851 spin_lock_irqsave(&domain->lock, flags);
2852
2853 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002854 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002855 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002856 s->dma_address = s->dma_length = 0;
2857 }
2858
Joerg Roedel17b124b2011-04-06 18:01:35 +02002859 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002860
2861 spin_unlock_irqrestore(&domain->lock, flags);
2862}
2863
Joerg Roedel431b2a22008-07-11 17:14:22 +02002864/*
2865 * The exported alloc_coherent function for dma_ops.
2866 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002867static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002868 dma_addr_t *dma_addr, gfp_t flag,
2869 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002870{
Joerg Roedel832a90c2008-09-18 15:54:23 +02002871 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002872 struct protection_domain *domain;
2873 unsigned long flags;
2874 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002875
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002876 INC_STATS_COUNTER(cnt_alloc_coherent);
2877
Joerg Roedel94f6d192009-11-24 16:40:02 +01002878 domain = get_domain(dev);
2879 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedel3b839a52015-04-01 14:58:47 +02002880 page = alloc_pages(flag, get_order(size));
2881 *dma_addr = page_to_phys(page);
2882 return page_address(page);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002883 } else if (IS_ERR(domain))
2884 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002885
Joerg Roedel3b839a52015-04-01 14:58:47 +02002886 size = PAGE_ALIGN(size);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002887 dma_mask = dev->coherent_dma_mask;
2888 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002889
Joerg Roedel3b839a52015-04-01 14:58:47 +02002890 page = alloc_pages(flag | __GFP_NOWARN, get_order(size));
2891 if (!page) {
2892 if (!(flag & __GFP_WAIT))
2893 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002894
Joerg Roedel3b839a52015-04-01 14:58:47 +02002895 page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
2896 get_order(size));
2897 if (!page)
2898 return NULL;
2899 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002900
Joerg Roedel832a90c2008-09-18 15:54:23 +02002901 if (!dma_mask)
2902 dma_mask = *dev->dma_mask;
2903
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002904 spin_lock_irqsave(&domain->lock, flags);
2905
Joerg Roedel3b839a52015-04-01 14:58:47 +02002906 *dma_addr = __map_single(dev, domain->priv, page_to_phys(page),
Joerg Roedel832a90c2008-09-18 15:54:23 +02002907 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002908
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002909 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002910 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002911 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002912 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002913
Joerg Roedel17b124b2011-04-06 18:01:35 +02002914 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002915
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002916 spin_unlock_irqrestore(&domain->lock, flags);
2917
Joerg Roedel3b839a52015-04-01 14:58:47 +02002918 return page_address(page);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002919
2920out_free:
2921
Joerg Roedel3b839a52015-04-01 14:58:47 +02002922 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2923 __free_pages(page, get_order(size));
Joerg Roedel5b28df62008-12-02 17:49:42 +01002924
2925 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002926}
2927
Joerg Roedel431b2a22008-07-11 17:14:22 +02002928/*
2929 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002930 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002931static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002932 void *virt_addr, dma_addr_t dma_addr,
2933 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002934{
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002935 struct protection_domain *domain;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002936 unsigned long flags;
2937 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002938
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002939 INC_STATS_COUNTER(cnt_free_coherent);
2940
Joerg Roedel3b839a52015-04-01 14:58:47 +02002941 page = virt_to_page(virt_addr);
2942 size = PAGE_ALIGN(size);
2943
Joerg Roedel94f6d192009-11-24 16:40:02 +01002944 domain = get_domain(dev);
2945 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002946 goto free_mem;
2947
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002948 spin_lock_irqsave(&domain->lock, flags);
2949
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002950 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002951
Joerg Roedel17b124b2011-04-06 18:01:35 +02002952 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002953
2954 spin_unlock_irqrestore(&domain->lock, flags);
2955
2956free_mem:
Joerg Roedel3b839a52015-04-01 14:58:47 +02002957 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2958 __free_pages(page, get_order(size));
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002959}
2960
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002961/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002962 * This function is called by the DMA layer to find out if we can handle a
2963 * particular device. It is part of the dma_ops.
2964 */
2965static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2966{
Joerg Roedel420aef82009-11-23 16:14:57 +01002967 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002968}
2969
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002970static struct dma_map_ops amd_iommu_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002971 .alloc = alloc_coherent,
2972 .free = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002973 .map_page = map_page,
2974 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002975 .map_sg = map_sg,
2976 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002977 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002978};
2979
Joerg Roedel27c21272011-05-30 15:56:24 +02002980static unsigned device_dma_ops_init(void)
2981{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002982 struct iommu_dev_data *dev_data;
Joerg Roedel27c21272011-05-30 15:56:24 +02002983 struct pci_dev *pdev = NULL;
2984 unsigned unhandled = 0;
2985
2986 for_each_pci_dev(pdev) {
2987 if (!check_device(&pdev->dev)) {
Joerg Roedelaf1be042012-01-18 14:03:11 +01002988
2989 iommu_ignore_device(&pdev->dev);
2990
Joerg Roedel27c21272011-05-30 15:56:24 +02002991 unhandled += 1;
2992 continue;
2993 }
2994
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002995 dev_data = get_dev_data(&pdev->dev);
2996
2997 if (!dev_data->passthrough)
2998 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
2999 else
3000 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
Joerg Roedel27c21272011-05-30 15:56:24 +02003001 }
3002
3003 return unhandled;
3004}
3005
Joerg Roedel431b2a22008-07-11 17:14:22 +02003006/*
3007 * The function which clues the AMD IOMMU driver into dma_ops.
3008 */
Joerg Roedelf5325092010-01-22 17:44:35 +01003009
3010void __init amd_iommu_init_api(void)
3011{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02003012 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01003013}
3014
Joerg Roedel6631ee92008-06-26 21:28:05 +02003015int __init amd_iommu_init_dma_ops(void)
3016{
3017 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02003018 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003019
Joerg Roedel431b2a22008-07-11 17:14:22 +02003020 /*
3021 * first allocate a default protection domain for every IOMMU we
3022 * found in the system. Devices not assigned to any other
3023 * protection domain will be assigned to the default one.
3024 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02003025 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01003026 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003027 if (iommu->default_dom == NULL)
3028 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01003029 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003030 ret = iommu_init_unity_mappings(iommu);
3031 if (ret)
3032 goto free_domains;
3033 }
3034
Joerg Roedel6631ee92008-06-26 21:28:05 +02003035 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09003036 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003037
Joerg Roedel431b2a22008-07-11 17:14:22 +02003038 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02003039 unhandled = device_dma_ops_init();
3040 if (unhandled && max_pfn > MAX_DMA32_PFN) {
3041 /* There are unhandled devices - initialize swiotlb for them */
3042 swiotlb = 1;
3043 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02003044
Joerg Roedel7f265082008-12-12 13:50:21 +01003045 amd_iommu_stats_init();
3046
Joerg Roedel62410ee2012-06-12 16:42:43 +02003047 if (amd_iommu_unmap_flush)
3048 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
3049 else
3050 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
3051
Joerg Roedel6631ee92008-06-26 21:28:05 +02003052 return 0;
3053
3054free_domains:
3055
Joerg Roedel3bd22172009-05-04 15:06:20 +02003056 for_each_iommu(iommu) {
Cyril Roelandt91457df2013-02-12 05:01:50 +01003057 dma_ops_domain_free(iommu->default_dom);
Joerg Roedel6631ee92008-06-26 21:28:05 +02003058 }
3059
3060 return ret;
3061}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003062
3063/*****************************************************************************
3064 *
3065 * The following functions belong to the exported interface of AMD IOMMU
3066 *
3067 * This interface allows access to lower level functions of the IOMMU
3068 * like protection domain handling and assignement of devices to domains
3069 * which is not possible with the dma_ops interface.
3070 *
3071 *****************************************************************************/
3072
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003073static void cleanup_domain(struct protection_domain *domain)
3074{
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02003075 struct iommu_dev_data *entry;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003076 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003077
3078 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3079
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02003080 while (!list_empty(&domain->dev_list)) {
3081 entry = list_first_entry(&domain->dev_list,
3082 struct iommu_dev_data, list);
3083 __detach_device(entry);
Joerg Roedel492667d2009-11-27 13:25:47 +01003084 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003085
3086 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3087}
3088
Joerg Roedel26508152009-08-26 16:52:40 +02003089static void protection_domain_free(struct protection_domain *domain)
3090{
3091 if (!domain)
3092 return;
3093
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003094 del_domain_from_list(domain);
3095
Joerg Roedel26508152009-08-26 16:52:40 +02003096 if (domain->id)
3097 domain_id_free(domain->id);
3098
3099 kfree(domain);
3100}
3101
3102static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01003103{
3104 struct protection_domain *domain;
3105
3106 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
3107 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02003108 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003109
3110 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003111 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01003112 domain->id = domain_id_alloc();
3113 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02003114 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01003115 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02003116
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003117 add_domain_to_list(domain);
3118
Joerg Roedel26508152009-08-26 16:52:40 +02003119 return domain;
3120
3121out_err:
3122 kfree(domain);
3123
3124 return NULL;
3125}
3126
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02003127static int alloc_passthrough_domain(void)
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003128{
3129 if (pt_domain != NULL)
3130 return 0;
3131
3132 /* allocate passthrough domain */
3133 pt_domain = protection_domain_alloc();
3134 if (!pt_domain)
3135 return -ENOMEM;
3136
3137 pt_domain->mode = PAGE_MODE_NONE;
3138
3139 return 0;
3140}
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003141
3142static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
3143{
3144 struct protection_domain *pdomain;
3145
3146 /* We only support unmanaged domains for now */
3147 if (type != IOMMU_DOMAIN_UNMANAGED)
3148 return NULL;
3149
3150 pdomain = protection_domain_alloc();
3151 if (!pdomain)
3152 goto out_free;
3153
3154 pdomain->mode = PAGE_MODE_3_LEVEL;
3155 pdomain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
3156 if (!pdomain->pt_root)
3157 goto out_free;
3158
3159 pdomain->domain.geometry.aperture_start = 0;
3160 pdomain->domain.geometry.aperture_end = ~0ULL;
3161 pdomain->domain.geometry.force_aperture = true;
3162
3163 return &pdomain->domain;
3164
3165out_free:
3166 protection_domain_free(pdomain);
3167
3168 return NULL;
3169}
3170
3171static void amd_iommu_domain_free(struct iommu_domain *dom)
Joerg Roedel26508152009-08-26 16:52:40 +02003172{
3173 struct protection_domain *domain;
3174
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003175 if (!dom)
Joerg Roedel98383fc2008-12-02 18:34:12 +01003176 return;
3177
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003178 domain = to_pdomain(dom);
3179
Joerg Roedel98383fc2008-12-02 18:34:12 +01003180 if (domain->dev_cnt > 0)
3181 cleanup_domain(domain);
3182
3183 BUG_ON(domain->dev_cnt != 0);
3184
Joerg Roedel132bd682011-11-17 14:18:46 +01003185 if (domain->mode != PAGE_MODE_NONE)
3186 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003187
Joerg Roedel52815b72011-11-17 17:24:28 +01003188 if (domain->flags & PD_IOMMUV2_MASK)
3189 free_gcr3_table(domain);
3190
Joerg Roedel8b408fe2010-03-08 14:20:07 +01003191 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003192}
3193
Joerg Roedel684f2882008-12-08 12:07:44 +01003194static void amd_iommu_detach_device(struct iommu_domain *dom,
3195 struct device *dev)
3196{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003197 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003198 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003199 u16 devid;
3200
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003201 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003202 return;
3203
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003204 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003205
Joerg Roedel657cbb62009-11-23 15:26:46 +01003206 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003207 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003208
3209 iommu = amd_iommu_rlookup_table[devid];
3210 if (!iommu)
3211 return;
3212
Joerg Roedel684f2882008-12-08 12:07:44 +01003213 iommu_completion_wait(iommu);
3214}
3215
Joerg Roedel01106062008-12-02 19:34:11 +01003216static int amd_iommu_attach_device(struct iommu_domain *dom,
3217 struct device *dev)
3218{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003219 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel657cbb62009-11-23 15:26:46 +01003220 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003221 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003222 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003223
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003224 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003225 return -EINVAL;
3226
Joerg Roedel657cbb62009-11-23 15:26:46 +01003227 dev_data = dev->archdata.iommu;
3228
Joerg Roedelf62dda62011-06-09 12:55:35 +02003229 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003230 if (!iommu)
3231 return -EINVAL;
3232
Joerg Roedel657cbb62009-11-23 15:26:46 +01003233 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003234 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003235
Joerg Roedel15898bb2009-11-24 15:39:42 +01003236 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003237
3238 iommu_completion_wait(iommu);
3239
Joerg Roedel15898bb2009-11-24 15:39:42 +01003240 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003241}
3242
Joerg Roedel468e2362010-01-21 16:37:36 +01003243static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003244 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003245{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003246 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003247 int prot = 0;
3248 int ret;
3249
Joerg Roedel132bd682011-11-17 14:18:46 +01003250 if (domain->mode == PAGE_MODE_NONE)
3251 return -EINVAL;
3252
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003253 if (iommu_prot & IOMMU_READ)
3254 prot |= IOMMU_PROT_IR;
3255 if (iommu_prot & IOMMU_WRITE)
3256 prot |= IOMMU_PROT_IW;
3257
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003258 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003259 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003260 mutex_unlock(&domain->api_lock);
3261
Joerg Roedel795e74f72010-05-11 17:40:57 +02003262 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003263}
3264
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003265static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3266 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003267{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003268 struct protection_domain *domain = to_pdomain(dom);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003269 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003270
Joerg Roedel132bd682011-11-17 14:18:46 +01003271 if (domain->mode == PAGE_MODE_NONE)
3272 return -EINVAL;
3273
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003274 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003275 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003276 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003277
Joerg Roedel17b124b2011-04-06 18:01:35 +02003278 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003279
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003280 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003281}
3282
Joerg Roedel645c4c82008-12-02 20:05:50 +01003283static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
Varun Sethibb5547ac2013-03-29 01:23:58 +05303284 dma_addr_t iova)
Joerg Roedel645c4c82008-12-02 20:05:50 +01003285{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003286 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel3039ca12015-04-01 14:58:48 +02003287 unsigned long offset_mask, pte_pgsize;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003288 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003289
Joerg Roedel132bd682011-11-17 14:18:46 +01003290 if (domain->mode == PAGE_MODE_NONE)
3291 return iova;
3292
Joerg Roedel3039ca12015-04-01 14:58:48 +02003293 pte = fetch_pte(domain, iova, &pte_pgsize);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003294
Joerg Roedela6d41a42009-09-02 17:08:55 +02003295 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003296 return 0;
3297
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003298 offset_mask = pte_pgsize - 1;
3299 __pte = *pte & PM_ADDR_MASK;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003300
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003301 return (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003302}
3303
Joerg Roedelab636482014-09-05 10:48:21 +02003304static bool amd_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003305{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003306 switch (cap) {
3307 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedelab636482014-09-05 10:48:21 +02003308 return true;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003309 case IOMMU_CAP_INTR_REMAP:
Joerg Roedelab636482014-09-05 10:48:21 +02003310 return (irq_remapping_enabled == 1);
Will Deaconcfdeec22014-10-27 11:24:48 +00003311 case IOMMU_CAP_NOEXEC:
3312 return false;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003313 }
3314
Joerg Roedelab636482014-09-05 10:48:21 +02003315 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003316}
3317
Joerg Roedel35cf2482015-05-28 18:41:37 +02003318static void amd_iommu_get_dm_regions(struct device *dev,
3319 struct list_head *head)
3320{
3321 struct unity_map_entry *entry;
3322 u16 devid;
3323
3324 devid = get_device_id(dev);
3325
3326 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
3327 struct iommu_dm_region *region;
3328
3329 if (devid < entry->devid_start || devid > entry->devid_end)
3330 continue;
3331
3332 region = kzalloc(sizeof(*region), GFP_KERNEL);
3333 if (!region) {
3334 pr_err("Out of memory allocating dm-regions for %s\n",
3335 dev_name(dev));
3336 return;
3337 }
3338
3339 region->start = entry->address_start;
3340 region->length = entry->address_end - entry->address_start;
3341 if (entry->prot & IOMMU_PROT_IR)
3342 region->prot |= IOMMU_READ;
3343 if (entry->prot & IOMMU_PROT_IW)
3344 region->prot |= IOMMU_WRITE;
3345
3346 list_add_tail(&region->list, head);
3347 }
3348}
3349
3350static void amd_iommu_put_dm_regions(struct device *dev,
3351 struct list_head *head)
3352{
3353 struct iommu_dm_region *entry, *next;
3354
3355 list_for_each_entry_safe(entry, next, head, list)
3356 kfree(entry);
3357}
3358
Thierry Redingb22f6432014-06-27 09:03:12 +02003359static const struct iommu_ops amd_iommu_ops = {
Joerg Roedelab636482014-09-05 10:48:21 +02003360 .capable = amd_iommu_capable,
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003361 .domain_alloc = amd_iommu_domain_alloc,
3362 .domain_free = amd_iommu_domain_free,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003363 .attach_dev = amd_iommu_attach_device,
3364 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003365 .map = amd_iommu_map,
3366 .unmap = amd_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07003367 .map_sg = default_iommu_map_sg,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003368 .iova_to_phys = amd_iommu_iova_to_phys,
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02003369 .add_device = amd_iommu_add_device,
3370 .remove_device = amd_iommu_remove_device,
Joerg Roedel35cf2482015-05-28 18:41:37 +02003371 .get_dm_regions = amd_iommu_get_dm_regions,
3372 .put_dm_regions = amd_iommu_put_dm_regions,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003373 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003374};
3375
Joerg Roedel0feae532009-08-26 15:26:30 +02003376/*****************************************************************************
3377 *
3378 * The next functions do a basic initialization of IOMMU for pass through
3379 * mode
3380 *
3381 * In passthrough mode the IOMMU is initialized and enabled but not used for
3382 * DMA-API translation.
3383 *
3384 *****************************************************************************/
3385
3386int __init amd_iommu_init_passthrough(void)
3387{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003388 struct iommu_dev_data *dev_data;
Joerg Roedel0feae532009-08-26 15:26:30 +02003389 struct pci_dev *dev = NULL;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003390 int ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003391
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003392 ret = alloc_passthrough_domain();
3393 if (ret)
3394 return ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003395
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04003396 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003397 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02003398 continue;
3399
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003400 dev_data = get_dev_data(&dev->dev);
3401 dev_data->passthrough = true;
3402
Joerg Roedel15898bb2009-11-24 15:39:42 +01003403 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02003404 }
3405
Joerg Roedel2655d7a2011-12-22 12:35:38 +01003406 amd_iommu_stats_init();
3407
Joerg Roedel0feae532009-08-26 15:26:30 +02003408 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
3409
3410 return 0;
3411}
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003412
3413/* IOMMUv2 specific functions */
3414int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3415{
3416 return atomic_notifier_chain_register(&ppr_notifier, nb);
3417}
3418EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3419
3420int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3421{
3422 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3423}
3424EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003425
3426void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3427{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003428 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel132bd682011-11-17 14:18:46 +01003429 unsigned long flags;
3430
3431 spin_lock_irqsave(&domain->lock, flags);
3432
3433 /* Update data structure */
3434 domain->mode = PAGE_MODE_NONE;
3435 domain->updated = true;
3436
3437 /* Make changes visible to IOMMUs */
3438 update_domain(domain);
3439
3440 /* Page-table is not visible to IOMMU anymore, so free it */
3441 free_pagetable(domain);
3442
3443 spin_unlock_irqrestore(&domain->lock, flags);
3444}
3445EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003446
3447int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3448{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003449 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel52815b72011-11-17 17:24:28 +01003450 unsigned long flags;
3451 int levels, ret;
3452
3453 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3454 return -EINVAL;
3455
3456 /* Number of GCR3 table levels required */
3457 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3458 levels += 1;
3459
3460 if (levels > amd_iommu_max_glx_val)
3461 return -EINVAL;
3462
3463 spin_lock_irqsave(&domain->lock, flags);
3464
3465 /*
3466 * Save us all sanity checks whether devices already in the
3467 * domain support IOMMUv2. Just force that the domain has no
3468 * devices attached when it is switched into IOMMUv2 mode.
3469 */
3470 ret = -EBUSY;
3471 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3472 goto out;
3473
3474 ret = -ENOMEM;
3475 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3476 if (domain->gcr3_tbl == NULL)
3477 goto out;
3478
3479 domain->glx = levels;
3480 domain->flags |= PD_IOMMUV2_MASK;
3481 domain->updated = true;
3482
3483 update_domain(domain);
3484
3485 ret = 0;
3486
3487out:
3488 spin_unlock_irqrestore(&domain->lock, flags);
3489
3490 return ret;
3491}
3492EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003493
3494static int __flush_pasid(struct protection_domain *domain, int pasid,
3495 u64 address, bool size)
3496{
3497 struct iommu_dev_data *dev_data;
3498 struct iommu_cmd cmd;
3499 int i, ret;
3500
3501 if (!(domain->flags & PD_IOMMUV2_MASK))
3502 return -EINVAL;
3503
3504 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3505
3506 /*
3507 * IOMMU TLB needs to be flushed before Device TLB to
3508 * prevent device TLB refill from IOMMU TLB
3509 */
3510 for (i = 0; i < amd_iommus_present; ++i) {
3511 if (domain->dev_iommu[i] == 0)
3512 continue;
3513
3514 ret = iommu_queue_command(amd_iommus[i], &cmd);
3515 if (ret != 0)
3516 goto out;
3517 }
3518
3519 /* Wait until IOMMU TLB flushes are complete */
3520 domain_flush_complete(domain);
3521
3522 /* Now flush device TLBs */
3523 list_for_each_entry(dev_data, &domain->dev_list, list) {
3524 struct amd_iommu *iommu;
3525 int qdep;
3526
3527 BUG_ON(!dev_data->ats.enabled);
3528
3529 qdep = dev_data->ats.qdep;
3530 iommu = amd_iommu_rlookup_table[dev_data->devid];
3531
3532 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3533 qdep, address, size);
3534
3535 ret = iommu_queue_command(iommu, &cmd);
3536 if (ret != 0)
3537 goto out;
3538 }
3539
3540 /* Wait until all device TLBs are flushed */
3541 domain_flush_complete(domain);
3542
3543 ret = 0;
3544
3545out:
3546
3547 return ret;
3548}
3549
3550static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3551 u64 address)
3552{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003553 INC_STATS_COUNTER(invalidate_iotlb);
3554
Joerg Roedel22e266c2011-11-21 15:59:08 +01003555 return __flush_pasid(domain, pasid, address, false);
3556}
3557
3558int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3559 u64 address)
3560{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003561 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003562 unsigned long flags;
3563 int ret;
3564
3565 spin_lock_irqsave(&domain->lock, flags);
3566 ret = __amd_iommu_flush_page(domain, pasid, address);
3567 spin_unlock_irqrestore(&domain->lock, flags);
3568
3569 return ret;
3570}
3571EXPORT_SYMBOL(amd_iommu_flush_page);
3572
3573static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3574{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003575 INC_STATS_COUNTER(invalidate_iotlb_all);
3576
Joerg Roedel22e266c2011-11-21 15:59:08 +01003577 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3578 true);
3579}
3580
3581int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3582{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003583 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003584 unsigned long flags;
3585 int ret;
3586
3587 spin_lock_irqsave(&domain->lock, flags);
3588 ret = __amd_iommu_flush_tlb(domain, pasid);
3589 spin_unlock_irqrestore(&domain->lock, flags);
3590
3591 return ret;
3592}
3593EXPORT_SYMBOL(amd_iommu_flush_tlb);
3594
Joerg Roedelb16137b2011-11-21 16:50:23 +01003595static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3596{
3597 int index;
3598 u64 *pte;
3599
3600 while (true) {
3601
3602 index = (pasid >> (9 * level)) & 0x1ff;
3603 pte = &root[index];
3604
3605 if (level == 0)
3606 break;
3607
3608 if (!(*pte & GCR3_VALID)) {
3609 if (!alloc)
3610 return NULL;
3611
3612 root = (void *)get_zeroed_page(GFP_ATOMIC);
3613 if (root == NULL)
3614 return NULL;
3615
3616 *pte = __pa(root) | GCR3_VALID;
3617 }
3618
3619 root = __va(*pte & PAGE_MASK);
3620
3621 level -= 1;
3622 }
3623
3624 return pte;
3625}
3626
3627static int __set_gcr3(struct protection_domain *domain, int pasid,
3628 unsigned long cr3)
3629{
3630 u64 *pte;
3631
3632 if (domain->mode != PAGE_MODE_NONE)
3633 return -EINVAL;
3634
3635 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3636 if (pte == NULL)
3637 return -ENOMEM;
3638
3639 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3640
3641 return __amd_iommu_flush_tlb(domain, pasid);
3642}
3643
3644static int __clear_gcr3(struct protection_domain *domain, int pasid)
3645{
3646 u64 *pte;
3647
3648 if (domain->mode != PAGE_MODE_NONE)
3649 return -EINVAL;
3650
3651 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3652 if (pte == NULL)
3653 return 0;
3654
3655 *pte = 0;
3656
3657 return __amd_iommu_flush_tlb(domain, pasid);
3658}
3659
3660int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3661 unsigned long cr3)
3662{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003663 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003664 unsigned long flags;
3665 int ret;
3666
3667 spin_lock_irqsave(&domain->lock, flags);
3668 ret = __set_gcr3(domain, pasid, cr3);
3669 spin_unlock_irqrestore(&domain->lock, flags);
3670
3671 return ret;
3672}
3673EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3674
3675int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3676{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003677 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003678 unsigned long flags;
3679 int ret;
3680
3681 spin_lock_irqsave(&domain->lock, flags);
3682 ret = __clear_gcr3(domain, pasid);
3683 spin_unlock_irqrestore(&domain->lock, flags);
3684
3685 return ret;
3686}
3687EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003688
3689int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3690 int status, int tag)
3691{
3692 struct iommu_dev_data *dev_data;
3693 struct amd_iommu *iommu;
3694 struct iommu_cmd cmd;
3695
Joerg Roedel399be2f2011-12-01 16:53:47 +01003696 INC_STATS_COUNTER(complete_ppr);
3697
Joerg Roedelc99afa22011-11-21 18:19:25 +01003698 dev_data = get_dev_data(&pdev->dev);
3699 iommu = amd_iommu_rlookup_table[dev_data->devid];
3700
3701 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3702 tag, dev_data->pri_tlp);
3703
3704 return iommu_queue_command(iommu, &cmd);
3705}
3706EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003707
3708struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3709{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003710 struct protection_domain *pdomain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003711
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003712 pdomain = get_domain(&pdev->dev);
3713 if (IS_ERR(pdomain))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003714 return NULL;
3715
3716 /* Only return IOMMUv2 domains */
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003717 if (!(pdomain->flags & PD_IOMMUV2_MASK))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003718 return NULL;
3719
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003720 return &pdomain->domain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003721}
3722EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003723
3724void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3725{
3726 struct iommu_dev_data *dev_data;
3727
3728 if (!amd_iommu_v2_supported())
3729 return;
3730
3731 dev_data = get_dev_data(&pdev->dev);
3732 dev_data->errata |= (1 << erratum);
3733}
3734EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003735
3736int amd_iommu_device_info(struct pci_dev *pdev,
3737 struct amd_iommu_device_info *info)
3738{
3739 int max_pasids;
3740 int pos;
3741
3742 if (pdev == NULL || info == NULL)
3743 return -EINVAL;
3744
3745 if (!amd_iommu_v2_supported())
3746 return -EINVAL;
3747
3748 memset(info, 0, sizeof(*info));
3749
3750 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3751 if (pos)
3752 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3753
3754 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3755 if (pos)
3756 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3757
3758 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3759 if (pos) {
3760 int features;
3761
3762 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3763 max_pasids = min(max_pasids, (1 << 20));
3764
3765 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3766 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3767
3768 features = pci_pasid_features(pdev);
3769 if (features & PCI_PASID_CAP_EXEC)
3770 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3771 if (features & PCI_PASID_CAP_PRIV)
3772 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3773 }
3774
3775 return 0;
3776}
3777EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003778
3779#ifdef CONFIG_IRQ_REMAP
3780
3781/*****************************************************************************
3782 *
3783 * Interrupt Remapping Implementation
3784 *
3785 *****************************************************************************/
3786
3787union irte {
3788 u32 val;
3789 struct {
3790 u32 valid : 1,
3791 no_fault : 1,
3792 int_type : 3,
3793 rq_eoi : 1,
3794 dm : 1,
3795 rsvd_1 : 1,
3796 destination : 8,
3797 vector : 8,
3798 rsvd_2 : 8;
3799 } fields;
3800};
3801
3802#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3803#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3804#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3805#define DTE_IRQ_REMAP_ENABLE 1ULL
3806
3807static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3808{
3809 u64 dte;
3810
3811 dte = amd_iommu_dev_table[devid].data[2];
3812 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3813 dte |= virt_to_phys(table->table);
3814 dte |= DTE_IRQ_REMAP_INTCTL;
3815 dte |= DTE_IRQ_TABLE_LEN;
3816 dte |= DTE_IRQ_REMAP_ENABLE;
3817
3818 amd_iommu_dev_table[devid].data[2] = dte;
3819}
3820
3821#define IRTE_ALLOCATED (~1U)
3822
3823static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3824{
3825 struct irq_remap_table *table = NULL;
3826 struct amd_iommu *iommu;
3827 unsigned long flags;
3828 u16 alias;
3829
3830 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3831
3832 iommu = amd_iommu_rlookup_table[devid];
3833 if (!iommu)
3834 goto out_unlock;
3835
3836 table = irq_lookup_table[devid];
3837 if (table)
3838 goto out;
3839
3840 alias = amd_iommu_alias_table[devid];
3841 table = irq_lookup_table[alias];
3842 if (table) {
3843 irq_lookup_table[devid] = table;
3844 set_dte_irq_entry(devid, table);
3845 iommu_flush_dte(iommu, devid);
3846 goto out;
3847 }
3848
3849 /* Nothing there yet, allocate new irq remapping table */
3850 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3851 if (!table)
3852 goto out;
3853
Joerg Roedel197887f2013-04-09 21:14:08 +02003854 /* Initialize table spin-lock */
3855 spin_lock_init(&table->lock);
3856
Joerg Roedel2b324502012-06-21 16:29:10 +02003857 if (ioapic)
3858 /* Keep the first 32 indexes free for IOAPIC interrupts */
3859 table->min_index = 32;
3860
3861 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3862 if (!table->table) {
3863 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003864 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003865 goto out;
3866 }
3867
3868 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3869
3870 if (ioapic) {
3871 int i;
3872
3873 for (i = 0; i < 32; ++i)
3874 table->table[i] = IRTE_ALLOCATED;
3875 }
3876
3877 irq_lookup_table[devid] = table;
3878 set_dte_irq_entry(devid, table);
3879 iommu_flush_dte(iommu, devid);
3880 if (devid != alias) {
3881 irq_lookup_table[alias] = table;
Alex Williamsone028a9e2014-04-22 10:08:40 -06003882 set_dte_irq_entry(alias, table);
Joerg Roedel2b324502012-06-21 16:29:10 +02003883 iommu_flush_dte(iommu, alias);
3884 }
3885
3886out:
3887 iommu_completion_wait(iommu);
3888
3889out_unlock:
3890 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3891
3892 return table;
3893}
3894
3895static int alloc_irq_index(struct irq_cfg *cfg, u16 devid, int count)
3896{
3897 struct irq_remap_table *table;
3898 unsigned long flags;
3899 int index, c;
3900
3901 table = get_irq_table(devid, false);
3902 if (!table)
3903 return -ENODEV;
3904
3905 spin_lock_irqsave(&table->lock, flags);
3906
3907 /* Scan table for free entries */
3908 for (c = 0, index = table->min_index;
3909 index < MAX_IRQS_PER_TABLE;
3910 ++index) {
3911 if (table->table[index] == 0)
3912 c += 1;
3913 else
3914 c = 0;
3915
3916 if (c == count) {
Joerg Roedel0dfedd62013-04-09 15:39:16 +02003917 struct irq_2_irte *irte_info;
Joerg Roedel2b324502012-06-21 16:29:10 +02003918
3919 for (; c != 0; --c)
3920 table->table[index - c + 1] = IRTE_ALLOCATED;
3921
3922 index -= count - 1;
3923
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02003924 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02003925 irte_info = &cfg->irq_2_irte;
3926 irte_info->devid = devid;
3927 irte_info->index = index;
Joerg Roedel2b324502012-06-21 16:29:10 +02003928
3929 goto out;
3930 }
3931 }
3932
3933 index = -ENOSPC;
3934
3935out:
3936 spin_unlock_irqrestore(&table->lock, flags);
3937
3938 return index;
3939}
3940
3941static int get_irte(u16 devid, int index, union irte *irte)
3942{
3943 struct irq_remap_table *table;
3944 unsigned long flags;
3945
3946 table = get_irq_table(devid, false);
3947 if (!table)
3948 return -ENOMEM;
3949
3950 spin_lock_irqsave(&table->lock, flags);
3951 irte->val = table->table[index];
3952 spin_unlock_irqrestore(&table->lock, flags);
3953
3954 return 0;
3955}
3956
3957static int modify_irte(u16 devid, int index, union irte irte)
3958{
3959 struct irq_remap_table *table;
3960 struct amd_iommu *iommu;
3961 unsigned long flags;
3962
3963 iommu = amd_iommu_rlookup_table[devid];
3964 if (iommu == NULL)
3965 return -EINVAL;
3966
3967 table = get_irq_table(devid, false);
3968 if (!table)
3969 return -ENOMEM;
3970
3971 spin_lock_irqsave(&table->lock, flags);
3972 table->table[index] = irte.val;
3973 spin_unlock_irqrestore(&table->lock, flags);
3974
3975 iommu_flush_irt(iommu, devid);
3976 iommu_completion_wait(iommu);
3977
3978 return 0;
3979}
3980
3981static void free_irte(u16 devid, int index)
3982{
3983 struct irq_remap_table *table;
3984 struct amd_iommu *iommu;
3985 unsigned long flags;
3986
3987 iommu = amd_iommu_rlookup_table[devid];
3988 if (iommu == NULL)
3989 return;
3990
3991 table = get_irq_table(devid, false);
3992 if (!table)
3993 return;
3994
3995 spin_lock_irqsave(&table->lock, flags);
3996 table->table[index] = 0;
3997 spin_unlock_irqrestore(&table->lock, flags);
3998
3999 iommu_flush_irt(iommu, devid);
4000 iommu_completion_wait(iommu);
4001}
4002
Joerg Roedel5527de72012-06-26 11:17:32 +02004003static int setup_ioapic_entry(int irq, struct IO_APIC_route_entry *entry,
4004 unsigned int destination, int vector,
4005 struct io_apic_irq_attr *attr)
4006{
4007 struct irq_remap_table *table;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004008 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004009 struct irq_cfg *cfg;
4010 union irte irte;
4011 int ioapic_id;
4012 int index;
4013 int devid;
4014 int ret;
4015
Jiang Liu719b5302014-10-27 16:12:10 +08004016 cfg = irq_cfg(irq);
Joerg Roedel5527de72012-06-26 11:17:32 +02004017 if (!cfg)
4018 return -EINVAL;
4019
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004020 irte_info = &cfg->irq_2_irte;
Joerg Roedel5527de72012-06-26 11:17:32 +02004021 ioapic_id = mpc_ioapic_id(attr->ioapic);
4022 devid = get_ioapic_devid(ioapic_id);
4023
4024 if (devid < 0)
4025 return devid;
4026
4027 table = get_irq_table(devid, true);
4028 if (table == NULL)
4029 return -ENOMEM;
4030
4031 index = attr->ioapic_pin;
4032
4033 /* Setup IRQ remapping info */
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004034 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004035 irte_info->devid = devid;
4036 irte_info->index = index;
Joerg Roedel5527de72012-06-26 11:17:32 +02004037
4038 /* Setup IRTE for IOMMU */
4039 irte.val = 0;
4040 irte.fields.vector = vector;
4041 irte.fields.int_type = apic->irq_delivery_mode;
4042 irte.fields.destination = destination;
4043 irte.fields.dm = apic->irq_dest_mode;
4044 irte.fields.valid = 1;
4045
4046 ret = modify_irte(devid, index, irte);
4047 if (ret)
4048 return ret;
4049
4050 /* Setup IOAPIC entry */
4051 memset(entry, 0, sizeof(*entry));
4052
4053 entry->vector = index;
4054 entry->mask = 0;
4055 entry->trigger = attr->trigger;
4056 entry->polarity = attr->polarity;
4057
4058 /*
4059 * Mask level triggered irqs.
Joerg Roedel5527de72012-06-26 11:17:32 +02004060 */
4061 if (attr->trigger)
4062 entry->mask = 1;
4063
4064 return 0;
4065}
4066
4067static int set_affinity(struct irq_data *data, const struct cpumask *mask,
4068 bool force)
4069{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004070 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004071 unsigned int dest, irq;
4072 struct irq_cfg *cfg;
4073 union irte irte;
4074 int err;
4075
4076 if (!config_enabled(CONFIG_SMP))
4077 return -1;
4078
Jiang Liu719b5302014-10-27 16:12:10 +08004079 cfg = irqd_cfg(data);
Joerg Roedel5527de72012-06-26 11:17:32 +02004080 irq = data->irq;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004081 irte_info = &cfg->irq_2_irte;
Joerg Roedel5527de72012-06-26 11:17:32 +02004082
4083 if (!cpumask_intersects(mask, cpu_online_mask))
4084 return -EINVAL;
4085
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004086 if (get_irte(irte_info->devid, irte_info->index, &irte))
Joerg Roedel5527de72012-06-26 11:17:32 +02004087 return -EBUSY;
4088
4089 if (assign_irq_vector(irq, cfg, mask))
4090 return -EBUSY;
4091
4092 err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
4093 if (err) {
4094 if (assign_irq_vector(irq, cfg, data->affinity))
4095 pr_err("AMD-Vi: Failed to recover vector for irq %d\n", irq);
4096 return err;
4097 }
4098
4099 irte.fields.vector = cfg->vector;
4100 irte.fields.destination = dest;
4101
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004102 modify_irte(irte_info->devid, irte_info->index, irte);
Joerg Roedel5527de72012-06-26 11:17:32 +02004103
4104 if (cfg->move_in_progress)
4105 send_cleanup_vector(cfg);
4106
4107 cpumask_copy(data->affinity, mask);
4108
4109 return 0;
4110}
4111
4112static int free_irq(int irq)
4113{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004114 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004115 struct irq_cfg *cfg;
4116
Jiang Liu719b5302014-10-27 16:12:10 +08004117 cfg = irq_cfg(irq);
Joerg Roedel5527de72012-06-26 11:17:32 +02004118 if (!cfg)
4119 return -EINVAL;
4120
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004121 irte_info = &cfg->irq_2_irte;
Joerg Roedel5527de72012-06-26 11:17:32 +02004122
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004123 free_irte(irte_info->devid, irte_info->index);
Joerg Roedel5527de72012-06-26 11:17:32 +02004124
4125 return 0;
4126}
4127
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004128static void compose_msi_msg(struct pci_dev *pdev,
4129 unsigned int irq, unsigned int dest,
4130 struct msi_msg *msg, u8 hpet_id)
4131{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004132 struct irq_2_irte *irte_info;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004133 struct irq_cfg *cfg;
4134 union irte irte;
4135
Jiang Liu719b5302014-10-27 16:12:10 +08004136 cfg = irq_cfg(irq);
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004137 if (!cfg)
4138 return;
4139
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004140 irte_info = &cfg->irq_2_irte;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004141
4142 irte.val = 0;
4143 irte.fields.vector = cfg->vector;
4144 irte.fields.int_type = apic->irq_delivery_mode;
4145 irte.fields.destination = dest;
4146 irte.fields.dm = apic->irq_dest_mode;
4147 irte.fields.valid = 1;
4148
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004149 modify_irte(irte_info->devid, irte_info->index, irte);
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004150
4151 msg->address_hi = MSI_ADDR_BASE_HI;
4152 msg->address_lo = MSI_ADDR_BASE_LO;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004153 msg->data = irte_info->index;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004154}
4155
4156static int msi_alloc_irq(struct pci_dev *pdev, int irq, int nvec)
4157{
4158 struct irq_cfg *cfg;
4159 int index;
4160 u16 devid;
4161
4162 if (!pdev)
4163 return -EINVAL;
4164
Jiang Liu719b5302014-10-27 16:12:10 +08004165 cfg = irq_cfg(irq);
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004166 if (!cfg)
4167 return -EINVAL;
4168
4169 devid = get_device_id(&pdev->dev);
4170 index = alloc_irq_index(cfg, devid, nvec);
4171
4172 return index < 0 ? MAX_IRQS_PER_TABLE : index;
4173}
4174
4175static int msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
4176 int index, int offset)
4177{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004178 struct irq_2_irte *irte_info;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004179 struct irq_cfg *cfg;
4180 u16 devid;
4181
4182 if (!pdev)
4183 return -EINVAL;
4184
Jiang Liu719b5302014-10-27 16:12:10 +08004185 cfg = irq_cfg(irq);
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004186 if (!cfg)
4187 return -EINVAL;
4188
4189 if (index >= MAX_IRQS_PER_TABLE)
4190 return 0;
4191
4192 devid = get_device_id(&pdev->dev);
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004193 irte_info = &cfg->irq_2_irte;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004194
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004195 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004196 irte_info->devid = devid;
4197 irte_info->index = index + offset;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004198
4199 return 0;
4200}
4201
Yijing Wang5fc24d82014-09-17 17:32:19 +08004202static int alloc_hpet_msi(unsigned int irq, unsigned int id)
Joerg Roedeld9761952012-06-26 16:00:08 +02004203{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004204 struct irq_2_irte *irte_info;
Joerg Roedeld9761952012-06-26 16:00:08 +02004205 struct irq_cfg *cfg;
4206 int index, devid;
4207
Jiang Liu719b5302014-10-27 16:12:10 +08004208 cfg = irq_cfg(irq);
Joerg Roedeld9761952012-06-26 16:00:08 +02004209 if (!cfg)
4210 return -EINVAL;
4211
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004212 irte_info = &cfg->irq_2_irte;
Joerg Roedeld9761952012-06-26 16:00:08 +02004213 devid = get_hpet_devid(id);
4214 if (devid < 0)
4215 return devid;
4216
4217 index = alloc_irq_index(cfg, devid, 1);
4218 if (index < 0)
4219 return index;
4220
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004221 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004222 irte_info->devid = devid;
4223 irte_info->index = index;
Joerg Roedeld9761952012-06-26 16:00:08 +02004224
4225 return 0;
4226}
4227
Joerg Roedel6b474b82012-06-26 16:46:04 +02004228struct irq_remap_ops amd_iommu_irq_ops = {
Joerg Roedel6b474b82012-06-26 16:46:04 +02004229 .prepare = amd_iommu_prepare,
4230 .enable = amd_iommu_enable,
4231 .disable = amd_iommu_disable,
4232 .reenable = amd_iommu_reenable,
4233 .enable_faulting = amd_iommu_enable_faulting,
4234 .setup_ioapic_entry = setup_ioapic_entry,
4235 .set_affinity = set_affinity,
4236 .free_irq = free_irq,
4237 .compose_msi_msg = compose_msi_msg,
4238 .msi_alloc_irq = msi_alloc_irq,
4239 .msi_setup_irq = msi_setup_irq,
Yijing Wang5fc24d82014-09-17 17:32:19 +08004240 .alloc_hpet_msi = alloc_hpet_msi,
Joerg Roedel6b474b82012-06-26 16:46:04 +02004241};
Joerg Roedel2b324502012-06-21 16:29:10 +02004242#endif