blob: eed355c5211dd8f47fe11f3b6a95836165f27514 [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 Roedel5f6bed52015-12-22 13:34:22 +010038#include <linux/percpu.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020039#include <asm/irq_remapping.h>
40#include <asm/io_apic.h>
41#include <asm/apic.h>
42#include <asm/hw_irq.h>
Joerg Roedel17f5b562011-07-06 17:14:44 +020043#include <asm/msidef.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020044#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090045#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010046#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020047#include <asm/dma.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020048
49#include "amd_iommu_proto.h"
50#include "amd_iommu_types.h"
Joerg Roedel6b474b82012-06-26 16:46:04 +020051#include "irq_remapping.h"
Joerg Roedelb6c02712008-06-26 21:27:53 +020052
53#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
54
Joerg Roedel815b33f2011-04-06 17:26:49 +020055#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020056
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020057/*
58 * This bitmap is used to advertise the page sizes our hardware support
59 * to the IOMMU core, which will then use this information to split
60 * physically contiguous memory regions it is mapping into page sizes
61 * that we support.
62 *
Joerg Roedel954e3dd2012-12-02 15:35:37 +010063 * 512GB Pages are not supported due to a hardware bug
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020064 */
Joerg Roedel954e3dd2012-12-02 15:35:37 +010065#define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020066
Joerg Roedelb6c02712008-06-26 21:27:53 +020067static DEFINE_RWLOCK(amd_iommu_devtable_lock);
68
Joerg Roedel8fa5f802011-06-09 12:24:45 +020069/* List of all available dev_data structures */
70static LIST_HEAD(dev_data_list);
71static DEFINE_SPINLOCK(dev_data_list_lock);
72
Joerg Roedel6efed632012-06-14 15:52:58 +020073LIST_HEAD(ioapic_map);
74LIST_HEAD(hpet_map);
75
Joerg Roedel0feae532009-08-26 15:26:30 +020076/*
77 * Domain for untranslated devices - only allocated
78 * if iommu=pt passed on kernel cmd line.
79 */
Thierry Redingb22f6432014-06-27 09:03:12 +020080static const struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010081
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010082static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +010083int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010084
Joerg Roedelac1534a2012-06-21 14:52:40 +020085static struct dma_map_ops amd_iommu_dma_ops;
86
Joerg Roedel431b2a22008-07-11 17:14:22 +020087/*
Joerg Roedel50917e22014-08-05 16:38:38 +020088 * This struct contains device specific data for the IOMMU
89 */
90struct iommu_dev_data {
91 struct list_head list; /* For domain->dev_list */
92 struct list_head dev_data_list; /* For global dev_data_list */
Joerg Roedel50917e22014-08-05 16:38:38 +020093 struct protection_domain *domain; /* Domain the device is bound to */
Joerg Roedel50917e22014-08-05 16:38:38 +020094 u16 devid; /* PCI Device ID */
95 bool iommu_v2; /* Device can make use of IOMMUv2 */
Joerg Roedel1e6a7b02015-07-28 16:58:48 +020096 bool passthrough; /* Device is identity mapped */
Joerg Roedel50917e22014-08-05 16:38:38 +020097 struct {
98 bool enabled;
99 int qdep;
100 } ats; /* ATS state */
101 bool pri_tlp; /* PASID TLB required for
102 PPR completions */
103 u32 errata; /* Bitmap for errata to apply */
104};
105
106/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200107 * general struct to manage commands send to an IOMMU
108 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200109struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +0200110 u32 data[4];
111};
112
Joerg Roedel05152a02012-06-15 16:53:51 +0200113struct kmem_cache *amd_iommu_irq_cache;
114
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200115static void update_domain(struct protection_domain *domain);
Joerg Roedel7a5a5662015-06-30 08:56:11 +0200116static int protection_domain_init(struct protection_domain *domain);
Chris Wrightc1eee672009-05-21 00:56:58 -0700117
Joerg Roedel007b74b2015-12-21 12:53:54 +0100118/*
119 * For dynamic growth the aperture size is split into ranges of 128MB of
120 * DMA address space each. This struct represents one such range.
121 */
122struct aperture_range {
123
Joerg Roedel08c5fb92015-12-21 13:04:49 +0100124 spinlock_t bitmap_lock;
125
Joerg Roedel007b74b2015-12-21 12:53:54 +0100126 /* address allocation bitmap */
127 unsigned long *bitmap;
Joerg Roedelae62d492015-12-21 16:28:45 +0100128 unsigned long offset;
Joerg Roedel60e6a7c2015-12-21 16:53:17 +0100129 unsigned long next_bit;
Joerg Roedel007b74b2015-12-21 12:53:54 +0100130
131 /*
132 * Array of PTE pages for the aperture. In this array we save all the
133 * leaf pages of the domain page table used for the aperture. This way
134 * we don't need to walk the page table to find a specific PTE. We can
135 * just calculate its address in constant time.
136 */
137 u64 *pte_pages[64];
Joerg Roedel007b74b2015-12-21 12:53:54 +0100138};
139
140/*
141 * Data container for a dma_ops specific protection domain
142 */
143struct dma_ops_domain {
144 /* generic protection domain information */
145 struct protection_domain domain;
146
147 /* size of the aperture for the mappings */
148 unsigned long aperture_size;
149
Joerg Roedelebaecb42015-12-21 18:11:32 +0100150 /* aperture index we start searching for free addresses */
Joerg Roedel5f6bed52015-12-22 13:34:22 +0100151 u32 __percpu *next_index;
Joerg Roedel007b74b2015-12-21 12:53:54 +0100152
153 /* address space relevant data */
154 struct aperture_range *aperture[APERTURE_MAX_RANGES];
Joerg Roedel007b74b2015-12-21 12:53:54 +0100155};
156
Joerg Roedel15898bb2009-11-24 15:39:42 +0100157/****************************************************************************
158 *
159 * Helper functions
160 *
161 ****************************************************************************/
162
Joerg Roedel3f4b87b2015-03-26 13:43:07 +0100163static struct protection_domain *to_pdomain(struct iommu_domain *dom)
164{
165 return container_of(dom, struct protection_domain, domain);
166}
167
Joerg Roedelf62dda62011-06-09 12:55:35 +0200168static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200169{
170 struct iommu_dev_data *dev_data;
171 unsigned long flags;
172
173 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
174 if (!dev_data)
175 return NULL;
176
Joerg Roedelf62dda62011-06-09 12:55:35 +0200177 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200178
179 spin_lock_irqsave(&dev_data_list_lock, flags);
180 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
181 spin_unlock_irqrestore(&dev_data_list_lock, flags);
182
183 return dev_data;
184}
185
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200186static struct iommu_dev_data *search_dev_data(u16 devid)
187{
188 struct iommu_dev_data *dev_data;
189 unsigned long flags;
190
191 spin_lock_irqsave(&dev_data_list_lock, flags);
192 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
193 if (dev_data->devid == devid)
194 goto out_unlock;
195 }
196
197 dev_data = NULL;
198
199out_unlock:
200 spin_unlock_irqrestore(&dev_data_list_lock, flags);
201
202 return dev_data;
203}
204
205static struct iommu_dev_data *find_dev_data(u16 devid)
206{
207 struct iommu_dev_data *dev_data;
208
209 dev_data = search_dev_data(devid);
210
211 if (dev_data == NULL)
212 dev_data = alloc_dev_data(devid);
213
214 return dev_data;
215}
216
Joerg Roedel15898bb2009-11-24 15:39:42 +0100217static inline u16 get_device_id(struct device *dev)
218{
219 struct pci_dev *pdev = to_pci_dev(dev);
220
Shuah Khan6f2729b2013-02-27 17:07:30 -0700221 return PCI_DEVID(pdev->bus->number, pdev->devfn);
Joerg Roedel15898bb2009-11-24 15:39:42 +0100222}
223
Joerg Roedel657cbb62009-11-23 15:26:46 +0100224static struct iommu_dev_data *get_dev_data(struct device *dev)
225{
226 return dev->archdata.iommu;
227}
228
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100229static bool pci_iommuv2_capable(struct pci_dev *pdev)
230{
231 static const int caps[] = {
232 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100233 PCI_EXT_CAP_ID_PRI,
234 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100235 };
236 int i, pos;
237
238 for (i = 0; i < 3; ++i) {
239 pos = pci_find_ext_capability(pdev, caps[i]);
240 if (pos == 0)
241 return false;
242 }
243
244 return true;
245}
246
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100247static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
248{
249 struct iommu_dev_data *dev_data;
250
251 dev_data = get_dev_data(&pdev->dev);
252
253 return dev_data->errata & (1 << erratum) ? true : false;
254}
255
Joerg Roedel71c70982009-11-24 16:43:06 +0100256/*
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200257 * This function actually applies the mapping to the page table of the
258 * dma_ops domain.
Joerg Roedel71c70982009-11-24 16:43:06 +0100259 */
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200260static void alloc_unity_mapping(struct dma_ops_domain *dma_dom,
261 struct unity_map_entry *e)
Joerg Roedel71c70982009-11-24 16:43:06 +0100262{
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200263 u64 addr;
Joerg Roedel71c70982009-11-24 16:43:06 +0100264
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200265 for (addr = e->address_start; addr < e->address_end;
266 addr += PAGE_SIZE) {
267 if (addr < dma_dom->aperture_size)
268 __set_bit(addr >> PAGE_SHIFT,
269 dma_dom->aperture[0]->bitmap);
Joerg Roedel71c70982009-11-24 16:43:06 +0100270 }
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200271}
Joerg Roedel71c70982009-11-24 16:43:06 +0100272
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200273/*
274 * Inits the unity mappings required for a specific device
275 */
276static void init_unity_mappings_for_device(struct device *dev,
277 struct dma_ops_domain *dma_dom)
278{
279 struct unity_map_entry *e;
280 u16 devid;
Joerg Roedel71c70982009-11-24 16:43:06 +0100281
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200282 devid = get_device_id(dev);
283
284 list_for_each_entry(e, &amd_iommu_unity_map, list) {
285 if (!(devid >= e->devid_start && devid <= e->devid_end))
286 continue;
287 alloc_unity_mapping(dma_dom, e);
288 }
Joerg Roedel71c70982009-11-24 16:43:06 +0100289}
290
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100291/*
292 * This function checks if the driver got a valid device from the caller to
293 * avoid dereferencing invalid pointers.
294 */
295static bool check_device(struct device *dev)
296{
297 u16 devid;
298
299 if (!dev || !dev->dma_mask)
300 return false;
301
Yijing Wangb82a2272013-12-05 19:42:41 +0800302 /* No PCI device */
303 if (!dev_is_pci(dev))
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100304 return false;
305
306 devid = get_device_id(dev);
307
308 /* Out of our scope? */
309 if (devid > amd_iommu_last_bdf)
310 return false;
311
312 if (amd_iommu_rlookup_table[devid] == NULL)
313 return false;
314
315 return true;
316}
317
Alex Williamson25b11ce2014-09-19 10:03:13 -0600318static void init_iommu_group(struct device *dev)
Alex Williamson2851db22012-10-08 22:49:41 -0600319{
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200320 struct dma_ops_domain *dma_domain;
321 struct iommu_domain *domain;
Alex Williamson2851db22012-10-08 22:49:41 -0600322 struct iommu_group *group;
Alex Williamson2851db22012-10-08 22:49:41 -0600323
Alex Williamson65d53522014-07-03 09:51:30 -0600324 group = iommu_group_get_for_dev(dev);
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200325 if (IS_ERR(group))
326 return;
327
328 domain = iommu_group_default_domain(group);
329 if (!domain)
330 goto out;
331
332 dma_domain = to_pdomain(domain)->priv;
333
334 init_unity_mappings_for_device(dev, dma_domain);
335out:
336 iommu_group_put(group);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600337}
338
339static int iommu_init_device(struct device *dev)
340{
341 struct pci_dev *pdev = to_pci_dev(dev);
342 struct iommu_dev_data *dev_data;
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600343
344 if (dev->archdata.iommu)
345 return 0;
346
347 dev_data = find_dev_data(get_device_id(dev));
348 if (!dev_data)
349 return -ENOMEM;
350
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100351 if (pci_iommuv2_capable(pdev)) {
352 struct amd_iommu *iommu;
353
354 iommu = amd_iommu_rlookup_table[dev_data->devid];
355 dev_data->iommu_v2 = iommu->is_iommu_v2;
356 }
357
Joerg Roedel657cbb62009-11-23 15:26:46 +0100358 dev->archdata.iommu = dev_data;
359
Alex Williamson066f2e92014-06-12 16:12:37 -0600360 iommu_device_link(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
361 dev);
362
Joerg Roedel657cbb62009-11-23 15:26:46 +0100363 return 0;
364}
365
Joerg Roedel26018872011-06-06 16:50:14 +0200366static void iommu_ignore_device(struct device *dev)
367{
368 u16 devid, alias;
369
370 devid = get_device_id(dev);
371 alias = amd_iommu_alias_table[devid];
372
373 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
374 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
375
376 amd_iommu_rlookup_table[devid] = NULL;
377 amd_iommu_rlookup_table[alias] = NULL;
378}
379
Joerg Roedel657cbb62009-11-23 15:26:46 +0100380static void iommu_uninit_device(struct device *dev)
381{
Alex Williamsonc1931092014-07-03 09:51:24 -0600382 struct iommu_dev_data *dev_data = search_dev_data(get_device_id(dev));
383
384 if (!dev_data)
385 return;
386
Alex Williamson066f2e92014-06-12 16:12:37 -0600387 iommu_device_unlink(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
388 dev);
389
Alex Williamson9dcd6132012-05-30 14:19:07 -0600390 iommu_group_remove_device(dev);
391
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200392 /* Remove dma-ops */
393 dev->archdata.dma_ops = NULL;
394
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200395 /*
Alex Williamsonc1931092014-07-03 09:51:24 -0600396 * We keep dev_data around for unplugged devices and reuse it when the
397 * device is re-plugged - not doing so would introduce a ton of races.
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200398 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100399}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100400
Joerg Roedel7f265082008-12-12 13:50:21 +0100401#ifdef CONFIG_AMD_IOMMU_STATS
402
403/*
404 * Initialization code for statistics collection
405 */
406
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100407DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100408DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100409DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100410DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100411DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100412DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100413DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100414DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100415DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100416DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100417DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100418DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100419DECLARE_STATS_COUNTER(complete_ppr);
420DECLARE_STATS_COUNTER(invalidate_iotlb);
421DECLARE_STATS_COUNTER(invalidate_iotlb_all);
422DECLARE_STATS_COUNTER(pri_requests);
423
Joerg Roedel7f265082008-12-12 13:50:21 +0100424static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100425static struct dentry *de_fflush;
426
427static void amd_iommu_stats_add(struct __iommu_counter *cnt)
428{
429 if (stats_dir == NULL)
430 return;
431
432 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
433 &cnt->value);
434}
435
436static void amd_iommu_stats_init(void)
437{
438 stats_dir = debugfs_create_dir("amd-iommu", NULL);
439 if (stats_dir == NULL)
440 return;
441
Joerg Roedel7f265082008-12-12 13:50:21 +0100442 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
Dan Carpenter3775d482012-06-27 12:09:18 +0300443 &amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100444
445 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100446 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100447 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100448 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100449 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100450 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100451 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100452 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100453 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100454 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100455 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100456 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100457 amd_iommu_stats_add(&complete_ppr);
458 amd_iommu_stats_add(&invalidate_iotlb);
459 amd_iommu_stats_add(&invalidate_iotlb_all);
460 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100461}
462
463#endif
464
Joerg Roedel431b2a22008-07-11 17:14:22 +0200465/****************************************************************************
466 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200467 * Interrupt handling functions
468 *
469 ****************************************************************************/
470
Joerg Roedele3e59872009-09-03 14:02:10 +0200471static void dump_dte_entry(u16 devid)
472{
473 int i;
474
Joerg Roedelee6c2862011-11-09 12:06:03 +0100475 for (i = 0; i < 4; ++i)
476 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200477 amd_iommu_dev_table[devid].data[i]);
478}
479
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200480static void dump_command(unsigned long phys_addr)
481{
482 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
483 int i;
484
485 for (i = 0; i < 4; ++i)
486 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
487}
488
Joerg Roedela345b232009-09-03 15:01:43 +0200489static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200490{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200491 int type, devid, domid, flags;
492 volatile u32 *event = __evt;
493 int count = 0;
494 u64 address;
495
496retry:
497 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
498 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
499 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
500 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
501 address = (u64)(((u64)event[3]) << 32) | event[2];
502
503 if (type == 0) {
504 /* Did we hit the erratum? */
505 if (++count == LOOP_TIMEOUT) {
506 pr_err("AMD-Vi: No event written to event log\n");
507 return;
508 }
509 udelay(1);
510 goto retry;
511 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200512
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200513 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200514
515 switch (type) {
516 case EVENT_TYPE_ILL_DEV:
517 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
518 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700519 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200520 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200521 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200522 break;
523 case EVENT_TYPE_IO_FAULT:
524 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
525 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700526 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200527 domid, address, flags);
528 break;
529 case EVENT_TYPE_DEV_TAB_ERR:
530 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
531 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700532 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200533 address, flags);
534 break;
535 case EVENT_TYPE_PAGE_TAB_ERR:
536 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
537 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700538 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200539 domid, address, flags);
540 break;
541 case EVENT_TYPE_ILL_CMD:
542 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200543 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200544 break;
545 case EVENT_TYPE_CMD_HARD_ERR:
546 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
547 "flags=0x%04x]\n", address, flags);
548 break;
549 case EVENT_TYPE_IOTLB_INV_TO:
550 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
551 "address=0x%016llx]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700552 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200553 address);
554 break;
555 case EVENT_TYPE_INV_DEV_REQ:
556 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
557 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700558 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200559 address, flags);
560 break;
561 default:
562 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
563 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200564
565 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200566}
567
568static void iommu_poll_events(struct amd_iommu *iommu)
569{
570 u32 head, tail;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200571
572 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
573 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
574
575 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200576 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200577 head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200578 }
579
580 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200581}
582
Joerg Roedeleee53532012-06-01 15:20:23 +0200583static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100584{
585 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100586
Joerg Roedel399be2f2011-12-01 16:53:47 +0100587 INC_STATS_COUNTER(pri_requests);
588
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100589 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
590 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
591 return;
592 }
593
594 fault.address = raw[1];
595 fault.pasid = PPR_PASID(raw[0]);
596 fault.device_id = PPR_DEVID(raw[0]);
597 fault.tag = PPR_TAG(raw[0]);
598 fault.flags = PPR_FLAGS(raw[0]);
599
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100600 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
601}
602
603static void iommu_poll_ppr_log(struct amd_iommu *iommu)
604{
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100605 u32 head, tail;
606
607 if (iommu->ppr_log == NULL)
608 return;
609
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100610 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
611 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
612
613 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200614 volatile u64 *raw;
615 u64 entry[2];
616 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100617
Joerg Roedeleee53532012-06-01 15:20:23 +0200618 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100619
Joerg Roedeleee53532012-06-01 15:20:23 +0200620 /*
621 * Hardware bug: Interrupt may arrive before the entry is
622 * written to memory. If this happens we need to wait for the
623 * entry to arrive.
624 */
625 for (i = 0; i < LOOP_TIMEOUT; ++i) {
626 if (PPR_REQ_TYPE(raw[0]) != 0)
627 break;
628 udelay(1);
629 }
630
631 /* Avoid memcpy function-call overhead */
632 entry[0] = raw[0];
633 entry[1] = raw[1];
634
635 /*
636 * To detect the hardware bug we need to clear the entry
637 * back to zero.
638 */
639 raw[0] = raw[1] = 0UL;
640
641 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100642 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
643 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200644
Joerg Roedeleee53532012-06-01 15:20:23 +0200645 /* Handle PPR entry */
646 iommu_handle_ppr_entry(iommu, entry);
647
Joerg Roedeleee53532012-06-01 15:20:23 +0200648 /* Refresh ring-buffer information */
649 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100650 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
651 }
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100652}
653
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200654irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200655{
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500656 struct amd_iommu *iommu = (struct amd_iommu *) data;
657 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200658
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500659 while (status & (MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK)) {
660 /* Enable EVT and PPR interrupts again */
661 writel((MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK),
662 iommu->mmio_base + MMIO_STATUS_OFFSET);
663
664 if (status & MMIO_STATUS_EVT_INT_MASK) {
665 pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
666 iommu_poll_events(iommu);
667 }
668
669 if (status & MMIO_STATUS_PPR_INT_MASK) {
670 pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
671 iommu_poll_ppr_log(iommu);
672 }
673
674 /*
675 * Hardware bug: ERBT1312
676 * When re-enabling interrupt (by writing 1
677 * to clear the bit), the hardware might also try to set
678 * the interrupt bit in the event status register.
679 * In this scenario, the bit will be set, and disable
680 * subsequent interrupts.
681 *
682 * Workaround: The IOMMU driver should read back the
683 * status register and check if the interrupt bits are cleared.
684 * If not, driver will need to go through the interrupt handler
685 * again and re-clear the bits
686 */
687 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100688 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200689 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200690}
691
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200692irqreturn_t amd_iommu_int_handler(int irq, void *data)
693{
694 return IRQ_WAKE_THREAD;
695}
696
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200697/****************************************************************************
698 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200699 * IOMMU command queuing functions
700 *
701 ****************************************************************************/
702
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200703static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200704{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200705 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200706
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200707 while (*sem == 0 && i < LOOP_TIMEOUT) {
708 udelay(1);
709 i += 1;
710 }
711
712 if (i == LOOP_TIMEOUT) {
713 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
714 return -EIO;
715 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200716
717 return 0;
718}
719
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200720static void copy_cmd_to_buffer(struct amd_iommu *iommu,
721 struct iommu_cmd *cmd,
722 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200723{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200724 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200725
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200726 target = iommu->cmd_buf + tail;
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200727 tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200728
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200729 /* Copy command to buffer */
730 memcpy(target, cmd, sizeof(*cmd));
731
732 /* Tell the IOMMU about it */
733 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
734}
735
Joerg Roedel815b33f2011-04-06 17:26:49 +0200736static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200737{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200738 WARN_ON(address & 0x7ULL);
739
Joerg Roedelded46732011-04-06 10:53:48 +0200740 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200741 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
742 cmd->data[1] = upper_32_bits(__pa(address));
743 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200744 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
745}
746
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200747static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
748{
749 memset(cmd, 0, sizeof(*cmd));
750 cmd->data[0] = devid;
751 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
752}
753
Joerg Roedel11b64022011-04-06 11:49:28 +0200754static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
755 size_t size, u16 domid, int pde)
756{
757 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100758 bool s;
Joerg Roedel11b64022011-04-06 11:49:28 +0200759
760 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100761 s = false;
Joerg Roedel11b64022011-04-06 11:49:28 +0200762
763 if (pages > 1) {
764 /*
765 * If we have to flush more than one page, flush all
766 * TLB entries for this domain
767 */
768 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100769 s = true;
Joerg Roedel11b64022011-04-06 11:49:28 +0200770 }
771
772 address &= PAGE_MASK;
773
774 memset(cmd, 0, sizeof(*cmd));
775 cmd->data[1] |= domid;
776 cmd->data[2] = lower_32_bits(address);
777 cmd->data[3] = upper_32_bits(address);
778 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
779 if (s) /* size bit - we flush more than one 4kb page */
780 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200781 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200782 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
783}
784
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200785static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
786 u64 address, size_t size)
787{
788 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100789 bool s;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200790
791 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100792 s = false;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200793
794 if (pages > 1) {
795 /*
796 * If we have to flush more than one page, flush all
797 * TLB entries for this domain
798 */
799 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100800 s = true;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200801 }
802
803 address &= PAGE_MASK;
804
805 memset(cmd, 0, sizeof(*cmd));
806 cmd->data[0] = devid;
807 cmd->data[0] |= (qdep & 0xff) << 24;
808 cmd->data[1] = devid;
809 cmd->data[2] = lower_32_bits(address);
810 cmd->data[3] = upper_32_bits(address);
811 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
812 if (s)
813 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
814}
815
Joerg Roedel22e266c2011-11-21 15:59:08 +0100816static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
817 u64 address, bool size)
818{
819 memset(cmd, 0, sizeof(*cmd));
820
821 address &= ~(0xfffULL);
822
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600823 cmd->data[0] = pasid;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100824 cmd->data[1] = domid;
825 cmd->data[2] = lower_32_bits(address);
826 cmd->data[3] = upper_32_bits(address);
827 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
828 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
829 if (size)
830 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
831 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
832}
833
834static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
835 int qdep, u64 address, bool size)
836{
837 memset(cmd, 0, sizeof(*cmd));
838
839 address &= ~(0xfffULL);
840
841 cmd->data[0] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600842 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100843 cmd->data[0] |= (qdep & 0xff) << 24;
844 cmd->data[1] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600845 cmd->data[1] |= (pasid & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100846 cmd->data[2] = lower_32_bits(address);
847 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
848 cmd->data[3] = upper_32_bits(address);
849 if (size)
850 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
851 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
852}
853
Joerg Roedelc99afa22011-11-21 18:19:25 +0100854static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
855 int status, int tag, bool gn)
856{
857 memset(cmd, 0, sizeof(*cmd));
858
859 cmd->data[0] = devid;
860 if (gn) {
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600861 cmd->data[1] = pasid;
Joerg Roedelc99afa22011-11-21 18:19:25 +0100862 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
863 }
864 cmd->data[3] = tag & 0x1ff;
865 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
866
867 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
868}
869
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200870static void build_inv_all(struct iommu_cmd *cmd)
871{
872 memset(cmd, 0, sizeof(*cmd));
873 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200874}
875
Joerg Roedel7ef27982012-06-21 16:46:04 +0200876static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
877{
878 memset(cmd, 0, sizeof(*cmd));
879 cmd->data[0] = devid;
880 CMD_SET_TYPE(cmd, CMD_INV_IRT);
881}
882
Joerg Roedel431b2a22008-07-11 17:14:22 +0200883/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200884 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200885 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200886 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200887static int iommu_queue_command_sync(struct amd_iommu *iommu,
888 struct iommu_cmd *cmd,
889 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200890{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200891 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200892 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200893
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200894again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200895 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200896
897 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
898 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200899 next_tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
900 left = (head - next_tail) % CMD_BUFFER_SIZE;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200901
902 if (left <= 2) {
903 struct iommu_cmd sync_cmd;
904 volatile u64 sem = 0;
905 int ret;
906
907 build_completion_wait(&sync_cmd, (u64)&sem);
908 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
909
910 spin_unlock_irqrestore(&iommu->lock, flags);
911
912 if ((ret = wait_on_sem(&sem)) != 0)
913 return ret;
914
915 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200916 }
917
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200918 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200919
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200920 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200921 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200922
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200923 spin_unlock_irqrestore(&iommu->lock, flags);
924
Joerg Roedel815b33f2011-04-06 17:26:49 +0200925 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100926}
927
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200928static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
929{
930 return iommu_queue_command_sync(iommu, cmd, true);
931}
932
Joerg Roedel8d201962008-12-02 20:34:41 +0100933/*
934 * This function queues a completion wait command into the command
935 * buffer of an IOMMU
936 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100937static int iommu_completion_wait(struct amd_iommu *iommu)
938{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200939 struct iommu_cmd cmd;
940 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200941 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100942
943 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200944 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100945
Joerg Roedel815b33f2011-04-06 17:26:49 +0200946 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100947
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200948 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +0100949 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200950 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100951
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200952 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200953}
954
Joerg Roedeld8c13082011-04-06 18:51:26 +0200955static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200956{
957 struct iommu_cmd cmd;
958
Joerg Roedeld8c13082011-04-06 18:51:26 +0200959 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200960
Joerg Roedeld8c13082011-04-06 18:51:26 +0200961 return iommu_queue_command(iommu, &cmd);
962}
963
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200964static void iommu_flush_dte_all(struct amd_iommu *iommu)
965{
966 u32 devid;
967
968 for (devid = 0; devid <= 0xffff; ++devid)
969 iommu_flush_dte(iommu, devid);
970
971 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200972}
973
974/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200975 * This function uses heavy locking and may disable irqs for some time. But
976 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200977 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200978static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200979{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200980 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200981
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200982 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
983 struct iommu_cmd cmd;
984 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
985 dom_id, 1);
986 iommu_queue_command(iommu, &cmd);
987 }
Joerg Roedel431b2a22008-07-11 17:14:22 +0200988
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200989 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200990}
991
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200992static void iommu_flush_all(struct amd_iommu *iommu)
993{
994 struct iommu_cmd cmd;
995
996 build_inv_all(&cmd);
997
998 iommu_queue_command(iommu, &cmd);
999 iommu_completion_wait(iommu);
1000}
1001
Joerg Roedel7ef27982012-06-21 16:46:04 +02001002static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1003{
1004 struct iommu_cmd cmd;
1005
1006 build_inv_irt(&cmd, devid);
1007
1008 iommu_queue_command(iommu, &cmd);
1009}
1010
1011static void iommu_flush_irt_all(struct amd_iommu *iommu)
1012{
1013 u32 devid;
1014
1015 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1016 iommu_flush_irt(iommu, devid);
1017
1018 iommu_completion_wait(iommu);
1019}
1020
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001021void iommu_flush_all_caches(struct amd_iommu *iommu)
1022{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001023 if (iommu_feature(iommu, FEATURE_IA)) {
1024 iommu_flush_all(iommu);
1025 } else {
1026 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001027 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001028 iommu_flush_tlb_all(iommu);
1029 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001030}
1031
Joerg Roedel431b2a22008-07-11 17:14:22 +02001032/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001033 * Command send function for flushing on-device TLB
1034 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001035static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1036 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001037{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001038 struct amd_iommu *iommu;
1039 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001040 int qdep;
1041
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001042 qdep = dev_data->ats.qdep;
1043 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001044
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001045 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001046
1047 return iommu_queue_command(iommu, &cmd);
1048}
1049
1050/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001051 * Command send function for invalidating a device table entry
1052 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001053static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001054{
1055 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001056 u16 alias;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001057 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001058
Joerg Roedel6c542042011-06-09 17:07:31 +02001059 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele25bfb52015-10-20 17:33:38 +02001060 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001061
Joerg Roedelf62dda62011-06-09 12:55:35 +02001062 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001063 if (!ret && alias != dev_data->devid)
1064 ret = iommu_flush_dte(iommu, alias);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001065 if (ret)
1066 return ret;
1067
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001068 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001069 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001070
1071 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001072}
1073
Joerg Roedel431b2a22008-07-11 17:14:22 +02001074/*
1075 * TLB invalidation function which is called from the mapping functions.
1076 * It invalidates a single PTE if the range to flush is within a single
1077 * page. Otherwise it flushes the whole TLB of the IOMMU.
1078 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001079static void __domain_flush_pages(struct protection_domain *domain,
1080 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001081{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001082 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001083 struct iommu_cmd cmd;
1084 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001085
Joerg Roedel11b64022011-04-06 11:49:28 +02001086 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001087
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001088 for (i = 0; i < amd_iommus_present; ++i) {
1089 if (!domain->dev_iommu[i])
1090 continue;
1091
1092 /*
1093 * Devices of this domain are behind this IOMMU
1094 * We need a TLB flush
1095 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001096 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001097 }
1098
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001099 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001100
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001101 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001102 continue;
1103
Joerg Roedel6c542042011-06-09 17:07:31 +02001104 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001105 }
1106
Joerg Roedel11b64022011-04-06 11:49:28 +02001107 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001108}
1109
Joerg Roedel17b124b2011-04-06 18:01:35 +02001110static void domain_flush_pages(struct protection_domain *domain,
1111 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001112{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001113 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001114}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001115
Joerg Roedel1c655772008-09-04 18:40:05 +02001116/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001117static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001118{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001119 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001120}
1121
Chris Wright42a49f92009-06-15 15:42:00 +02001122/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001123static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001124{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001125 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1126}
1127
1128static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001129{
1130 int i;
1131
1132 for (i = 0; i < amd_iommus_present; ++i) {
1133 if (!domain->dev_iommu[i])
1134 continue;
1135
1136 /*
1137 * Devices of this domain are behind this IOMMU
1138 * We need to wait for completion of all commands.
1139 */
1140 iommu_completion_wait(amd_iommus[i]);
1141 }
1142}
1143
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001144
Joerg Roedel43f49602008-12-02 21:01:12 +01001145/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001146 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001147 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001148static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001149{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001150 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001151
1152 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001153 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001154}
1155
Joerg Roedel431b2a22008-07-11 17:14:22 +02001156/****************************************************************************
1157 *
1158 * The functions below are used the create the page table mappings for
1159 * unity mapped regions.
1160 *
1161 ****************************************************************************/
1162
1163/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001164 * This function is used to add another level to an IO page table. Adding
1165 * another level increases the size of the address space by 9 bits to a size up
1166 * to 64 bits.
1167 */
1168static bool increase_address_space(struct protection_domain *domain,
1169 gfp_t gfp)
1170{
1171 u64 *pte;
1172
1173 if (domain->mode == PAGE_MODE_6_LEVEL)
1174 /* address space already 64 bit large */
1175 return false;
1176
1177 pte = (void *)get_zeroed_page(gfp);
1178 if (!pte)
1179 return false;
1180
1181 *pte = PM_LEVEL_PDE(domain->mode,
1182 virt_to_phys(domain->pt_root));
1183 domain->pt_root = pte;
1184 domain->mode += 1;
1185 domain->updated = true;
1186
1187 return true;
1188}
1189
1190static u64 *alloc_pte(struct protection_domain *domain,
1191 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001192 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001193 u64 **pte_page,
1194 gfp_t gfp)
1195{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001196 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001197 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001198
1199 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001200
1201 while (address > PM_LEVEL_SIZE(domain->mode))
1202 increase_address_space(domain, gfp);
1203
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001204 level = domain->mode - 1;
1205 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1206 address = PAGE_SIZE_ALIGN(address, page_size);
1207 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001208
1209 while (level > end_lvl) {
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001210 u64 __pte, __npte;
1211
1212 __pte = *pte;
1213
1214 if (!IOMMU_PTE_PRESENT(__pte)) {
Joerg Roedel308973d2009-11-24 17:43:32 +01001215 page = (u64 *)get_zeroed_page(gfp);
1216 if (!page)
1217 return NULL;
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001218
1219 __npte = PM_LEVEL_PDE(level, virt_to_phys(page));
1220
1221 if (cmpxchg64(pte, __pte, __npte)) {
1222 free_page((unsigned long)page);
1223 continue;
1224 }
Joerg Roedel308973d2009-11-24 17:43:32 +01001225 }
1226
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001227 /* No level skipping support yet */
1228 if (PM_PTE_LEVEL(*pte) != level)
1229 return NULL;
1230
Joerg Roedel308973d2009-11-24 17:43:32 +01001231 level -= 1;
1232
1233 pte = IOMMU_PTE_PAGE(*pte);
1234
1235 if (pte_page && level == end_lvl)
1236 *pte_page = pte;
1237
1238 pte = &pte[PM_LEVEL_INDEX(level, address)];
1239 }
1240
1241 return pte;
1242}
1243
1244/*
1245 * This function checks if there is a PTE for a given dma address. If
1246 * there is one, it returns the pointer to it.
1247 */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001248static u64 *fetch_pte(struct protection_domain *domain,
1249 unsigned long address,
1250 unsigned long *page_size)
Joerg Roedel308973d2009-11-24 17:43:32 +01001251{
1252 int level;
1253 u64 *pte;
1254
Joerg Roedel24cd7722010-01-19 17:27:39 +01001255 if (address > PM_LEVEL_SIZE(domain->mode))
1256 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001257
Joerg Roedel3039ca12015-04-01 14:58:48 +02001258 level = domain->mode - 1;
1259 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1260 *page_size = PTE_LEVEL_PAGE_SIZE(level);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001261
1262 while (level > 0) {
1263
1264 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001265 if (!IOMMU_PTE_PRESENT(*pte))
1266 return NULL;
1267
Joerg Roedel24cd7722010-01-19 17:27:39 +01001268 /* Large PTE */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001269 if (PM_PTE_LEVEL(*pte) == 7 ||
1270 PM_PTE_LEVEL(*pte) == 0)
1271 break;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001272
1273 /* No level skipping support yet */
1274 if (PM_PTE_LEVEL(*pte) != level)
1275 return NULL;
1276
Joerg Roedel308973d2009-11-24 17:43:32 +01001277 level -= 1;
1278
Joerg Roedel24cd7722010-01-19 17:27:39 +01001279 /* Walk to the next level */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001280 pte = IOMMU_PTE_PAGE(*pte);
1281 pte = &pte[PM_LEVEL_INDEX(level, address)];
1282 *page_size = PTE_LEVEL_PAGE_SIZE(level);
1283 }
1284
1285 if (PM_PTE_LEVEL(*pte) == 0x07) {
1286 unsigned long pte_mask;
1287
1288 /*
1289 * If we have a series of large PTEs, make
1290 * sure to return a pointer to the first one.
1291 */
1292 *page_size = pte_mask = PTE_PAGE_SIZE(*pte);
1293 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1294 pte = (u64 *)(((unsigned long)pte) & pte_mask);
Joerg Roedel308973d2009-11-24 17:43:32 +01001295 }
1296
1297 return pte;
1298}
1299
1300/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001301 * Generic mapping functions. It maps a physical address into a DMA
1302 * address space. It allocates the page table pages if necessary.
1303 * In the future it can be extended to a generic mapping function
1304 * supporting all features of AMD IOMMU page tables like level skipping
1305 * and full 64 bit address spaces.
1306 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001307static int iommu_map_page(struct protection_domain *dom,
1308 unsigned long bus_addr,
1309 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001310 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001311 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001312{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001313 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001314 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001315
Joerg Roedeld4b03662015-04-01 14:58:52 +02001316 BUG_ON(!IS_ALIGNED(bus_addr, page_size));
1317 BUG_ON(!IS_ALIGNED(phys_addr, page_size));
1318
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001319 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001320 return -EINVAL;
1321
Joerg Roedeld4b03662015-04-01 14:58:52 +02001322 count = PAGE_SIZE_PTE_COUNT(page_size);
1323 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001324
Maurizio Lombardi63eaa752014-09-11 12:28:03 +02001325 if (!pte)
1326 return -ENOMEM;
1327
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001328 for (i = 0; i < count; ++i)
1329 if (IOMMU_PTE_PRESENT(pte[i]))
1330 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001331
Joerg Roedeld4b03662015-04-01 14:58:52 +02001332 if (count > 1) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001333 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1334 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1335 } else
1336 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1337
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001338 if (prot & IOMMU_PROT_IR)
1339 __pte |= IOMMU_PTE_IR;
1340 if (prot & IOMMU_PROT_IW)
1341 __pte |= IOMMU_PTE_IW;
1342
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001343 for (i = 0; i < count; ++i)
1344 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001345
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001346 update_domain(dom);
1347
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001348 return 0;
1349}
1350
Joerg Roedel24cd7722010-01-19 17:27:39 +01001351static unsigned long iommu_unmap_page(struct protection_domain *dom,
1352 unsigned long bus_addr,
1353 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001354{
Joerg Roedel71b390e2015-04-01 14:58:49 +02001355 unsigned long long unmapped;
1356 unsigned long unmap_size;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001357 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001358
Joerg Roedel24cd7722010-01-19 17:27:39 +01001359 BUG_ON(!is_power_of_2(page_size));
1360
1361 unmapped = 0;
1362
1363 while (unmapped < page_size) {
1364
Joerg Roedel71b390e2015-04-01 14:58:49 +02001365 pte = fetch_pte(dom, bus_addr, &unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001366
Joerg Roedel71b390e2015-04-01 14:58:49 +02001367 if (pte) {
1368 int i, count;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001369
Joerg Roedel71b390e2015-04-01 14:58:49 +02001370 count = PAGE_SIZE_PTE_COUNT(unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001371 for (i = 0; i < count; i++)
1372 pte[i] = 0ULL;
1373 }
1374
1375 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1376 unmapped += unmap_size;
1377 }
1378
Alex Williamson60d0ca32013-06-21 14:33:19 -06001379 BUG_ON(unmapped && !is_power_of_2(unmapped));
Joerg Roedel24cd7722010-01-19 17:27:39 +01001380
1381 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001382}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001383
Joerg Roedel431b2a22008-07-11 17:14:22 +02001384/****************************************************************************
1385 *
1386 * The next functions belong to the address allocator for the dma_ops
1387 * interface functions. They work like the allocators in the other IOMMU
1388 * drivers. Its basically a bitmap which marks the allocated pages in
1389 * the aperture. Maybe it could be enhanced in the future to a more
1390 * efficient allocator.
1391 *
1392 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001393
Joerg Roedel431b2a22008-07-11 17:14:22 +02001394/*
Joerg Roedel384de722009-05-15 12:30:05 +02001395 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001396 *
1397 * called with domain->lock held
1398 */
Joerg Roedel384de722009-05-15 12:30:05 +02001399
Joerg Roedel9cabe892009-05-18 16:38:55 +02001400/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001401 * Used to reserve address ranges in the aperture (e.g. for exclusion
1402 * ranges.
1403 */
1404static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1405 unsigned long start_page,
1406 unsigned int pages)
1407{
1408 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1409
1410 if (start_page + pages > last_page)
1411 pages = last_page - start_page;
1412
1413 for (i = start_page; i < start_page + pages; ++i) {
1414 int index = i / APERTURE_RANGE_PAGES;
1415 int page = i % APERTURE_RANGE_PAGES;
1416 __set_bit(page, dom->aperture[index]->bitmap);
1417 }
1418}
1419
1420/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001421 * This function is used to add a new aperture range to an existing
1422 * aperture in case of dma_ops domain allocation or address allocation
1423 * failure.
1424 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001425static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001426 bool populate, gfp_t gfp)
1427{
1428 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001429 unsigned long i, old_size, pte_pgsize;
Joerg Roedela73c1562015-12-21 19:25:56 +01001430 struct aperture_range *range;
1431 struct amd_iommu *iommu;
1432 unsigned long flags;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001433
Joerg Roedelf5e97052009-05-22 12:31:53 +02001434#ifdef CONFIG_IOMMU_STRESS
1435 populate = false;
1436#endif
1437
Joerg Roedel9cabe892009-05-18 16:38:55 +02001438 if (index >= APERTURE_MAX_RANGES)
1439 return -ENOMEM;
1440
Joerg Roedela73c1562015-12-21 19:25:56 +01001441 range = kzalloc(sizeof(struct aperture_range), gfp);
1442 if (!range)
Joerg Roedel9cabe892009-05-18 16:38:55 +02001443 return -ENOMEM;
1444
Joerg Roedela73c1562015-12-21 19:25:56 +01001445 range->bitmap = (void *)get_zeroed_page(gfp);
1446 if (!range->bitmap)
Joerg Roedel9cabe892009-05-18 16:38:55 +02001447 goto out_free;
1448
Joerg Roedela73c1562015-12-21 19:25:56 +01001449 range->offset = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001450
Joerg Roedela73c1562015-12-21 19:25:56 +01001451 spin_lock_init(&range->bitmap_lock);
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001452
Joerg Roedel9cabe892009-05-18 16:38:55 +02001453 if (populate) {
1454 unsigned long address = dma_dom->aperture_size;
1455 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1456 u64 *pte, *pte_page;
1457
1458 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001459 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001460 &pte_page, gfp);
1461 if (!pte)
1462 goto out_free;
1463
Joerg Roedela73c1562015-12-21 19:25:56 +01001464 range->pte_pages[i] = pte_page;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001465
1466 address += APERTURE_RANGE_SIZE / 64;
1467 }
1468 }
1469
Joerg Roedel92d420e2015-12-21 19:31:33 +01001470 spin_lock_irqsave(&dma_dom->domain.lock, flags);
1471
Joerg Roedela73c1562015-12-21 19:25:56 +01001472 /* First take the bitmap_lock and then publish the range */
Joerg Roedel92d420e2015-12-21 19:31:33 +01001473 spin_lock(&range->bitmap_lock);
Joerg Roedela73c1562015-12-21 19:25:56 +01001474
1475 old_size = dma_dom->aperture_size;
1476 dma_dom->aperture[index] = range;
1477 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001478
Joerg Roedel17f5b562011-07-06 17:14:44 +02001479 /* Reserve address range used for MSI messages */
1480 if (old_size < MSI_ADDR_BASE_LO &&
1481 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1482 unsigned long spage;
1483 int pages;
1484
1485 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1486 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1487
1488 dma_ops_reserve_addresses(dma_dom, spage, pages);
1489 }
1490
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001491 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001492 for_each_iommu(iommu) {
1493 if (iommu->exclusion_start &&
1494 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1495 && iommu->exclusion_start < dma_dom->aperture_size) {
1496 unsigned long startpage;
1497 int pages = iommu_num_pages(iommu->exclusion_start,
1498 iommu->exclusion_length,
1499 PAGE_SIZE);
1500 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1501 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1502 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001503 }
1504
1505 /*
1506 * Check for areas already mapped as present in the new aperture
1507 * range and mark those pages as reserved in the allocator. Such
1508 * mappings may already exist as a result of requested unity
1509 * mappings for devices.
1510 */
1511 for (i = dma_dom->aperture[index]->offset;
1512 i < dma_dom->aperture_size;
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001513 i += pte_pgsize) {
Joerg Roedel3039ca12015-04-01 14:58:48 +02001514 u64 *pte = fetch_pte(&dma_dom->domain, i, &pte_pgsize);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001515 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1516 continue;
1517
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001518 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT,
1519 pte_pgsize >> 12);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001520 }
1521
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001522 update_domain(&dma_dom->domain);
1523
Joerg Roedel92d420e2015-12-21 19:31:33 +01001524 spin_unlock(&range->bitmap_lock);
1525
1526 spin_unlock_irqrestore(&dma_dom->domain.lock, flags);
Joerg Roedela73c1562015-12-21 19:25:56 +01001527
Joerg Roedel9cabe892009-05-18 16:38:55 +02001528 return 0;
1529
1530out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001531 update_domain(&dma_dom->domain);
1532
Joerg Roedela73c1562015-12-21 19:25:56 +01001533 free_page((unsigned long)range->bitmap);
Joerg Roedel9cabe892009-05-18 16:38:55 +02001534
Joerg Roedela73c1562015-12-21 19:25:56 +01001535 kfree(range);
Joerg Roedel9cabe892009-05-18 16:38:55 +02001536
1537 return -ENOMEM;
1538}
1539
Joerg Roedelccb50e02015-12-21 17:49:34 +01001540static dma_addr_t dma_ops_aperture_alloc(struct dma_ops_domain *dom,
1541 struct aperture_range *range,
Joerg Roedela0f51442015-12-21 16:20:09 +01001542 unsigned long pages,
Joerg Roedela0f51442015-12-21 16:20:09 +01001543 unsigned long dma_mask,
1544 unsigned long boundary_size,
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001545 unsigned long align_mask,
1546 bool trylock)
Joerg Roedela0f51442015-12-21 16:20:09 +01001547{
1548 unsigned long offset, limit, flags;
1549 dma_addr_t address;
Joerg Roedelccb50e02015-12-21 17:49:34 +01001550 bool flush = false;
Joerg Roedela0f51442015-12-21 16:20:09 +01001551
1552 offset = range->offset >> PAGE_SHIFT;
1553 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1554 dma_mask >> PAGE_SHIFT);
1555
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001556 if (trylock) {
1557 if (!spin_trylock_irqsave(&range->bitmap_lock, flags))
1558 return -1;
1559 } else {
1560 spin_lock_irqsave(&range->bitmap_lock, flags);
1561 }
1562
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001563 address = iommu_area_alloc(range->bitmap, limit, range->next_bit,
1564 pages, offset, boundary_size, align_mask);
Joerg Roedelccb50e02015-12-21 17:49:34 +01001565 if (address == -1) {
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001566 /* Nothing found, retry one time */
1567 address = iommu_area_alloc(range->bitmap, limit,
1568 0, pages, offset, boundary_size,
1569 align_mask);
Joerg Roedelccb50e02015-12-21 17:49:34 +01001570 flush = true;
1571 }
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001572
1573 if (address != -1)
1574 range->next_bit = address + pages;
1575
Joerg Roedela0f51442015-12-21 16:20:09 +01001576 spin_unlock_irqrestore(&range->bitmap_lock, flags);
1577
Joerg Roedelccb50e02015-12-21 17:49:34 +01001578 if (flush) {
1579 domain_flush_tlb(&dom->domain);
1580 domain_flush_complete(&dom->domain);
1581 }
1582
Joerg Roedela0f51442015-12-21 16:20:09 +01001583 return address;
1584}
1585
Joerg Roedel384de722009-05-15 12:30:05 +02001586static unsigned long dma_ops_area_alloc(struct device *dev,
1587 struct dma_ops_domain *dom,
1588 unsigned int pages,
1589 unsigned long align_mask,
Joerg Roedel05ab49e2015-12-21 17:58:26 +01001590 u64 dma_mask)
Joerg Roedel384de722009-05-15 12:30:05 +02001591{
Joerg Roedelab7032b2015-12-21 18:47:11 +01001592 unsigned long boundary_size, mask;
Joerg Roedel384de722009-05-15 12:30:05 +02001593 unsigned long address = -1;
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001594 bool first = true;
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001595 u32 start, i;
1596
1597 preempt_disable();
Joerg Roedel384de722009-05-15 12:30:05 +02001598
Joerg Roedele6aabee2015-05-27 09:26:09 +02001599 mask = dma_get_seg_boundary(dev);
1600
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001601again:
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001602 start = this_cpu_read(*dom->next_index);
1603
1604 /* Sanity check - is it really necessary? */
1605 if (unlikely(start > APERTURE_MAX_RANGES)) {
1606 start = 0;
1607 this_cpu_write(*dom->next_index, 0);
1608 }
1609
Joerg Roedele6aabee2015-05-27 09:26:09 +02001610 boundary_size = mask + 1 ? ALIGN(mask + 1, PAGE_SIZE) >> PAGE_SHIFT :
1611 1UL << (BITS_PER_LONG - PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001612
Joerg Roedel2a874422015-12-21 18:34:47 +01001613 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1614 struct aperture_range *range;
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001615 int index;
Joerg Roedelccb50e02015-12-21 17:49:34 +01001616
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001617 index = (start + i) % APERTURE_MAX_RANGES;
1618
1619 range = dom->aperture[index];
Joerg Roedel2a874422015-12-21 18:34:47 +01001620
1621 if (!range || range->offset >= dma_mask)
1622 continue;
Joerg Roedel384de722009-05-15 12:30:05 +02001623
Joerg Roedel2a874422015-12-21 18:34:47 +01001624 address = dma_ops_aperture_alloc(dom, range, pages,
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001625 dma_mask, boundary_size,
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001626 align_mask, first);
Joerg Roedel384de722009-05-15 12:30:05 +02001627 if (address != -1) {
Joerg Roedel2a874422015-12-21 18:34:47 +01001628 address = range->offset + (address << PAGE_SHIFT);
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001629 this_cpu_write(*dom->next_index, index);
Joerg Roedel384de722009-05-15 12:30:05 +02001630 break;
1631 }
Joerg Roedel384de722009-05-15 12:30:05 +02001632 }
1633
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001634 if (address == -1 && first) {
1635 first = false;
1636 goto again;
1637 }
1638
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001639 preempt_enable();
1640
Joerg Roedel384de722009-05-15 12:30:05 +02001641 return address;
1642}
1643
Joerg Roedeld3086442008-06-26 21:27:57 +02001644static unsigned long dma_ops_alloc_addresses(struct device *dev,
1645 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001646 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001647 unsigned long align_mask,
1648 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001649{
Joerg Roedel266a3bd2015-12-21 18:54:24 +01001650 unsigned long address = -1;
Joerg Roedeld3086442008-06-26 21:27:57 +02001651
Joerg Roedel266a3bd2015-12-21 18:54:24 +01001652 while (address == -1) {
1653 address = dma_ops_area_alloc(dev, dom, pages,
1654 align_mask, dma_mask);
1655
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001656 if (address == -1 && alloc_new_range(dom, false, GFP_ATOMIC))
Joerg Roedel266a3bd2015-12-21 18:54:24 +01001657 break;
1658 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001659
Joerg Roedel384de722009-05-15 12:30:05 +02001660 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001661 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001662
1663 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1664
1665 return address;
1666}
1667
Joerg Roedel431b2a22008-07-11 17:14:22 +02001668/*
1669 * The address free function.
1670 *
1671 * called with domain->lock held
1672 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001673static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1674 unsigned long address,
1675 unsigned int pages)
1676{
Joerg Roedel384de722009-05-15 12:30:05 +02001677 unsigned i = address >> APERTURE_RANGE_SHIFT;
1678 struct aperture_range *range = dom->aperture[i];
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001679 unsigned long flags;
Joerg Roedel80be3082008-11-06 14:59:05 +01001680
Joerg Roedel384de722009-05-15 12:30:05 +02001681 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1682
Joerg Roedel47bccd62009-05-22 12:40:54 +02001683#ifdef CONFIG_IOMMU_STRESS
1684 if (i < 4)
1685 return;
1686#endif
1687
Joerg Roedel4eeca8c2015-12-22 12:15:35 +01001688 if (amd_iommu_unmap_flush) {
Joerg Roedeld41ab092015-12-21 18:20:03 +01001689 domain_flush_tlb(&dom->domain);
1690 domain_flush_complete(&dom->domain);
1691 }
Joerg Roedel384de722009-05-15 12:30:05 +02001692
1693 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001694
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001695 spin_lock_irqsave(&range->bitmap_lock, flags);
Joerg Roedel4eeca8c2015-12-22 12:15:35 +01001696 if (address + pages > range->next_bit)
1697 range->next_bit = address + pages;
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001698 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001699 spin_unlock_irqrestore(&range->bitmap_lock, flags);
Joerg Roedel384de722009-05-15 12:30:05 +02001700
Joerg Roedeld3086442008-06-26 21:27:57 +02001701}
1702
Joerg Roedel431b2a22008-07-11 17:14:22 +02001703/****************************************************************************
1704 *
1705 * The next functions belong to the domain allocation. A domain is
1706 * allocated for every IOMMU as the default domain. If device isolation
1707 * is enabled, every device get its own domain. The most important thing
1708 * about domains is the page table mapping the DMA address space they
1709 * contain.
1710 *
1711 ****************************************************************************/
1712
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001713/*
1714 * This function adds a protection domain to the global protection domain list
1715 */
1716static void add_domain_to_list(struct protection_domain *domain)
1717{
1718 unsigned long flags;
1719
1720 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1721 list_add(&domain->list, &amd_iommu_pd_list);
1722 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1723}
1724
1725/*
1726 * This function removes a protection domain to the global
1727 * protection domain list
1728 */
1729static void del_domain_from_list(struct protection_domain *domain)
1730{
1731 unsigned long flags;
1732
1733 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1734 list_del(&domain->list);
1735 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1736}
1737
Joerg Roedelec487d12008-06-26 21:27:58 +02001738static u16 domain_id_alloc(void)
1739{
1740 unsigned long flags;
1741 int id;
1742
1743 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1744 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1745 BUG_ON(id == 0);
1746 if (id > 0 && id < MAX_DOMAIN_ID)
1747 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1748 else
1749 id = 0;
1750 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1751
1752 return id;
1753}
1754
Joerg Roedela2acfb72008-12-02 18:28:53 +01001755static void domain_id_free(int id)
1756{
1757 unsigned long flags;
1758
1759 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1760 if (id > 0 && id < MAX_DOMAIN_ID)
1761 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1762 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1763}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001764
Joerg Roedel5c34c402013-06-20 20:22:58 +02001765#define DEFINE_FREE_PT_FN(LVL, FN) \
1766static void free_pt_##LVL (unsigned long __pt) \
1767{ \
1768 unsigned long p; \
1769 u64 *pt; \
1770 int i; \
1771 \
1772 pt = (u64 *)__pt; \
1773 \
1774 for (i = 0; i < 512; ++i) { \
Joerg Roedel0b3fff542015-06-18 10:48:34 +02001775 /* PTE present? */ \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001776 if (!IOMMU_PTE_PRESENT(pt[i])) \
1777 continue; \
1778 \
Joerg Roedel0b3fff542015-06-18 10:48:34 +02001779 /* Large PTE? */ \
1780 if (PM_PTE_LEVEL(pt[i]) == 0 || \
1781 PM_PTE_LEVEL(pt[i]) == 7) \
1782 continue; \
1783 \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001784 p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
1785 FN(p); \
1786 } \
1787 free_page((unsigned long)pt); \
1788}
1789
1790DEFINE_FREE_PT_FN(l2, free_page)
1791DEFINE_FREE_PT_FN(l3, free_pt_l2)
1792DEFINE_FREE_PT_FN(l4, free_pt_l3)
1793DEFINE_FREE_PT_FN(l5, free_pt_l4)
1794DEFINE_FREE_PT_FN(l6, free_pt_l5)
1795
Joerg Roedel86db2e52008-12-02 18:20:21 +01001796static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001797{
Joerg Roedel5c34c402013-06-20 20:22:58 +02001798 unsigned long root = (unsigned long)domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001799
Joerg Roedel5c34c402013-06-20 20:22:58 +02001800 switch (domain->mode) {
1801 case PAGE_MODE_NONE:
1802 break;
1803 case PAGE_MODE_1_LEVEL:
1804 free_page(root);
1805 break;
1806 case PAGE_MODE_2_LEVEL:
1807 free_pt_l2(root);
1808 break;
1809 case PAGE_MODE_3_LEVEL:
1810 free_pt_l3(root);
1811 break;
1812 case PAGE_MODE_4_LEVEL:
1813 free_pt_l4(root);
1814 break;
1815 case PAGE_MODE_5_LEVEL:
1816 free_pt_l5(root);
1817 break;
1818 case PAGE_MODE_6_LEVEL:
1819 free_pt_l6(root);
1820 break;
1821 default:
1822 BUG();
Joerg Roedelec487d12008-06-26 21:27:58 +02001823 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001824}
1825
Joerg Roedelb16137b2011-11-21 16:50:23 +01001826static void free_gcr3_tbl_level1(u64 *tbl)
1827{
1828 u64 *ptr;
1829 int i;
1830
1831 for (i = 0; i < 512; ++i) {
1832 if (!(tbl[i] & GCR3_VALID))
1833 continue;
1834
1835 ptr = __va(tbl[i] & PAGE_MASK);
1836
1837 free_page((unsigned long)ptr);
1838 }
1839}
1840
1841static void free_gcr3_tbl_level2(u64 *tbl)
1842{
1843 u64 *ptr;
1844 int i;
1845
1846 for (i = 0; i < 512; ++i) {
1847 if (!(tbl[i] & GCR3_VALID))
1848 continue;
1849
1850 ptr = __va(tbl[i] & PAGE_MASK);
1851
1852 free_gcr3_tbl_level1(ptr);
1853 }
1854}
1855
Joerg Roedel52815b72011-11-17 17:24:28 +01001856static void free_gcr3_table(struct protection_domain *domain)
1857{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001858 if (domain->glx == 2)
1859 free_gcr3_tbl_level2(domain->gcr3_tbl);
1860 else if (domain->glx == 1)
1861 free_gcr3_tbl_level1(domain->gcr3_tbl);
Joerg Roedel23d3a982015-08-13 11:15:13 +02001862 else
1863 BUG_ON(domain->glx != 0);
Joerg Roedelb16137b2011-11-21 16:50:23 +01001864
Joerg Roedel52815b72011-11-17 17:24:28 +01001865 free_page((unsigned long)domain->gcr3_tbl);
1866}
1867
Joerg Roedel431b2a22008-07-11 17:14:22 +02001868/*
1869 * Free a domain, only used if something went wrong in the
1870 * allocation path and we need to free an already allocated page table
1871 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001872static void dma_ops_domain_free(struct dma_ops_domain *dom)
1873{
Joerg Roedel384de722009-05-15 12:30:05 +02001874 int i;
1875
Joerg Roedelec487d12008-06-26 21:27:58 +02001876 if (!dom)
1877 return;
1878
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001879 free_percpu(dom->next_index);
1880
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001881 del_domain_from_list(&dom->domain);
1882
Joerg Roedel86db2e52008-12-02 18:20:21 +01001883 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001884
Joerg Roedel384de722009-05-15 12:30:05 +02001885 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1886 if (!dom->aperture[i])
1887 continue;
1888 free_page((unsigned long)dom->aperture[i]->bitmap);
1889 kfree(dom->aperture[i]);
1890 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001891
1892 kfree(dom);
1893}
1894
Joerg Roedel431b2a22008-07-11 17:14:22 +02001895/*
1896 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001897 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001898 * structures required for the dma_ops interface
1899 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001900static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001901{
1902 struct dma_ops_domain *dma_dom;
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001903 int cpu;
Joerg Roedelec487d12008-06-26 21:27:58 +02001904
1905 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1906 if (!dma_dom)
1907 return NULL;
1908
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001909 if (protection_domain_init(&dma_dom->domain))
Joerg Roedelec487d12008-06-26 21:27:58 +02001910 goto free_dma_dom;
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001911
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001912 dma_dom->next_index = alloc_percpu(u32);
1913 if (!dma_dom->next_index)
1914 goto free_dma_dom;
1915
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001916 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001917 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001918 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001919 dma_dom->domain.priv = dma_dom;
1920 if (!dma_dom->domain.pt_root)
1921 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001922
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001923 add_domain_to_list(&dma_dom->domain);
1924
Joerg Roedel576175c2009-11-23 19:08:46 +01001925 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001926 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001927
Joerg Roedel431b2a22008-07-11 17:14:22 +02001928 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001929 * mark the first page as allocated so we never return 0 as
1930 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001931 */
Joerg Roedel384de722009-05-15 12:30:05 +02001932 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedelec487d12008-06-26 21:27:58 +02001933
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001934 for_each_possible_cpu(cpu)
1935 *per_cpu_ptr(dma_dom->next_index, cpu) = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001936
1937 return dma_dom;
1938
1939free_dma_dom:
1940 dma_ops_domain_free(dma_dom);
1941
1942 return NULL;
1943}
1944
Joerg Roedel431b2a22008-07-11 17:14:22 +02001945/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001946 * little helper function to check whether a given protection domain is a
1947 * dma_ops domain
1948 */
1949static bool dma_ops_domain(struct protection_domain *domain)
1950{
1951 return domain->flags & PD_DMA_OPS_MASK;
1952}
1953
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001954static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001955{
Joerg Roedel132bd682011-11-17 14:18:46 +01001956 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001957 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001958
Joerg Roedel132bd682011-11-17 14:18:46 +01001959 if (domain->mode != PAGE_MODE_NONE)
1960 pte_root = virt_to_phys(domain->pt_root);
1961
Joerg Roedel38ddf412008-09-11 10:38:32 +02001962 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1963 << DEV_ENTRY_MODE_SHIFT;
1964 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001965
Joerg Roedelee6c2862011-11-09 12:06:03 +01001966 flags = amd_iommu_dev_table[devid].data[1];
1967
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001968 if (ats)
1969 flags |= DTE_FLAG_IOTLB;
1970
Joerg Roedel52815b72011-11-17 17:24:28 +01001971 if (domain->flags & PD_IOMMUV2_MASK) {
1972 u64 gcr3 = __pa(domain->gcr3_tbl);
1973 u64 glx = domain->glx;
1974 u64 tmp;
1975
1976 pte_root |= DTE_FLAG_GV;
1977 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1978
1979 /* First mask out possible old values for GCR3 table */
1980 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1981 flags &= ~tmp;
1982
1983 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1984 flags &= ~tmp;
1985
1986 /* Encode GCR3 table into DTE */
1987 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1988 pte_root |= tmp;
1989
1990 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1991 flags |= tmp;
1992
1993 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1994 flags |= tmp;
1995 }
1996
Joerg Roedelee6c2862011-11-09 12:06:03 +01001997 flags &= ~(0xffffUL);
1998 flags |= domain->id;
1999
2000 amd_iommu_dev_table[devid].data[1] = flags;
2001 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002002}
2003
Joerg Roedel15898bb2009-11-24 15:39:42 +01002004static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01002005{
Joerg Roedel355bf552008-12-08 12:02:41 +01002006 /* remove entry from the device table seen by the hardware */
Joerg Roedelcbf3ccd2015-10-20 14:59:36 +02002007 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
2008 amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
Joerg Roedel355bf552008-12-08 12:02:41 +01002009
Joerg Roedelc5cca142009-10-09 18:31:20 +02002010 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002011}
2012
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002013static void do_attach(struct iommu_dev_data *dev_data,
2014 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002015{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002016 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02002017 u16 alias;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002018 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002019
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002020 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele25bfb52015-10-20 17:33:38 +02002021 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002022 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002023
2024 /* Update data structures */
2025 dev_data->domain = domain;
2026 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002027
2028 /* Do reference counting */
2029 domain->dev_iommu[iommu->index] += 1;
2030 domain->dev_cnt += 1;
2031
Joerg Roedele25bfb52015-10-20 17:33:38 +02002032 /* Update device table */
2033 set_dte_entry(dev_data->devid, domain, ats);
2034 if (alias != dev_data->devid)
2035 set_dte_entry(dev_data->devid, domain, ats);
2036
Joerg Roedel6c542042011-06-09 17:07:31 +02002037 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002038}
2039
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002040static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002041{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002042 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02002043 u16 alias;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002044
Joerg Roedel5adad992015-10-09 16:23:33 +02002045 /*
2046 * First check if the device is still attached. It might already
2047 * be detached from its domain because the generic
2048 * iommu_detach_group code detached it and we try again here in
2049 * our alias handling.
2050 */
2051 if (!dev_data->domain)
2052 return;
2053
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002054 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele25bfb52015-10-20 17:33:38 +02002055 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02002056
Joerg Roedelc4596112009-11-20 14:57:32 +01002057 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002058 dev_data->domain->dev_iommu[iommu->index] -= 1;
2059 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01002060
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002061 /* Update data structures */
2062 dev_data->domain = NULL;
2063 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002064 clear_dte_entry(dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02002065 if (alias != dev_data->devid)
2066 clear_dte_entry(alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002067
2068 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002069 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002070}
2071
2072/*
2073 * If a device is not yet associated with a domain, this function does
2074 * assigns it visible for the hardware
2075 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002076static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002077 struct protection_domain *domain)
2078{
Julia Lawall84fe6c12010-05-27 12:31:51 +02002079 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002080
Joerg Roedel272e4f92015-10-20 17:33:37 +02002081 /*
2082 * Must be called with IRQs disabled. Warn here to detect early
2083 * when its not.
2084 */
2085 WARN_ON(!irqs_disabled());
2086
Joerg Roedel15898bb2009-11-24 15:39:42 +01002087 /* lock domain */
2088 spin_lock(&domain->lock);
2089
Joerg Roedel397111a2014-08-05 17:31:51 +02002090 ret = -EBUSY;
Joerg Roedel150952f2015-10-20 17:33:35 +02002091 if (dev_data->domain != NULL)
Joerg Roedel397111a2014-08-05 17:31:51 +02002092 goto out_unlock;
Joerg Roedel24100052009-11-25 15:59:57 +01002093
Joerg Roedel397111a2014-08-05 17:31:51 +02002094 /* Attach alias group root */
Joerg Roedel150952f2015-10-20 17:33:35 +02002095 do_attach(dev_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002096
Julia Lawall84fe6c12010-05-27 12:31:51 +02002097 ret = 0;
2098
2099out_unlock:
2100
Joerg Roedel355bf552008-12-08 12:02:41 +01002101 /* ready */
2102 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002103
Julia Lawall84fe6c12010-05-27 12:31:51 +02002104 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002105}
2106
Joerg Roedel52815b72011-11-17 17:24:28 +01002107
2108static void pdev_iommuv2_disable(struct pci_dev *pdev)
2109{
2110 pci_disable_ats(pdev);
2111 pci_disable_pri(pdev);
2112 pci_disable_pasid(pdev);
2113}
2114
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002115/* FIXME: Change generic reset-function to do the same */
2116static int pri_reset_while_enabled(struct pci_dev *pdev)
2117{
2118 u16 control;
2119 int pos;
2120
Joerg Roedel46277b72011-12-07 14:34:02 +01002121 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002122 if (!pos)
2123 return -EINVAL;
2124
Joerg Roedel46277b72011-12-07 14:34:02 +01002125 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2126 control |= PCI_PRI_CTRL_RESET;
2127 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002128
2129 return 0;
2130}
2131
Joerg Roedel52815b72011-11-17 17:24:28 +01002132static int pdev_iommuv2_enable(struct pci_dev *pdev)
2133{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002134 bool reset_enable;
2135 int reqs, ret;
2136
2137 /* FIXME: Hardcode number of outstanding requests for now */
2138 reqs = 32;
2139 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2140 reqs = 1;
2141 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002142
2143 /* Only allow access to user-accessible pages */
2144 ret = pci_enable_pasid(pdev, 0);
2145 if (ret)
2146 goto out_err;
2147
2148 /* First reset the PRI state of the device */
2149 ret = pci_reset_pri(pdev);
2150 if (ret)
2151 goto out_err;
2152
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002153 /* Enable PRI */
2154 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002155 if (ret)
2156 goto out_err;
2157
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002158 if (reset_enable) {
2159 ret = pri_reset_while_enabled(pdev);
2160 if (ret)
2161 goto out_err;
2162 }
2163
Joerg Roedel52815b72011-11-17 17:24:28 +01002164 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2165 if (ret)
2166 goto out_err;
2167
2168 return 0;
2169
2170out_err:
2171 pci_disable_pri(pdev);
2172 pci_disable_pasid(pdev);
2173
2174 return ret;
2175}
2176
Joerg Roedelc99afa22011-11-21 18:19:25 +01002177/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002178#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002179
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002180static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002181{
Joerg Roedela3b93122012-04-12 12:49:26 +02002182 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002183 int pos;
2184
Joerg Roedel46277b72011-12-07 14:34:02 +01002185 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002186 if (!pos)
2187 return false;
2188
Joerg Roedela3b93122012-04-12 12:49:26 +02002189 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002190
Joerg Roedela3b93122012-04-12 12:49:26 +02002191 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002192}
2193
Joerg Roedel15898bb2009-11-24 15:39:42 +01002194/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02002195 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01002196 * assigns it visible for the hardware
2197 */
2198static int attach_device(struct device *dev,
2199 struct protection_domain *domain)
2200{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002201 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002202 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002203 unsigned long flags;
2204 int ret;
2205
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002206 dev_data = get_dev_data(dev);
2207
Joerg Roedel52815b72011-11-17 17:24:28 +01002208 if (domain->flags & PD_IOMMUV2_MASK) {
Joerg Roedel02ca2022015-07-28 16:58:49 +02002209 if (!dev_data->passthrough)
Joerg Roedel52815b72011-11-17 17:24:28 +01002210 return -EINVAL;
2211
Joerg Roedel02ca2022015-07-28 16:58:49 +02002212 if (dev_data->iommu_v2) {
2213 if (pdev_iommuv2_enable(pdev) != 0)
2214 return -EINVAL;
Joerg Roedel52815b72011-11-17 17:24:28 +01002215
Joerg Roedel02ca2022015-07-28 16:58:49 +02002216 dev_data->ats.enabled = true;
2217 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2218 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
2219 }
Joerg Roedel52815b72011-11-17 17:24:28 +01002220 } else if (amd_iommu_iotlb_sup &&
2221 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002222 dev_data->ats.enabled = true;
2223 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2224 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002225
Joerg Roedel15898bb2009-11-24 15:39:42 +01002226 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002227 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002228 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2229
2230 /*
2231 * We might boot into a crash-kernel here. The crashed kernel
2232 * left the caches in the IOMMU dirty. So we have to flush
2233 * here to evict all dirty stuff.
2234 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002235 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002236
2237 return ret;
2238}
2239
2240/*
2241 * Removes a device from a protection domain (unlocked)
2242 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002243static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002244{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002245 struct protection_domain *domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002246
Joerg Roedel272e4f92015-10-20 17:33:37 +02002247 /*
2248 * Must be called with IRQs disabled. Warn here to detect early
2249 * when its not.
2250 */
2251 WARN_ON(!irqs_disabled());
2252
Joerg Roedelf34c73f2015-10-20 17:33:34 +02002253 if (WARN_ON(!dev_data->domain))
2254 return;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002255
Joerg Roedel2ca76272010-01-22 16:45:31 +01002256 domain = dev_data->domain;
2257
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002258 spin_lock(&domain->lock);
Joerg Roedel24100052009-11-25 15:59:57 +01002259
Joerg Roedel150952f2015-10-20 17:33:35 +02002260 do_detach(dev_data);
Joerg Roedel71f77582011-06-09 19:03:15 +02002261
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002262 spin_unlock(&domain->lock);
Joerg Roedel355bf552008-12-08 12:02:41 +01002263}
2264
2265/*
2266 * Removes a device from a protection domain (with devtable_lock held)
2267 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002268static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002269{
Joerg Roedel52815b72011-11-17 17:24:28 +01002270 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002271 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002272 unsigned long flags;
2273
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002274 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002275 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002276
Joerg Roedel355bf552008-12-08 12:02:41 +01002277 /* lock device table */
2278 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002279 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002280 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002281
Joerg Roedel02ca2022015-07-28 16:58:49 +02002282 if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
Joerg Roedel52815b72011-11-17 17:24:28 +01002283 pdev_iommuv2_disable(to_pci_dev(dev));
2284 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002285 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002286
2287 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002288}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002289
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002290static int amd_iommu_add_device(struct device *dev)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002291{
Joerg Roedel71f77582011-06-09 19:03:15 +02002292 struct iommu_dev_data *dev_data;
Joerg Roedel07ee8692015-05-28 18:41:42 +02002293 struct iommu_domain *domain;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002294 struct amd_iommu *iommu;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002295 u16 devid;
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002296 int ret;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002297
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002298 if (!check_device(dev) || get_dev_data(dev))
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002299 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002300
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002301 devid = get_device_id(dev);
2302 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01002303
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002304 ret = iommu_init_device(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002305 if (ret) {
2306 if (ret != -ENOTSUPP)
2307 pr_err("Failed to initialize device %s - trying to proceed anyway\n",
2308 dev_name(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +01002309
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002310 iommu_ignore_device(dev);
Joerg Roedel343e9ca2015-05-28 18:41:43 +02002311 dev->archdata.dma_ops = &nommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002312 goto out;
2313 }
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002314 init_iommu_group(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002315
Joerg Roedel07ee8692015-05-28 18:41:42 +02002316 dev_data = get_dev_data(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002317
2318 BUG_ON(!dev_data);
2319
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002320 if (iommu_pass_through || dev_data->iommu_v2)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002321 iommu_request_dm_for_dev(dev);
2322
2323 /* Domains are initialized for this device - have a look what we ended up with */
2324 domain = iommu_get_domain_for_dev(dev);
Joerg Roedel32302322015-07-28 16:58:50 +02002325 if (domain->type == IOMMU_DOMAIN_IDENTITY)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002326 dev_data->passthrough = true;
Joerg Roedel32302322015-07-28 16:58:50 +02002327 else
Joerg Roedel07ee8692015-05-28 18:41:42 +02002328 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002329
2330out:
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002331 iommu_completion_wait(iommu);
2332
Joerg Roedele275a2a2008-12-10 18:27:25 +01002333 return 0;
2334}
2335
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002336static void amd_iommu_remove_device(struct device *dev)
Joerg Roedel8638c492009-12-10 11:12:25 +01002337{
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002338 struct amd_iommu *iommu;
2339 u16 devid;
2340
2341 if (!check_device(dev))
2342 return;
2343
2344 devid = get_device_id(dev);
2345 iommu = amd_iommu_rlookup_table[devid];
2346
2347 iommu_uninit_device(dev);
2348 iommu_completion_wait(iommu);
Joerg Roedel8638c492009-12-10 11:12:25 +01002349}
2350
Joerg Roedel431b2a22008-07-11 17:14:22 +02002351/*****************************************************************************
2352 *
2353 * The next functions belong to the dma_ops mapping/unmapping code.
2354 *
2355 *****************************************************************************/
2356
2357/*
2358 * In the dma_ops path we only have the struct device. This function
2359 * finds the corresponding IOMMU, the protection domain and the
2360 * requestor id for a given device.
2361 * If the device is not yet associated with a domain this is also done
2362 * in this function.
2363 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002364static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002365{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002366 struct protection_domain *domain;
Joerg Roedel063071d2015-05-28 18:41:38 +02002367 struct iommu_domain *io_domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002368
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002369 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002370 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002371
Joerg Roedel063071d2015-05-28 18:41:38 +02002372 io_domain = iommu_get_domain_for_dev(dev);
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002373 if (!io_domain)
2374 return NULL;
Joerg Roedel063071d2015-05-28 18:41:38 +02002375
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002376 domain = to_pdomain(io_domain);
2377 if (!dma_ops_domain(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002378 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002379
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002380 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002381}
2382
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002383static void update_device_table(struct protection_domain *domain)
2384{
Joerg Roedel492667d2009-11-27 13:25:47 +01002385 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002386
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002387 list_for_each_entry(dev_data, &domain->dev_list, list)
2388 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002389}
2390
2391static void update_domain(struct protection_domain *domain)
2392{
2393 if (!domain->updated)
2394 return;
2395
2396 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002397
2398 domain_flush_devices(domain);
2399 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002400
2401 domain->updated = false;
2402}
2403
Joerg Roedel431b2a22008-07-11 17:14:22 +02002404/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002405 * This function fetches the PTE for a given address in the aperture
2406 */
2407static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2408 unsigned long address)
2409{
Joerg Roedel384de722009-05-15 12:30:05 +02002410 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002411 u64 *pte, *pte_page;
2412
Joerg Roedel384de722009-05-15 12:30:05 +02002413 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2414 if (!aperture)
2415 return NULL;
2416
2417 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002418 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002419 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002420 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002421 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2422 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002423 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002424
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002425 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002426
2427 return pte;
2428}
2429
2430/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002431 * This is the generic map function. It maps one 4kb page at paddr to
2432 * the given address in the DMA address space for the domain.
2433 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002434static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002435 unsigned long address,
2436 phys_addr_t paddr,
2437 int direction)
2438{
2439 u64 *pte, __pte;
2440
2441 WARN_ON(address > dom->aperture_size);
2442
2443 paddr &= PAGE_MASK;
2444
Joerg Roedel8bda3092009-05-12 12:02:46 +02002445 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002446 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002447 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002448
2449 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2450
2451 if (direction == DMA_TO_DEVICE)
2452 __pte |= IOMMU_PTE_IR;
2453 else if (direction == DMA_FROM_DEVICE)
2454 __pte |= IOMMU_PTE_IW;
2455 else if (direction == DMA_BIDIRECTIONAL)
2456 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2457
Joerg Roedela7fb6682015-12-21 12:50:54 +01002458 WARN_ON_ONCE(*pte);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002459
2460 *pte = __pte;
2461
2462 return (dma_addr_t)address;
2463}
2464
Joerg Roedel431b2a22008-07-11 17:14:22 +02002465/*
2466 * The generic unmapping function for on page in the DMA address space.
2467 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002468static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002469 unsigned long address)
2470{
Joerg Roedel384de722009-05-15 12:30:05 +02002471 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002472 u64 *pte;
2473
2474 if (address >= dom->aperture_size)
2475 return;
2476
Joerg Roedel384de722009-05-15 12:30:05 +02002477 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2478 if (!aperture)
2479 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002480
Joerg Roedel384de722009-05-15 12:30:05 +02002481 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2482 if (!pte)
2483 return;
2484
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002485 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002486
Joerg Roedela7fb6682015-12-21 12:50:54 +01002487 WARN_ON_ONCE(!*pte);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002488
2489 *pte = 0ULL;
2490}
2491
Joerg Roedel431b2a22008-07-11 17:14:22 +02002492/*
2493 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002494 * contiguous memory region into DMA address space. It is used by all
2495 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002496 * Must be called with the domain lock held.
2497 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002498static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002499 struct dma_ops_domain *dma_dom,
2500 phys_addr_t paddr,
2501 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002502 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002503 bool align,
2504 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002505{
2506 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002507 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002508 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002509 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002510 int i;
2511
Joerg Roedele3c449f2008-10-15 22:02:11 -07002512 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002513 paddr &= PAGE_MASK;
2514
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002515 INC_STATS_COUNTER(total_map_requests);
2516
Joerg Roedelc1858972008-12-12 15:42:39 +01002517 if (pages > 1)
2518 INC_STATS_COUNTER(cross_page);
2519
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002520 if (align)
2521 align_mask = (1UL << get_order(size)) - 1;
2522
Joerg Roedel832a90c2008-09-18 15:54:23 +02002523 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2524 dma_mask);
Joerg Roedelebaecb42015-12-21 18:11:32 +01002525
Joerg Roedel266a3bd2015-12-21 18:54:24 +01002526 if (address == DMA_ERROR_CODE)
2527 goto out;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002528
2529 start = address;
2530 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002531 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002532 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002533 goto out_unmap;
2534
Joerg Roedelcb76c322008-06-26 21:28:00 +02002535 paddr += PAGE_SIZE;
2536 start += PAGE_SIZE;
2537 }
2538 address += offset;
2539
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002540 ADD_STATS_COUNTER(alloced_io_mem, size);
2541
Joerg Roedelab7032b2015-12-21 18:47:11 +01002542 if (unlikely(amd_iommu_np_cache)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002543 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedelab7032b2015-12-21 18:47:11 +01002544 domain_flush_complete(&dma_dom->domain);
2545 }
Joerg Roedel270cab242008-09-04 15:49:46 +02002546
Joerg Roedelcb76c322008-06-26 21:28:00 +02002547out:
2548 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002549
2550out_unmap:
2551
2552 for (--i; i >= 0; --i) {
2553 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002554 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002555 }
2556
2557 dma_ops_free_addresses(dma_dom, address, pages);
2558
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002559 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002560}
2561
Joerg Roedel431b2a22008-07-11 17:14:22 +02002562/*
2563 * Does the reverse of the __map_single function. Must be called with
2564 * the domain lock held too
2565 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002566static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002567 dma_addr_t dma_addr,
2568 size_t size,
2569 int dir)
2570{
Joerg Roedel04e04632010-09-23 16:12:48 +02002571 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002572 dma_addr_t i, start;
2573 unsigned int pages;
2574
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002575 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002576 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002577 return;
2578
Joerg Roedel04e04632010-09-23 16:12:48 +02002579 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002580 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002581 dma_addr &= PAGE_MASK;
2582 start = dma_addr;
2583
2584 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002585 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002586 start += PAGE_SIZE;
2587 }
2588
Joerg Roedel84b3a0b2015-12-21 13:23:59 +01002589 SUB_STATS_COUNTER(alloced_io_mem, size);
2590
2591 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002592}
2593
Joerg Roedel431b2a22008-07-11 17:14:22 +02002594/*
2595 * The exported map_single function for dma_ops.
2596 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002597static dma_addr_t map_page(struct device *dev, struct page *page,
2598 unsigned long offset, size_t size,
2599 enum dma_data_direction dir,
2600 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002601{
FUJITA Tomonori51491362009-01-05 23:47:25 +09002602 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel92d420e2015-12-21 19:31:33 +01002603 struct protection_domain *domain;
2604 u64 dma_mask;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002605
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002606 INC_STATS_COUNTER(cnt_map_single);
2607
Joerg Roedel94f6d192009-11-24 16:40:02 +01002608 domain = get_domain(dev);
2609 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002610 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002611 else if (IS_ERR(domain))
2612 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002613
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002614 dma_mask = *dev->dma_mask;
2615
Joerg Roedel92d420e2015-12-21 19:31:33 +01002616 return __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002617 dma_mask);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002618}
2619
Joerg Roedel431b2a22008-07-11 17:14:22 +02002620/*
2621 * The exported unmap_single function for dma_ops.
2622 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002623static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2624 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002625{
Joerg Roedel4da70b92008-06-26 21:28:01 +02002626 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002627
Joerg Roedel146a6912008-12-12 15:07:12 +01002628 INC_STATS_COUNTER(cnt_unmap_single);
2629
Joerg Roedel94f6d192009-11-24 16:40:02 +01002630 domain = get_domain(dev);
2631 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002632 return;
2633
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002634 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002635}
2636
Joerg Roedel431b2a22008-07-11 17:14:22 +02002637/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002638 * The exported map_sg function for dma_ops (handles scatter-gather
2639 * lists).
2640 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002641static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002642 int nelems, enum dma_data_direction dir,
2643 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002644{
Joerg Roedel65b050a2008-06-26 21:28:02 +02002645 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002646 int i;
2647 struct scatterlist *s;
2648 phys_addr_t paddr;
2649 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002650 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002651
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002652 INC_STATS_COUNTER(cnt_map_sg);
2653
Joerg Roedel94f6d192009-11-24 16:40:02 +01002654 domain = get_domain(dev);
Joerg Roedela0e191b2013-04-09 15:04:36 +02002655 if (IS_ERR(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002656 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002657
Joerg Roedel832a90c2008-09-18 15:54:23 +02002658 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002659
Joerg Roedel65b050a2008-06-26 21:28:02 +02002660 for_each_sg(sglist, s, nelems, i) {
2661 paddr = sg_phys(s);
2662
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002663 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002664 paddr, s->length, dir, false,
2665 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002666
2667 if (s->dma_address) {
2668 s->dma_length = s->length;
2669 mapped_elems++;
2670 } else
2671 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002672 }
2673
Joerg Roedel65b050a2008-06-26 21:28:02 +02002674 return mapped_elems;
Joerg Roedel92d420e2015-12-21 19:31:33 +01002675
Joerg Roedel65b050a2008-06-26 21:28:02 +02002676unmap:
2677 for_each_sg(sglist, s, mapped_elems, i) {
2678 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002679 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002680 s->dma_length, dir);
2681 s->dma_address = s->dma_length = 0;
2682 }
2683
Joerg Roedel92d420e2015-12-21 19:31:33 +01002684 return 0;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002685}
2686
Joerg Roedel431b2a22008-07-11 17:14:22 +02002687/*
2688 * The exported map_sg function for dma_ops (handles scatter-gather
2689 * lists).
2690 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002691static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002692 int nelems, enum dma_data_direction dir,
2693 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002694{
Joerg Roedel65b050a2008-06-26 21:28:02 +02002695 struct protection_domain *domain;
2696 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002697 int i;
2698
Joerg Roedel55877a62008-12-12 15:12:14 +01002699 INC_STATS_COUNTER(cnt_unmap_sg);
2700
Joerg Roedel94f6d192009-11-24 16:40:02 +01002701 domain = get_domain(dev);
2702 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002703 return;
2704
Joerg Roedel65b050a2008-06-26 21:28:02 +02002705 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002706 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002707 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002708 s->dma_address = s->dma_length = 0;
2709 }
Joerg Roedel65b050a2008-06-26 21:28:02 +02002710}
2711
Joerg Roedel431b2a22008-07-11 17:14:22 +02002712/*
2713 * The exported alloc_coherent function for dma_ops.
2714 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002715static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002716 dma_addr_t *dma_addr, gfp_t flag,
2717 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002718{
Joerg Roedel832a90c2008-09-18 15:54:23 +02002719 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002720 struct protection_domain *domain;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002721 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 Roedel3b839a52015-04-01 14:58:47 +02002752 *dma_addr = __map_single(dev, domain->priv, page_to_phys(page),
Joerg Roedel832a90c2008-09-18 15:54:23 +02002753 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002754
Joerg Roedel92d420e2015-12-21 19:31:33 +01002755 if (*dma_addr == DMA_ERROR_CODE)
Joerg Roedel5b28df62008-12-02 17:49:42 +01002756 goto out_free;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002757
Joerg Roedel3b839a52015-04-01 14:58:47 +02002758 return page_address(page);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002759
2760out_free:
2761
Joerg Roedel3b839a52015-04-01 14:58:47 +02002762 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2763 __free_pages(page, get_order(size));
Joerg Roedel5b28df62008-12-02 17:49:42 +01002764
2765 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002766}
2767
Joerg Roedel431b2a22008-07-11 17:14:22 +02002768/*
2769 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002770 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002771static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002772 void *virt_addr, dma_addr_t dma_addr,
2773 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002774{
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002775 struct protection_domain *domain;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002776 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002777
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002778 INC_STATS_COUNTER(cnt_free_coherent);
2779
Joerg Roedel3b839a52015-04-01 14:58:47 +02002780 page = virt_to_page(virt_addr);
2781 size = PAGE_ALIGN(size);
2782
Joerg Roedel94f6d192009-11-24 16:40:02 +01002783 domain = get_domain(dev);
2784 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002785 goto free_mem;
2786
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002787 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002788
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002789free_mem:
Joerg Roedel3b839a52015-04-01 14:58:47 +02002790 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2791 __free_pages(page, get_order(size));
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002792}
2793
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002794/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002795 * This function is called by the DMA layer to find out if we can handle a
2796 * particular device. It is part of the dma_ops.
2797 */
2798static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2799{
Joerg Roedel420aef82009-11-23 16:14:57 +01002800 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002801}
2802
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002803static struct dma_map_ops amd_iommu_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002804 .alloc = alloc_coherent,
2805 .free = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002806 .map_page = map_page,
2807 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002808 .map_sg = map_sg,
2809 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002810 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002811};
2812
Joerg Roedel3a18404c2015-05-28 18:41:45 +02002813int __init amd_iommu_init_api(void)
Joerg Roedel27c21272011-05-30 15:56:24 +02002814{
Joerg Roedel3a18404c2015-05-28 18:41:45 +02002815 return bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01002816}
2817
Joerg Roedel6631ee92008-06-26 21:28:05 +02002818int __init amd_iommu_init_dma_ops(void)
2819{
Joerg Roedel32302322015-07-28 16:58:50 +02002820 swiotlb = iommu_pass_through ? 1 : 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002821 iommu_detected = 1;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002822
Joerg Roedel52717822015-07-28 16:58:51 +02002823 /*
2824 * In case we don't initialize SWIOTLB (actually the common case
2825 * when AMD IOMMU is enabled), make sure there are global
2826 * dma_ops set as a fall-back for devices not handled by this
2827 * driver (for example non-PCI devices).
2828 */
2829 if (!swiotlb)
2830 dma_ops = &nommu_dma_ops;
2831
Joerg Roedel7f265082008-12-12 13:50:21 +01002832 amd_iommu_stats_init();
2833
Joerg Roedel62410ee2012-06-12 16:42:43 +02002834 if (amd_iommu_unmap_flush)
2835 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
2836 else
2837 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
2838
Joerg Roedel6631ee92008-06-26 21:28:05 +02002839 return 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002840}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002841
2842/*****************************************************************************
2843 *
2844 * The following functions belong to the exported interface of AMD IOMMU
2845 *
2846 * This interface allows access to lower level functions of the IOMMU
2847 * like protection domain handling and assignement of devices to domains
2848 * which is not possible with the dma_ops interface.
2849 *
2850 *****************************************************************************/
2851
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002852static void cleanup_domain(struct protection_domain *domain)
2853{
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002854 struct iommu_dev_data *entry;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002855 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002856
2857 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2858
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002859 while (!list_empty(&domain->dev_list)) {
2860 entry = list_first_entry(&domain->dev_list,
2861 struct iommu_dev_data, list);
2862 __detach_device(entry);
Joerg Roedel492667d2009-11-27 13:25:47 +01002863 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002864
2865 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2866}
2867
Joerg Roedel26508152009-08-26 16:52:40 +02002868static void protection_domain_free(struct protection_domain *domain)
2869{
2870 if (!domain)
2871 return;
2872
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002873 del_domain_from_list(domain);
2874
Joerg Roedel26508152009-08-26 16:52:40 +02002875 if (domain->id)
2876 domain_id_free(domain->id);
2877
2878 kfree(domain);
2879}
2880
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002881static int protection_domain_init(struct protection_domain *domain)
2882{
2883 spin_lock_init(&domain->lock);
2884 mutex_init(&domain->api_lock);
2885 domain->id = domain_id_alloc();
2886 if (!domain->id)
2887 return -ENOMEM;
2888 INIT_LIST_HEAD(&domain->dev_list);
2889
2890 return 0;
2891}
2892
Joerg Roedel26508152009-08-26 16:52:40 +02002893static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002894{
2895 struct protection_domain *domain;
2896
2897 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2898 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002899 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002900
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002901 if (protection_domain_init(domain))
Joerg Roedel26508152009-08-26 16:52:40 +02002902 goto out_err;
2903
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002904 add_domain_to_list(domain);
2905
Joerg Roedel26508152009-08-26 16:52:40 +02002906 return domain;
2907
2908out_err:
2909 kfree(domain);
2910
2911 return NULL;
2912}
2913
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002914static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
2915{
2916 struct protection_domain *pdomain;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002917 struct dma_ops_domain *dma_domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002918
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002919 switch (type) {
2920 case IOMMU_DOMAIN_UNMANAGED:
2921 pdomain = protection_domain_alloc();
2922 if (!pdomain)
2923 return NULL;
2924
2925 pdomain->mode = PAGE_MODE_3_LEVEL;
2926 pdomain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2927 if (!pdomain->pt_root) {
2928 protection_domain_free(pdomain);
2929 return NULL;
2930 }
2931
2932 pdomain->domain.geometry.aperture_start = 0;
2933 pdomain->domain.geometry.aperture_end = ~0ULL;
2934 pdomain->domain.geometry.force_aperture = true;
2935
2936 break;
2937 case IOMMU_DOMAIN_DMA:
2938 dma_domain = dma_ops_domain_alloc();
2939 if (!dma_domain) {
2940 pr_err("AMD-Vi: Failed to allocate\n");
2941 return NULL;
2942 }
2943 pdomain = &dma_domain->domain;
2944 break;
Joerg Roedel07f643a2015-05-28 18:41:41 +02002945 case IOMMU_DOMAIN_IDENTITY:
2946 pdomain = protection_domain_alloc();
2947 if (!pdomain)
2948 return NULL;
2949
2950 pdomain->mode = PAGE_MODE_NONE;
2951 break;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002952 default:
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002953 return NULL;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002954 }
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002955
2956 return &pdomain->domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002957}
2958
2959static void amd_iommu_domain_free(struct iommu_domain *dom)
Joerg Roedel26508152009-08-26 16:52:40 +02002960{
2961 struct protection_domain *domain;
2962
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002963 if (!dom)
Joerg Roedel98383fc2008-12-02 18:34:12 +01002964 return;
2965
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002966 domain = to_pdomain(dom);
2967
Joerg Roedel98383fc2008-12-02 18:34:12 +01002968 if (domain->dev_cnt > 0)
2969 cleanup_domain(domain);
2970
2971 BUG_ON(domain->dev_cnt != 0);
2972
Joerg Roedel132bd682011-11-17 14:18:46 +01002973 if (domain->mode != PAGE_MODE_NONE)
2974 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002975
Joerg Roedel52815b72011-11-17 17:24:28 +01002976 if (domain->flags & PD_IOMMUV2_MASK)
2977 free_gcr3_table(domain);
2978
Joerg Roedel8b408fe2010-03-08 14:20:07 +01002979 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002980}
2981
Joerg Roedel684f2882008-12-08 12:07:44 +01002982static void amd_iommu_detach_device(struct iommu_domain *dom,
2983 struct device *dev)
2984{
Joerg Roedel657cbb62009-11-23 15:26:46 +01002985 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002986 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002987 u16 devid;
2988
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002989 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01002990 return;
2991
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002992 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002993
Joerg Roedel657cbb62009-11-23 15:26:46 +01002994 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002995 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002996
2997 iommu = amd_iommu_rlookup_table[devid];
2998 if (!iommu)
2999 return;
3000
Joerg Roedel684f2882008-12-08 12:07:44 +01003001 iommu_completion_wait(iommu);
3002}
3003
Joerg Roedel01106062008-12-02 19:34:11 +01003004static int amd_iommu_attach_device(struct iommu_domain *dom,
3005 struct device *dev)
3006{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003007 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel657cbb62009-11-23 15:26:46 +01003008 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003009 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003010 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003011
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003012 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003013 return -EINVAL;
3014
Joerg Roedel657cbb62009-11-23 15:26:46 +01003015 dev_data = dev->archdata.iommu;
3016
Joerg Roedelf62dda62011-06-09 12:55:35 +02003017 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003018 if (!iommu)
3019 return -EINVAL;
3020
Joerg Roedel657cbb62009-11-23 15:26:46 +01003021 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003022 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003023
Joerg Roedel15898bb2009-11-24 15:39:42 +01003024 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003025
3026 iommu_completion_wait(iommu);
3027
Joerg Roedel15898bb2009-11-24 15:39:42 +01003028 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003029}
3030
Joerg Roedel468e2362010-01-21 16:37:36 +01003031static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003032 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003033{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003034 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003035 int prot = 0;
3036 int ret;
3037
Joerg Roedel132bd682011-11-17 14:18:46 +01003038 if (domain->mode == PAGE_MODE_NONE)
3039 return -EINVAL;
3040
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003041 if (iommu_prot & IOMMU_READ)
3042 prot |= IOMMU_PROT_IR;
3043 if (iommu_prot & IOMMU_WRITE)
3044 prot |= IOMMU_PROT_IW;
3045
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003046 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003047 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003048 mutex_unlock(&domain->api_lock);
3049
Joerg Roedel795e74f72010-05-11 17:40:57 +02003050 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003051}
3052
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003053static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3054 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003055{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003056 struct protection_domain *domain = to_pdomain(dom);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003057 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003058
Joerg Roedel132bd682011-11-17 14:18:46 +01003059 if (domain->mode == PAGE_MODE_NONE)
3060 return -EINVAL;
3061
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003062 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003063 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003064 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003065
Joerg Roedel17b124b2011-04-06 18:01:35 +02003066 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003067
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003068 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003069}
3070
Joerg Roedel645c4c82008-12-02 20:05:50 +01003071static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
Varun Sethibb5547ac2013-03-29 01:23:58 +05303072 dma_addr_t iova)
Joerg Roedel645c4c82008-12-02 20:05:50 +01003073{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003074 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel3039ca12015-04-01 14:58:48 +02003075 unsigned long offset_mask, pte_pgsize;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003076 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003077
Joerg Roedel132bd682011-11-17 14:18:46 +01003078 if (domain->mode == PAGE_MODE_NONE)
3079 return iova;
3080
Joerg Roedel3039ca12015-04-01 14:58:48 +02003081 pte = fetch_pte(domain, iova, &pte_pgsize);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003082
Joerg Roedela6d41a42009-09-02 17:08:55 +02003083 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003084 return 0;
3085
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003086 offset_mask = pte_pgsize - 1;
3087 __pte = *pte & PM_ADDR_MASK;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003088
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003089 return (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003090}
3091
Joerg Roedelab636482014-09-05 10:48:21 +02003092static bool amd_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003093{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003094 switch (cap) {
3095 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedelab636482014-09-05 10:48:21 +02003096 return true;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003097 case IOMMU_CAP_INTR_REMAP:
Joerg Roedelab636482014-09-05 10:48:21 +02003098 return (irq_remapping_enabled == 1);
Will Deaconcfdeec22014-10-27 11:24:48 +00003099 case IOMMU_CAP_NOEXEC:
3100 return false;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003101 }
3102
Joerg Roedelab636482014-09-05 10:48:21 +02003103 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003104}
3105
Joerg Roedel35cf2482015-05-28 18:41:37 +02003106static void amd_iommu_get_dm_regions(struct device *dev,
3107 struct list_head *head)
3108{
3109 struct unity_map_entry *entry;
3110 u16 devid;
3111
3112 devid = get_device_id(dev);
3113
3114 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
3115 struct iommu_dm_region *region;
3116
3117 if (devid < entry->devid_start || devid > entry->devid_end)
3118 continue;
3119
3120 region = kzalloc(sizeof(*region), GFP_KERNEL);
3121 if (!region) {
3122 pr_err("Out of memory allocating dm-regions for %s\n",
3123 dev_name(dev));
3124 return;
3125 }
3126
3127 region->start = entry->address_start;
3128 region->length = entry->address_end - entry->address_start;
3129 if (entry->prot & IOMMU_PROT_IR)
3130 region->prot |= IOMMU_READ;
3131 if (entry->prot & IOMMU_PROT_IW)
3132 region->prot |= IOMMU_WRITE;
3133
3134 list_add_tail(&region->list, head);
3135 }
3136}
3137
3138static void amd_iommu_put_dm_regions(struct device *dev,
3139 struct list_head *head)
3140{
3141 struct iommu_dm_region *entry, *next;
3142
3143 list_for_each_entry_safe(entry, next, head, list)
3144 kfree(entry);
3145}
3146
Thierry Redingb22f6432014-06-27 09:03:12 +02003147static const struct iommu_ops amd_iommu_ops = {
Joerg Roedelab636482014-09-05 10:48:21 +02003148 .capable = amd_iommu_capable,
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003149 .domain_alloc = amd_iommu_domain_alloc,
3150 .domain_free = amd_iommu_domain_free,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003151 .attach_dev = amd_iommu_attach_device,
3152 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003153 .map = amd_iommu_map,
3154 .unmap = amd_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07003155 .map_sg = default_iommu_map_sg,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003156 .iova_to_phys = amd_iommu_iova_to_phys,
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02003157 .add_device = amd_iommu_add_device,
3158 .remove_device = amd_iommu_remove_device,
Joerg Roedela960fad2015-10-21 23:51:39 +02003159 .device_group = pci_device_group,
Joerg Roedel35cf2482015-05-28 18:41:37 +02003160 .get_dm_regions = amd_iommu_get_dm_regions,
3161 .put_dm_regions = amd_iommu_put_dm_regions,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003162 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003163};
3164
Joerg Roedel0feae532009-08-26 15:26:30 +02003165/*****************************************************************************
3166 *
3167 * The next functions do a basic initialization of IOMMU for pass through
3168 * mode
3169 *
3170 * In passthrough mode the IOMMU is initialized and enabled but not used for
3171 * DMA-API translation.
3172 *
3173 *****************************************************************************/
3174
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003175/* IOMMUv2 specific functions */
3176int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3177{
3178 return atomic_notifier_chain_register(&ppr_notifier, nb);
3179}
3180EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3181
3182int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3183{
3184 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3185}
3186EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003187
3188void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3189{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003190 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel132bd682011-11-17 14:18:46 +01003191 unsigned long flags;
3192
3193 spin_lock_irqsave(&domain->lock, flags);
3194
3195 /* Update data structure */
3196 domain->mode = PAGE_MODE_NONE;
3197 domain->updated = true;
3198
3199 /* Make changes visible to IOMMUs */
3200 update_domain(domain);
3201
3202 /* Page-table is not visible to IOMMU anymore, so free it */
3203 free_pagetable(domain);
3204
3205 spin_unlock_irqrestore(&domain->lock, flags);
3206}
3207EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003208
3209int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3210{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003211 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel52815b72011-11-17 17:24:28 +01003212 unsigned long flags;
3213 int levels, ret;
3214
3215 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3216 return -EINVAL;
3217
3218 /* Number of GCR3 table levels required */
3219 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3220 levels += 1;
3221
3222 if (levels > amd_iommu_max_glx_val)
3223 return -EINVAL;
3224
3225 spin_lock_irqsave(&domain->lock, flags);
3226
3227 /*
3228 * Save us all sanity checks whether devices already in the
3229 * domain support IOMMUv2. Just force that the domain has no
3230 * devices attached when it is switched into IOMMUv2 mode.
3231 */
3232 ret = -EBUSY;
3233 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3234 goto out;
3235
3236 ret = -ENOMEM;
3237 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3238 if (domain->gcr3_tbl == NULL)
3239 goto out;
3240
3241 domain->glx = levels;
3242 domain->flags |= PD_IOMMUV2_MASK;
3243 domain->updated = true;
3244
3245 update_domain(domain);
3246
3247 ret = 0;
3248
3249out:
3250 spin_unlock_irqrestore(&domain->lock, flags);
3251
3252 return ret;
3253}
3254EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003255
3256static int __flush_pasid(struct protection_domain *domain, int pasid,
3257 u64 address, bool size)
3258{
3259 struct iommu_dev_data *dev_data;
3260 struct iommu_cmd cmd;
3261 int i, ret;
3262
3263 if (!(domain->flags & PD_IOMMUV2_MASK))
3264 return -EINVAL;
3265
3266 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3267
3268 /*
3269 * IOMMU TLB needs to be flushed before Device TLB to
3270 * prevent device TLB refill from IOMMU TLB
3271 */
3272 for (i = 0; i < amd_iommus_present; ++i) {
3273 if (domain->dev_iommu[i] == 0)
3274 continue;
3275
3276 ret = iommu_queue_command(amd_iommus[i], &cmd);
3277 if (ret != 0)
3278 goto out;
3279 }
3280
3281 /* Wait until IOMMU TLB flushes are complete */
3282 domain_flush_complete(domain);
3283
3284 /* Now flush device TLBs */
3285 list_for_each_entry(dev_data, &domain->dev_list, list) {
3286 struct amd_iommu *iommu;
3287 int qdep;
3288
Joerg Roedel1c1cc452015-07-30 11:24:45 +02003289 /*
3290 There might be non-IOMMUv2 capable devices in an IOMMUv2
3291 * domain.
3292 */
3293 if (!dev_data->ats.enabled)
3294 continue;
Joerg Roedel22e266c2011-11-21 15:59:08 +01003295
3296 qdep = dev_data->ats.qdep;
3297 iommu = amd_iommu_rlookup_table[dev_data->devid];
3298
3299 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3300 qdep, address, size);
3301
3302 ret = iommu_queue_command(iommu, &cmd);
3303 if (ret != 0)
3304 goto out;
3305 }
3306
3307 /* Wait until all device TLBs are flushed */
3308 domain_flush_complete(domain);
3309
3310 ret = 0;
3311
3312out:
3313
3314 return ret;
3315}
3316
3317static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3318 u64 address)
3319{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003320 INC_STATS_COUNTER(invalidate_iotlb);
3321
Joerg Roedel22e266c2011-11-21 15:59:08 +01003322 return __flush_pasid(domain, pasid, address, false);
3323}
3324
3325int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3326 u64 address)
3327{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003328 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003329 unsigned long flags;
3330 int ret;
3331
3332 spin_lock_irqsave(&domain->lock, flags);
3333 ret = __amd_iommu_flush_page(domain, pasid, address);
3334 spin_unlock_irqrestore(&domain->lock, flags);
3335
3336 return ret;
3337}
3338EXPORT_SYMBOL(amd_iommu_flush_page);
3339
3340static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3341{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003342 INC_STATS_COUNTER(invalidate_iotlb_all);
3343
Joerg Roedel22e266c2011-11-21 15:59:08 +01003344 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3345 true);
3346}
3347
3348int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3349{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003350 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003351 unsigned long flags;
3352 int ret;
3353
3354 spin_lock_irqsave(&domain->lock, flags);
3355 ret = __amd_iommu_flush_tlb(domain, pasid);
3356 spin_unlock_irqrestore(&domain->lock, flags);
3357
3358 return ret;
3359}
3360EXPORT_SYMBOL(amd_iommu_flush_tlb);
3361
Joerg Roedelb16137b2011-11-21 16:50:23 +01003362static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3363{
3364 int index;
3365 u64 *pte;
3366
3367 while (true) {
3368
3369 index = (pasid >> (9 * level)) & 0x1ff;
3370 pte = &root[index];
3371
3372 if (level == 0)
3373 break;
3374
3375 if (!(*pte & GCR3_VALID)) {
3376 if (!alloc)
3377 return NULL;
3378
3379 root = (void *)get_zeroed_page(GFP_ATOMIC);
3380 if (root == NULL)
3381 return NULL;
3382
3383 *pte = __pa(root) | GCR3_VALID;
3384 }
3385
3386 root = __va(*pte & PAGE_MASK);
3387
3388 level -= 1;
3389 }
3390
3391 return pte;
3392}
3393
3394static int __set_gcr3(struct protection_domain *domain, int pasid,
3395 unsigned long cr3)
3396{
3397 u64 *pte;
3398
3399 if (domain->mode != PAGE_MODE_NONE)
3400 return -EINVAL;
3401
3402 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3403 if (pte == NULL)
3404 return -ENOMEM;
3405
3406 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3407
3408 return __amd_iommu_flush_tlb(domain, pasid);
3409}
3410
3411static int __clear_gcr3(struct protection_domain *domain, int pasid)
3412{
3413 u64 *pte;
3414
3415 if (domain->mode != PAGE_MODE_NONE)
3416 return -EINVAL;
3417
3418 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3419 if (pte == NULL)
3420 return 0;
3421
3422 *pte = 0;
3423
3424 return __amd_iommu_flush_tlb(domain, pasid);
3425}
3426
3427int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3428 unsigned long cr3)
3429{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003430 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003431 unsigned long flags;
3432 int ret;
3433
3434 spin_lock_irqsave(&domain->lock, flags);
3435 ret = __set_gcr3(domain, pasid, cr3);
3436 spin_unlock_irqrestore(&domain->lock, flags);
3437
3438 return ret;
3439}
3440EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3441
3442int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3443{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003444 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003445 unsigned long flags;
3446 int ret;
3447
3448 spin_lock_irqsave(&domain->lock, flags);
3449 ret = __clear_gcr3(domain, pasid);
3450 spin_unlock_irqrestore(&domain->lock, flags);
3451
3452 return ret;
3453}
3454EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003455
3456int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3457 int status, int tag)
3458{
3459 struct iommu_dev_data *dev_data;
3460 struct amd_iommu *iommu;
3461 struct iommu_cmd cmd;
3462
Joerg Roedel399be2f2011-12-01 16:53:47 +01003463 INC_STATS_COUNTER(complete_ppr);
3464
Joerg Roedelc99afa22011-11-21 18:19:25 +01003465 dev_data = get_dev_data(&pdev->dev);
3466 iommu = amd_iommu_rlookup_table[dev_data->devid];
3467
3468 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3469 tag, dev_data->pri_tlp);
3470
3471 return iommu_queue_command(iommu, &cmd);
3472}
3473EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003474
3475struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3476{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003477 struct protection_domain *pdomain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003478
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003479 pdomain = get_domain(&pdev->dev);
3480 if (IS_ERR(pdomain))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003481 return NULL;
3482
3483 /* Only return IOMMUv2 domains */
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003484 if (!(pdomain->flags & PD_IOMMUV2_MASK))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003485 return NULL;
3486
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003487 return &pdomain->domain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003488}
3489EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003490
3491void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3492{
3493 struct iommu_dev_data *dev_data;
3494
3495 if (!amd_iommu_v2_supported())
3496 return;
3497
3498 dev_data = get_dev_data(&pdev->dev);
3499 dev_data->errata |= (1 << erratum);
3500}
3501EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003502
3503int amd_iommu_device_info(struct pci_dev *pdev,
3504 struct amd_iommu_device_info *info)
3505{
3506 int max_pasids;
3507 int pos;
3508
3509 if (pdev == NULL || info == NULL)
3510 return -EINVAL;
3511
3512 if (!amd_iommu_v2_supported())
3513 return -EINVAL;
3514
3515 memset(info, 0, sizeof(*info));
3516
3517 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3518 if (pos)
3519 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3520
3521 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3522 if (pos)
3523 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3524
3525 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3526 if (pos) {
3527 int features;
3528
3529 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3530 max_pasids = min(max_pasids, (1 << 20));
3531
3532 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3533 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3534
3535 features = pci_pasid_features(pdev);
3536 if (features & PCI_PASID_CAP_EXEC)
3537 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3538 if (features & PCI_PASID_CAP_PRIV)
3539 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3540 }
3541
3542 return 0;
3543}
3544EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003545
3546#ifdef CONFIG_IRQ_REMAP
3547
3548/*****************************************************************************
3549 *
3550 * Interrupt Remapping Implementation
3551 *
3552 *****************************************************************************/
3553
3554union irte {
3555 u32 val;
3556 struct {
3557 u32 valid : 1,
3558 no_fault : 1,
3559 int_type : 3,
3560 rq_eoi : 1,
3561 dm : 1,
3562 rsvd_1 : 1,
3563 destination : 8,
3564 vector : 8,
3565 rsvd_2 : 8;
3566 } fields;
3567};
3568
Jiang Liu9c724962015-04-14 10:29:52 +08003569struct irq_2_irte {
3570 u16 devid; /* Device ID for IRTE table */
3571 u16 index; /* Index into IRTE table*/
3572};
3573
Jiang Liu7c71d302015-04-13 14:11:33 +08003574struct amd_ir_data {
3575 struct irq_2_irte irq_2_irte;
3576 union irte irte_entry;
3577 union {
3578 struct msi_msg msi_entry;
3579 };
3580};
3581
3582static struct irq_chip amd_ir_chip;
3583
Joerg Roedel2b324502012-06-21 16:29:10 +02003584#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3585#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3586#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3587#define DTE_IRQ_REMAP_ENABLE 1ULL
3588
3589static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3590{
3591 u64 dte;
3592
3593 dte = amd_iommu_dev_table[devid].data[2];
3594 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3595 dte |= virt_to_phys(table->table);
3596 dte |= DTE_IRQ_REMAP_INTCTL;
3597 dte |= DTE_IRQ_TABLE_LEN;
3598 dte |= DTE_IRQ_REMAP_ENABLE;
3599
3600 amd_iommu_dev_table[devid].data[2] = dte;
3601}
3602
3603#define IRTE_ALLOCATED (~1U)
3604
3605static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3606{
3607 struct irq_remap_table *table = NULL;
3608 struct amd_iommu *iommu;
3609 unsigned long flags;
3610 u16 alias;
3611
3612 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3613
3614 iommu = amd_iommu_rlookup_table[devid];
3615 if (!iommu)
3616 goto out_unlock;
3617
3618 table = irq_lookup_table[devid];
3619 if (table)
3620 goto out;
3621
3622 alias = amd_iommu_alias_table[devid];
3623 table = irq_lookup_table[alias];
3624 if (table) {
3625 irq_lookup_table[devid] = table;
3626 set_dte_irq_entry(devid, table);
3627 iommu_flush_dte(iommu, devid);
3628 goto out;
3629 }
3630
3631 /* Nothing there yet, allocate new irq remapping table */
3632 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3633 if (!table)
3634 goto out;
3635
Joerg Roedel197887f2013-04-09 21:14:08 +02003636 /* Initialize table spin-lock */
3637 spin_lock_init(&table->lock);
3638
Joerg Roedel2b324502012-06-21 16:29:10 +02003639 if (ioapic)
3640 /* Keep the first 32 indexes free for IOAPIC interrupts */
3641 table->min_index = 32;
3642
3643 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3644 if (!table->table) {
3645 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003646 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003647 goto out;
3648 }
3649
3650 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3651
3652 if (ioapic) {
3653 int i;
3654
3655 for (i = 0; i < 32; ++i)
3656 table->table[i] = IRTE_ALLOCATED;
3657 }
3658
3659 irq_lookup_table[devid] = table;
3660 set_dte_irq_entry(devid, table);
3661 iommu_flush_dte(iommu, devid);
3662 if (devid != alias) {
3663 irq_lookup_table[alias] = table;
Alex Williamsone028a9e2014-04-22 10:08:40 -06003664 set_dte_irq_entry(alias, table);
Joerg Roedel2b324502012-06-21 16:29:10 +02003665 iommu_flush_dte(iommu, alias);
3666 }
3667
3668out:
3669 iommu_completion_wait(iommu);
3670
3671out_unlock:
3672 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3673
3674 return table;
3675}
3676
Jiang Liu3c3d4f92015-04-13 14:11:38 +08003677static int alloc_irq_index(u16 devid, int count)
Joerg Roedel2b324502012-06-21 16:29:10 +02003678{
3679 struct irq_remap_table *table;
3680 unsigned long flags;
3681 int index, c;
3682
3683 table = get_irq_table(devid, false);
3684 if (!table)
3685 return -ENODEV;
3686
3687 spin_lock_irqsave(&table->lock, flags);
3688
3689 /* Scan table for free entries */
3690 for (c = 0, index = table->min_index;
3691 index < MAX_IRQS_PER_TABLE;
3692 ++index) {
3693 if (table->table[index] == 0)
3694 c += 1;
3695 else
3696 c = 0;
3697
3698 if (c == count) {
Joerg Roedel2b324502012-06-21 16:29:10 +02003699 for (; c != 0; --c)
3700 table->table[index - c + 1] = IRTE_ALLOCATED;
3701
3702 index -= count - 1;
Joerg Roedel2b324502012-06-21 16:29:10 +02003703 goto out;
3704 }
3705 }
3706
3707 index = -ENOSPC;
3708
3709out:
3710 spin_unlock_irqrestore(&table->lock, flags);
3711
3712 return index;
3713}
3714
Joerg Roedel2b324502012-06-21 16:29:10 +02003715static int modify_irte(u16 devid, int index, union irte irte)
3716{
3717 struct irq_remap_table *table;
3718 struct amd_iommu *iommu;
3719 unsigned long flags;
3720
3721 iommu = amd_iommu_rlookup_table[devid];
3722 if (iommu == NULL)
3723 return -EINVAL;
3724
3725 table = get_irq_table(devid, false);
3726 if (!table)
3727 return -ENOMEM;
3728
3729 spin_lock_irqsave(&table->lock, flags);
3730 table->table[index] = irte.val;
3731 spin_unlock_irqrestore(&table->lock, flags);
3732
3733 iommu_flush_irt(iommu, devid);
3734 iommu_completion_wait(iommu);
3735
3736 return 0;
3737}
3738
3739static void free_irte(u16 devid, int index)
3740{
3741 struct irq_remap_table *table;
3742 struct amd_iommu *iommu;
3743 unsigned long flags;
3744
3745 iommu = amd_iommu_rlookup_table[devid];
3746 if (iommu == NULL)
3747 return;
3748
3749 table = get_irq_table(devid, false);
3750 if (!table)
3751 return;
3752
3753 spin_lock_irqsave(&table->lock, flags);
3754 table->table[index] = 0;
3755 spin_unlock_irqrestore(&table->lock, flags);
3756
3757 iommu_flush_irt(iommu, devid);
3758 iommu_completion_wait(iommu);
3759}
3760
Jiang Liu7c71d302015-04-13 14:11:33 +08003761static int get_devid(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02003762{
Jiang Liu7c71d302015-04-13 14:11:33 +08003763 int devid = -1;
Joerg Roedel5527de72012-06-26 11:17:32 +02003764
Jiang Liu7c71d302015-04-13 14:11:33 +08003765 switch (info->type) {
3766 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3767 devid = get_ioapic_devid(info->ioapic_id);
3768 break;
3769 case X86_IRQ_ALLOC_TYPE_HPET:
3770 devid = get_hpet_devid(info->hpet_id);
3771 break;
3772 case X86_IRQ_ALLOC_TYPE_MSI:
3773 case X86_IRQ_ALLOC_TYPE_MSIX:
3774 devid = get_device_id(&info->msi_dev->dev);
3775 break;
3776 default:
3777 BUG_ON(1);
3778 break;
Joerg Roedel5527de72012-06-26 11:17:32 +02003779 }
3780
Jiang Liu7c71d302015-04-13 14:11:33 +08003781 return devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02003782}
3783
Jiang Liu7c71d302015-04-13 14:11:33 +08003784static struct irq_domain *get_ir_irq_domain(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02003785{
Jiang Liu7c71d302015-04-13 14:11:33 +08003786 struct amd_iommu *iommu;
3787 int devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02003788
Jiang Liu7c71d302015-04-13 14:11:33 +08003789 if (!info)
3790 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02003791
Jiang Liu7c71d302015-04-13 14:11:33 +08003792 devid = get_devid(info);
3793 if (devid >= 0) {
3794 iommu = amd_iommu_rlookup_table[devid];
3795 if (iommu)
3796 return iommu->ir_domain;
3797 }
Joerg Roedel5527de72012-06-26 11:17:32 +02003798
Jiang Liu7c71d302015-04-13 14:11:33 +08003799 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02003800}
3801
Jiang Liu7c71d302015-04-13 14:11:33 +08003802static struct irq_domain *get_irq_domain(struct irq_alloc_info *info)
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003803{
Jiang Liu7c71d302015-04-13 14:11:33 +08003804 struct amd_iommu *iommu;
3805 int devid;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003806
Jiang Liu7c71d302015-04-13 14:11:33 +08003807 if (!info)
3808 return NULL;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003809
Jiang Liu7c71d302015-04-13 14:11:33 +08003810 switch (info->type) {
3811 case X86_IRQ_ALLOC_TYPE_MSI:
3812 case X86_IRQ_ALLOC_TYPE_MSIX:
3813 devid = get_device_id(&info->msi_dev->dev);
3814 if (devid >= 0) {
3815 iommu = amd_iommu_rlookup_table[devid];
3816 if (iommu)
3817 return iommu->msi_domain;
3818 }
3819 break;
3820 default:
3821 break;
3822 }
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003823
Jiang Liu7c71d302015-04-13 14:11:33 +08003824 return NULL;
Joerg Roedeld9761952012-06-26 16:00:08 +02003825}
3826
Joerg Roedel6b474b82012-06-26 16:46:04 +02003827struct irq_remap_ops amd_iommu_irq_ops = {
Joerg Roedel6b474b82012-06-26 16:46:04 +02003828 .prepare = amd_iommu_prepare,
3829 .enable = amd_iommu_enable,
3830 .disable = amd_iommu_disable,
3831 .reenable = amd_iommu_reenable,
3832 .enable_faulting = amd_iommu_enable_faulting,
Jiang Liu7c71d302015-04-13 14:11:33 +08003833 .get_ir_irq_domain = get_ir_irq_domain,
3834 .get_irq_domain = get_irq_domain,
Joerg Roedel6b474b82012-06-26 16:46:04 +02003835};
Jiang Liu7c71d302015-04-13 14:11:33 +08003836
3837static void irq_remapping_prepare_irte(struct amd_ir_data *data,
3838 struct irq_cfg *irq_cfg,
3839 struct irq_alloc_info *info,
3840 int devid, int index, int sub_handle)
3841{
3842 struct irq_2_irte *irte_info = &data->irq_2_irte;
3843 struct msi_msg *msg = &data->msi_entry;
3844 union irte *irte = &data->irte_entry;
3845 struct IO_APIC_route_entry *entry;
3846
Jiang Liu7c71d302015-04-13 14:11:33 +08003847 data->irq_2_irte.devid = devid;
3848 data->irq_2_irte.index = index + sub_handle;
3849
3850 /* Setup IRTE for IOMMU */
3851 irte->val = 0;
3852 irte->fields.vector = irq_cfg->vector;
3853 irte->fields.int_type = apic->irq_delivery_mode;
3854 irte->fields.destination = irq_cfg->dest_apicid;
3855 irte->fields.dm = apic->irq_dest_mode;
3856 irte->fields.valid = 1;
3857
3858 switch (info->type) {
3859 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3860 /* Setup IOAPIC entry */
3861 entry = info->ioapic_entry;
3862 info->ioapic_entry = NULL;
3863 memset(entry, 0, sizeof(*entry));
3864 entry->vector = index;
3865 entry->mask = 0;
3866 entry->trigger = info->ioapic_trigger;
3867 entry->polarity = info->ioapic_polarity;
3868 /* Mask level triggered irqs. */
3869 if (info->ioapic_trigger)
3870 entry->mask = 1;
3871 break;
3872
3873 case X86_IRQ_ALLOC_TYPE_HPET:
3874 case X86_IRQ_ALLOC_TYPE_MSI:
3875 case X86_IRQ_ALLOC_TYPE_MSIX:
3876 msg->address_hi = MSI_ADDR_BASE_HI;
3877 msg->address_lo = MSI_ADDR_BASE_LO;
3878 msg->data = irte_info->index;
3879 break;
3880
3881 default:
3882 BUG_ON(1);
3883 break;
3884 }
3885}
3886
3887static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
3888 unsigned int nr_irqs, void *arg)
3889{
3890 struct irq_alloc_info *info = arg;
3891 struct irq_data *irq_data;
3892 struct amd_ir_data *data;
3893 struct irq_cfg *cfg;
3894 int i, ret, devid;
3895 int index = -1;
3896
3897 if (!info)
3898 return -EINVAL;
3899 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI &&
3900 info->type != X86_IRQ_ALLOC_TYPE_MSIX)
3901 return -EINVAL;
3902
3903 /*
3904 * With IRQ remapping enabled, don't need contiguous CPU vectors
3905 * to support multiple MSI interrupts.
3906 */
3907 if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
3908 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
3909
3910 devid = get_devid(info);
3911 if (devid < 0)
3912 return -EINVAL;
3913
3914 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
3915 if (ret < 0)
3916 return ret;
3917
Jiang Liu7c71d302015-04-13 14:11:33 +08003918 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
3919 if (get_irq_table(devid, true))
3920 index = info->ioapic_pin;
3921 else
3922 ret = -ENOMEM;
3923 } else {
Jiang Liu3c3d4f92015-04-13 14:11:38 +08003924 index = alloc_irq_index(devid, nr_irqs);
Jiang Liu7c71d302015-04-13 14:11:33 +08003925 }
3926 if (index < 0) {
3927 pr_warn("Failed to allocate IRTE\n");
Jiang Liu7c71d302015-04-13 14:11:33 +08003928 goto out_free_parent;
3929 }
3930
3931 for (i = 0; i < nr_irqs; i++) {
3932 irq_data = irq_domain_get_irq_data(domain, virq + i);
3933 cfg = irqd_cfg(irq_data);
3934 if (!irq_data || !cfg) {
3935 ret = -EINVAL;
3936 goto out_free_data;
3937 }
3938
Joerg Roedela130e692015-08-13 11:07:25 +02003939 ret = -ENOMEM;
3940 data = kzalloc(sizeof(*data), GFP_KERNEL);
3941 if (!data)
3942 goto out_free_data;
3943
Jiang Liu7c71d302015-04-13 14:11:33 +08003944 irq_data->hwirq = (devid << 16) + i;
3945 irq_data->chip_data = data;
3946 irq_data->chip = &amd_ir_chip;
3947 irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
3948 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
3949 }
Joerg Roedela130e692015-08-13 11:07:25 +02003950
Jiang Liu7c71d302015-04-13 14:11:33 +08003951 return 0;
3952
3953out_free_data:
3954 for (i--; i >= 0; i--) {
3955 irq_data = irq_domain_get_irq_data(domain, virq + i);
3956 if (irq_data)
3957 kfree(irq_data->chip_data);
3958 }
3959 for (i = 0; i < nr_irqs; i++)
3960 free_irte(devid, index + i);
3961out_free_parent:
3962 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3963 return ret;
3964}
3965
3966static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
3967 unsigned int nr_irqs)
3968{
3969 struct irq_2_irte *irte_info;
3970 struct irq_data *irq_data;
3971 struct amd_ir_data *data;
3972 int i;
3973
3974 for (i = 0; i < nr_irqs; i++) {
3975 irq_data = irq_domain_get_irq_data(domain, virq + i);
3976 if (irq_data && irq_data->chip_data) {
3977 data = irq_data->chip_data;
3978 irte_info = &data->irq_2_irte;
3979 free_irte(irte_info->devid, irte_info->index);
3980 kfree(data);
3981 }
3982 }
3983 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3984}
3985
3986static void irq_remapping_activate(struct irq_domain *domain,
3987 struct irq_data *irq_data)
3988{
3989 struct amd_ir_data *data = irq_data->chip_data;
3990 struct irq_2_irte *irte_info = &data->irq_2_irte;
3991
3992 modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
3993}
3994
3995static void irq_remapping_deactivate(struct irq_domain *domain,
3996 struct irq_data *irq_data)
3997{
3998 struct amd_ir_data *data = irq_data->chip_data;
3999 struct irq_2_irte *irte_info = &data->irq_2_irte;
4000 union irte entry;
4001
4002 entry.val = 0;
4003 modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
4004}
4005
4006static struct irq_domain_ops amd_ir_domain_ops = {
4007 .alloc = irq_remapping_alloc,
4008 .free = irq_remapping_free,
4009 .activate = irq_remapping_activate,
4010 .deactivate = irq_remapping_deactivate,
4011};
4012
4013static int amd_ir_set_affinity(struct irq_data *data,
4014 const struct cpumask *mask, bool force)
4015{
4016 struct amd_ir_data *ir_data = data->chip_data;
4017 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
4018 struct irq_cfg *cfg = irqd_cfg(data);
4019 struct irq_data *parent = data->parent_data;
4020 int ret;
4021
4022 ret = parent->chip->irq_set_affinity(parent, mask, force);
4023 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
4024 return ret;
4025
4026 /*
4027 * Atomically updates the IRTE with the new destination, vector
4028 * and flushes the interrupt entry cache.
4029 */
4030 ir_data->irte_entry.fields.vector = cfg->vector;
4031 ir_data->irte_entry.fields.destination = cfg->dest_apicid;
4032 modify_irte(irte_info->devid, irte_info->index, ir_data->irte_entry);
4033
4034 /*
4035 * After this point, all the interrupts will start arriving
4036 * at the new destination. So, time to cleanup the previous
4037 * vector allocation.
4038 */
Jiang Liuc6c20022015-04-14 10:30:02 +08004039 send_cleanup_vector(cfg);
Jiang Liu7c71d302015-04-13 14:11:33 +08004040
4041 return IRQ_SET_MASK_OK_DONE;
4042}
4043
4044static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
4045{
4046 struct amd_ir_data *ir_data = irq_data->chip_data;
4047
4048 *msg = ir_data->msi_entry;
4049}
4050
4051static struct irq_chip amd_ir_chip = {
4052 .irq_ack = ir_ack_apic_edge,
4053 .irq_set_affinity = amd_ir_set_affinity,
4054 .irq_compose_msi_msg = ir_compose_msi_msg,
4055};
4056
4057int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
4058{
4059 iommu->ir_domain = irq_domain_add_tree(NULL, &amd_ir_domain_ops, iommu);
4060 if (!iommu->ir_domain)
4061 return -ENOMEM;
4062
4063 iommu->ir_domain->parent = arch_get_ir_parent_domain();
4064 iommu->msi_domain = arch_create_msi_irq_domain(iommu->ir_domain);
4065
4066 return 0;
4067}
Joerg Roedel2b324502012-06-21 16:29:10 +02004068#endif