blob: 11ee8855a2e098b082fe37d16019d7aba70827e7 [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>
Jiang Liu7c71d302015-04-13 14:11:33 +080037#include <linux/irqdomain.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020038#include <asm/irq_remapping.h>
39#include <asm/io_apic.h>
40#include <asm/apic.h>
41#include <asm/hw_irq.h>
Joerg Roedel17f5b562011-07-06 17:14:44 +020042#include <asm/msidef.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020043#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090044#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010045#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020046#include <asm/dma.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020047
48#include "amd_iommu_proto.h"
49#include "amd_iommu_types.h"
Joerg Roedel6b474b82012-06-26 16:46:04 +020050#include "irq_remapping.h"
Joerg Roedelb6c02712008-06-26 21:27:53 +020051
52#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
53
Joerg Roedel815b33f2011-04-06 17:26:49 +020054#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020055
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020056/*
57 * This bitmap is used to advertise the page sizes our hardware support
58 * to the IOMMU core, which will then use this information to split
59 * physically contiguous memory regions it is mapping into page sizes
60 * that we support.
61 *
Joerg Roedel954e3dd2012-12-02 15:35:37 +010062 * 512GB Pages are not supported due to a hardware bug
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020063 */
Joerg Roedel954e3dd2012-12-02 15:35:37 +010064#define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020065
Joerg Roedelb6c02712008-06-26 21:27:53 +020066static DEFINE_RWLOCK(amd_iommu_devtable_lock);
67
Joerg Roedel8fa5f802011-06-09 12:24:45 +020068/* List of all available dev_data structures */
69static LIST_HEAD(dev_data_list);
70static DEFINE_SPINLOCK(dev_data_list_lock);
71
Joerg Roedel6efed632012-06-14 15:52:58 +020072LIST_HEAD(ioapic_map);
73LIST_HEAD(hpet_map);
74
Joerg Roedel0feae532009-08-26 15:26:30 +020075/*
76 * Domain for untranslated devices - only allocated
77 * if iommu=pt passed on kernel cmd line.
78 */
Thierry Redingb22f6432014-06-27 09:03:12 +020079static const struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010080
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010081static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +010082int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010083
Joerg Roedelac1534a2012-06-21 14:52:40 +020084static struct dma_map_ops amd_iommu_dma_ops;
85
Joerg Roedel431b2a22008-07-11 17:14:22 +020086/*
Joerg Roedel50917e22014-08-05 16:38:38 +020087 * This struct contains device specific data for the IOMMU
88 */
89struct iommu_dev_data {
90 struct list_head list; /* For domain->dev_list */
91 struct list_head dev_data_list; /* For global dev_data_list */
Joerg Roedel50917e22014-08-05 16:38:38 +020092 struct protection_domain *domain; /* Domain the device is bound to */
Joerg Roedel50917e22014-08-05 16:38:38 +020093 u16 devid; /* PCI Device ID */
94 bool iommu_v2; /* Device can make use of IOMMUv2 */
Joerg Roedel1e6a7b02015-07-28 16:58:48 +020095 bool passthrough; /* Device is identity mapped */
Joerg Roedel50917e22014-08-05 16:38:38 +020096 struct {
97 bool enabled;
98 int qdep;
99 } ats; /* ATS state */
100 bool pri_tlp; /* PASID TLB required for
101 PPR completions */
102 u32 errata; /* Bitmap for errata to apply */
103};
104
105/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200106 * general struct to manage commands send to an IOMMU
107 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200108struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +0200109 u32 data[4];
110};
111
Joerg Roedel05152a02012-06-15 16:53:51 +0200112struct kmem_cache *amd_iommu_irq_cache;
113
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200114static void update_domain(struct protection_domain *domain);
Joerg Roedel7a5a5662015-06-30 08:56:11 +0200115static int protection_domain_init(struct protection_domain *domain);
Chris Wrightc1eee672009-05-21 00:56:58 -0700116
Joerg Roedel007b74b2015-12-21 12:53:54 +0100117/*
118 * For dynamic growth the aperture size is split into ranges of 128MB of
119 * DMA address space each. This struct represents one such range.
120 */
121struct aperture_range {
122
Joerg Roedel08c5fb92015-12-21 13:04:49 +0100123 spinlock_t bitmap_lock;
124
Joerg Roedel007b74b2015-12-21 12:53:54 +0100125 /* address allocation bitmap */
126 unsigned long *bitmap;
Joerg Roedelae62d492015-12-21 16:28:45 +0100127 unsigned long offset;
Joerg Roedel60e6a7c2015-12-21 16:53:17 +0100128 unsigned long next_bit;
Joerg Roedel007b74b2015-12-21 12:53:54 +0100129
130 /*
131 * Array of PTE pages for the aperture. In this array we save all the
132 * leaf pages of the domain page table used for the aperture. This way
133 * we don't need to walk the page table to find a specific PTE. We can
134 * just calculate its address in constant time.
135 */
136 u64 *pte_pages[64];
Joerg Roedel007b74b2015-12-21 12:53:54 +0100137};
138
139/*
140 * Data container for a dma_ops specific protection domain
141 */
142struct dma_ops_domain {
143 /* generic protection domain information */
144 struct protection_domain domain;
145
146 /* size of the aperture for the mappings */
147 unsigned long aperture_size;
148
Joerg Roedelebaecb42015-12-21 18:11:32 +0100149 /* aperture index we start searching for free addresses */
150 unsigned long next_index;
Joerg Roedel007b74b2015-12-21 12:53:54 +0100151
152 /* address space relevant data */
153 struct aperture_range *aperture[APERTURE_MAX_RANGES];
Joerg Roedel007b74b2015-12-21 12:53:54 +0100154};
155
Joerg Roedel15898bb2009-11-24 15:39:42 +0100156/****************************************************************************
157 *
158 * Helper functions
159 *
160 ****************************************************************************/
161
Joerg Roedel3f4b87b2015-03-26 13:43:07 +0100162static struct protection_domain *to_pdomain(struct iommu_domain *dom)
163{
164 return container_of(dom, struct protection_domain, domain);
165}
166
Joerg Roedelf62dda62011-06-09 12:55:35 +0200167static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200168{
169 struct iommu_dev_data *dev_data;
170 unsigned long flags;
171
172 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
173 if (!dev_data)
174 return NULL;
175
Joerg Roedelf62dda62011-06-09 12:55:35 +0200176 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200177
178 spin_lock_irqsave(&dev_data_list_lock, flags);
179 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
180 spin_unlock_irqrestore(&dev_data_list_lock, flags);
181
182 return dev_data;
183}
184
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200185static struct iommu_dev_data *search_dev_data(u16 devid)
186{
187 struct iommu_dev_data *dev_data;
188 unsigned long flags;
189
190 spin_lock_irqsave(&dev_data_list_lock, flags);
191 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
192 if (dev_data->devid == devid)
193 goto out_unlock;
194 }
195
196 dev_data = NULL;
197
198out_unlock:
199 spin_unlock_irqrestore(&dev_data_list_lock, flags);
200
201 return dev_data;
202}
203
204static struct iommu_dev_data *find_dev_data(u16 devid)
205{
206 struct iommu_dev_data *dev_data;
207
208 dev_data = search_dev_data(devid);
209
210 if (dev_data == NULL)
211 dev_data = alloc_dev_data(devid);
212
213 return dev_data;
214}
215
Joerg Roedel15898bb2009-11-24 15:39:42 +0100216static inline u16 get_device_id(struct device *dev)
217{
218 struct pci_dev *pdev = to_pci_dev(dev);
219
Shuah Khan6f2729b2013-02-27 17:07:30 -0700220 return PCI_DEVID(pdev->bus->number, pdev->devfn);
Joerg Roedel15898bb2009-11-24 15:39:42 +0100221}
222
Joerg Roedel657cbb62009-11-23 15:26:46 +0100223static struct iommu_dev_data *get_dev_data(struct device *dev)
224{
225 return dev->archdata.iommu;
226}
227
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100228static bool pci_iommuv2_capable(struct pci_dev *pdev)
229{
230 static const int caps[] = {
231 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100232 PCI_EXT_CAP_ID_PRI,
233 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100234 };
235 int i, pos;
236
237 for (i = 0; i < 3; ++i) {
238 pos = pci_find_ext_capability(pdev, caps[i]);
239 if (pos == 0)
240 return false;
241 }
242
243 return true;
244}
245
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100246static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
247{
248 struct iommu_dev_data *dev_data;
249
250 dev_data = get_dev_data(&pdev->dev);
251
252 return dev_data->errata & (1 << erratum) ? true : false;
253}
254
Joerg Roedel71c70982009-11-24 16:43:06 +0100255/*
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200256 * This function actually applies the mapping to the page table of the
257 * dma_ops domain.
Joerg Roedel71c70982009-11-24 16:43:06 +0100258 */
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200259static void alloc_unity_mapping(struct dma_ops_domain *dma_dom,
260 struct unity_map_entry *e)
Joerg Roedel71c70982009-11-24 16:43:06 +0100261{
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200262 u64 addr;
Joerg Roedel71c70982009-11-24 16:43:06 +0100263
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200264 for (addr = e->address_start; addr < e->address_end;
265 addr += PAGE_SIZE) {
266 if (addr < dma_dom->aperture_size)
267 __set_bit(addr >> PAGE_SHIFT,
268 dma_dom->aperture[0]->bitmap);
Joerg Roedel71c70982009-11-24 16:43:06 +0100269 }
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200270}
Joerg Roedel71c70982009-11-24 16:43:06 +0100271
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200272/*
273 * Inits the unity mappings required for a specific device
274 */
275static void init_unity_mappings_for_device(struct device *dev,
276 struct dma_ops_domain *dma_dom)
277{
278 struct unity_map_entry *e;
279 u16 devid;
Joerg Roedel71c70982009-11-24 16:43:06 +0100280
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200281 devid = get_device_id(dev);
282
283 list_for_each_entry(e, &amd_iommu_unity_map, list) {
284 if (!(devid >= e->devid_start && devid <= e->devid_end))
285 continue;
286 alloc_unity_mapping(dma_dom, e);
287 }
Joerg Roedel71c70982009-11-24 16:43:06 +0100288}
289
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100290/*
291 * This function checks if the driver got a valid device from the caller to
292 * avoid dereferencing invalid pointers.
293 */
294static bool check_device(struct device *dev)
295{
296 u16 devid;
297
298 if (!dev || !dev->dma_mask)
299 return false;
300
Yijing Wangb82a2272013-12-05 19:42:41 +0800301 /* No PCI device */
302 if (!dev_is_pci(dev))
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100303 return false;
304
305 devid = get_device_id(dev);
306
307 /* Out of our scope? */
308 if (devid > amd_iommu_last_bdf)
309 return false;
310
311 if (amd_iommu_rlookup_table[devid] == NULL)
312 return false;
313
314 return true;
315}
316
Alex Williamson25b11ce2014-09-19 10:03:13 -0600317static void init_iommu_group(struct device *dev)
Alex Williamson2851db22012-10-08 22:49:41 -0600318{
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200319 struct dma_ops_domain *dma_domain;
320 struct iommu_domain *domain;
Alex Williamson2851db22012-10-08 22:49:41 -0600321 struct iommu_group *group;
Alex Williamson2851db22012-10-08 22:49:41 -0600322
Alex Williamson65d53522014-07-03 09:51:30 -0600323 group = iommu_group_get_for_dev(dev);
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200324 if (IS_ERR(group))
325 return;
326
327 domain = iommu_group_default_domain(group);
328 if (!domain)
329 goto out;
330
331 dma_domain = to_pdomain(domain)->priv;
332
333 init_unity_mappings_for_device(dev, dma_domain);
334out:
335 iommu_group_put(group);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600336}
337
338static int iommu_init_device(struct device *dev)
339{
340 struct pci_dev *pdev = to_pci_dev(dev);
341 struct iommu_dev_data *dev_data;
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600342
343 if (dev->archdata.iommu)
344 return 0;
345
346 dev_data = find_dev_data(get_device_id(dev));
347 if (!dev_data)
348 return -ENOMEM;
349
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100350 if (pci_iommuv2_capable(pdev)) {
351 struct amd_iommu *iommu;
352
353 iommu = amd_iommu_rlookup_table[dev_data->devid];
354 dev_data->iommu_v2 = iommu->is_iommu_v2;
355 }
356
Joerg Roedel657cbb62009-11-23 15:26:46 +0100357 dev->archdata.iommu = dev_data;
358
Alex Williamson066f2e92014-06-12 16:12:37 -0600359 iommu_device_link(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
360 dev);
361
Joerg Roedel657cbb62009-11-23 15:26:46 +0100362 return 0;
363}
364
Joerg Roedel26018872011-06-06 16:50:14 +0200365static void iommu_ignore_device(struct device *dev)
366{
367 u16 devid, alias;
368
369 devid = get_device_id(dev);
370 alias = amd_iommu_alias_table[devid];
371
372 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
373 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
374
375 amd_iommu_rlookup_table[devid] = NULL;
376 amd_iommu_rlookup_table[alias] = NULL;
377}
378
Joerg Roedel657cbb62009-11-23 15:26:46 +0100379static void iommu_uninit_device(struct device *dev)
380{
Alex Williamsonc1931092014-07-03 09:51:24 -0600381 struct iommu_dev_data *dev_data = search_dev_data(get_device_id(dev));
382
383 if (!dev_data)
384 return;
385
Alex Williamson066f2e92014-06-12 16:12:37 -0600386 iommu_device_unlink(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
387 dev);
388
Alex Williamson9dcd6132012-05-30 14:19:07 -0600389 iommu_group_remove_device(dev);
390
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200391 /* Remove dma-ops */
392 dev->archdata.dma_ops = NULL;
393
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200394 /*
Alex Williamsonc1931092014-07-03 09:51:24 -0600395 * We keep dev_data around for unplugged devices and reuse it when the
396 * device is re-plugged - not doing so would introduce a ton of races.
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200397 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100398}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100399
Joerg Roedel7f265082008-12-12 13:50:21 +0100400#ifdef CONFIG_AMD_IOMMU_STATS
401
402/*
403 * Initialization code for statistics collection
404 */
405
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100406DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100407DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100408DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100409DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100410DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100411DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100412DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100413DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100414DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100415DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100416DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100417DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100418DECLARE_STATS_COUNTER(complete_ppr);
419DECLARE_STATS_COUNTER(invalidate_iotlb);
420DECLARE_STATS_COUNTER(invalidate_iotlb_all);
421DECLARE_STATS_COUNTER(pri_requests);
422
Joerg Roedel7f265082008-12-12 13:50:21 +0100423static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100424static struct dentry *de_fflush;
425
426static void amd_iommu_stats_add(struct __iommu_counter *cnt)
427{
428 if (stats_dir == NULL)
429 return;
430
431 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
432 &cnt->value);
433}
434
435static void amd_iommu_stats_init(void)
436{
437 stats_dir = debugfs_create_dir("amd-iommu", NULL);
438 if (stats_dir == NULL)
439 return;
440
Joerg Roedel7f265082008-12-12 13:50:21 +0100441 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
Dan Carpenter3775d482012-06-27 12:09:18 +0300442 &amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100443
444 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100445 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100446 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100447 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100448 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100449 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100450 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100451 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100452 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100453 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100454 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100455 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100456 amd_iommu_stats_add(&complete_ppr);
457 amd_iommu_stats_add(&invalidate_iotlb);
458 amd_iommu_stats_add(&invalidate_iotlb_all);
459 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100460}
461
462#endif
463
Joerg Roedel431b2a22008-07-11 17:14:22 +0200464/****************************************************************************
465 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200466 * Interrupt handling functions
467 *
468 ****************************************************************************/
469
Joerg Roedele3e59872009-09-03 14:02:10 +0200470static void dump_dte_entry(u16 devid)
471{
472 int i;
473
Joerg Roedelee6c2862011-11-09 12:06:03 +0100474 for (i = 0; i < 4; ++i)
475 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200476 amd_iommu_dev_table[devid].data[i]);
477}
478
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200479static void dump_command(unsigned long phys_addr)
480{
481 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
482 int i;
483
484 for (i = 0; i < 4; ++i)
485 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
486}
487
Joerg Roedela345b232009-09-03 15:01:43 +0200488static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200489{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200490 int type, devid, domid, flags;
491 volatile u32 *event = __evt;
492 int count = 0;
493 u64 address;
494
495retry:
496 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
497 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
498 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
499 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
500 address = (u64)(((u64)event[3]) << 32) | event[2];
501
502 if (type == 0) {
503 /* Did we hit the erratum? */
504 if (++count == LOOP_TIMEOUT) {
505 pr_err("AMD-Vi: No event written to event log\n");
506 return;
507 }
508 udelay(1);
509 goto retry;
510 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200511
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200512 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200513
514 switch (type) {
515 case EVENT_TYPE_ILL_DEV:
516 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
517 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700518 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200519 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200520 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200521 break;
522 case EVENT_TYPE_IO_FAULT:
523 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
524 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700525 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200526 domid, address, flags);
527 break;
528 case EVENT_TYPE_DEV_TAB_ERR:
529 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
530 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700531 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200532 address, flags);
533 break;
534 case EVENT_TYPE_PAGE_TAB_ERR:
535 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
536 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700537 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200538 domid, address, flags);
539 break;
540 case EVENT_TYPE_ILL_CMD:
541 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200542 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200543 break;
544 case EVENT_TYPE_CMD_HARD_ERR:
545 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
546 "flags=0x%04x]\n", address, flags);
547 break;
548 case EVENT_TYPE_IOTLB_INV_TO:
549 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
550 "address=0x%016llx]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700551 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200552 address);
553 break;
554 case EVENT_TYPE_INV_DEV_REQ:
555 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
556 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700557 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200558 address, flags);
559 break;
560 default:
561 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
562 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200563
564 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200565}
566
567static void iommu_poll_events(struct amd_iommu *iommu)
568{
569 u32 head, tail;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200570
571 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
572 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
573
574 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200575 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200576 head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200577 }
578
579 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200580}
581
Joerg Roedeleee53532012-06-01 15:20:23 +0200582static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100583{
584 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100585
Joerg Roedel399be2f2011-12-01 16:53:47 +0100586 INC_STATS_COUNTER(pri_requests);
587
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100588 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
589 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
590 return;
591 }
592
593 fault.address = raw[1];
594 fault.pasid = PPR_PASID(raw[0]);
595 fault.device_id = PPR_DEVID(raw[0]);
596 fault.tag = PPR_TAG(raw[0]);
597 fault.flags = PPR_FLAGS(raw[0]);
598
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100599 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
600}
601
602static void iommu_poll_ppr_log(struct amd_iommu *iommu)
603{
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100604 u32 head, tail;
605
606 if (iommu->ppr_log == NULL)
607 return;
608
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100609 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
610 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
611
612 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200613 volatile u64 *raw;
614 u64 entry[2];
615 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100616
Joerg Roedeleee53532012-06-01 15:20:23 +0200617 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100618
Joerg Roedeleee53532012-06-01 15:20:23 +0200619 /*
620 * Hardware bug: Interrupt may arrive before the entry is
621 * written to memory. If this happens we need to wait for the
622 * entry to arrive.
623 */
624 for (i = 0; i < LOOP_TIMEOUT; ++i) {
625 if (PPR_REQ_TYPE(raw[0]) != 0)
626 break;
627 udelay(1);
628 }
629
630 /* Avoid memcpy function-call overhead */
631 entry[0] = raw[0];
632 entry[1] = raw[1];
633
634 /*
635 * To detect the hardware bug we need to clear the entry
636 * back to zero.
637 */
638 raw[0] = raw[1] = 0UL;
639
640 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100641 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
642 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200643
Joerg Roedeleee53532012-06-01 15:20:23 +0200644 /* Handle PPR entry */
645 iommu_handle_ppr_entry(iommu, entry);
646
Joerg Roedeleee53532012-06-01 15:20:23 +0200647 /* Refresh ring-buffer information */
648 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100649 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
650 }
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100651}
652
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200653irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200654{
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500655 struct amd_iommu *iommu = (struct amd_iommu *) data;
656 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200657
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500658 while (status & (MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK)) {
659 /* Enable EVT and PPR interrupts again */
660 writel((MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK),
661 iommu->mmio_base + MMIO_STATUS_OFFSET);
662
663 if (status & MMIO_STATUS_EVT_INT_MASK) {
664 pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
665 iommu_poll_events(iommu);
666 }
667
668 if (status & MMIO_STATUS_PPR_INT_MASK) {
669 pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
670 iommu_poll_ppr_log(iommu);
671 }
672
673 /*
674 * Hardware bug: ERBT1312
675 * When re-enabling interrupt (by writing 1
676 * to clear the bit), the hardware might also try to set
677 * the interrupt bit in the event status register.
678 * In this scenario, the bit will be set, and disable
679 * subsequent interrupts.
680 *
681 * Workaround: The IOMMU driver should read back the
682 * status register and check if the interrupt bits are cleared.
683 * If not, driver will need to go through the interrupt handler
684 * again and re-clear the bits
685 */
686 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100687 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200688 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200689}
690
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200691irqreturn_t amd_iommu_int_handler(int irq, void *data)
692{
693 return IRQ_WAKE_THREAD;
694}
695
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200696/****************************************************************************
697 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200698 * IOMMU command queuing functions
699 *
700 ****************************************************************************/
701
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200702static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200703{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200704 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200705
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200706 while (*sem == 0 && i < LOOP_TIMEOUT) {
707 udelay(1);
708 i += 1;
709 }
710
711 if (i == LOOP_TIMEOUT) {
712 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
713 return -EIO;
714 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200715
716 return 0;
717}
718
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200719static void copy_cmd_to_buffer(struct amd_iommu *iommu,
720 struct iommu_cmd *cmd,
721 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200722{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200723 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200724
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200725 target = iommu->cmd_buf + tail;
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200726 tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200727
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200728 /* Copy command to buffer */
729 memcpy(target, cmd, sizeof(*cmd));
730
731 /* Tell the IOMMU about it */
732 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
733}
734
Joerg Roedel815b33f2011-04-06 17:26:49 +0200735static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200736{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200737 WARN_ON(address & 0x7ULL);
738
Joerg Roedelded46732011-04-06 10:53:48 +0200739 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200740 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
741 cmd->data[1] = upper_32_bits(__pa(address));
742 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200743 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
744}
745
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200746static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
747{
748 memset(cmd, 0, sizeof(*cmd));
749 cmd->data[0] = devid;
750 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
751}
752
Joerg Roedel11b64022011-04-06 11:49:28 +0200753static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
754 size_t size, u16 domid, int pde)
755{
756 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100757 bool s;
Joerg Roedel11b64022011-04-06 11:49:28 +0200758
759 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100760 s = false;
Joerg Roedel11b64022011-04-06 11:49:28 +0200761
762 if (pages > 1) {
763 /*
764 * If we have to flush more than one page, flush all
765 * TLB entries for this domain
766 */
767 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100768 s = true;
Joerg Roedel11b64022011-04-06 11:49:28 +0200769 }
770
771 address &= PAGE_MASK;
772
773 memset(cmd, 0, sizeof(*cmd));
774 cmd->data[1] |= domid;
775 cmd->data[2] = lower_32_bits(address);
776 cmd->data[3] = upper_32_bits(address);
777 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
778 if (s) /* size bit - we flush more than one 4kb page */
779 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200780 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200781 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
782}
783
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200784static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
785 u64 address, size_t size)
786{
787 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100788 bool s;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200789
790 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100791 s = false;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200792
793 if (pages > 1) {
794 /*
795 * If we have to flush more than one page, flush all
796 * TLB entries for this domain
797 */
798 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100799 s = true;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200800 }
801
802 address &= PAGE_MASK;
803
804 memset(cmd, 0, sizeof(*cmd));
805 cmd->data[0] = devid;
806 cmd->data[0] |= (qdep & 0xff) << 24;
807 cmd->data[1] = devid;
808 cmd->data[2] = lower_32_bits(address);
809 cmd->data[3] = upper_32_bits(address);
810 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
811 if (s)
812 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
813}
814
Joerg Roedel22e266c2011-11-21 15:59:08 +0100815static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
816 u64 address, bool size)
817{
818 memset(cmd, 0, sizeof(*cmd));
819
820 address &= ~(0xfffULL);
821
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600822 cmd->data[0] = pasid;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100823 cmd->data[1] = domid;
824 cmd->data[2] = lower_32_bits(address);
825 cmd->data[3] = upper_32_bits(address);
826 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
827 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
828 if (size)
829 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
830 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
831}
832
833static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
834 int qdep, u64 address, bool size)
835{
836 memset(cmd, 0, sizeof(*cmd));
837
838 address &= ~(0xfffULL);
839
840 cmd->data[0] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600841 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100842 cmd->data[0] |= (qdep & 0xff) << 24;
843 cmd->data[1] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600844 cmd->data[1] |= (pasid & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100845 cmd->data[2] = lower_32_bits(address);
846 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
847 cmd->data[3] = upper_32_bits(address);
848 if (size)
849 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
850 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
851}
852
Joerg Roedelc99afa22011-11-21 18:19:25 +0100853static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
854 int status, int tag, bool gn)
855{
856 memset(cmd, 0, sizeof(*cmd));
857
858 cmd->data[0] = devid;
859 if (gn) {
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600860 cmd->data[1] = pasid;
Joerg Roedelc99afa22011-11-21 18:19:25 +0100861 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
862 }
863 cmd->data[3] = tag & 0x1ff;
864 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
865
866 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
867}
868
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200869static void build_inv_all(struct iommu_cmd *cmd)
870{
871 memset(cmd, 0, sizeof(*cmd));
872 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200873}
874
Joerg Roedel7ef27982012-06-21 16:46:04 +0200875static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
876{
877 memset(cmd, 0, sizeof(*cmd));
878 cmd->data[0] = devid;
879 CMD_SET_TYPE(cmd, CMD_INV_IRT);
880}
881
Joerg Roedel431b2a22008-07-11 17:14:22 +0200882/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200883 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200884 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200885 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200886static int iommu_queue_command_sync(struct amd_iommu *iommu,
887 struct iommu_cmd *cmd,
888 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200889{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200890 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200891 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200892
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200893again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200894 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200895
896 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
897 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200898 next_tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
899 left = (head - next_tail) % CMD_BUFFER_SIZE;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200900
901 if (left <= 2) {
902 struct iommu_cmd sync_cmd;
903 volatile u64 sem = 0;
904 int ret;
905
906 build_completion_wait(&sync_cmd, (u64)&sem);
907 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
908
909 spin_unlock_irqrestore(&iommu->lock, flags);
910
911 if ((ret = wait_on_sem(&sem)) != 0)
912 return ret;
913
914 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200915 }
916
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200917 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200918
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200919 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200920 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200921
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200922 spin_unlock_irqrestore(&iommu->lock, flags);
923
Joerg Roedel815b33f2011-04-06 17:26:49 +0200924 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100925}
926
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200927static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
928{
929 return iommu_queue_command_sync(iommu, cmd, true);
930}
931
Joerg Roedel8d201962008-12-02 20:34:41 +0100932/*
933 * This function queues a completion wait command into the command
934 * buffer of an IOMMU
935 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100936static int iommu_completion_wait(struct amd_iommu *iommu)
937{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200938 struct iommu_cmd cmd;
939 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200940 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100941
942 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200943 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100944
Joerg Roedel815b33f2011-04-06 17:26:49 +0200945 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100946
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200947 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +0100948 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200949 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100950
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200951 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200952}
953
Joerg Roedeld8c13082011-04-06 18:51:26 +0200954static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200955{
956 struct iommu_cmd cmd;
957
Joerg Roedeld8c13082011-04-06 18:51:26 +0200958 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200959
Joerg Roedeld8c13082011-04-06 18:51:26 +0200960 return iommu_queue_command(iommu, &cmd);
961}
962
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200963static void iommu_flush_dte_all(struct amd_iommu *iommu)
964{
965 u32 devid;
966
967 for (devid = 0; devid <= 0xffff; ++devid)
968 iommu_flush_dte(iommu, devid);
969
970 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200971}
972
973/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200974 * This function uses heavy locking and may disable irqs for some time. But
975 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200976 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200977static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200978{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200979 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200980
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200981 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
982 struct iommu_cmd cmd;
983 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
984 dom_id, 1);
985 iommu_queue_command(iommu, &cmd);
986 }
Joerg Roedel431b2a22008-07-11 17:14:22 +0200987
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200988 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200989}
990
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200991static void iommu_flush_all(struct amd_iommu *iommu)
992{
993 struct iommu_cmd cmd;
994
995 build_inv_all(&cmd);
996
997 iommu_queue_command(iommu, &cmd);
998 iommu_completion_wait(iommu);
999}
1000
Joerg Roedel7ef27982012-06-21 16:46:04 +02001001static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1002{
1003 struct iommu_cmd cmd;
1004
1005 build_inv_irt(&cmd, devid);
1006
1007 iommu_queue_command(iommu, &cmd);
1008}
1009
1010static void iommu_flush_irt_all(struct amd_iommu *iommu)
1011{
1012 u32 devid;
1013
1014 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1015 iommu_flush_irt(iommu, devid);
1016
1017 iommu_completion_wait(iommu);
1018}
1019
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001020void iommu_flush_all_caches(struct amd_iommu *iommu)
1021{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001022 if (iommu_feature(iommu, FEATURE_IA)) {
1023 iommu_flush_all(iommu);
1024 } else {
1025 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001026 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001027 iommu_flush_tlb_all(iommu);
1028 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001029}
1030
Joerg Roedel431b2a22008-07-11 17:14:22 +02001031/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001032 * Command send function for flushing on-device TLB
1033 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001034static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1035 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001036{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001037 struct amd_iommu *iommu;
1038 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001039 int qdep;
1040
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001041 qdep = dev_data->ats.qdep;
1042 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001043
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001044 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001045
1046 return iommu_queue_command(iommu, &cmd);
1047}
1048
1049/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001050 * Command send function for invalidating a device table entry
1051 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001052static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001053{
1054 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001055 u16 alias;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001056 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001057
Joerg Roedel6c542042011-06-09 17:07:31 +02001058 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele25bfb52015-10-20 17:33:38 +02001059 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001060
Joerg Roedelf62dda62011-06-09 12:55:35 +02001061 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001062 if (!ret && alias != dev_data->devid)
1063 ret = iommu_flush_dte(iommu, alias);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001064 if (ret)
1065 return ret;
1066
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001067 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001068 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001069
1070 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001071}
1072
Joerg Roedel431b2a22008-07-11 17:14:22 +02001073/*
1074 * TLB invalidation function which is called from the mapping functions.
1075 * It invalidates a single PTE if the range to flush is within a single
1076 * page. Otherwise it flushes the whole TLB of the IOMMU.
1077 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001078static void __domain_flush_pages(struct protection_domain *domain,
1079 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001080{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001081 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001082 struct iommu_cmd cmd;
1083 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001084
Joerg Roedel11b64022011-04-06 11:49:28 +02001085 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001086
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001087 for (i = 0; i < amd_iommus_present; ++i) {
1088 if (!domain->dev_iommu[i])
1089 continue;
1090
1091 /*
1092 * Devices of this domain are behind this IOMMU
1093 * We need a TLB flush
1094 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001095 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001096 }
1097
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001098 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001099
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001100 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001101 continue;
1102
Joerg Roedel6c542042011-06-09 17:07:31 +02001103 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001104 }
1105
Joerg Roedel11b64022011-04-06 11:49:28 +02001106 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001107}
1108
Joerg Roedel17b124b2011-04-06 18:01:35 +02001109static void domain_flush_pages(struct protection_domain *domain,
1110 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001111{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001112 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001113}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001114
Joerg Roedel1c655772008-09-04 18:40:05 +02001115/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001116static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001117{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001118 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001119}
1120
Chris Wright42a49f92009-06-15 15:42:00 +02001121/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001122static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001123{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001124 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1125}
1126
1127static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001128{
1129 int i;
1130
1131 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 to wait for completion of all commands.
1138 */
1139 iommu_completion_wait(amd_iommus[i]);
1140 }
1141}
1142
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001143
Joerg Roedel43f49602008-12-02 21:01:12 +01001144/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001145 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001146 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001147static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001148{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001149 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001150
1151 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001152 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001153}
1154
Joerg Roedel431b2a22008-07-11 17:14:22 +02001155/****************************************************************************
1156 *
1157 * The functions below are used the create the page table mappings for
1158 * unity mapped regions.
1159 *
1160 ****************************************************************************/
1161
1162/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001163 * This function is used to add another level to an IO page table. Adding
1164 * another level increases the size of the address space by 9 bits to a size up
1165 * to 64 bits.
1166 */
1167static bool increase_address_space(struct protection_domain *domain,
1168 gfp_t gfp)
1169{
1170 u64 *pte;
1171
1172 if (domain->mode == PAGE_MODE_6_LEVEL)
1173 /* address space already 64 bit large */
1174 return false;
1175
1176 pte = (void *)get_zeroed_page(gfp);
1177 if (!pte)
1178 return false;
1179
1180 *pte = PM_LEVEL_PDE(domain->mode,
1181 virt_to_phys(domain->pt_root));
1182 domain->pt_root = pte;
1183 domain->mode += 1;
1184 domain->updated = true;
1185
1186 return true;
1187}
1188
1189static u64 *alloc_pte(struct protection_domain *domain,
1190 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001191 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001192 u64 **pte_page,
1193 gfp_t gfp)
1194{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001195 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001196 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001197
1198 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001199
1200 while (address > PM_LEVEL_SIZE(domain->mode))
1201 increase_address_space(domain, gfp);
1202
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001203 level = domain->mode - 1;
1204 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1205 address = PAGE_SIZE_ALIGN(address, page_size);
1206 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001207
1208 while (level > end_lvl) {
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001209 u64 __pte, __npte;
1210
1211 __pte = *pte;
1212
1213 if (!IOMMU_PTE_PRESENT(__pte)) {
Joerg Roedel308973d2009-11-24 17:43:32 +01001214 page = (u64 *)get_zeroed_page(gfp);
1215 if (!page)
1216 return NULL;
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001217
1218 __npte = PM_LEVEL_PDE(level, virt_to_phys(page));
1219
1220 if (cmpxchg64(pte, __pte, __npte)) {
1221 free_page((unsigned long)page);
1222 continue;
1223 }
Joerg Roedel308973d2009-11-24 17:43:32 +01001224 }
1225
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001226 /* No level skipping support yet */
1227 if (PM_PTE_LEVEL(*pte) != level)
1228 return NULL;
1229
Joerg Roedel308973d2009-11-24 17:43:32 +01001230 level -= 1;
1231
1232 pte = IOMMU_PTE_PAGE(*pte);
1233
1234 if (pte_page && level == end_lvl)
1235 *pte_page = pte;
1236
1237 pte = &pte[PM_LEVEL_INDEX(level, address)];
1238 }
1239
1240 return pte;
1241}
1242
1243/*
1244 * This function checks if there is a PTE for a given dma address. If
1245 * there is one, it returns the pointer to it.
1246 */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001247static u64 *fetch_pte(struct protection_domain *domain,
1248 unsigned long address,
1249 unsigned long *page_size)
Joerg Roedel308973d2009-11-24 17:43:32 +01001250{
1251 int level;
1252 u64 *pte;
1253
Joerg Roedel24cd7722010-01-19 17:27:39 +01001254 if (address > PM_LEVEL_SIZE(domain->mode))
1255 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001256
Joerg Roedel3039ca12015-04-01 14:58:48 +02001257 level = domain->mode - 1;
1258 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1259 *page_size = PTE_LEVEL_PAGE_SIZE(level);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001260
1261 while (level > 0) {
1262
1263 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001264 if (!IOMMU_PTE_PRESENT(*pte))
1265 return NULL;
1266
Joerg Roedel24cd7722010-01-19 17:27:39 +01001267 /* Large PTE */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001268 if (PM_PTE_LEVEL(*pte) == 7 ||
1269 PM_PTE_LEVEL(*pte) == 0)
1270 break;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001271
1272 /* No level skipping support yet */
1273 if (PM_PTE_LEVEL(*pte) != level)
1274 return NULL;
1275
Joerg Roedel308973d2009-11-24 17:43:32 +01001276 level -= 1;
1277
Joerg Roedel24cd7722010-01-19 17:27:39 +01001278 /* Walk to the next level */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001279 pte = IOMMU_PTE_PAGE(*pte);
1280 pte = &pte[PM_LEVEL_INDEX(level, address)];
1281 *page_size = PTE_LEVEL_PAGE_SIZE(level);
1282 }
1283
1284 if (PM_PTE_LEVEL(*pte) == 0x07) {
1285 unsigned long pte_mask;
1286
1287 /*
1288 * If we have a series of large PTEs, make
1289 * sure to return a pointer to the first one.
1290 */
1291 *page_size = pte_mask = PTE_PAGE_SIZE(*pte);
1292 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1293 pte = (u64 *)(((unsigned long)pte) & pte_mask);
Joerg Roedel308973d2009-11-24 17:43:32 +01001294 }
1295
1296 return pte;
1297}
1298
1299/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001300 * Generic mapping functions. It maps a physical address into a DMA
1301 * address space. It allocates the page table pages if necessary.
1302 * In the future it can be extended to a generic mapping function
1303 * supporting all features of AMD IOMMU page tables like level skipping
1304 * and full 64 bit address spaces.
1305 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001306static int iommu_map_page(struct protection_domain *dom,
1307 unsigned long bus_addr,
1308 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001309 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001310 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001311{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001312 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001313 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001314
Joerg Roedeld4b03662015-04-01 14:58:52 +02001315 BUG_ON(!IS_ALIGNED(bus_addr, page_size));
1316 BUG_ON(!IS_ALIGNED(phys_addr, page_size));
1317
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001318 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001319 return -EINVAL;
1320
Joerg Roedeld4b03662015-04-01 14:58:52 +02001321 count = PAGE_SIZE_PTE_COUNT(page_size);
1322 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001323
Maurizio Lombardi63eaa752014-09-11 12:28:03 +02001324 if (!pte)
1325 return -ENOMEM;
1326
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001327 for (i = 0; i < count; ++i)
1328 if (IOMMU_PTE_PRESENT(pte[i]))
1329 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001330
Joerg Roedeld4b03662015-04-01 14:58:52 +02001331 if (count > 1) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001332 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1333 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1334 } else
1335 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1336
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001337 if (prot & IOMMU_PROT_IR)
1338 __pte |= IOMMU_PTE_IR;
1339 if (prot & IOMMU_PROT_IW)
1340 __pte |= IOMMU_PTE_IW;
1341
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001342 for (i = 0; i < count; ++i)
1343 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001344
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001345 update_domain(dom);
1346
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001347 return 0;
1348}
1349
Joerg Roedel24cd7722010-01-19 17:27:39 +01001350static unsigned long iommu_unmap_page(struct protection_domain *dom,
1351 unsigned long bus_addr,
1352 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001353{
Joerg Roedel71b390e2015-04-01 14:58:49 +02001354 unsigned long long unmapped;
1355 unsigned long unmap_size;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001356 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001357
Joerg Roedel24cd7722010-01-19 17:27:39 +01001358 BUG_ON(!is_power_of_2(page_size));
1359
1360 unmapped = 0;
1361
1362 while (unmapped < page_size) {
1363
Joerg Roedel71b390e2015-04-01 14:58:49 +02001364 pte = fetch_pte(dom, bus_addr, &unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001365
Joerg Roedel71b390e2015-04-01 14:58:49 +02001366 if (pte) {
1367 int i, count;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001368
Joerg Roedel71b390e2015-04-01 14:58:49 +02001369 count = PAGE_SIZE_PTE_COUNT(unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001370 for (i = 0; i < count; i++)
1371 pte[i] = 0ULL;
1372 }
1373
1374 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1375 unmapped += unmap_size;
1376 }
1377
Alex Williamson60d0ca32013-06-21 14:33:19 -06001378 BUG_ON(unmapped && !is_power_of_2(unmapped));
Joerg Roedel24cd7722010-01-19 17:27:39 +01001379
1380 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001381}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001382
Joerg Roedel431b2a22008-07-11 17:14:22 +02001383/****************************************************************************
1384 *
1385 * The next functions belong to the address allocator for the dma_ops
1386 * interface functions. They work like the allocators in the other IOMMU
1387 * drivers. Its basically a bitmap which marks the allocated pages in
1388 * the aperture. Maybe it could be enhanced in the future to a more
1389 * efficient allocator.
1390 *
1391 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001392
Joerg Roedel431b2a22008-07-11 17:14:22 +02001393/*
Joerg Roedel384de722009-05-15 12:30:05 +02001394 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001395 *
1396 * called with domain->lock held
1397 */
Joerg Roedel384de722009-05-15 12:30:05 +02001398
Joerg Roedel9cabe892009-05-18 16:38:55 +02001399/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001400 * Used to reserve address ranges in the aperture (e.g. for exclusion
1401 * ranges.
1402 */
1403static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1404 unsigned long start_page,
1405 unsigned int pages)
1406{
1407 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1408
1409 if (start_page + pages > last_page)
1410 pages = last_page - start_page;
1411
1412 for (i = start_page; i < start_page + pages; ++i) {
1413 int index = i / APERTURE_RANGE_PAGES;
1414 int page = i % APERTURE_RANGE_PAGES;
1415 __set_bit(page, dom->aperture[index]->bitmap);
1416 }
1417}
1418
1419/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001420 * This function is used to add a new aperture range to an existing
1421 * aperture in case of dma_ops domain allocation or address allocation
1422 * failure.
1423 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001424static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001425 bool populate, gfp_t gfp)
1426{
1427 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001428 unsigned long i, old_size, pte_pgsize;
Joerg Roedela73c1562015-12-21 19:25:56 +01001429 struct aperture_range *range;
1430 struct amd_iommu *iommu;
1431 unsigned long flags;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001432
Joerg Roedelf5e97052009-05-22 12:31:53 +02001433#ifdef CONFIG_IOMMU_STRESS
1434 populate = false;
1435#endif
1436
Joerg Roedel9cabe892009-05-18 16:38:55 +02001437 if (index >= APERTURE_MAX_RANGES)
1438 return -ENOMEM;
1439
Joerg Roedela73c1562015-12-21 19:25:56 +01001440 range = kzalloc(sizeof(struct aperture_range), gfp);
1441 if (!range)
Joerg Roedel9cabe892009-05-18 16:38:55 +02001442 return -ENOMEM;
1443
Joerg Roedela73c1562015-12-21 19:25:56 +01001444 range->bitmap = (void *)get_zeroed_page(gfp);
1445 if (!range->bitmap)
Joerg Roedel9cabe892009-05-18 16:38:55 +02001446 goto out_free;
1447
Joerg Roedela73c1562015-12-21 19:25:56 +01001448 range->offset = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001449
Joerg Roedela73c1562015-12-21 19:25:56 +01001450 spin_lock_init(&range->bitmap_lock);
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001451
Joerg Roedel9cabe892009-05-18 16:38:55 +02001452 if (populate) {
1453 unsigned long address = dma_dom->aperture_size;
1454 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1455 u64 *pte, *pte_page;
1456
1457 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001458 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001459 &pte_page, gfp);
1460 if (!pte)
1461 goto out_free;
1462
Joerg Roedela73c1562015-12-21 19:25:56 +01001463 range->pte_pages[i] = pte_page;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001464
1465 address += APERTURE_RANGE_SIZE / 64;
1466 }
1467 }
1468
Joerg Roedela73c1562015-12-21 19:25:56 +01001469 /* First take the bitmap_lock and then publish the range */
1470 spin_lock_irqsave(&range->bitmap_lock, flags);
1471
1472 old_size = dma_dom->aperture_size;
1473 dma_dom->aperture[index] = range;
1474 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001475
Joerg Roedel17f5b562011-07-06 17:14:44 +02001476 /* Reserve address range used for MSI messages */
1477 if (old_size < MSI_ADDR_BASE_LO &&
1478 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1479 unsigned long spage;
1480 int pages;
1481
1482 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1483 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1484
1485 dma_ops_reserve_addresses(dma_dom, spage, pages);
1486 }
1487
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001488 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001489 for_each_iommu(iommu) {
1490 if (iommu->exclusion_start &&
1491 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1492 && iommu->exclusion_start < dma_dom->aperture_size) {
1493 unsigned long startpage;
1494 int pages = iommu_num_pages(iommu->exclusion_start,
1495 iommu->exclusion_length,
1496 PAGE_SIZE);
1497 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1498 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1499 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001500 }
1501
1502 /*
1503 * Check for areas already mapped as present in the new aperture
1504 * range and mark those pages as reserved in the allocator. Such
1505 * mappings may already exist as a result of requested unity
1506 * mappings for devices.
1507 */
1508 for (i = dma_dom->aperture[index]->offset;
1509 i < dma_dom->aperture_size;
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001510 i += pte_pgsize) {
Joerg Roedel3039ca12015-04-01 14:58:48 +02001511 u64 *pte = fetch_pte(&dma_dom->domain, i, &pte_pgsize);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001512 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1513 continue;
1514
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001515 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT,
1516 pte_pgsize >> 12);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001517 }
1518
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001519 update_domain(&dma_dom->domain);
1520
Joerg Roedela73c1562015-12-21 19:25:56 +01001521 spin_unlock_irqrestore(&range->bitmap_lock, flags);
1522
Joerg Roedel9cabe892009-05-18 16:38:55 +02001523 return 0;
1524
1525out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001526 update_domain(&dma_dom->domain);
1527
Joerg Roedela73c1562015-12-21 19:25:56 +01001528 free_page((unsigned long)range->bitmap);
Joerg Roedel9cabe892009-05-18 16:38:55 +02001529
Joerg Roedela73c1562015-12-21 19:25:56 +01001530 kfree(range);
Joerg Roedel9cabe892009-05-18 16:38:55 +02001531
1532 return -ENOMEM;
1533}
1534
Joerg Roedelccb50e02015-12-21 17:49:34 +01001535static dma_addr_t dma_ops_aperture_alloc(struct dma_ops_domain *dom,
1536 struct aperture_range *range,
Joerg Roedela0f51442015-12-21 16:20:09 +01001537 unsigned long pages,
Joerg Roedela0f51442015-12-21 16:20:09 +01001538 unsigned long dma_mask,
1539 unsigned long boundary_size,
1540 unsigned long align_mask)
1541{
1542 unsigned long offset, limit, flags;
1543 dma_addr_t address;
Joerg Roedelccb50e02015-12-21 17:49:34 +01001544 bool flush = false;
Joerg Roedela0f51442015-12-21 16:20:09 +01001545
1546 offset = range->offset >> PAGE_SHIFT;
1547 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1548 dma_mask >> PAGE_SHIFT);
1549
1550 spin_lock_irqsave(&range->bitmap_lock, flags);
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001551 address = iommu_area_alloc(range->bitmap, limit, range->next_bit,
1552 pages, offset, boundary_size, align_mask);
Joerg Roedelccb50e02015-12-21 17:49:34 +01001553 if (address == -1) {
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001554 /* Nothing found, retry one time */
1555 address = iommu_area_alloc(range->bitmap, limit,
1556 0, pages, offset, boundary_size,
1557 align_mask);
Joerg Roedelccb50e02015-12-21 17:49:34 +01001558 flush = true;
1559 }
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001560
1561 if (address != -1)
1562 range->next_bit = address + pages;
1563
Joerg Roedela0f51442015-12-21 16:20:09 +01001564 spin_unlock_irqrestore(&range->bitmap_lock, flags);
1565
Joerg Roedelccb50e02015-12-21 17:49:34 +01001566 if (flush) {
1567 domain_flush_tlb(&dom->domain);
1568 domain_flush_complete(&dom->domain);
1569 }
1570
Joerg Roedela0f51442015-12-21 16:20:09 +01001571 return address;
1572}
1573
Joerg Roedel384de722009-05-15 12:30:05 +02001574static unsigned long dma_ops_area_alloc(struct device *dev,
1575 struct dma_ops_domain *dom,
1576 unsigned int pages,
1577 unsigned long align_mask,
Joerg Roedel05ab49e2015-12-21 17:58:26 +01001578 u64 dma_mask)
Joerg Roedel384de722009-05-15 12:30:05 +02001579{
Joerg Roedelab7032b2015-12-21 18:47:11 +01001580 unsigned long boundary_size, mask;
Joerg Roedel384de722009-05-15 12:30:05 +02001581 unsigned long address = -1;
Joerg Roedel2a874422015-12-21 18:34:47 +01001582 int start = dom->next_index;
1583 int i;
Joerg Roedel384de722009-05-15 12:30:05 +02001584
Joerg Roedele6aabee2015-05-27 09:26:09 +02001585 mask = dma_get_seg_boundary(dev);
1586
1587 boundary_size = mask + 1 ? ALIGN(mask + 1, PAGE_SIZE) >> PAGE_SHIFT :
1588 1UL << (BITS_PER_LONG - PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001589
Joerg Roedel2a874422015-12-21 18:34:47 +01001590 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1591 struct aperture_range *range;
Joerg Roedelccb50e02015-12-21 17:49:34 +01001592
Joerg Roedel2a874422015-12-21 18:34:47 +01001593 range = dom->aperture[(start + i) % APERTURE_MAX_RANGES];
1594
1595 if (!range || range->offset >= dma_mask)
1596 continue;
Joerg Roedel384de722009-05-15 12:30:05 +02001597
Joerg Roedel2a874422015-12-21 18:34:47 +01001598 address = dma_ops_aperture_alloc(dom, range, pages,
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001599 dma_mask, boundary_size,
1600 align_mask);
Joerg Roedel384de722009-05-15 12:30:05 +02001601 if (address != -1) {
Joerg Roedel2a874422015-12-21 18:34:47 +01001602 address = range->offset + (address << PAGE_SHIFT);
Joerg Roedelebaecb42015-12-21 18:11:32 +01001603 dom->next_index = i;
Joerg Roedel384de722009-05-15 12:30:05 +02001604 break;
1605 }
Joerg Roedel384de722009-05-15 12:30:05 +02001606 }
1607
1608 return address;
1609}
1610
Joerg Roedeld3086442008-06-26 21:27:57 +02001611static unsigned long dma_ops_alloc_addresses(struct device *dev,
1612 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001613 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001614 unsigned long align_mask,
1615 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001616{
Joerg Roedel266a3bd2015-12-21 18:54:24 +01001617 unsigned long address = -1;
Joerg Roedeld3086442008-06-26 21:27:57 +02001618
Joerg Roedelfe16f082009-05-22 12:27:53 +02001619#ifdef CONFIG_IOMMU_STRESS
Joerg Roedelebaecb42015-12-21 18:11:32 +01001620 dom->next_index = 0;
Joerg Roedelfe16f082009-05-22 12:27:53 +02001621#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001622
Joerg Roedel266a3bd2015-12-21 18:54:24 +01001623 while (address == -1) {
1624 address = dma_ops_area_alloc(dev, dom, pages,
1625 align_mask, dma_mask);
1626
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001627 if (address == -1 && alloc_new_range(dom, false, GFP_ATOMIC))
Joerg Roedel266a3bd2015-12-21 18:54:24 +01001628 break;
1629 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001630
Joerg Roedel384de722009-05-15 12:30:05 +02001631 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001632 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001633
1634 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1635
1636 return address;
1637}
1638
Joerg Roedel431b2a22008-07-11 17:14:22 +02001639/*
1640 * The address free function.
1641 *
1642 * called with domain->lock held
1643 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001644static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1645 unsigned long address,
1646 unsigned int pages)
1647{
Joerg Roedel384de722009-05-15 12:30:05 +02001648 unsigned i = address >> APERTURE_RANGE_SHIFT;
1649 struct aperture_range *range = dom->aperture[i];
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001650 unsigned long flags;
Joerg Roedel80be3082008-11-06 14:59:05 +01001651
Joerg Roedel384de722009-05-15 12:30:05 +02001652 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1653
Joerg Roedel47bccd62009-05-22 12:40:54 +02001654#ifdef CONFIG_IOMMU_STRESS
1655 if (i < 4)
1656 return;
1657#endif
1658
Joerg Roedel4eeca8c2015-12-22 12:15:35 +01001659 if (amd_iommu_unmap_flush) {
Joerg Roedeld41ab092015-12-21 18:20:03 +01001660 domain_flush_tlb(&dom->domain);
1661 domain_flush_complete(&dom->domain);
1662 }
Joerg Roedel384de722009-05-15 12:30:05 +02001663
1664 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001665
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001666 spin_lock_irqsave(&range->bitmap_lock, flags);
Joerg Roedel4eeca8c2015-12-22 12:15:35 +01001667 if (address + pages > range->next_bit)
1668 range->next_bit = address + pages;
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001669 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001670 spin_unlock_irqrestore(&range->bitmap_lock, flags);
Joerg Roedel384de722009-05-15 12:30:05 +02001671
Joerg Roedeld3086442008-06-26 21:27:57 +02001672}
1673
Joerg Roedel431b2a22008-07-11 17:14:22 +02001674/****************************************************************************
1675 *
1676 * The next functions belong to the domain allocation. A domain is
1677 * allocated for every IOMMU as the default domain. If device isolation
1678 * is enabled, every device get its own domain. The most important thing
1679 * about domains is the page table mapping the DMA address space they
1680 * contain.
1681 *
1682 ****************************************************************************/
1683
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001684/*
1685 * This function adds a protection domain to the global protection domain list
1686 */
1687static void add_domain_to_list(struct protection_domain *domain)
1688{
1689 unsigned long flags;
1690
1691 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1692 list_add(&domain->list, &amd_iommu_pd_list);
1693 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1694}
1695
1696/*
1697 * This function removes a protection domain to the global
1698 * protection domain list
1699 */
1700static void del_domain_from_list(struct protection_domain *domain)
1701{
1702 unsigned long flags;
1703
1704 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1705 list_del(&domain->list);
1706 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1707}
1708
Joerg Roedelec487d12008-06-26 21:27:58 +02001709static u16 domain_id_alloc(void)
1710{
1711 unsigned long flags;
1712 int id;
1713
1714 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1715 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1716 BUG_ON(id == 0);
1717 if (id > 0 && id < MAX_DOMAIN_ID)
1718 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1719 else
1720 id = 0;
1721 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1722
1723 return id;
1724}
1725
Joerg Roedela2acfb72008-12-02 18:28:53 +01001726static void domain_id_free(int id)
1727{
1728 unsigned long flags;
1729
1730 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1731 if (id > 0 && id < MAX_DOMAIN_ID)
1732 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1733 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1734}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001735
Joerg Roedel5c34c402013-06-20 20:22:58 +02001736#define DEFINE_FREE_PT_FN(LVL, FN) \
1737static void free_pt_##LVL (unsigned long __pt) \
1738{ \
1739 unsigned long p; \
1740 u64 *pt; \
1741 int i; \
1742 \
1743 pt = (u64 *)__pt; \
1744 \
1745 for (i = 0; i < 512; ++i) { \
Joerg Roedel0b3fff52015-06-18 10:48:34 +02001746 /* PTE present? */ \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001747 if (!IOMMU_PTE_PRESENT(pt[i])) \
1748 continue; \
1749 \
Joerg Roedel0b3fff52015-06-18 10:48:34 +02001750 /* Large PTE? */ \
1751 if (PM_PTE_LEVEL(pt[i]) == 0 || \
1752 PM_PTE_LEVEL(pt[i]) == 7) \
1753 continue; \
1754 \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001755 p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
1756 FN(p); \
1757 } \
1758 free_page((unsigned long)pt); \
1759}
1760
1761DEFINE_FREE_PT_FN(l2, free_page)
1762DEFINE_FREE_PT_FN(l3, free_pt_l2)
1763DEFINE_FREE_PT_FN(l4, free_pt_l3)
1764DEFINE_FREE_PT_FN(l5, free_pt_l4)
1765DEFINE_FREE_PT_FN(l6, free_pt_l5)
1766
Joerg Roedel86db2e52008-12-02 18:20:21 +01001767static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001768{
Joerg Roedel5c34c402013-06-20 20:22:58 +02001769 unsigned long root = (unsigned long)domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001770
Joerg Roedel5c34c402013-06-20 20:22:58 +02001771 switch (domain->mode) {
1772 case PAGE_MODE_NONE:
1773 break;
1774 case PAGE_MODE_1_LEVEL:
1775 free_page(root);
1776 break;
1777 case PAGE_MODE_2_LEVEL:
1778 free_pt_l2(root);
1779 break;
1780 case PAGE_MODE_3_LEVEL:
1781 free_pt_l3(root);
1782 break;
1783 case PAGE_MODE_4_LEVEL:
1784 free_pt_l4(root);
1785 break;
1786 case PAGE_MODE_5_LEVEL:
1787 free_pt_l5(root);
1788 break;
1789 case PAGE_MODE_6_LEVEL:
1790 free_pt_l6(root);
1791 break;
1792 default:
1793 BUG();
Joerg Roedelec487d12008-06-26 21:27:58 +02001794 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001795}
1796
Joerg Roedelb16137b2011-11-21 16:50:23 +01001797static void free_gcr3_tbl_level1(u64 *tbl)
1798{
1799 u64 *ptr;
1800 int i;
1801
1802 for (i = 0; i < 512; ++i) {
1803 if (!(tbl[i] & GCR3_VALID))
1804 continue;
1805
1806 ptr = __va(tbl[i] & PAGE_MASK);
1807
1808 free_page((unsigned long)ptr);
1809 }
1810}
1811
1812static void free_gcr3_tbl_level2(u64 *tbl)
1813{
1814 u64 *ptr;
1815 int i;
1816
1817 for (i = 0; i < 512; ++i) {
1818 if (!(tbl[i] & GCR3_VALID))
1819 continue;
1820
1821 ptr = __va(tbl[i] & PAGE_MASK);
1822
1823 free_gcr3_tbl_level1(ptr);
1824 }
1825}
1826
Joerg Roedel52815b72011-11-17 17:24:28 +01001827static void free_gcr3_table(struct protection_domain *domain)
1828{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001829 if (domain->glx == 2)
1830 free_gcr3_tbl_level2(domain->gcr3_tbl);
1831 else if (domain->glx == 1)
1832 free_gcr3_tbl_level1(domain->gcr3_tbl);
Joerg Roedel23d3a982015-08-13 11:15:13 +02001833 else
1834 BUG_ON(domain->glx != 0);
Joerg Roedelb16137b2011-11-21 16:50:23 +01001835
Joerg Roedel52815b72011-11-17 17:24:28 +01001836 free_page((unsigned long)domain->gcr3_tbl);
1837}
1838
Joerg Roedel431b2a22008-07-11 17:14:22 +02001839/*
1840 * Free a domain, only used if something went wrong in the
1841 * allocation path and we need to free an already allocated page table
1842 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001843static void dma_ops_domain_free(struct dma_ops_domain *dom)
1844{
Joerg Roedel384de722009-05-15 12:30:05 +02001845 int i;
1846
Joerg Roedelec487d12008-06-26 21:27:58 +02001847 if (!dom)
1848 return;
1849
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001850 del_domain_from_list(&dom->domain);
1851
Joerg Roedel86db2e52008-12-02 18:20:21 +01001852 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001853
Joerg Roedel384de722009-05-15 12:30:05 +02001854 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1855 if (!dom->aperture[i])
1856 continue;
1857 free_page((unsigned long)dom->aperture[i]->bitmap);
1858 kfree(dom->aperture[i]);
1859 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001860
1861 kfree(dom);
1862}
1863
Joerg Roedel431b2a22008-07-11 17:14:22 +02001864/*
1865 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001866 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001867 * structures required for the dma_ops interface
1868 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001869static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001870{
1871 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001872
1873 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1874 if (!dma_dom)
1875 return NULL;
1876
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001877 if (protection_domain_init(&dma_dom->domain))
Joerg Roedelec487d12008-06-26 21:27:58 +02001878 goto free_dma_dom;
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001879
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001880 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001881 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001882 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001883 dma_dom->domain.priv = dma_dom;
1884 if (!dma_dom->domain.pt_root)
1885 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001886
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001887 add_domain_to_list(&dma_dom->domain);
1888
Joerg Roedel576175c2009-11-23 19:08:46 +01001889 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001890 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001891
Joerg Roedel431b2a22008-07-11 17:14:22 +02001892 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001893 * mark the first page as allocated so we never return 0 as
1894 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001895 */
Joerg Roedel384de722009-05-15 12:30:05 +02001896 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedelebaecb42015-12-21 18:11:32 +01001897 dma_dom->next_index = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001898
Joerg Roedelec487d12008-06-26 21:27:58 +02001899
1900 return dma_dom;
1901
1902free_dma_dom:
1903 dma_ops_domain_free(dma_dom);
1904
1905 return NULL;
1906}
1907
Joerg Roedel431b2a22008-07-11 17:14:22 +02001908/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001909 * little helper function to check whether a given protection domain is a
1910 * dma_ops domain
1911 */
1912static bool dma_ops_domain(struct protection_domain *domain)
1913{
1914 return domain->flags & PD_DMA_OPS_MASK;
1915}
1916
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001917static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001918{
Joerg Roedel132bd682011-11-17 14:18:46 +01001919 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001920 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001921
Joerg Roedel132bd682011-11-17 14:18:46 +01001922 if (domain->mode != PAGE_MODE_NONE)
1923 pte_root = virt_to_phys(domain->pt_root);
1924
Joerg Roedel38ddf412008-09-11 10:38:32 +02001925 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1926 << DEV_ENTRY_MODE_SHIFT;
1927 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001928
Joerg Roedelee6c2862011-11-09 12:06:03 +01001929 flags = amd_iommu_dev_table[devid].data[1];
1930
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001931 if (ats)
1932 flags |= DTE_FLAG_IOTLB;
1933
Joerg Roedel52815b72011-11-17 17:24:28 +01001934 if (domain->flags & PD_IOMMUV2_MASK) {
1935 u64 gcr3 = __pa(domain->gcr3_tbl);
1936 u64 glx = domain->glx;
1937 u64 tmp;
1938
1939 pte_root |= DTE_FLAG_GV;
1940 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1941
1942 /* First mask out possible old values for GCR3 table */
1943 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1944 flags &= ~tmp;
1945
1946 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1947 flags &= ~tmp;
1948
1949 /* Encode GCR3 table into DTE */
1950 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1951 pte_root |= tmp;
1952
1953 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1954 flags |= tmp;
1955
1956 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1957 flags |= tmp;
1958 }
1959
Joerg Roedelee6c2862011-11-09 12:06:03 +01001960 flags &= ~(0xffffUL);
1961 flags |= domain->id;
1962
1963 amd_iommu_dev_table[devid].data[1] = flags;
1964 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001965}
1966
Joerg Roedel15898bb2009-11-24 15:39:42 +01001967static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001968{
Joerg Roedel355bf552008-12-08 12:02:41 +01001969 /* remove entry from the device table seen by the hardware */
Joerg Roedelcbf3ccd2015-10-20 14:59:36 +02001970 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1971 amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
Joerg Roedel355bf552008-12-08 12:02:41 +01001972
Joerg Roedelc5cca142009-10-09 18:31:20 +02001973 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001974}
1975
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001976static void do_attach(struct iommu_dev_data *dev_data,
1977 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001978{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001979 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001980 u16 alias;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001981 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001982
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001983 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele25bfb52015-10-20 17:33:38 +02001984 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001985 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001986
1987 /* Update data structures */
1988 dev_data->domain = domain;
1989 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001990
1991 /* Do reference counting */
1992 domain->dev_iommu[iommu->index] += 1;
1993 domain->dev_cnt += 1;
1994
Joerg Roedele25bfb52015-10-20 17:33:38 +02001995 /* Update device table */
1996 set_dte_entry(dev_data->devid, domain, ats);
1997 if (alias != dev_data->devid)
1998 set_dte_entry(dev_data->devid, domain, ats);
1999
Joerg Roedel6c542042011-06-09 17:07:31 +02002000 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002001}
2002
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002003static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002004{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002005 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02002006 u16 alias;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002007
Joerg Roedel5adad992015-10-09 16:23:33 +02002008 /*
2009 * First check if the device is still attached. It might already
2010 * be detached from its domain because the generic
2011 * iommu_detach_group code detached it and we try again here in
2012 * our alias handling.
2013 */
2014 if (!dev_data->domain)
2015 return;
2016
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002017 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele25bfb52015-10-20 17:33:38 +02002018 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02002019
Joerg Roedelc4596112009-11-20 14:57:32 +01002020 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002021 dev_data->domain->dev_iommu[iommu->index] -= 1;
2022 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01002023
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002024 /* Update data structures */
2025 dev_data->domain = NULL;
2026 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002027 clear_dte_entry(dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02002028 if (alias != dev_data->devid)
2029 clear_dte_entry(alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002030
2031 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002032 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002033}
2034
2035/*
2036 * If a device is not yet associated with a domain, this function does
2037 * assigns it visible for the hardware
2038 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002039static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002040 struct protection_domain *domain)
2041{
Julia Lawall84fe6c12010-05-27 12:31:51 +02002042 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002043
Joerg Roedel272e4f92015-10-20 17:33:37 +02002044 /*
2045 * Must be called with IRQs disabled. Warn here to detect early
2046 * when its not.
2047 */
2048 WARN_ON(!irqs_disabled());
2049
Joerg Roedel15898bb2009-11-24 15:39:42 +01002050 /* lock domain */
2051 spin_lock(&domain->lock);
2052
Joerg Roedel397111a2014-08-05 17:31:51 +02002053 ret = -EBUSY;
Joerg Roedel150952f2015-10-20 17:33:35 +02002054 if (dev_data->domain != NULL)
Joerg Roedel397111a2014-08-05 17:31:51 +02002055 goto out_unlock;
Joerg Roedel24100052009-11-25 15:59:57 +01002056
Joerg Roedel397111a2014-08-05 17:31:51 +02002057 /* Attach alias group root */
Joerg Roedel150952f2015-10-20 17:33:35 +02002058 do_attach(dev_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002059
Julia Lawall84fe6c12010-05-27 12:31:51 +02002060 ret = 0;
2061
2062out_unlock:
2063
Joerg Roedel355bf552008-12-08 12:02:41 +01002064 /* ready */
2065 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002066
Julia Lawall84fe6c12010-05-27 12:31:51 +02002067 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002068}
2069
Joerg Roedel52815b72011-11-17 17:24:28 +01002070
2071static void pdev_iommuv2_disable(struct pci_dev *pdev)
2072{
2073 pci_disable_ats(pdev);
2074 pci_disable_pri(pdev);
2075 pci_disable_pasid(pdev);
2076}
2077
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002078/* FIXME: Change generic reset-function to do the same */
2079static int pri_reset_while_enabled(struct pci_dev *pdev)
2080{
2081 u16 control;
2082 int pos;
2083
Joerg Roedel46277b72011-12-07 14:34:02 +01002084 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002085 if (!pos)
2086 return -EINVAL;
2087
Joerg Roedel46277b72011-12-07 14:34:02 +01002088 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2089 control |= PCI_PRI_CTRL_RESET;
2090 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002091
2092 return 0;
2093}
2094
Joerg Roedel52815b72011-11-17 17:24:28 +01002095static int pdev_iommuv2_enable(struct pci_dev *pdev)
2096{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002097 bool reset_enable;
2098 int reqs, ret;
2099
2100 /* FIXME: Hardcode number of outstanding requests for now */
2101 reqs = 32;
2102 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2103 reqs = 1;
2104 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002105
2106 /* Only allow access to user-accessible pages */
2107 ret = pci_enable_pasid(pdev, 0);
2108 if (ret)
2109 goto out_err;
2110
2111 /* First reset the PRI state of the device */
2112 ret = pci_reset_pri(pdev);
2113 if (ret)
2114 goto out_err;
2115
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002116 /* Enable PRI */
2117 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002118 if (ret)
2119 goto out_err;
2120
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002121 if (reset_enable) {
2122 ret = pri_reset_while_enabled(pdev);
2123 if (ret)
2124 goto out_err;
2125 }
2126
Joerg Roedel52815b72011-11-17 17:24:28 +01002127 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2128 if (ret)
2129 goto out_err;
2130
2131 return 0;
2132
2133out_err:
2134 pci_disable_pri(pdev);
2135 pci_disable_pasid(pdev);
2136
2137 return ret;
2138}
2139
Joerg Roedelc99afa22011-11-21 18:19:25 +01002140/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002141#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002142
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002143static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002144{
Joerg Roedela3b93122012-04-12 12:49:26 +02002145 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002146 int pos;
2147
Joerg Roedel46277b72011-12-07 14:34:02 +01002148 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002149 if (!pos)
2150 return false;
2151
Joerg Roedela3b93122012-04-12 12:49:26 +02002152 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002153
Joerg Roedela3b93122012-04-12 12:49:26 +02002154 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002155}
2156
Joerg Roedel15898bb2009-11-24 15:39:42 +01002157/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02002158 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01002159 * assigns it visible for the hardware
2160 */
2161static int attach_device(struct device *dev,
2162 struct protection_domain *domain)
2163{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002164 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002165 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002166 unsigned long flags;
2167 int ret;
2168
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002169 dev_data = get_dev_data(dev);
2170
Joerg Roedel52815b72011-11-17 17:24:28 +01002171 if (domain->flags & PD_IOMMUV2_MASK) {
Joerg Roedel02ca2022015-07-28 16:58:49 +02002172 if (!dev_data->passthrough)
Joerg Roedel52815b72011-11-17 17:24:28 +01002173 return -EINVAL;
2174
Joerg Roedel02ca2022015-07-28 16:58:49 +02002175 if (dev_data->iommu_v2) {
2176 if (pdev_iommuv2_enable(pdev) != 0)
2177 return -EINVAL;
Joerg Roedel52815b72011-11-17 17:24:28 +01002178
Joerg Roedel02ca2022015-07-28 16:58:49 +02002179 dev_data->ats.enabled = true;
2180 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2181 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
2182 }
Joerg Roedel52815b72011-11-17 17:24:28 +01002183 } else if (amd_iommu_iotlb_sup &&
2184 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002185 dev_data->ats.enabled = true;
2186 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2187 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002188
Joerg Roedel15898bb2009-11-24 15:39:42 +01002189 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002190 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002191 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2192
2193 /*
2194 * We might boot into a crash-kernel here. The crashed kernel
2195 * left the caches in the IOMMU dirty. So we have to flush
2196 * here to evict all dirty stuff.
2197 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002198 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002199
2200 return ret;
2201}
2202
2203/*
2204 * Removes a device from a protection domain (unlocked)
2205 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002206static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002207{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002208 struct protection_domain *domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002209
Joerg Roedel272e4f92015-10-20 17:33:37 +02002210 /*
2211 * Must be called with IRQs disabled. Warn here to detect early
2212 * when its not.
2213 */
2214 WARN_ON(!irqs_disabled());
2215
Joerg Roedelf34c73f2015-10-20 17:33:34 +02002216 if (WARN_ON(!dev_data->domain))
2217 return;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002218
Joerg Roedel2ca76272010-01-22 16:45:31 +01002219 domain = dev_data->domain;
2220
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002221 spin_lock(&domain->lock);
Joerg Roedel24100052009-11-25 15:59:57 +01002222
Joerg Roedel150952f2015-10-20 17:33:35 +02002223 do_detach(dev_data);
Joerg Roedel71f77582011-06-09 19:03:15 +02002224
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002225 spin_unlock(&domain->lock);
Joerg Roedel355bf552008-12-08 12:02:41 +01002226}
2227
2228/*
2229 * Removes a device from a protection domain (with devtable_lock held)
2230 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002231static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002232{
Joerg Roedel52815b72011-11-17 17:24:28 +01002233 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002234 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002235 unsigned long flags;
2236
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002237 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002238 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002239
Joerg Roedel355bf552008-12-08 12:02:41 +01002240 /* lock device table */
2241 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002242 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002243 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002244
Joerg Roedel02ca2022015-07-28 16:58:49 +02002245 if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
Joerg Roedel52815b72011-11-17 17:24:28 +01002246 pdev_iommuv2_disable(to_pci_dev(dev));
2247 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002248 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002249
2250 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002251}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002252
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002253static int amd_iommu_add_device(struct device *dev)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002254{
Joerg Roedel71f77582011-06-09 19:03:15 +02002255 struct iommu_dev_data *dev_data;
Joerg Roedel07ee8692015-05-28 18:41:42 +02002256 struct iommu_domain *domain;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002257 struct amd_iommu *iommu;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002258 u16 devid;
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002259 int ret;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002260
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002261 if (!check_device(dev) || get_dev_data(dev))
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002262 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002263
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002264 devid = get_device_id(dev);
2265 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01002266
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002267 ret = iommu_init_device(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002268 if (ret) {
2269 if (ret != -ENOTSUPP)
2270 pr_err("Failed to initialize device %s - trying to proceed anyway\n",
2271 dev_name(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +01002272
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002273 iommu_ignore_device(dev);
Joerg Roedel343e9ca2015-05-28 18:41:43 +02002274 dev->archdata.dma_ops = &nommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002275 goto out;
2276 }
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002277 init_iommu_group(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002278
Joerg Roedel07ee8692015-05-28 18:41:42 +02002279 dev_data = get_dev_data(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002280
2281 BUG_ON(!dev_data);
2282
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002283 if (iommu_pass_through || dev_data->iommu_v2)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002284 iommu_request_dm_for_dev(dev);
2285
2286 /* Domains are initialized for this device - have a look what we ended up with */
2287 domain = iommu_get_domain_for_dev(dev);
Joerg Roedel32302322015-07-28 16:58:50 +02002288 if (domain->type == IOMMU_DOMAIN_IDENTITY)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002289 dev_data->passthrough = true;
Joerg Roedel32302322015-07-28 16:58:50 +02002290 else
Joerg Roedel07ee8692015-05-28 18:41:42 +02002291 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002292
2293out:
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002294 iommu_completion_wait(iommu);
2295
Joerg Roedele275a2a2008-12-10 18:27:25 +01002296 return 0;
2297}
2298
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002299static void amd_iommu_remove_device(struct device *dev)
Joerg Roedel8638c492009-12-10 11:12:25 +01002300{
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002301 struct amd_iommu *iommu;
2302 u16 devid;
2303
2304 if (!check_device(dev))
2305 return;
2306
2307 devid = get_device_id(dev);
2308 iommu = amd_iommu_rlookup_table[devid];
2309
2310 iommu_uninit_device(dev);
2311 iommu_completion_wait(iommu);
Joerg Roedel8638c492009-12-10 11:12:25 +01002312}
2313
Joerg Roedel431b2a22008-07-11 17:14:22 +02002314/*****************************************************************************
2315 *
2316 * The next functions belong to the dma_ops mapping/unmapping code.
2317 *
2318 *****************************************************************************/
2319
2320/*
2321 * In the dma_ops path we only have the struct device. This function
2322 * finds the corresponding IOMMU, the protection domain and the
2323 * requestor id for a given device.
2324 * If the device is not yet associated with a domain this is also done
2325 * in this function.
2326 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002327static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002328{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002329 struct protection_domain *domain;
Joerg Roedel063071d2015-05-28 18:41:38 +02002330 struct iommu_domain *io_domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002331
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002332 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002333 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002334
Joerg Roedel063071d2015-05-28 18:41:38 +02002335 io_domain = iommu_get_domain_for_dev(dev);
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002336 if (!io_domain)
2337 return NULL;
Joerg Roedel063071d2015-05-28 18:41:38 +02002338
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002339 domain = to_pdomain(io_domain);
2340 if (!dma_ops_domain(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002341 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002342
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002343 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002344}
2345
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002346static void update_device_table(struct protection_domain *domain)
2347{
Joerg Roedel492667d2009-11-27 13:25:47 +01002348 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002349
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002350 list_for_each_entry(dev_data, &domain->dev_list, list)
2351 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002352}
2353
2354static void update_domain(struct protection_domain *domain)
2355{
2356 if (!domain->updated)
2357 return;
2358
2359 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002360
2361 domain_flush_devices(domain);
2362 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002363
2364 domain->updated = false;
2365}
2366
Joerg Roedel431b2a22008-07-11 17:14:22 +02002367/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002368 * This function fetches the PTE for a given address in the aperture
2369 */
2370static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2371 unsigned long address)
2372{
Joerg Roedel384de722009-05-15 12:30:05 +02002373 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002374 u64 *pte, *pte_page;
2375
Joerg Roedel384de722009-05-15 12:30:05 +02002376 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2377 if (!aperture)
2378 return NULL;
2379
2380 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002381 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002382 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002383 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002384 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2385 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002386 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002387
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002388 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002389
2390 return pte;
2391}
2392
2393/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002394 * This is the generic map function. It maps one 4kb page at paddr to
2395 * the given address in the DMA address space for the domain.
2396 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002397static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002398 unsigned long address,
2399 phys_addr_t paddr,
2400 int direction)
2401{
2402 u64 *pte, __pte;
2403
2404 WARN_ON(address > dom->aperture_size);
2405
2406 paddr &= PAGE_MASK;
2407
Joerg Roedel8bda3092009-05-12 12:02:46 +02002408 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002409 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002410 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002411
2412 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2413
2414 if (direction == DMA_TO_DEVICE)
2415 __pte |= IOMMU_PTE_IR;
2416 else if (direction == DMA_FROM_DEVICE)
2417 __pte |= IOMMU_PTE_IW;
2418 else if (direction == DMA_BIDIRECTIONAL)
2419 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2420
Joerg Roedela7fb6682015-12-21 12:50:54 +01002421 WARN_ON_ONCE(*pte);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002422
2423 *pte = __pte;
2424
2425 return (dma_addr_t)address;
2426}
2427
Joerg Roedel431b2a22008-07-11 17:14:22 +02002428/*
2429 * The generic unmapping function for on page in the DMA address space.
2430 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002431static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002432 unsigned long address)
2433{
Joerg Roedel384de722009-05-15 12:30:05 +02002434 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002435 u64 *pte;
2436
2437 if (address >= dom->aperture_size)
2438 return;
2439
Joerg Roedel384de722009-05-15 12:30:05 +02002440 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2441 if (!aperture)
2442 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002443
Joerg Roedel384de722009-05-15 12:30:05 +02002444 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2445 if (!pte)
2446 return;
2447
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002448 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002449
Joerg Roedela7fb6682015-12-21 12:50:54 +01002450 WARN_ON_ONCE(!*pte);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002451
2452 *pte = 0ULL;
2453}
2454
Joerg Roedel431b2a22008-07-11 17:14:22 +02002455/*
2456 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002457 * contiguous memory region into DMA address space. It is used by all
2458 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002459 * Must be called with the domain lock held.
2460 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002461static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002462 struct dma_ops_domain *dma_dom,
2463 phys_addr_t paddr,
2464 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002465 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002466 bool align,
2467 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002468{
2469 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002470 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002471 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002472 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002473 int i;
2474
Joerg Roedele3c449f2008-10-15 22:02:11 -07002475 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002476 paddr &= PAGE_MASK;
2477
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002478 INC_STATS_COUNTER(total_map_requests);
2479
Joerg Roedelc1858972008-12-12 15:42:39 +01002480 if (pages > 1)
2481 INC_STATS_COUNTER(cross_page);
2482
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002483 if (align)
2484 align_mask = (1UL << get_order(size)) - 1;
2485
Joerg Roedel832a90c2008-09-18 15:54:23 +02002486 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2487 dma_mask);
Joerg Roedelebaecb42015-12-21 18:11:32 +01002488
Joerg Roedel266a3bd2015-12-21 18:54:24 +01002489 if (address == DMA_ERROR_CODE)
2490 goto out;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002491
2492 start = address;
2493 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002494 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002495 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002496 goto out_unmap;
2497
Joerg Roedelcb76c322008-06-26 21:28:00 +02002498 paddr += PAGE_SIZE;
2499 start += PAGE_SIZE;
2500 }
2501 address += offset;
2502
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002503 ADD_STATS_COUNTER(alloced_io_mem, size);
2504
Joerg Roedelab7032b2015-12-21 18:47:11 +01002505 if (unlikely(amd_iommu_np_cache)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002506 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedelab7032b2015-12-21 18:47:11 +01002507 domain_flush_complete(&dma_dom->domain);
2508 }
Joerg Roedel270cab242008-09-04 15:49:46 +02002509
Joerg Roedelcb76c322008-06-26 21:28:00 +02002510out:
2511 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002512
2513out_unmap:
2514
2515 for (--i; i >= 0; --i) {
2516 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002517 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002518 }
2519
2520 dma_ops_free_addresses(dma_dom, address, pages);
2521
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002522 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002523}
2524
Joerg Roedel431b2a22008-07-11 17:14:22 +02002525/*
2526 * Does the reverse of the __map_single function. Must be called with
2527 * the domain lock held too
2528 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002529static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002530 dma_addr_t dma_addr,
2531 size_t size,
2532 int dir)
2533{
Joerg Roedel04e04632010-09-23 16:12:48 +02002534 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002535 dma_addr_t i, start;
2536 unsigned int pages;
2537
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002538 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002539 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002540 return;
2541
Joerg Roedel04e04632010-09-23 16:12:48 +02002542 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002543 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002544 dma_addr &= PAGE_MASK;
2545 start = dma_addr;
2546
2547 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002548 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002549 start += PAGE_SIZE;
2550 }
2551
Joerg Roedel84b3a0b2015-12-21 13:23:59 +01002552 SUB_STATS_COUNTER(alloced_io_mem, size);
2553
2554 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002555}
2556
Joerg Roedel431b2a22008-07-11 17:14:22 +02002557/*
2558 * The exported map_single function for dma_ops.
2559 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002560static dma_addr_t map_page(struct device *dev, struct page *page,
2561 unsigned long offset, size_t size,
2562 enum dma_data_direction dir,
2563 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002564{
2565 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002566 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002567 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002568 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002569 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002570
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002571 INC_STATS_COUNTER(cnt_map_single);
2572
Joerg Roedel94f6d192009-11-24 16:40:02 +01002573 domain = get_domain(dev);
2574 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002575 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002576 else if (IS_ERR(domain))
2577 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002578
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002579 dma_mask = *dev->dma_mask;
2580
Joerg Roedel4da70b92008-06-26 21:28:01 +02002581 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002582
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002583 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002584 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002585 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002586 goto out;
2587
Joerg Roedel17b124b2011-04-06 18:01:35 +02002588 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002589
2590out:
2591 spin_unlock_irqrestore(&domain->lock, flags);
2592
2593 return addr;
2594}
2595
Joerg Roedel431b2a22008-07-11 17:14:22 +02002596/*
2597 * The exported unmap_single function for dma_ops.
2598 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002599static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2600 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002601{
2602 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002603 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002604
Joerg Roedel146a6912008-12-12 15:07:12 +01002605 INC_STATS_COUNTER(cnt_unmap_single);
2606
Joerg Roedel94f6d192009-11-24 16:40:02 +01002607 domain = get_domain(dev);
2608 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002609 return;
2610
Joerg Roedel4da70b92008-06-26 21:28:01 +02002611 spin_lock_irqsave(&domain->lock, flags);
2612
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002613 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002614
Joerg Roedel17b124b2011-04-06 18:01:35 +02002615 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002616
2617 spin_unlock_irqrestore(&domain->lock, flags);
2618}
2619
Joerg Roedel431b2a22008-07-11 17:14:22 +02002620/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002621 * The exported map_sg function for dma_ops (handles scatter-gather
2622 * lists).
2623 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002624static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002625 int nelems, enum dma_data_direction dir,
2626 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002627{
2628 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002629 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002630 int i;
2631 struct scatterlist *s;
2632 phys_addr_t paddr;
2633 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002634 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002635
Joerg Roedeld03f0672008-12-12 15:09:48 +01002636 INC_STATS_COUNTER(cnt_map_sg);
2637
Joerg Roedel94f6d192009-11-24 16:40:02 +01002638 domain = get_domain(dev);
Joerg Roedela0e191b2013-04-09 15:04:36 +02002639 if (IS_ERR(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002640 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002641
Joerg Roedel832a90c2008-09-18 15:54:23 +02002642 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002643
Joerg Roedel65b050a2008-06-26 21:28:02 +02002644 spin_lock_irqsave(&domain->lock, flags);
2645
2646 for_each_sg(sglist, s, nelems, i) {
2647 paddr = sg_phys(s);
2648
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002649 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002650 paddr, s->length, dir, false,
2651 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002652
2653 if (s->dma_address) {
2654 s->dma_length = s->length;
2655 mapped_elems++;
2656 } else
2657 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002658 }
2659
Joerg Roedel17b124b2011-04-06 18:01:35 +02002660 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002661
2662out:
2663 spin_unlock_irqrestore(&domain->lock, flags);
2664
2665 return mapped_elems;
2666unmap:
2667 for_each_sg(sglist, s, mapped_elems, i) {
2668 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002669 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002670 s->dma_length, dir);
2671 s->dma_address = s->dma_length = 0;
2672 }
2673
2674 mapped_elems = 0;
2675
2676 goto out;
2677}
2678
Joerg Roedel431b2a22008-07-11 17:14:22 +02002679/*
2680 * The exported map_sg function for dma_ops (handles scatter-gather
2681 * lists).
2682 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002683static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002684 int nelems, enum dma_data_direction dir,
2685 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002686{
2687 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002688 struct protection_domain *domain;
2689 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002690 int i;
2691
Joerg Roedel55877a62008-12-12 15:12:14 +01002692 INC_STATS_COUNTER(cnt_unmap_sg);
2693
Joerg Roedel94f6d192009-11-24 16:40:02 +01002694 domain = get_domain(dev);
2695 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002696 return;
2697
Joerg Roedel65b050a2008-06-26 21:28:02 +02002698 spin_lock_irqsave(&domain->lock, flags);
2699
2700 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002701 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002702 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002703 s->dma_address = s->dma_length = 0;
2704 }
2705
Joerg Roedel17b124b2011-04-06 18:01:35 +02002706 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002707
2708 spin_unlock_irqrestore(&domain->lock, flags);
2709}
2710
Joerg Roedel431b2a22008-07-11 17:14:22 +02002711/*
2712 * The exported alloc_coherent function for dma_ops.
2713 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002714static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002715 dma_addr_t *dma_addr, gfp_t flag,
2716 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002717{
Joerg Roedel832a90c2008-09-18 15:54:23 +02002718 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002719 struct protection_domain *domain;
2720 unsigned long flags;
2721 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002722
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002723 INC_STATS_COUNTER(cnt_alloc_coherent);
2724
Joerg Roedel94f6d192009-11-24 16:40:02 +01002725 domain = get_domain(dev);
2726 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedel3b839a52015-04-01 14:58:47 +02002727 page = alloc_pages(flag, get_order(size));
2728 *dma_addr = page_to_phys(page);
2729 return page_address(page);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002730 } else if (IS_ERR(domain))
2731 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002732
Joerg Roedel3b839a52015-04-01 14:58:47 +02002733 size = PAGE_ALIGN(size);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002734 dma_mask = dev->coherent_dma_mask;
2735 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
Joerg Roedel2d0ec7a2015-06-01 17:30:57 +02002736 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002737
Joerg Roedel3b839a52015-04-01 14:58:47 +02002738 page = alloc_pages(flag | __GFP_NOWARN, get_order(size));
2739 if (!page) {
Mel Gormand0164ad2015-11-06 16:28:21 -08002740 if (!gfpflags_allow_blocking(flag))
Joerg Roedel3b839a52015-04-01 14:58:47 +02002741 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002742
Joerg Roedel3b839a52015-04-01 14:58:47 +02002743 page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
2744 get_order(size));
2745 if (!page)
2746 return NULL;
2747 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002748
Joerg Roedel832a90c2008-09-18 15:54:23 +02002749 if (!dma_mask)
2750 dma_mask = *dev->dma_mask;
2751
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002752 spin_lock_irqsave(&domain->lock, flags);
2753
Joerg Roedel3b839a52015-04-01 14:58:47 +02002754 *dma_addr = __map_single(dev, domain->priv, page_to_phys(page),
Joerg Roedel832a90c2008-09-18 15:54:23 +02002755 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002756
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002757 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002758 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002759 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002760 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002761
Joerg Roedel17b124b2011-04-06 18:01:35 +02002762 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002763
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002764 spin_unlock_irqrestore(&domain->lock, flags);
2765
Joerg Roedel3b839a52015-04-01 14:58:47 +02002766 return page_address(page);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002767
2768out_free:
2769
Joerg Roedel3b839a52015-04-01 14:58:47 +02002770 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2771 __free_pages(page, get_order(size));
Joerg Roedel5b28df62008-12-02 17:49:42 +01002772
2773 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002774}
2775
Joerg Roedel431b2a22008-07-11 17:14:22 +02002776/*
2777 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002778 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002779static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002780 void *virt_addr, dma_addr_t dma_addr,
2781 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002782{
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002783 struct protection_domain *domain;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002784 unsigned long flags;
2785 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002786
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002787 INC_STATS_COUNTER(cnt_free_coherent);
2788
Joerg Roedel3b839a52015-04-01 14:58:47 +02002789 page = virt_to_page(virt_addr);
2790 size = PAGE_ALIGN(size);
2791
Joerg Roedel94f6d192009-11-24 16:40:02 +01002792 domain = get_domain(dev);
2793 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002794 goto free_mem;
2795
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002796 spin_lock_irqsave(&domain->lock, flags);
2797
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002798 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002799
Joerg Roedel17b124b2011-04-06 18:01:35 +02002800 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002801
2802 spin_unlock_irqrestore(&domain->lock, flags);
2803
2804free_mem:
Joerg Roedel3b839a52015-04-01 14:58:47 +02002805 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2806 __free_pages(page, get_order(size));
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002807}
2808
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002809/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002810 * This function is called by the DMA layer to find out if we can handle a
2811 * particular device. It is part of the dma_ops.
2812 */
2813static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2814{
Joerg Roedel420aef82009-11-23 16:14:57 +01002815 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002816}
2817
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002818static struct dma_map_ops amd_iommu_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002819 .alloc = alloc_coherent,
2820 .free = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002821 .map_page = map_page,
2822 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002823 .map_sg = map_sg,
2824 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002825 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002826};
2827
Joerg Roedel3a18404c2015-05-28 18:41:45 +02002828int __init amd_iommu_init_api(void)
Joerg Roedel27c21272011-05-30 15:56:24 +02002829{
Joerg Roedel3a18404c2015-05-28 18:41:45 +02002830 return bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01002831}
2832
Joerg Roedel6631ee92008-06-26 21:28:05 +02002833int __init amd_iommu_init_dma_ops(void)
2834{
Joerg Roedel32302322015-07-28 16:58:50 +02002835 swiotlb = iommu_pass_through ? 1 : 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002836 iommu_detected = 1;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002837
Joerg Roedel52717822015-07-28 16:58:51 +02002838 /*
2839 * In case we don't initialize SWIOTLB (actually the common case
2840 * when AMD IOMMU is enabled), make sure there are global
2841 * dma_ops set as a fall-back for devices not handled by this
2842 * driver (for example non-PCI devices).
2843 */
2844 if (!swiotlb)
2845 dma_ops = &nommu_dma_ops;
2846
Joerg Roedel7f265082008-12-12 13:50:21 +01002847 amd_iommu_stats_init();
2848
Joerg Roedel62410ee2012-06-12 16:42:43 +02002849 if (amd_iommu_unmap_flush)
2850 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
2851 else
2852 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
2853
Joerg Roedel6631ee92008-06-26 21:28:05 +02002854 return 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002855}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002856
2857/*****************************************************************************
2858 *
2859 * The following functions belong to the exported interface of AMD IOMMU
2860 *
2861 * This interface allows access to lower level functions of the IOMMU
2862 * like protection domain handling and assignement of devices to domains
2863 * which is not possible with the dma_ops interface.
2864 *
2865 *****************************************************************************/
2866
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002867static void cleanup_domain(struct protection_domain *domain)
2868{
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002869 struct iommu_dev_data *entry;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002870 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002871
2872 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2873
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002874 while (!list_empty(&domain->dev_list)) {
2875 entry = list_first_entry(&domain->dev_list,
2876 struct iommu_dev_data, list);
2877 __detach_device(entry);
Joerg Roedel492667d2009-11-27 13:25:47 +01002878 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002879
2880 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2881}
2882
Joerg Roedel26508152009-08-26 16:52:40 +02002883static void protection_domain_free(struct protection_domain *domain)
2884{
2885 if (!domain)
2886 return;
2887
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002888 del_domain_from_list(domain);
2889
Joerg Roedel26508152009-08-26 16:52:40 +02002890 if (domain->id)
2891 domain_id_free(domain->id);
2892
2893 kfree(domain);
2894}
2895
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002896static int protection_domain_init(struct protection_domain *domain)
2897{
2898 spin_lock_init(&domain->lock);
2899 mutex_init(&domain->api_lock);
2900 domain->id = domain_id_alloc();
2901 if (!domain->id)
2902 return -ENOMEM;
2903 INIT_LIST_HEAD(&domain->dev_list);
2904
2905 return 0;
2906}
2907
Joerg Roedel26508152009-08-26 16:52:40 +02002908static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002909{
2910 struct protection_domain *domain;
2911
2912 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2913 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002914 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002915
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002916 if (protection_domain_init(domain))
Joerg Roedel26508152009-08-26 16:52:40 +02002917 goto out_err;
2918
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002919 add_domain_to_list(domain);
2920
Joerg Roedel26508152009-08-26 16:52:40 +02002921 return domain;
2922
2923out_err:
2924 kfree(domain);
2925
2926 return NULL;
2927}
2928
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002929static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
2930{
2931 struct protection_domain *pdomain;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002932 struct dma_ops_domain *dma_domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002933
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002934 switch (type) {
2935 case IOMMU_DOMAIN_UNMANAGED:
2936 pdomain = protection_domain_alloc();
2937 if (!pdomain)
2938 return NULL;
2939
2940 pdomain->mode = PAGE_MODE_3_LEVEL;
2941 pdomain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2942 if (!pdomain->pt_root) {
2943 protection_domain_free(pdomain);
2944 return NULL;
2945 }
2946
2947 pdomain->domain.geometry.aperture_start = 0;
2948 pdomain->domain.geometry.aperture_end = ~0ULL;
2949 pdomain->domain.geometry.force_aperture = true;
2950
2951 break;
2952 case IOMMU_DOMAIN_DMA:
2953 dma_domain = dma_ops_domain_alloc();
2954 if (!dma_domain) {
2955 pr_err("AMD-Vi: Failed to allocate\n");
2956 return NULL;
2957 }
2958 pdomain = &dma_domain->domain;
2959 break;
Joerg Roedel07f643a2015-05-28 18:41:41 +02002960 case IOMMU_DOMAIN_IDENTITY:
2961 pdomain = protection_domain_alloc();
2962 if (!pdomain)
2963 return NULL;
2964
2965 pdomain->mode = PAGE_MODE_NONE;
2966 break;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002967 default:
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002968 return NULL;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002969 }
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002970
2971 return &pdomain->domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002972}
2973
2974static void amd_iommu_domain_free(struct iommu_domain *dom)
Joerg Roedel26508152009-08-26 16:52:40 +02002975{
2976 struct protection_domain *domain;
2977
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002978 if (!dom)
Joerg Roedel98383fc2008-12-02 18:34:12 +01002979 return;
2980
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002981 domain = to_pdomain(dom);
2982
Joerg Roedel98383fc2008-12-02 18:34:12 +01002983 if (domain->dev_cnt > 0)
2984 cleanup_domain(domain);
2985
2986 BUG_ON(domain->dev_cnt != 0);
2987
Joerg Roedel132bd682011-11-17 14:18:46 +01002988 if (domain->mode != PAGE_MODE_NONE)
2989 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002990
Joerg Roedel52815b72011-11-17 17:24:28 +01002991 if (domain->flags & PD_IOMMUV2_MASK)
2992 free_gcr3_table(domain);
2993
Joerg Roedel8b408fe2010-03-08 14:20:07 +01002994 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002995}
2996
Joerg Roedel684f2882008-12-08 12:07:44 +01002997static void amd_iommu_detach_device(struct iommu_domain *dom,
2998 struct device *dev)
2999{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003000 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003001 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003002 u16 devid;
3003
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003004 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003005 return;
3006
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003007 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003008
Joerg Roedel657cbb62009-11-23 15:26:46 +01003009 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003010 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003011
3012 iommu = amd_iommu_rlookup_table[devid];
3013 if (!iommu)
3014 return;
3015
Joerg Roedel684f2882008-12-08 12:07:44 +01003016 iommu_completion_wait(iommu);
3017}
3018
Joerg Roedel01106062008-12-02 19:34:11 +01003019static int amd_iommu_attach_device(struct iommu_domain *dom,
3020 struct device *dev)
3021{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003022 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel657cbb62009-11-23 15:26:46 +01003023 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003024 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003025 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003026
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003027 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003028 return -EINVAL;
3029
Joerg Roedel657cbb62009-11-23 15:26:46 +01003030 dev_data = dev->archdata.iommu;
3031
Joerg Roedelf62dda62011-06-09 12:55:35 +02003032 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003033 if (!iommu)
3034 return -EINVAL;
3035
Joerg Roedel657cbb62009-11-23 15:26:46 +01003036 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003037 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003038
Joerg Roedel15898bb2009-11-24 15:39:42 +01003039 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003040
3041 iommu_completion_wait(iommu);
3042
Joerg Roedel15898bb2009-11-24 15:39:42 +01003043 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003044}
3045
Joerg Roedel468e2362010-01-21 16:37:36 +01003046static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003047 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003048{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003049 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003050 int prot = 0;
3051 int ret;
3052
Joerg Roedel132bd682011-11-17 14:18:46 +01003053 if (domain->mode == PAGE_MODE_NONE)
3054 return -EINVAL;
3055
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003056 if (iommu_prot & IOMMU_READ)
3057 prot |= IOMMU_PROT_IR;
3058 if (iommu_prot & IOMMU_WRITE)
3059 prot |= IOMMU_PROT_IW;
3060
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003061 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003062 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003063 mutex_unlock(&domain->api_lock);
3064
Joerg Roedel795e74f72010-05-11 17:40:57 +02003065 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003066}
3067
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003068static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3069 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003070{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003071 struct protection_domain *domain = to_pdomain(dom);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003072 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003073
Joerg Roedel132bd682011-11-17 14:18:46 +01003074 if (domain->mode == PAGE_MODE_NONE)
3075 return -EINVAL;
3076
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003077 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003078 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003079 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003080
Joerg Roedel17b124b2011-04-06 18:01:35 +02003081 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003082
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003083 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003084}
3085
Joerg Roedel645c4c82008-12-02 20:05:50 +01003086static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
Varun Sethibb5547a2013-03-29 01:23:58 +05303087 dma_addr_t iova)
Joerg Roedel645c4c82008-12-02 20:05:50 +01003088{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003089 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel3039ca12015-04-01 14:58:48 +02003090 unsigned long offset_mask, pte_pgsize;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003091 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003092
Joerg Roedel132bd682011-11-17 14:18:46 +01003093 if (domain->mode == PAGE_MODE_NONE)
3094 return iova;
3095
Joerg Roedel3039ca12015-04-01 14:58:48 +02003096 pte = fetch_pte(domain, iova, &pte_pgsize);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003097
Joerg Roedela6d41a42009-09-02 17:08:55 +02003098 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003099 return 0;
3100
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003101 offset_mask = pte_pgsize - 1;
3102 __pte = *pte & PM_ADDR_MASK;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003103
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003104 return (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003105}
3106
Joerg Roedelab636482014-09-05 10:48:21 +02003107static bool amd_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003108{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003109 switch (cap) {
3110 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedelab636482014-09-05 10:48:21 +02003111 return true;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003112 case IOMMU_CAP_INTR_REMAP:
Joerg Roedelab636482014-09-05 10:48:21 +02003113 return (irq_remapping_enabled == 1);
Will Deaconcfdeec22014-10-27 11:24:48 +00003114 case IOMMU_CAP_NOEXEC:
3115 return false;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003116 }
3117
Joerg Roedelab636482014-09-05 10:48:21 +02003118 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003119}
3120
Joerg Roedel35cf2482015-05-28 18:41:37 +02003121static void amd_iommu_get_dm_regions(struct device *dev,
3122 struct list_head *head)
3123{
3124 struct unity_map_entry *entry;
3125 u16 devid;
3126
3127 devid = get_device_id(dev);
3128
3129 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
3130 struct iommu_dm_region *region;
3131
3132 if (devid < entry->devid_start || devid > entry->devid_end)
3133 continue;
3134
3135 region = kzalloc(sizeof(*region), GFP_KERNEL);
3136 if (!region) {
3137 pr_err("Out of memory allocating dm-regions for %s\n",
3138 dev_name(dev));
3139 return;
3140 }
3141
3142 region->start = entry->address_start;
3143 region->length = entry->address_end - entry->address_start;
3144 if (entry->prot & IOMMU_PROT_IR)
3145 region->prot |= IOMMU_READ;
3146 if (entry->prot & IOMMU_PROT_IW)
3147 region->prot |= IOMMU_WRITE;
3148
3149 list_add_tail(&region->list, head);
3150 }
3151}
3152
3153static void amd_iommu_put_dm_regions(struct device *dev,
3154 struct list_head *head)
3155{
3156 struct iommu_dm_region *entry, *next;
3157
3158 list_for_each_entry_safe(entry, next, head, list)
3159 kfree(entry);
3160}
3161
Thierry Redingb22f6432014-06-27 09:03:12 +02003162static const struct iommu_ops amd_iommu_ops = {
Joerg Roedelab636482014-09-05 10:48:21 +02003163 .capable = amd_iommu_capable,
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003164 .domain_alloc = amd_iommu_domain_alloc,
3165 .domain_free = amd_iommu_domain_free,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003166 .attach_dev = amd_iommu_attach_device,
3167 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003168 .map = amd_iommu_map,
3169 .unmap = amd_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07003170 .map_sg = default_iommu_map_sg,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003171 .iova_to_phys = amd_iommu_iova_to_phys,
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02003172 .add_device = amd_iommu_add_device,
3173 .remove_device = amd_iommu_remove_device,
Joerg Roedela960fad2015-10-21 23:51:39 +02003174 .device_group = pci_device_group,
Joerg Roedel35cf2482015-05-28 18:41:37 +02003175 .get_dm_regions = amd_iommu_get_dm_regions,
3176 .put_dm_regions = amd_iommu_put_dm_regions,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003177 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003178};
3179
Joerg Roedel0feae532009-08-26 15:26:30 +02003180/*****************************************************************************
3181 *
3182 * The next functions do a basic initialization of IOMMU for pass through
3183 * mode
3184 *
3185 * In passthrough mode the IOMMU is initialized and enabled but not used for
3186 * DMA-API translation.
3187 *
3188 *****************************************************************************/
3189
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003190/* IOMMUv2 specific functions */
3191int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3192{
3193 return atomic_notifier_chain_register(&ppr_notifier, nb);
3194}
3195EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3196
3197int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3198{
3199 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3200}
3201EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003202
3203void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3204{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003205 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel132bd682011-11-17 14:18:46 +01003206 unsigned long flags;
3207
3208 spin_lock_irqsave(&domain->lock, flags);
3209
3210 /* Update data structure */
3211 domain->mode = PAGE_MODE_NONE;
3212 domain->updated = true;
3213
3214 /* Make changes visible to IOMMUs */
3215 update_domain(domain);
3216
3217 /* Page-table is not visible to IOMMU anymore, so free it */
3218 free_pagetable(domain);
3219
3220 spin_unlock_irqrestore(&domain->lock, flags);
3221}
3222EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003223
3224int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3225{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003226 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel52815b72011-11-17 17:24:28 +01003227 unsigned long flags;
3228 int levels, ret;
3229
3230 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3231 return -EINVAL;
3232
3233 /* Number of GCR3 table levels required */
3234 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3235 levels += 1;
3236
3237 if (levels > amd_iommu_max_glx_val)
3238 return -EINVAL;
3239
3240 spin_lock_irqsave(&domain->lock, flags);
3241
3242 /*
3243 * Save us all sanity checks whether devices already in the
3244 * domain support IOMMUv2. Just force that the domain has no
3245 * devices attached when it is switched into IOMMUv2 mode.
3246 */
3247 ret = -EBUSY;
3248 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3249 goto out;
3250
3251 ret = -ENOMEM;
3252 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3253 if (domain->gcr3_tbl == NULL)
3254 goto out;
3255
3256 domain->glx = levels;
3257 domain->flags |= PD_IOMMUV2_MASK;
3258 domain->updated = true;
3259
3260 update_domain(domain);
3261
3262 ret = 0;
3263
3264out:
3265 spin_unlock_irqrestore(&domain->lock, flags);
3266
3267 return ret;
3268}
3269EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003270
3271static int __flush_pasid(struct protection_domain *domain, int pasid,
3272 u64 address, bool size)
3273{
3274 struct iommu_dev_data *dev_data;
3275 struct iommu_cmd cmd;
3276 int i, ret;
3277
3278 if (!(domain->flags & PD_IOMMUV2_MASK))
3279 return -EINVAL;
3280
3281 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3282
3283 /*
3284 * IOMMU TLB needs to be flushed before Device TLB to
3285 * prevent device TLB refill from IOMMU TLB
3286 */
3287 for (i = 0; i < amd_iommus_present; ++i) {
3288 if (domain->dev_iommu[i] == 0)
3289 continue;
3290
3291 ret = iommu_queue_command(amd_iommus[i], &cmd);
3292 if (ret != 0)
3293 goto out;
3294 }
3295
3296 /* Wait until IOMMU TLB flushes are complete */
3297 domain_flush_complete(domain);
3298
3299 /* Now flush device TLBs */
3300 list_for_each_entry(dev_data, &domain->dev_list, list) {
3301 struct amd_iommu *iommu;
3302 int qdep;
3303
Joerg Roedel1c1cc452015-07-30 11:24:45 +02003304 /*
3305 There might be non-IOMMUv2 capable devices in an IOMMUv2
3306 * domain.
3307 */
3308 if (!dev_data->ats.enabled)
3309 continue;
Joerg Roedel22e266c2011-11-21 15:59:08 +01003310
3311 qdep = dev_data->ats.qdep;
3312 iommu = amd_iommu_rlookup_table[dev_data->devid];
3313
3314 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3315 qdep, address, size);
3316
3317 ret = iommu_queue_command(iommu, &cmd);
3318 if (ret != 0)
3319 goto out;
3320 }
3321
3322 /* Wait until all device TLBs are flushed */
3323 domain_flush_complete(domain);
3324
3325 ret = 0;
3326
3327out:
3328
3329 return ret;
3330}
3331
3332static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3333 u64 address)
3334{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003335 INC_STATS_COUNTER(invalidate_iotlb);
3336
Joerg Roedel22e266c2011-11-21 15:59:08 +01003337 return __flush_pasid(domain, pasid, address, false);
3338}
3339
3340int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3341 u64 address)
3342{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003343 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003344 unsigned long flags;
3345 int ret;
3346
3347 spin_lock_irqsave(&domain->lock, flags);
3348 ret = __amd_iommu_flush_page(domain, pasid, address);
3349 spin_unlock_irqrestore(&domain->lock, flags);
3350
3351 return ret;
3352}
3353EXPORT_SYMBOL(amd_iommu_flush_page);
3354
3355static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3356{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003357 INC_STATS_COUNTER(invalidate_iotlb_all);
3358
Joerg Roedel22e266c2011-11-21 15:59:08 +01003359 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3360 true);
3361}
3362
3363int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3364{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003365 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003366 unsigned long flags;
3367 int ret;
3368
3369 spin_lock_irqsave(&domain->lock, flags);
3370 ret = __amd_iommu_flush_tlb(domain, pasid);
3371 spin_unlock_irqrestore(&domain->lock, flags);
3372
3373 return ret;
3374}
3375EXPORT_SYMBOL(amd_iommu_flush_tlb);
3376
Joerg Roedelb16137b2011-11-21 16:50:23 +01003377static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3378{
3379 int index;
3380 u64 *pte;
3381
3382 while (true) {
3383
3384 index = (pasid >> (9 * level)) & 0x1ff;
3385 pte = &root[index];
3386
3387 if (level == 0)
3388 break;
3389
3390 if (!(*pte & GCR3_VALID)) {
3391 if (!alloc)
3392 return NULL;
3393
3394 root = (void *)get_zeroed_page(GFP_ATOMIC);
3395 if (root == NULL)
3396 return NULL;
3397
3398 *pte = __pa(root) | GCR3_VALID;
3399 }
3400
3401 root = __va(*pte & PAGE_MASK);
3402
3403 level -= 1;
3404 }
3405
3406 return pte;
3407}
3408
3409static int __set_gcr3(struct protection_domain *domain, int pasid,
3410 unsigned long cr3)
3411{
3412 u64 *pte;
3413
3414 if (domain->mode != PAGE_MODE_NONE)
3415 return -EINVAL;
3416
3417 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3418 if (pte == NULL)
3419 return -ENOMEM;
3420
3421 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3422
3423 return __amd_iommu_flush_tlb(domain, pasid);
3424}
3425
3426static int __clear_gcr3(struct protection_domain *domain, int pasid)
3427{
3428 u64 *pte;
3429
3430 if (domain->mode != PAGE_MODE_NONE)
3431 return -EINVAL;
3432
3433 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3434 if (pte == NULL)
3435 return 0;
3436
3437 *pte = 0;
3438
3439 return __amd_iommu_flush_tlb(domain, pasid);
3440}
3441
3442int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3443 unsigned long cr3)
3444{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003445 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003446 unsigned long flags;
3447 int ret;
3448
3449 spin_lock_irqsave(&domain->lock, flags);
3450 ret = __set_gcr3(domain, pasid, cr3);
3451 spin_unlock_irqrestore(&domain->lock, flags);
3452
3453 return ret;
3454}
3455EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3456
3457int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3458{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003459 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003460 unsigned long flags;
3461 int ret;
3462
3463 spin_lock_irqsave(&domain->lock, flags);
3464 ret = __clear_gcr3(domain, pasid);
3465 spin_unlock_irqrestore(&domain->lock, flags);
3466
3467 return ret;
3468}
3469EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003470
3471int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3472 int status, int tag)
3473{
3474 struct iommu_dev_data *dev_data;
3475 struct amd_iommu *iommu;
3476 struct iommu_cmd cmd;
3477
Joerg Roedel399be2f2011-12-01 16:53:47 +01003478 INC_STATS_COUNTER(complete_ppr);
3479
Joerg Roedelc99afa22011-11-21 18:19:25 +01003480 dev_data = get_dev_data(&pdev->dev);
3481 iommu = amd_iommu_rlookup_table[dev_data->devid];
3482
3483 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3484 tag, dev_data->pri_tlp);
3485
3486 return iommu_queue_command(iommu, &cmd);
3487}
3488EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003489
3490struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3491{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003492 struct protection_domain *pdomain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003493
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003494 pdomain = get_domain(&pdev->dev);
3495 if (IS_ERR(pdomain))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003496 return NULL;
3497
3498 /* Only return IOMMUv2 domains */
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003499 if (!(pdomain->flags & PD_IOMMUV2_MASK))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003500 return NULL;
3501
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003502 return &pdomain->domain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003503}
3504EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003505
3506void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3507{
3508 struct iommu_dev_data *dev_data;
3509
3510 if (!amd_iommu_v2_supported())
3511 return;
3512
3513 dev_data = get_dev_data(&pdev->dev);
3514 dev_data->errata |= (1 << erratum);
3515}
3516EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003517
3518int amd_iommu_device_info(struct pci_dev *pdev,
3519 struct amd_iommu_device_info *info)
3520{
3521 int max_pasids;
3522 int pos;
3523
3524 if (pdev == NULL || info == NULL)
3525 return -EINVAL;
3526
3527 if (!amd_iommu_v2_supported())
3528 return -EINVAL;
3529
3530 memset(info, 0, sizeof(*info));
3531
3532 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3533 if (pos)
3534 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3535
3536 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3537 if (pos)
3538 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3539
3540 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3541 if (pos) {
3542 int features;
3543
3544 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3545 max_pasids = min(max_pasids, (1 << 20));
3546
3547 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3548 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3549
3550 features = pci_pasid_features(pdev);
3551 if (features & PCI_PASID_CAP_EXEC)
3552 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3553 if (features & PCI_PASID_CAP_PRIV)
3554 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3555 }
3556
3557 return 0;
3558}
3559EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003560
3561#ifdef CONFIG_IRQ_REMAP
3562
3563/*****************************************************************************
3564 *
3565 * Interrupt Remapping Implementation
3566 *
3567 *****************************************************************************/
3568
3569union irte {
3570 u32 val;
3571 struct {
3572 u32 valid : 1,
3573 no_fault : 1,
3574 int_type : 3,
3575 rq_eoi : 1,
3576 dm : 1,
3577 rsvd_1 : 1,
3578 destination : 8,
3579 vector : 8,
3580 rsvd_2 : 8;
3581 } fields;
3582};
3583
Jiang Liu9c724962015-04-14 10:29:52 +08003584struct irq_2_irte {
3585 u16 devid; /* Device ID for IRTE table */
3586 u16 index; /* Index into IRTE table*/
3587};
3588
Jiang Liu7c71d302015-04-13 14:11:33 +08003589struct amd_ir_data {
3590 struct irq_2_irte irq_2_irte;
3591 union irte irte_entry;
3592 union {
3593 struct msi_msg msi_entry;
3594 };
3595};
3596
3597static struct irq_chip amd_ir_chip;
3598
Joerg Roedel2b324502012-06-21 16:29:10 +02003599#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3600#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3601#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3602#define DTE_IRQ_REMAP_ENABLE 1ULL
3603
3604static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3605{
3606 u64 dte;
3607
3608 dte = amd_iommu_dev_table[devid].data[2];
3609 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3610 dte |= virt_to_phys(table->table);
3611 dte |= DTE_IRQ_REMAP_INTCTL;
3612 dte |= DTE_IRQ_TABLE_LEN;
3613 dte |= DTE_IRQ_REMAP_ENABLE;
3614
3615 amd_iommu_dev_table[devid].data[2] = dte;
3616}
3617
3618#define IRTE_ALLOCATED (~1U)
3619
3620static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3621{
3622 struct irq_remap_table *table = NULL;
3623 struct amd_iommu *iommu;
3624 unsigned long flags;
3625 u16 alias;
3626
3627 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3628
3629 iommu = amd_iommu_rlookup_table[devid];
3630 if (!iommu)
3631 goto out_unlock;
3632
3633 table = irq_lookup_table[devid];
3634 if (table)
3635 goto out;
3636
3637 alias = amd_iommu_alias_table[devid];
3638 table = irq_lookup_table[alias];
3639 if (table) {
3640 irq_lookup_table[devid] = table;
3641 set_dte_irq_entry(devid, table);
3642 iommu_flush_dte(iommu, devid);
3643 goto out;
3644 }
3645
3646 /* Nothing there yet, allocate new irq remapping table */
3647 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3648 if (!table)
3649 goto out;
3650
Joerg Roedel197887f2013-04-09 21:14:08 +02003651 /* Initialize table spin-lock */
3652 spin_lock_init(&table->lock);
3653
Joerg Roedel2b324502012-06-21 16:29:10 +02003654 if (ioapic)
3655 /* Keep the first 32 indexes free for IOAPIC interrupts */
3656 table->min_index = 32;
3657
3658 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3659 if (!table->table) {
3660 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003661 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003662 goto out;
3663 }
3664
3665 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3666
3667 if (ioapic) {
3668 int i;
3669
3670 for (i = 0; i < 32; ++i)
3671 table->table[i] = IRTE_ALLOCATED;
3672 }
3673
3674 irq_lookup_table[devid] = table;
3675 set_dte_irq_entry(devid, table);
3676 iommu_flush_dte(iommu, devid);
3677 if (devid != alias) {
3678 irq_lookup_table[alias] = table;
Alex Williamsone028a9e2014-04-22 10:08:40 -06003679 set_dte_irq_entry(alias, table);
Joerg Roedel2b324502012-06-21 16:29:10 +02003680 iommu_flush_dte(iommu, alias);
3681 }
3682
3683out:
3684 iommu_completion_wait(iommu);
3685
3686out_unlock:
3687 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3688
3689 return table;
3690}
3691
Jiang Liu3c3d4f92015-04-13 14:11:38 +08003692static int alloc_irq_index(u16 devid, int count)
Joerg Roedel2b324502012-06-21 16:29:10 +02003693{
3694 struct irq_remap_table *table;
3695 unsigned long flags;
3696 int index, c;
3697
3698 table = get_irq_table(devid, false);
3699 if (!table)
3700 return -ENODEV;
3701
3702 spin_lock_irqsave(&table->lock, flags);
3703
3704 /* Scan table for free entries */
3705 for (c = 0, index = table->min_index;
3706 index < MAX_IRQS_PER_TABLE;
3707 ++index) {
3708 if (table->table[index] == 0)
3709 c += 1;
3710 else
3711 c = 0;
3712
3713 if (c == count) {
Joerg Roedel2b324502012-06-21 16:29:10 +02003714 for (; c != 0; --c)
3715 table->table[index - c + 1] = IRTE_ALLOCATED;
3716
3717 index -= count - 1;
Joerg Roedel2b324502012-06-21 16:29:10 +02003718 goto out;
3719 }
3720 }
3721
3722 index = -ENOSPC;
3723
3724out:
3725 spin_unlock_irqrestore(&table->lock, flags);
3726
3727 return index;
3728}
3729
Joerg Roedel2b324502012-06-21 16:29:10 +02003730static int modify_irte(u16 devid, int index, union irte irte)
3731{
3732 struct irq_remap_table *table;
3733 struct amd_iommu *iommu;
3734 unsigned long flags;
3735
3736 iommu = amd_iommu_rlookup_table[devid];
3737 if (iommu == NULL)
3738 return -EINVAL;
3739
3740 table = get_irq_table(devid, false);
3741 if (!table)
3742 return -ENOMEM;
3743
3744 spin_lock_irqsave(&table->lock, flags);
3745 table->table[index] = irte.val;
3746 spin_unlock_irqrestore(&table->lock, flags);
3747
3748 iommu_flush_irt(iommu, devid);
3749 iommu_completion_wait(iommu);
3750
3751 return 0;
3752}
3753
3754static void free_irte(u16 devid, int index)
3755{
3756 struct irq_remap_table *table;
3757 struct amd_iommu *iommu;
3758 unsigned long flags;
3759
3760 iommu = amd_iommu_rlookup_table[devid];
3761 if (iommu == NULL)
3762 return;
3763
3764 table = get_irq_table(devid, false);
3765 if (!table)
3766 return;
3767
3768 spin_lock_irqsave(&table->lock, flags);
3769 table->table[index] = 0;
3770 spin_unlock_irqrestore(&table->lock, flags);
3771
3772 iommu_flush_irt(iommu, devid);
3773 iommu_completion_wait(iommu);
3774}
3775
Jiang Liu7c71d302015-04-13 14:11:33 +08003776static int get_devid(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02003777{
Jiang Liu7c71d302015-04-13 14:11:33 +08003778 int devid = -1;
Joerg Roedel5527de72012-06-26 11:17:32 +02003779
Jiang Liu7c71d302015-04-13 14:11:33 +08003780 switch (info->type) {
3781 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3782 devid = get_ioapic_devid(info->ioapic_id);
3783 break;
3784 case X86_IRQ_ALLOC_TYPE_HPET:
3785 devid = get_hpet_devid(info->hpet_id);
3786 break;
3787 case X86_IRQ_ALLOC_TYPE_MSI:
3788 case X86_IRQ_ALLOC_TYPE_MSIX:
3789 devid = get_device_id(&info->msi_dev->dev);
3790 break;
3791 default:
3792 BUG_ON(1);
3793 break;
Joerg Roedel5527de72012-06-26 11:17:32 +02003794 }
3795
Jiang Liu7c71d302015-04-13 14:11:33 +08003796 return devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02003797}
3798
Jiang Liu7c71d302015-04-13 14:11:33 +08003799static struct irq_domain *get_ir_irq_domain(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02003800{
Jiang Liu7c71d302015-04-13 14:11:33 +08003801 struct amd_iommu *iommu;
3802 int devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02003803
Jiang Liu7c71d302015-04-13 14:11:33 +08003804 if (!info)
3805 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02003806
Jiang Liu7c71d302015-04-13 14:11:33 +08003807 devid = get_devid(info);
3808 if (devid >= 0) {
3809 iommu = amd_iommu_rlookup_table[devid];
3810 if (iommu)
3811 return iommu->ir_domain;
3812 }
Joerg Roedel5527de72012-06-26 11:17:32 +02003813
Jiang Liu7c71d302015-04-13 14:11:33 +08003814 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02003815}
3816
Jiang Liu7c71d302015-04-13 14:11:33 +08003817static struct irq_domain *get_irq_domain(struct irq_alloc_info *info)
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003818{
Jiang Liu7c71d302015-04-13 14:11:33 +08003819 struct amd_iommu *iommu;
3820 int devid;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003821
Jiang Liu7c71d302015-04-13 14:11:33 +08003822 if (!info)
3823 return NULL;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003824
Jiang Liu7c71d302015-04-13 14:11:33 +08003825 switch (info->type) {
3826 case X86_IRQ_ALLOC_TYPE_MSI:
3827 case X86_IRQ_ALLOC_TYPE_MSIX:
3828 devid = get_device_id(&info->msi_dev->dev);
3829 if (devid >= 0) {
3830 iommu = amd_iommu_rlookup_table[devid];
3831 if (iommu)
3832 return iommu->msi_domain;
3833 }
3834 break;
3835 default:
3836 break;
3837 }
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003838
Jiang Liu7c71d302015-04-13 14:11:33 +08003839 return NULL;
Joerg Roedeld9761952012-06-26 16:00:08 +02003840}
3841
Joerg Roedel6b474b82012-06-26 16:46:04 +02003842struct irq_remap_ops amd_iommu_irq_ops = {
Joerg Roedel6b474b82012-06-26 16:46:04 +02003843 .prepare = amd_iommu_prepare,
3844 .enable = amd_iommu_enable,
3845 .disable = amd_iommu_disable,
3846 .reenable = amd_iommu_reenable,
3847 .enable_faulting = amd_iommu_enable_faulting,
Jiang Liu7c71d302015-04-13 14:11:33 +08003848 .get_ir_irq_domain = get_ir_irq_domain,
3849 .get_irq_domain = get_irq_domain,
Joerg Roedel6b474b82012-06-26 16:46:04 +02003850};
Jiang Liu7c71d302015-04-13 14:11:33 +08003851
3852static void irq_remapping_prepare_irte(struct amd_ir_data *data,
3853 struct irq_cfg *irq_cfg,
3854 struct irq_alloc_info *info,
3855 int devid, int index, int sub_handle)
3856{
3857 struct irq_2_irte *irte_info = &data->irq_2_irte;
3858 struct msi_msg *msg = &data->msi_entry;
3859 union irte *irte = &data->irte_entry;
3860 struct IO_APIC_route_entry *entry;
3861
Jiang Liu7c71d302015-04-13 14:11:33 +08003862 data->irq_2_irte.devid = devid;
3863 data->irq_2_irte.index = index + sub_handle;
3864
3865 /* Setup IRTE for IOMMU */
3866 irte->val = 0;
3867 irte->fields.vector = irq_cfg->vector;
3868 irte->fields.int_type = apic->irq_delivery_mode;
3869 irte->fields.destination = irq_cfg->dest_apicid;
3870 irte->fields.dm = apic->irq_dest_mode;
3871 irte->fields.valid = 1;
3872
3873 switch (info->type) {
3874 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3875 /* Setup IOAPIC entry */
3876 entry = info->ioapic_entry;
3877 info->ioapic_entry = NULL;
3878 memset(entry, 0, sizeof(*entry));
3879 entry->vector = index;
3880 entry->mask = 0;
3881 entry->trigger = info->ioapic_trigger;
3882 entry->polarity = info->ioapic_polarity;
3883 /* Mask level triggered irqs. */
3884 if (info->ioapic_trigger)
3885 entry->mask = 1;
3886 break;
3887
3888 case X86_IRQ_ALLOC_TYPE_HPET:
3889 case X86_IRQ_ALLOC_TYPE_MSI:
3890 case X86_IRQ_ALLOC_TYPE_MSIX:
3891 msg->address_hi = MSI_ADDR_BASE_HI;
3892 msg->address_lo = MSI_ADDR_BASE_LO;
3893 msg->data = irte_info->index;
3894 break;
3895
3896 default:
3897 BUG_ON(1);
3898 break;
3899 }
3900}
3901
3902static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
3903 unsigned int nr_irqs, void *arg)
3904{
3905 struct irq_alloc_info *info = arg;
3906 struct irq_data *irq_data;
3907 struct amd_ir_data *data;
3908 struct irq_cfg *cfg;
3909 int i, ret, devid;
3910 int index = -1;
3911
3912 if (!info)
3913 return -EINVAL;
3914 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI &&
3915 info->type != X86_IRQ_ALLOC_TYPE_MSIX)
3916 return -EINVAL;
3917
3918 /*
3919 * With IRQ remapping enabled, don't need contiguous CPU vectors
3920 * to support multiple MSI interrupts.
3921 */
3922 if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
3923 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
3924
3925 devid = get_devid(info);
3926 if (devid < 0)
3927 return -EINVAL;
3928
3929 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
3930 if (ret < 0)
3931 return ret;
3932
Jiang Liu7c71d302015-04-13 14:11:33 +08003933 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
3934 if (get_irq_table(devid, true))
3935 index = info->ioapic_pin;
3936 else
3937 ret = -ENOMEM;
3938 } else {
Jiang Liu3c3d4f92015-04-13 14:11:38 +08003939 index = alloc_irq_index(devid, nr_irqs);
Jiang Liu7c71d302015-04-13 14:11:33 +08003940 }
3941 if (index < 0) {
3942 pr_warn("Failed to allocate IRTE\n");
Jiang Liu7c71d302015-04-13 14:11:33 +08003943 goto out_free_parent;
3944 }
3945
3946 for (i = 0; i < nr_irqs; i++) {
3947 irq_data = irq_domain_get_irq_data(domain, virq + i);
3948 cfg = irqd_cfg(irq_data);
3949 if (!irq_data || !cfg) {
3950 ret = -EINVAL;
3951 goto out_free_data;
3952 }
3953
Joerg Roedela130e692015-08-13 11:07:25 +02003954 ret = -ENOMEM;
3955 data = kzalloc(sizeof(*data), GFP_KERNEL);
3956 if (!data)
3957 goto out_free_data;
3958
Jiang Liu7c71d302015-04-13 14:11:33 +08003959 irq_data->hwirq = (devid << 16) + i;
3960 irq_data->chip_data = data;
3961 irq_data->chip = &amd_ir_chip;
3962 irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
3963 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
3964 }
Joerg Roedela130e692015-08-13 11:07:25 +02003965
Jiang Liu7c71d302015-04-13 14:11:33 +08003966 return 0;
3967
3968out_free_data:
3969 for (i--; i >= 0; i--) {
3970 irq_data = irq_domain_get_irq_data(domain, virq + i);
3971 if (irq_data)
3972 kfree(irq_data->chip_data);
3973 }
3974 for (i = 0; i < nr_irqs; i++)
3975 free_irte(devid, index + i);
3976out_free_parent:
3977 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3978 return ret;
3979}
3980
3981static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
3982 unsigned int nr_irqs)
3983{
3984 struct irq_2_irte *irte_info;
3985 struct irq_data *irq_data;
3986 struct amd_ir_data *data;
3987 int i;
3988
3989 for (i = 0; i < nr_irqs; i++) {
3990 irq_data = irq_domain_get_irq_data(domain, virq + i);
3991 if (irq_data && irq_data->chip_data) {
3992 data = irq_data->chip_data;
3993 irte_info = &data->irq_2_irte;
3994 free_irte(irte_info->devid, irte_info->index);
3995 kfree(data);
3996 }
3997 }
3998 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3999}
4000
4001static void irq_remapping_activate(struct irq_domain *domain,
4002 struct irq_data *irq_data)
4003{
4004 struct amd_ir_data *data = irq_data->chip_data;
4005 struct irq_2_irte *irte_info = &data->irq_2_irte;
4006
4007 modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
4008}
4009
4010static void irq_remapping_deactivate(struct irq_domain *domain,
4011 struct irq_data *irq_data)
4012{
4013 struct amd_ir_data *data = irq_data->chip_data;
4014 struct irq_2_irte *irte_info = &data->irq_2_irte;
4015 union irte entry;
4016
4017 entry.val = 0;
4018 modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
4019}
4020
4021static struct irq_domain_ops amd_ir_domain_ops = {
4022 .alloc = irq_remapping_alloc,
4023 .free = irq_remapping_free,
4024 .activate = irq_remapping_activate,
4025 .deactivate = irq_remapping_deactivate,
4026};
4027
4028static int amd_ir_set_affinity(struct irq_data *data,
4029 const struct cpumask *mask, bool force)
4030{
4031 struct amd_ir_data *ir_data = data->chip_data;
4032 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
4033 struct irq_cfg *cfg = irqd_cfg(data);
4034 struct irq_data *parent = data->parent_data;
4035 int ret;
4036
4037 ret = parent->chip->irq_set_affinity(parent, mask, force);
4038 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
4039 return ret;
4040
4041 /*
4042 * Atomically updates the IRTE with the new destination, vector
4043 * and flushes the interrupt entry cache.
4044 */
4045 ir_data->irte_entry.fields.vector = cfg->vector;
4046 ir_data->irte_entry.fields.destination = cfg->dest_apicid;
4047 modify_irte(irte_info->devid, irte_info->index, ir_data->irte_entry);
4048
4049 /*
4050 * After this point, all the interrupts will start arriving
4051 * at the new destination. So, time to cleanup the previous
4052 * vector allocation.
4053 */
Jiang Liuc6c20022015-04-14 10:30:02 +08004054 send_cleanup_vector(cfg);
Jiang Liu7c71d302015-04-13 14:11:33 +08004055
4056 return IRQ_SET_MASK_OK_DONE;
4057}
4058
4059static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
4060{
4061 struct amd_ir_data *ir_data = irq_data->chip_data;
4062
4063 *msg = ir_data->msi_entry;
4064}
4065
4066static struct irq_chip amd_ir_chip = {
4067 .irq_ack = ir_ack_apic_edge,
4068 .irq_set_affinity = amd_ir_set_affinity,
4069 .irq_compose_msi_msg = ir_compose_msi_msg,
4070};
4071
4072int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
4073{
4074 iommu->ir_domain = irq_domain_add_tree(NULL, &amd_ir_domain_ops, iommu);
4075 if (!iommu->ir_domain)
4076 return -ENOMEM;
4077
4078 iommu->ir_domain->parent = arch_get_ir_parent_domain();
4079 iommu->msi_domain = arch_create_msi_irq_domain(iommu->ir_domain);
4080
4081 return 0;
4082}
Joerg Roedel2b324502012-06-21 16:29:10 +02004083#endif