blob: faf51a066e983c1676fca5e73aefa7572b9006b6 [file] [log] [blame]
Joerg Roedelb6c02712008-06-26 21:27:53 +02001/*
Joerg Roedel5d0d7152010-10-13 11:13:21 +02002 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
Joerg Roedel63ce3ae2015-02-04 16:12:55 +01003 * Author: Joerg Roedel <jroedel@suse.de>
Joerg Roedelb6c02712008-06-26 21:27:53 +02004 * Leo Duran <leo.duran@amd.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010020#include <linux/ratelimit.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020021#include <linux/pci.h>
Joerg Roedelcb41ed82011-04-05 11:00:53 +020022#include <linux/pci-ats.h>
Akinobu Mitaa66022c2009-12-15 16:48:28 -080023#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Joerg Roedel7f265082008-12-12 13:50:21 +010025#include <linux/debugfs.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020026#include <linux/scatterlist.h>
FUJITA Tomonori51491362009-01-05 23:47:25 +090027#include <linux/dma-mapping.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020028#include <linux/iommu-helper.h>
Joerg Roedelc156e342008-12-02 18:13:27 +010029#include <linux/iommu.h>
Joerg Roedel815b33f2011-04-06 17:26:49 +020030#include <linux/delay.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020031#include <linux/amd-iommu.h>
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010032#include <linux/notifier.h>
33#include <linux/export.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020034#include <linux/irq.h>
35#include <linux/msi.h>
Joerg Roedel3b839a52015-04-01 14:58:47 +020036#include <linux/dma-contiguous.h>
Jiang Liu7c71d302015-04-13 14:11:33 +080037#include <linux/irqdomain.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020038#include <asm/irq_remapping.h>
39#include <asm/io_apic.h>
40#include <asm/apic.h>
41#include <asm/hw_irq.h>
Joerg Roedel17f5b562011-07-06 17:14:44 +020042#include <asm/msidef.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020043#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090044#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010045#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020046#include <asm/dma.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020047
48#include "amd_iommu_proto.h"
49#include "amd_iommu_types.h"
Joerg Roedel6b474b82012-06-26 16:46:04 +020050#include "irq_remapping.h"
Joerg Roedelb6c02712008-06-26 21:27:53 +020051
52#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
53
Joerg Roedel815b33f2011-04-06 17:26:49 +020054#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020055
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020056/*
57 * This bitmap is used to advertise the page sizes our hardware support
58 * to the IOMMU core, which will then use this information to split
59 * physically contiguous memory regions it is mapping into page sizes
60 * that we support.
61 *
Joerg Roedel954e3dd2012-12-02 15:35:37 +010062 * 512GB Pages are not supported due to a hardware bug
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020063 */
Joerg Roedel954e3dd2012-12-02 15:35:37 +010064#define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020065
Joerg Roedelb6c02712008-06-26 21:27:53 +020066static DEFINE_RWLOCK(amd_iommu_devtable_lock);
67
Joerg Roedel8fa5f802011-06-09 12:24:45 +020068/* List of all available dev_data structures */
69static LIST_HEAD(dev_data_list);
70static DEFINE_SPINLOCK(dev_data_list_lock);
71
Joerg Roedel6efed632012-06-14 15:52:58 +020072LIST_HEAD(ioapic_map);
73LIST_HEAD(hpet_map);
74
Joerg Roedel0feae532009-08-26 15:26:30 +020075/*
76 * Domain for untranslated devices - only allocated
77 * if iommu=pt passed on kernel cmd line.
78 */
Thierry Redingb22f6432014-06-27 09:03:12 +020079static const struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010080
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010081static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +010082int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010083
Joerg Roedelac1534a2012-06-21 14:52:40 +020084static struct dma_map_ops amd_iommu_dma_ops;
85
Joerg Roedel431b2a22008-07-11 17:14:22 +020086/*
Joerg Roedel50917e22014-08-05 16:38:38 +020087 * This struct contains device specific data for the IOMMU
88 */
89struct iommu_dev_data {
90 struct list_head list; /* For domain->dev_list */
91 struct list_head dev_data_list; /* For global dev_data_list */
Joerg Roedel50917e22014-08-05 16:38:38 +020092 struct protection_domain *domain; /* Domain the device is bound to */
Joerg Roedel50917e22014-08-05 16:38:38 +020093 u16 devid; /* PCI Device ID */
94 bool iommu_v2; /* Device can make use of IOMMUv2 */
Joerg Roedel1e6a7b02015-07-28 16:58:48 +020095 bool passthrough; /* Device is identity mapped */
Joerg Roedel50917e22014-08-05 16:38:38 +020096 struct {
97 bool enabled;
98 int qdep;
99 } ats; /* ATS state */
100 bool pri_tlp; /* PASID TLB required for
101 PPR completions */
102 u32 errata; /* Bitmap for errata to apply */
103};
104
105/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200106 * general struct to manage commands send to an IOMMU
107 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200108struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +0200109 u32 data[4];
110};
111
Joerg Roedel05152a02012-06-15 16:53:51 +0200112struct kmem_cache *amd_iommu_irq_cache;
113
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200114static void update_domain(struct protection_domain *domain);
Joerg Roedel7a5a5662015-06-30 08:56:11 +0200115static int protection_domain_init(struct protection_domain *domain);
Chris Wrightc1eee672009-05-21 00:56:58 -0700116
Joerg Roedel007b74b2015-12-21 12:53:54 +0100117/*
118 * For dynamic growth the aperture size is split into ranges of 128MB of
119 * DMA address space each. This struct represents one such range.
120 */
121struct aperture_range {
122
Joerg Roedel08c5fb92015-12-21 13:04:49 +0100123 spinlock_t bitmap_lock;
124
Joerg Roedel007b74b2015-12-21 12:53:54 +0100125 /* address allocation bitmap */
126 unsigned long *bitmap;
Joerg Roedelae62d492015-12-21 16:28:45 +0100127 unsigned long offset;
Joerg Roedel60e6a7c2015-12-21 16:53:17 +0100128 unsigned long next_bit;
Joerg Roedel007b74b2015-12-21 12:53:54 +0100129
130 /*
131 * Array of PTE pages for the aperture. In this array we save all the
132 * leaf pages of the domain page table used for the aperture. This way
133 * we don't need to walk the page table to find a specific PTE. We can
134 * just calculate its address in constant time.
135 */
136 u64 *pte_pages[64];
Joerg Roedel007b74b2015-12-21 12:53:54 +0100137};
138
139/*
140 * Data container for a dma_ops specific protection domain
141 */
142struct dma_ops_domain {
143 /* generic protection domain information */
144 struct protection_domain domain;
145
146 /* size of the aperture for the mappings */
147 unsigned long aperture_size;
148
Joerg Roedelebaecb42015-12-21 18:11:32 +0100149 /* aperture index we start searching for free addresses */
150 unsigned long next_index;
Joerg Roedel007b74b2015-12-21 12:53:54 +0100151
152 /* address space relevant data */
153 struct aperture_range *aperture[APERTURE_MAX_RANGES];
154
155 /* This will be set to true when TLB needs to be flushed */
156 bool need_flush;
157};
158
Joerg Roedel15898bb2009-11-24 15:39:42 +0100159/****************************************************************************
160 *
161 * Helper functions
162 *
163 ****************************************************************************/
164
Joerg Roedel3f4b87b2015-03-26 13:43:07 +0100165static struct protection_domain *to_pdomain(struct iommu_domain *dom)
166{
167 return container_of(dom, struct protection_domain, domain);
168}
169
Joerg Roedelf62dda62011-06-09 12:55:35 +0200170static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200171{
172 struct iommu_dev_data *dev_data;
173 unsigned long flags;
174
175 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
176 if (!dev_data)
177 return NULL;
178
Joerg Roedelf62dda62011-06-09 12:55:35 +0200179 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200180
181 spin_lock_irqsave(&dev_data_list_lock, flags);
182 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
183 spin_unlock_irqrestore(&dev_data_list_lock, flags);
184
185 return dev_data;
186}
187
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200188static struct iommu_dev_data *search_dev_data(u16 devid)
189{
190 struct iommu_dev_data *dev_data;
191 unsigned long flags;
192
193 spin_lock_irqsave(&dev_data_list_lock, flags);
194 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
195 if (dev_data->devid == devid)
196 goto out_unlock;
197 }
198
199 dev_data = NULL;
200
201out_unlock:
202 spin_unlock_irqrestore(&dev_data_list_lock, flags);
203
204 return dev_data;
205}
206
207static struct iommu_dev_data *find_dev_data(u16 devid)
208{
209 struct iommu_dev_data *dev_data;
210
211 dev_data = search_dev_data(devid);
212
213 if (dev_data == NULL)
214 dev_data = alloc_dev_data(devid);
215
216 return dev_data;
217}
218
Joerg Roedel15898bb2009-11-24 15:39:42 +0100219static inline u16 get_device_id(struct device *dev)
220{
221 struct pci_dev *pdev = to_pci_dev(dev);
222
Shuah Khan6f2729b2013-02-27 17:07:30 -0700223 return PCI_DEVID(pdev->bus->number, pdev->devfn);
Joerg Roedel15898bb2009-11-24 15:39:42 +0100224}
225
Joerg Roedel657cbb62009-11-23 15:26:46 +0100226static struct iommu_dev_data *get_dev_data(struct device *dev)
227{
228 return dev->archdata.iommu;
229}
230
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100231static bool pci_iommuv2_capable(struct pci_dev *pdev)
232{
233 static const int caps[] = {
234 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100235 PCI_EXT_CAP_ID_PRI,
236 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100237 };
238 int i, pos;
239
240 for (i = 0; i < 3; ++i) {
241 pos = pci_find_ext_capability(pdev, caps[i]);
242 if (pos == 0)
243 return false;
244 }
245
246 return true;
247}
248
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100249static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
250{
251 struct iommu_dev_data *dev_data;
252
253 dev_data = get_dev_data(&pdev->dev);
254
255 return dev_data->errata & (1 << erratum) ? true : false;
256}
257
Joerg Roedel71c70982009-11-24 16:43:06 +0100258/*
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200259 * This function actually applies the mapping to the page table of the
260 * dma_ops domain.
Joerg Roedel71c70982009-11-24 16:43:06 +0100261 */
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200262static void alloc_unity_mapping(struct dma_ops_domain *dma_dom,
263 struct unity_map_entry *e)
Joerg Roedel71c70982009-11-24 16:43:06 +0100264{
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200265 u64 addr;
Joerg Roedel71c70982009-11-24 16:43:06 +0100266
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200267 for (addr = e->address_start; addr < e->address_end;
268 addr += PAGE_SIZE) {
269 if (addr < dma_dom->aperture_size)
270 __set_bit(addr >> PAGE_SHIFT,
271 dma_dom->aperture[0]->bitmap);
Joerg Roedel71c70982009-11-24 16:43:06 +0100272 }
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200273}
Joerg Roedel71c70982009-11-24 16:43:06 +0100274
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200275/*
276 * Inits the unity mappings required for a specific device
277 */
278static void init_unity_mappings_for_device(struct device *dev,
279 struct dma_ops_domain *dma_dom)
280{
281 struct unity_map_entry *e;
282 u16 devid;
Joerg Roedel71c70982009-11-24 16:43:06 +0100283
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200284 devid = get_device_id(dev);
285
286 list_for_each_entry(e, &amd_iommu_unity_map, list) {
287 if (!(devid >= e->devid_start && devid <= e->devid_end))
288 continue;
289 alloc_unity_mapping(dma_dom, e);
290 }
Joerg Roedel71c70982009-11-24 16:43:06 +0100291}
292
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100293/*
294 * This function checks if the driver got a valid device from the caller to
295 * avoid dereferencing invalid pointers.
296 */
297static bool check_device(struct device *dev)
298{
299 u16 devid;
300
301 if (!dev || !dev->dma_mask)
302 return false;
303
Yijing Wangb82a2272013-12-05 19:42:41 +0800304 /* No PCI device */
305 if (!dev_is_pci(dev))
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100306 return false;
307
308 devid = get_device_id(dev);
309
310 /* Out of our scope? */
311 if (devid > amd_iommu_last_bdf)
312 return false;
313
314 if (amd_iommu_rlookup_table[devid] == NULL)
315 return false;
316
317 return true;
318}
319
Alex Williamson25b11ce2014-09-19 10:03:13 -0600320static void init_iommu_group(struct device *dev)
Alex Williamson2851db22012-10-08 22:49:41 -0600321{
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200322 struct dma_ops_domain *dma_domain;
323 struct iommu_domain *domain;
Alex Williamson2851db22012-10-08 22:49:41 -0600324 struct iommu_group *group;
Alex Williamson2851db22012-10-08 22:49:41 -0600325
Alex Williamson65d53522014-07-03 09:51:30 -0600326 group = iommu_group_get_for_dev(dev);
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200327 if (IS_ERR(group))
328 return;
329
330 domain = iommu_group_default_domain(group);
331 if (!domain)
332 goto out;
333
334 dma_domain = to_pdomain(domain)->priv;
335
336 init_unity_mappings_for_device(dev, dma_domain);
337out:
338 iommu_group_put(group);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600339}
340
341static int iommu_init_device(struct device *dev)
342{
343 struct pci_dev *pdev = to_pci_dev(dev);
344 struct iommu_dev_data *dev_data;
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600345
346 if (dev->archdata.iommu)
347 return 0;
348
349 dev_data = find_dev_data(get_device_id(dev));
350 if (!dev_data)
351 return -ENOMEM;
352
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100353 if (pci_iommuv2_capable(pdev)) {
354 struct amd_iommu *iommu;
355
356 iommu = amd_iommu_rlookup_table[dev_data->devid];
357 dev_data->iommu_v2 = iommu->is_iommu_v2;
358 }
359
Joerg Roedel657cbb62009-11-23 15:26:46 +0100360 dev->archdata.iommu = dev_data;
361
Alex Williamson066f2e92014-06-12 16:12:37 -0600362 iommu_device_link(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
363 dev);
364
Joerg Roedel657cbb62009-11-23 15:26:46 +0100365 return 0;
366}
367
Joerg Roedel26018872011-06-06 16:50:14 +0200368static void iommu_ignore_device(struct device *dev)
369{
370 u16 devid, alias;
371
372 devid = get_device_id(dev);
373 alias = amd_iommu_alias_table[devid];
374
375 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
376 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
377
378 amd_iommu_rlookup_table[devid] = NULL;
379 amd_iommu_rlookup_table[alias] = NULL;
380}
381
Joerg Roedel657cbb62009-11-23 15:26:46 +0100382static void iommu_uninit_device(struct device *dev)
383{
Alex Williamsonc1931092014-07-03 09:51:24 -0600384 struct iommu_dev_data *dev_data = search_dev_data(get_device_id(dev));
385
386 if (!dev_data)
387 return;
388
Alex Williamson066f2e92014-06-12 16:12:37 -0600389 iommu_device_unlink(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
390 dev);
391
Alex Williamson9dcd6132012-05-30 14:19:07 -0600392 iommu_group_remove_device(dev);
393
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200394 /* Remove dma-ops */
395 dev->archdata.dma_ops = NULL;
396
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200397 /*
Alex Williamsonc1931092014-07-03 09:51:24 -0600398 * We keep dev_data around for unplugged devices and reuse it when the
399 * device is re-plugged - not doing so would introduce a ton of races.
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200400 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100401}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100402
Joerg Roedel7f265082008-12-12 13:50:21 +0100403#ifdef CONFIG_AMD_IOMMU_STATS
404
405/*
406 * Initialization code for statistics collection
407 */
408
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100409DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100410DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100411DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100412DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100413DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100414DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100415DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100416DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100417DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100418DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100419DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100420DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100421DECLARE_STATS_COUNTER(complete_ppr);
422DECLARE_STATS_COUNTER(invalidate_iotlb);
423DECLARE_STATS_COUNTER(invalidate_iotlb_all);
424DECLARE_STATS_COUNTER(pri_requests);
425
Joerg Roedel7f265082008-12-12 13:50:21 +0100426static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100427static struct dentry *de_fflush;
428
429static void amd_iommu_stats_add(struct __iommu_counter *cnt)
430{
431 if (stats_dir == NULL)
432 return;
433
434 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
435 &cnt->value);
436}
437
438static void amd_iommu_stats_init(void)
439{
440 stats_dir = debugfs_create_dir("amd-iommu", NULL);
441 if (stats_dir == NULL)
442 return;
443
Joerg Roedel7f265082008-12-12 13:50:21 +0100444 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
Dan Carpenter3775d482012-06-27 12:09:18 +0300445 &amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100446
447 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100448 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100449 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100450 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100451 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100452 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100453 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100454 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100455 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100456 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100457 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100458 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100459 amd_iommu_stats_add(&complete_ppr);
460 amd_iommu_stats_add(&invalidate_iotlb);
461 amd_iommu_stats_add(&invalidate_iotlb_all);
462 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100463}
464
465#endif
466
Joerg Roedel431b2a22008-07-11 17:14:22 +0200467/****************************************************************************
468 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200469 * Interrupt handling functions
470 *
471 ****************************************************************************/
472
Joerg Roedele3e59872009-09-03 14:02:10 +0200473static void dump_dte_entry(u16 devid)
474{
475 int i;
476
Joerg Roedelee6c2862011-11-09 12:06:03 +0100477 for (i = 0; i < 4; ++i)
478 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200479 amd_iommu_dev_table[devid].data[i]);
480}
481
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200482static void dump_command(unsigned long phys_addr)
483{
484 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
485 int i;
486
487 for (i = 0; i < 4; ++i)
488 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
489}
490
Joerg Roedela345b232009-09-03 15:01:43 +0200491static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200492{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200493 int type, devid, domid, flags;
494 volatile u32 *event = __evt;
495 int count = 0;
496 u64 address;
497
498retry:
499 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
500 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
501 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
502 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
503 address = (u64)(((u64)event[3]) << 32) | event[2];
504
505 if (type == 0) {
506 /* Did we hit the erratum? */
507 if (++count == LOOP_TIMEOUT) {
508 pr_err("AMD-Vi: No event written to event log\n");
509 return;
510 }
511 udelay(1);
512 goto retry;
513 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200514
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200515 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200516
517 switch (type) {
518 case EVENT_TYPE_ILL_DEV:
519 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
520 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700521 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200522 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200523 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200524 break;
525 case EVENT_TYPE_IO_FAULT:
526 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
527 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700528 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200529 domid, address, flags);
530 break;
531 case EVENT_TYPE_DEV_TAB_ERR:
532 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
533 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700534 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200535 address, flags);
536 break;
537 case EVENT_TYPE_PAGE_TAB_ERR:
538 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
539 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700540 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200541 domid, address, flags);
542 break;
543 case EVENT_TYPE_ILL_CMD:
544 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200545 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200546 break;
547 case EVENT_TYPE_CMD_HARD_ERR:
548 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
549 "flags=0x%04x]\n", address, flags);
550 break;
551 case EVENT_TYPE_IOTLB_INV_TO:
552 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
553 "address=0x%016llx]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700554 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200555 address);
556 break;
557 case EVENT_TYPE_INV_DEV_REQ:
558 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
559 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700560 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200561 address, flags);
562 break;
563 default:
564 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
565 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200566
567 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200568}
569
570static void iommu_poll_events(struct amd_iommu *iommu)
571{
572 u32 head, tail;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200573
574 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
575 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
576
577 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200578 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200579 head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200580 }
581
582 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200583}
584
Joerg Roedeleee53532012-06-01 15:20:23 +0200585static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100586{
587 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100588
Joerg Roedel399be2f2011-12-01 16:53:47 +0100589 INC_STATS_COUNTER(pri_requests);
590
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100591 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
592 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
593 return;
594 }
595
596 fault.address = raw[1];
597 fault.pasid = PPR_PASID(raw[0]);
598 fault.device_id = PPR_DEVID(raw[0]);
599 fault.tag = PPR_TAG(raw[0]);
600 fault.flags = PPR_FLAGS(raw[0]);
601
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100602 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
603}
604
605static void iommu_poll_ppr_log(struct amd_iommu *iommu)
606{
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100607 u32 head, tail;
608
609 if (iommu->ppr_log == NULL)
610 return;
611
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100612 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
613 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
614
615 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200616 volatile u64 *raw;
617 u64 entry[2];
618 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100619
Joerg Roedeleee53532012-06-01 15:20:23 +0200620 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100621
Joerg Roedeleee53532012-06-01 15:20:23 +0200622 /*
623 * Hardware bug: Interrupt may arrive before the entry is
624 * written to memory. If this happens we need to wait for the
625 * entry to arrive.
626 */
627 for (i = 0; i < LOOP_TIMEOUT; ++i) {
628 if (PPR_REQ_TYPE(raw[0]) != 0)
629 break;
630 udelay(1);
631 }
632
633 /* Avoid memcpy function-call overhead */
634 entry[0] = raw[0];
635 entry[1] = raw[1];
636
637 /*
638 * To detect the hardware bug we need to clear the entry
639 * back to zero.
640 */
641 raw[0] = raw[1] = 0UL;
642
643 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100644 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
645 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200646
Joerg Roedeleee53532012-06-01 15:20:23 +0200647 /* Handle PPR entry */
648 iommu_handle_ppr_entry(iommu, entry);
649
Joerg Roedeleee53532012-06-01 15:20:23 +0200650 /* Refresh ring-buffer information */
651 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100652 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
653 }
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100654}
655
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200656irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200657{
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500658 struct amd_iommu *iommu = (struct amd_iommu *) data;
659 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200660
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500661 while (status & (MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK)) {
662 /* Enable EVT and PPR interrupts again */
663 writel((MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK),
664 iommu->mmio_base + MMIO_STATUS_OFFSET);
665
666 if (status & MMIO_STATUS_EVT_INT_MASK) {
667 pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
668 iommu_poll_events(iommu);
669 }
670
671 if (status & MMIO_STATUS_PPR_INT_MASK) {
672 pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
673 iommu_poll_ppr_log(iommu);
674 }
675
676 /*
677 * Hardware bug: ERBT1312
678 * When re-enabling interrupt (by writing 1
679 * to clear the bit), the hardware might also try to set
680 * the interrupt bit in the event status register.
681 * In this scenario, the bit will be set, and disable
682 * subsequent interrupts.
683 *
684 * Workaround: The IOMMU driver should read back the
685 * status register and check if the interrupt bits are cleared.
686 * If not, driver will need to go through the interrupt handler
687 * again and re-clear the bits
688 */
689 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100690 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200691 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200692}
693
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200694irqreturn_t amd_iommu_int_handler(int irq, void *data)
695{
696 return IRQ_WAKE_THREAD;
697}
698
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200699/****************************************************************************
700 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200701 * IOMMU command queuing functions
702 *
703 ****************************************************************************/
704
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200705static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200706{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200707 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200708
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200709 while (*sem == 0 && i < LOOP_TIMEOUT) {
710 udelay(1);
711 i += 1;
712 }
713
714 if (i == LOOP_TIMEOUT) {
715 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
716 return -EIO;
717 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200718
719 return 0;
720}
721
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200722static void copy_cmd_to_buffer(struct amd_iommu *iommu,
723 struct iommu_cmd *cmd,
724 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200725{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200726 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200727
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200728 target = iommu->cmd_buf + tail;
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200729 tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200730
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200731 /* Copy command to buffer */
732 memcpy(target, cmd, sizeof(*cmd));
733
734 /* Tell the IOMMU about it */
735 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
736}
737
Joerg Roedel815b33f2011-04-06 17:26:49 +0200738static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200739{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200740 WARN_ON(address & 0x7ULL);
741
Joerg Roedelded46732011-04-06 10:53:48 +0200742 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200743 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
744 cmd->data[1] = upper_32_bits(__pa(address));
745 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200746 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
747}
748
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200749static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
750{
751 memset(cmd, 0, sizeof(*cmd));
752 cmd->data[0] = devid;
753 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
754}
755
Joerg Roedel11b64022011-04-06 11:49:28 +0200756static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
757 size_t size, u16 domid, int pde)
758{
759 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100760 bool s;
Joerg Roedel11b64022011-04-06 11:49:28 +0200761
762 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100763 s = false;
Joerg Roedel11b64022011-04-06 11:49:28 +0200764
765 if (pages > 1) {
766 /*
767 * If we have to flush more than one page, flush all
768 * TLB entries for this domain
769 */
770 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100771 s = true;
Joerg Roedel11b64022011-04-06 11:49:28 +0200772 }
773
774 address &= PAGE_MASK;
775
776 memset(cmd, 0, sizeof(*cmd));
777 cmd->data[1] |= domid;
778 cmd->data[2] = lower_32_bits(address);
779 cmd->data[3] = upper_32_bits(address);
780 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
781 if (s) /* size bit - we flush more than one 4kb page */
782 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200783 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200784 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
785}
786
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200787static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
788 u64 address, size_t size)
789{
790 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100791 bool s;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200792
793 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100794 s = false;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200795
796 if (pages > 1) {
797 /*
798 * If we have to flush more than one page, flush all
799 * TLB entries for this domain
800 */
801 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100802 s = true;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200803 }
804
805 address &= PAGE_MASK;
806
807 memset(cmd, 0, sizeof(*cmd));
808 cmd->data[0] = devid;
809 cmd->data[0] |= (qdep & 0xff) << 24;
810 cmd->data[1] = devid;
811 cmd->data[2] = lower_32_bits(address);
812 cmd->data[3] = upper_32_bits(address);
813 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
814 if (s)
815 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
816}
817
Joerg Roedel22e266c2011-11-21 15:59:08 +0100818static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
819 u64 address, bool size)
820{
821 memset(cmd, 0, sizeof(*cmd));
822
823 address &= ~(0xfffULL);
824
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600825 cmd->data[0] = pasid;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100826 cmd->data[1] = domid;
827 cmd->data[2] = lower_32_bits(address);
828 cmd->data[3] = upper_32_bits(address);
829 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
830 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
831 if (size)
832 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
833 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
834}
835
836static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
837 int qdep, u64 address, bool size)
838{
839 memset(cmd, 0, sizeof(*cmd));
840
841 address &= ~(0xfffULL);
842
843 cmd->data[0] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600844 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100845 cmd->data[0] |= (qdep & 0xff) << 24;
846 cmd->data[1] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600847 cmd->data[1] |= (pasid & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100848 cmd->data[2] = lower_32_bits(address);
849 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
850 cmd->data[3] = upper_32_bits(address);
851 if (size)
852 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
853 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
854}
855
Joerg Roedelc99afa22011-11-21 18:19:25 +0100856static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
857 int status, int tag, bool gn)
858{
859 memset(cmd, 0, sizeof(*cmd));
860
861 cmd->data[0] = devid;
862 if (gn) {
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600863 cmd->data[1] = pasid;
Joerg Roedelc99afa22011-11-21 18:19:25 +0100864 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
865 }
866 cmd->data[3] = tag & 0x1ff;
867 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
868
869 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
870}
871
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200872static void build_inv_all(struct iommu_cmd *cmd)
873{
874 memset(cmd, 0, sizeof(*cmd));
875 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200876}
877
Joerg Roedel7ef27982012-06-21 16:46:04 +0200878static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
879{
880 memset(cmd, 0, sizeof(*cmd));
881 cmd->data[0] = devid;
882 CMD_SET_TYPE(cmd, CMD_INV_IRT);
883}
884
Joerg Roedel431b2a22008-07-11 17:14:22 +0200885/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200886 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200887 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200888 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200889static int iommu_queue_command_sync(struct amd_iommu *iommu,
890 struct iommu_cmd *cmd,
891 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200892{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200893 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200894 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200895
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200896again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200897 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200898
899 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
900 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200901 next_tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
902 left = (head - next_tail) % CMD_BUFFER_SIZE;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200903
904 if (left <= 2) {
905 struct iommu_cmd sync_cmd;
906 volatile u64 sem = 0;
907 int ret;
908
909 build_completion_wait(&sync_cmd, (u64)&sem);
910 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
911
912 spin_unlock_irqrestore(&iommu->lock, flags);
913
914 if ((ret = wait_on_sem(&sem)) != 0)
915 return ret;
916
917 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200918 }
919
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200920 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200921
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200922 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200923 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200924
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200925 spin_unlock_irqrestore(&iommu->lock, flags);
926
Joerg Roedel815b33f2011-04-06 17:26:49 +0200927 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100928}
929
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200930static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
931{
932 return iommu_queue_command_sync(iommu, cmd, true);
933}
934
Joerg Roedel8d201962008-12-02 20:34:41 +0100935/*
936 * This function queues a completion wait command into the command
937 * buffer of an IOMMU
938 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100939static int iommu_completion_wait(struct amd_iommu *iommu)
940{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200941 struct iommu_cmd cmd;
942 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200943 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100944
945 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200946 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100947
Joerg Roedel815b33f2011-04-06 17:26:49 +0200948 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100949
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200950 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +0100951 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200952 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100953
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200954 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200955}
956
Joerg Roedeld8c13082011-04-06 18:51:26 +0200957static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200958{
959 struct iommu_cmd cmd;
960
Joerg Roedeld8c13082011-04-06 18:51:26 +0200961 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200962
Joerg Roedeld8c13082011-04-06 18:51:26 +0200963 return iommu_queue_command(iommu, &cmd);
964}
965
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200966static void iommu_flush_dte_all(struct amd_iommu *iommu)
967{
968 u32 devid;
969
970 for (devid = 0; devid <= 0xffff; ++devid)
971 iommu_flush_dte(iommu, devid);
972
973 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200974}
975
976/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200977 * This function uses heavy locking and may disable irqs for some time. But
978 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200979 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200980static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200981{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200982 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200983
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200984 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
985 struct iommu_cmd cmd;
986 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
987 dom_id, 1);
988 iommu_queue_command(iommu, &cmd);
989 }
Joerg Roedel431b2a22008-07-11 17:14:22 +0200990
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200991 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200992}
993
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200994static void iommu_flush_all(struct amd_iommu *iommu)
995{
996 struct iommu_cmd cmd;
997
998 build_inv_all(&cmd);
999
1000 iommu_queue_command(iommu, &cmd);
1001 iommu_completion_wait(iommu);
1002}
1003
Joerg Roedel7ef27982012-06-21 16:46:04 +02001004static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1005{
1006 struct iommu_cmd cmd;
1007
1008 build_inv_irt(&cmd, devid);
1009
1010 iommu_queue_command(iommu, &cmd);
1011}
1012
1013static void iommu_flush_irt_all(struct amd_iommu *iommu)
1014{
1015 u32 devid;
1016
1017 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1018 iommu_flush_irt(iommu, devid);
1019
1020 iommu_completion_wait(iommu);
1021}
1022
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001023void iommu_flush_all_caches(struct amd_iommu *iommu)
1024{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001025 if (iommu_feature(iommu, FEATURE_IA)) {
1026 iommu_flush_all(iommu);
1027 } else {
1028 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001029 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001030 iommu_flush_tlb_all(iommu);
1031 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001032}
1033
Joerg Roedel431b2a22008-07-11 17:14:22 +02001034/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001035 * Command send function for flushing on-device TLB
1036 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001037static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1038 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001039{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001040 struct amd_iommu *iommu;
1041 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001042 int qdep;
1043
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001044 qdep = dev_data->ats.qdep;
1045 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001046
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001047 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001048
1049 return iommu_queue_command(iommu, &cmd);
1050}
1051
1052/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001053 * Command send function for invalidating a device table entry
1054 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001055static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001056{
1057 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001058 u16 alias;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001059 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001060
Joerg Roedel6c542042011-06-09 17:07:31 +02001061 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele25bfb52015-10-20 17:33:38 +02001062 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001063
Joerg Roedelf62dda62011-06-09 12:55:35 +02001064 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001065 if (!ret && alias != dev_data->devid)
1066 ret = iommu_flush_dte(iommu, alias);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001067 if (ret)
1068 return ret;
1069
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001070 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001071 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001072
1073 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001074}
1075
Joerg Roedel431b2a22008-07-11 17:14:22 +02001076/*
1077 * TLB invalidation function which is called from the mapping functions.
1078 * It invalidates a single PTE if the range to flush is within a single
1079 * page. Otherwise it flushes the whole TLB of the IOMMU.
1080 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001081static void __domain_flush_pages(struct protection_domain *domain,
1082 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001083{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001084 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001085 struct iommu_cmd cmd;
1086 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001087
Joerg Roedel11b64022011-04-06 11:49:28 +02001088 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001089
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001090 for (i = 0; i < amd_iommus_present; ++i) {
1091 if (!domain->dev_iommu[i])
1092 continue;
1093
1094 /*
1095 * Devices of this domain are behind this IOMMU
1096 * We need a TLB flush
1097 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001098 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001099 }
1100
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001101 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001102
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001103 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001104 continue;
1105
Joerg Roedel6c542042011-06-09 17:07:31 +02001106 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001107 }
1108
Joerg Roedel11b64022011-04-06 11:49:28 +02001109 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001110}
1111
Joerg Roedel17b124b2011-04-06 18:01:35 +02001112static void domain_flush_pages(struct protection_domain *domain,
1113 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001114{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001115 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001116}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001117
Joerg Roedel1c655772008-09-04 18:40:05 +02001118/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001119static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001120{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001121 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001122}
1123
Chris Wright42a49f92009-06-15 15:42:00 +02001124/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001125static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001126{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001127 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1128}
1129
1130static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001131{
1132 int i;
1133
1134 for (i = 0; i < amd_iommus_present; ++i) {
1135 if (!domain->dev_iommu[i])
1136 continue;
1137
1138 /*
1139 * Devices of this domain are behind this IOMMU
1140 * We need to wait for completion of all commands.
1141 */
1142 iommu_completion_wait(amd_iommus[i]);
1143 }
1144}
1145
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001146
Joerg Roedel43f49602008-12-02 21:01:12 +01001147/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001148 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001149 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001150static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001151{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001152 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001153
1154 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001155 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001156}
1157
Joerg Roedel431b2a22008-07-11 17:14:22 +02001158/****************************************************************************
1159 *
1160 * The functions below are used the create the page table mappings for
1161 * unity mapped regions.
1162 *
1163 ****************************************************************************/
1164
1165/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001166 * This function is used to add another level to an IO page table. Adding
1167 * another level increases the size of the address space by 9 bits to a size up
1168 * to 64 bits.
1169 */
1170static bool increase_address_space(struct protection_domain *domain,
1171 gfp_t gfp)
1172{
1173 u64 *pte;
1174
1175 if (domain->mode == PAGE_MODE_6_LEVEL)
1176 /* address space already 64 bit large */
1177 return false;
1178
1179 pte = (void *)get_zeroed_page(gfp);
1180 if (!pte)
1181 return false;
1182
1183 *pte = PM_LEVEL_PDE(domain->mode,
1184 virt_to_phys(domain->pt_root));
1185 domain->pt_root = pte;
1186 domain->mode += 1;
1187 domain->updated = true;
1188
1189 return true;
1190}
1191
1192static u64 *alloc_pte(struct protection_domain *domain,
1193 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001194 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001195 u64 **pte_page,
1196 gfp_t gfp)
1197{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001198 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001199 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001200
1201 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001202
1203 while (address > PM_LEVEL_SIZE(domain->mode))
1204 increase_address_space(domain, gfp);
1205
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001206 level = domain->mode - 1;
1207 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1208 address = PAGE_SIZE_ALIGN(address, page_size);
1209 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001210
1211 while (level > end_lvl) {
1212 if (!IOMMU_PTE_PRESENT(*pte)) {
1213 page = (u64 *)get_zeroed_page(gfp);
1214 if (!page)
1215 return NULL;
1216 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1217 }
1218
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001219 /* No level skipping support yet */
1220 if (PM_PTE_LEVEL(*pte) != level)
1221 return NULL;
1222
Joerg Roedel308973d2009-11-24 17:43:32 +01001223 level -= 1;
1224
1225 pte = IOMMU_PTE_PAGE(*pte);
1226
1227 if (pte_page && level == end_lvl)
1228 *pte_page = pte;
1229
1230 pte = &pte[PM_LEVEL_INDEX(level, address)];
1231 }
1232
1233 return pte;
1234}
1235
1236/*
1237 * This function checks if there is a PTE for a given dma address. If
1238 * there is one, it returns the pointer to it.
1239 */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001240static u64 *fetch_pte(struct protection_domain *domain,
1241 unsigned long address,
1242 unsigned long *page_size)
Joerg Roedel308973d2009-11-24 17:43:32 +01001243{
1244 int level;
1245 u64 *pte;
1246
Joerg Roedel24cd7722010-01-19 17:27:39 +01001247 if (address > PM_LEVEL_SIZE(domain->mode))
1248 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001249
Joerg Roedel3039ca12015-04-01 14:58:48 +02001250 level = domain->mode - 1;
1251 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1252 *page_size = PTE_LEVEL_PAGE_SIZE(level);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001253
1254 while (level > 0) {
1255
1256 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001257 if (!IOMMU_PTE_PRESENT(*pte))
1258 return NULL;
1259
Joerg Roedel24cd7722010-01-19 17:27:39 +01001260 /* Large PTE */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001261 if (PM_PTE_LEVEL(*pte) == 7 ||
1262 PM_PTE_LEVEL(*pte) == 0)
1263 break;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001264
1265 /* No level skipping support yet */
1266 if (PM_PTE_LEVEL(*pte) != level)
1267 return NULL;
1268
Joerg Roedel308973d2009-11-24 17:43:32 +01001269 level -= 1;
1270
Joerg Roedel24cd7722010-01-19 17:27:39 +01001271 /* Walk to the next level */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001272 pte = IOMMU_PTE_PAGE(*pte);
1273 pte = &pte[PM_LEVEL_INDEX(level, address)];
1274 *page_size = PTE_LEVEL_PAGE_SIZE(level);
1275 }
1276
1277 if (PM_PTE_LEVEL(*pte) == 0x07) {
1278 unsigned long pte_mask;
1279
1280 /*
1281 * If we have a series of large PTEs, make
1282 * sure to return a pointer to the first one.
1283 */
1284 *page_size = pte_mask = PTE_PAGE_SIZE(*pte);
1285 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1286 pte = (u64 *)(((unsigned long)pte) & pte_mask);
Joerg Roedel308973d2009-11-24 17:43:32 +01001287 }
1288
1289 return pte;
1290}
1291
1292/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001293 * Generic mapping functions. It maps a physical address into a DMA
1294 * address space. It allocates the page table pages if necessary.
1295 * In the future it can be extended to a generic mapping function
1296 * supporting all features of AMD IOMMU page tables like level skipping
1297 * and full 64 bit address spaces.
1298 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001299static int iommu_map_page(struct protection_domain *dom,
1300 unsigned long bus_addr,
1301 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001302 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001303 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001304{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001305 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001306 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001307
Joerg Roedeld4b03662015-04-01 14:58:52 +02001308 BUG_ON(!IS_ALIGNED(bus_addr, page_size));
1309 BUG_ON(!IS_ALIGNED(phys_addr, page_size));
1310
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001311 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001312 return -EINVAL;
1313
Joerg Roedeld4b03662015-04-01 14:58:52 +02001314 count = PAGE_SIZE_PTE_COUNT(page_size);
1315 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001316
Maurizio Lombardi63eaa752014-09-11 12:28:03 +02001317 if (!pte)
1318 return -ENOMEM;
1319
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001320 for (i = 0; i < count; ++i)
1321 if (IOMMU_PTE_PRESENT(pte[i]))
1322 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001323
Joerg Roedeld4b03662015-04-01 14:58:52 +02001324 if (count > 1) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001325 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1326 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1327 } else
1328 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1329
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001330 if (prot & IOMMU_PROT_IR)
1331 __pte |= IOMMU_PTE_IR;
1332 if (prot & IOMMU_PROT_IW)
1333 __pte |= IOMMU_PTE_IW;
1334
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001335 for (i = 0; i < count; ++i)
1336 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001337
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001338 update_domain(dom);
1339
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001340 return 0;
1341}
1342
Joerg Roedel24cd7722010-01-19 17:27:39 +01001343static unsigned long iommu_unmap_page(struct protection_domain *dom,
1344 unsigned long bus_addr,
1345 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001346{
Joerg Roedel71b390e2015-04-01 14:58:49 +02001347 unsigned long long unmapped;
1348 unsigned long unmap_size;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001349 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001350
Joerg Roedel24cd7722010-01-19 17:27:39 +01001351 BUG_ON(!is_power_of_2(page_size));
1352
1353 unmapped = 0;
1354
1355 while (unmapped < page_size) {
1356
Joerg Roedel71b390e2015-04-01 14:58:49 +02001357 pte = fetch_pte(dom, bus_addr, &unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001358
Joerg Roedel71b390e2015-04-01 14:58:49 +02001359 if (pte) {
1360 int i, count;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001361
Joerg Roedel71b390e2015-04-01 14:58:49 +02001362 count = PAGE_SIZE_PTE_COUNT(unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001363 for (i = 0; i < count; i++)
1364 pte[i] = 0ULL;
1365 }
1366
1367 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1368 unmapped += unmap_size;
1369 }
1370
Alex Williamson60d0ca32013-06-21 14:33:19 -06001371 BUG_ON(unmapped && !is_power_of_2(unmapped));
Joerg Roedel24cd7722010-01-19 17:27:39 +01001372
1373 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001374}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001375
Joerg Roedel431b2a22008-07-11 17:14:22 +02001376/****************************************************************************
1377 *
1378 * The next functions belong to the address allocator for the dma_ops
1379 * interface functions. They work like the allocators in the other IOMMU
1380 * drivers. Its basically a bitmap which marks the allocated pages in
1381 * the aperture. Maybe it could be enhanced in the future to a more
1382 * efficient allocator.
1383 *
1384 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001385
Joerg Roedel431b2a22008-07-11 17:14:22 +02001386/*
Joerg Roedel384de722009-05-15 12:30:05 +02001387 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001388 *
1389 * called with domain->lock held
1390 */
Joerg Roedel384de722009-05-15 12:30:05 +02001391
Joerg Roedel9cabe892009-05-18 16:38:55 +02001392/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001393 * Used to reserve address ranges in the aperture (e.g. for exclusion
1394 * ranges.
1395 */
1396static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1397 unsigned long start_page,
1398 unsigned int pages)
1399{
1400 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1401
1402 if (start_page + pages > last_page)
1403 pages = last_page - start_page;
1404
1405 for (i = start_page; i < start_page + pages; ++i) {
1406 int index = i / APERTURE_RANGE_PAGES;
1407 int page = i % APERTURE_RANGE_PAGES;
1408 __set_bit(page, dom->aperture[index]->bitmap);
1409 }
1410}
1411
1412/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001413 * This function is used to add a new aperture range to an existing
1414 * aperture in case of dma_ops domain allocation or address allocation
1415 * failure.
1416 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001417static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001418 bool populate, gfp_t gfp)
1419{
1420 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001421 struct amd_iommu *iommu;
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001422 unsigned long i, old_size, pte_pgsize;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001423
Joerg Roedelf5e97052009-05-22 12:31:53 +02001424#ifdef CONFIG_IOMMU_STRESS
1425 populate = false;
1426#endif
1427
Joerg Roedel9cabe892009-05-18 16:38:55 +02001428 if (index >= APERTURE_MAX_RANGES)
1429 return -ENOMEM;
1430
1431 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1432 if (!dma_dom->aperture[index])
1433 return -ENOMEM;
1434
1435 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1436 if (!dma_dom->aperture[index]->bitmap)
1437 goto out_free;
1438
1439 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1440
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001441 spin_lock_init(&dma_dom->aperture[index]->bitmap_lock);
1442
Joerg Roedel9cabe892009-05-18 16:38:55 +02001443 if (populate) {
1444 unsigned long address = dma_dom->aperture_size;
1445 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1446 u64 *pte, *pte_page;
1447
1448 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001449 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001450 &pte_page, gfp);
1451 if (!pte)
1452 goto out_free;
1453
1454 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1455
1456 address += APERTURE_RANGE_SIZE / 64;
1457 }
1458 }
1459
Joerg Roedel17f5b562011-07-06 17:14:44 +02001460 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001461 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1462
Joerg Roedel17f5b562011-07-06 17:14:44 +02001463 /* Reserve address range used for MSI messages */
1464 if (old_size < MSI_ADDR_BASE_LO &&
1465 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1466 unsigned long spage;
1467 int pages;
1468
1469 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1470 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1471
1472 dma_ops_reserve_addresses(dma_dom, spage, pages);
1473 }
1474
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001475 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001476 for_each_iommu(iommu) {
1477 if (iommu->exclusion_start &&
1478 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1479 && iommu->exclusion_start < dma_dom->aperture_size) {
1480 unsigned long startpage;
1481 int pages = iommu_num_pages(iommu->exclusion_start,
1482 iommu->exclusion_length,
1483 PAGE_SIZE);
1484 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1485 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1486 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001487 }
1488
1489 /*
1490 * Check for areas already mapped as present in the new aperture
1491 * range and mark those pages as reserved in the allocator. Such
1492 * mappings may already exist as a result of requested unity
1493 * mappings for devices.
1494 */
1495 for (i = dma_dom->aperture[index]->offset;
1496 i < dma_dom->aperture_size;
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001497 i += pte_pgsize) {
Joerg Roedel3039ca12015-04-01 14:58:48 +02001498 u64 *pte = fetch_pte(&dma_dom->domain, i, &pte_pgsize);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001499 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1500 continue;
1501
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001502 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT,
1503 pte_pgsize >> 12);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001504 }
1505
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001506 update_domain(&dma_dom->domain);
1507
Joerg Roedel9cabe892009-05-18 16:38:55 +02001508 return 0;
1509
1510out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001511 update_domain(&dma_dom->domain);
1512
Joerg Roedel9cabe892009-05-18 16:38:55 +02001513 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1514
1515 kfree(dma_dom->aperture[index]);
1516 dma_dom->aperture[index] = NULL;
1517
1518 return -ENOMEM;
1519}
1520
Joerg Roedelccb50e02015-12-21 17:49:34 +01001521static dma_addr_t dma_ops_aperture_alloc(struct dma_ops_domain *dom,
1522 struct aperture_range *range,
Joerg Roedela0f51442015-12-21 16:20:09 +01001523 unsigned long pages,
Joerg Roedela0f51442015-12-21 16:20:09 +01001524 unsigned long dma_mask,
1525 unsigned long boundary_size,
1526 unsigned long align_mask)
1527{
1528 unsigned long offset, limit, flags;
1529 dma_addr_t address;
Joerg Roedelccb50e02015-12-21 17:49:34 +01001530 bool flush = false;
Joerg Roedela0f51442015-12-21 16:20:09 +01001531
1532 offset = range->offset >> PAGE_SHIFT;
1533 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1534 dma_mask >> PAGE_SHIFT);
1535
1536 spin_lock_irqsave(&range->bitmap_lock, flags);
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001537 address = iommu_area_alloc(range->bitmap, limit, range->next_bit,
1538 pages, offset, boundary_size, align_mask);
Joerg Roedelccb50e02015-12-21 17:49:34 +01001539 if (address == -1) {
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001540 /* Nothing found, retry one time */
1541 address = iommu_area_alloc(range->bitmap, limit,
1542 0, pages, offset, boundary_size,
1543 align_mask);
Joerg Roedelccb50e02015-12-21 17:49:34 +01001544 flush = true;
1545 }
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001546
1547 if (address != -1)
1548 range->next_bit = address + pages;
1549
Joerg Roedela0f51442015-12-21 16:20:09 +01001550 spin_unlock_irqrestore(&range->bitmap_lock, flags);
1551
Joerg Roedelccb50e02015-12-21 17:49:34 +01001552 if (flush) {
1553 domain_flush_tlb(&dom->domain);
1554 domain_flush_complete(&dom->domain);
1555 }
1556
Joerg Roedela0f51442015-12-21 16:20:09 +01001557 return address;
1558}
1559
Joerg Roedel384de722009-05-15 12:30:05 +02001560static unsigned long dma_ops_area_alloc(struct device *dev,
1561 struct dma_ops_domain *dom,
1562 unsigned int pages,
1563 unsigned long align_mask,
Joerg Roedel05ab49e2015-12-21 17:58:26 +01001564 u64 dma_mask)
Joerg Roedel384de722009-05-15 12:30:05 +02001565{
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001566 unsigned long next_bit, boundary_size, mask;
Joerg Roedel384de722009-05-15 12:30:05 +02001567 unsigned long address = -1;
Joerg Roedel2a874422015-12-21 18:34:47 +01001568 int start = dom->next_index;
1569 int i;
Joerg Roedel384de722009-05-15 12:30:05 +02001570
Joerg Roedele6aabee2015-05-27 09:26:09 +02001571 mask = dma_get_seg_boundary(dev);
1572
1573 boundary_size = mask + 1 ? ALIGN(mask + 1, PAGE_SIZE) >> PAGE_SHIFT :
1574 1UL << (BITS_PER_LONG - PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001575
Joerg Roedel2a874422015-12-21 18:34:47 +01001576 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1577 struct aperture_range *range;
Joerg Roedelccb50e02015-12-21 17:49:34 +01001578
Joerg Roedel2a874422015-12-21 18:34:47 +01001579 range = dom->aperture[(start + i) % APERTURE_MAX_RANGES];
1580
1581 if (!range || range->offset >= dma_mask)
1582 continue;
Joerg Roedel384de722009-05-15 12:30:05 +02001583
Joerg Roedelccb50e02015-12-21 17:49:34 +01001584 next_bit = range->next_bit;
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001585
Joerg Roedel2a874422015-12-21 18:34:47 +01001586 address = dma_ops_aperture_alloc(dom, range, pages,
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001587 dma_mask, boundary_size,
1588 align_mask);
Joerg Roedel384de722009-05-15 12:30:05 +02001589 if (address != -1) {
Joerg Roedel2a874422015-12-21 18:34:47 +01001590 address = range->offset + (address << PAGE_SHIFT);
Joerg Roedelebaecb42015-12-21 18:11:32 +01001591 dom->next_index = i;
Joerg Roedel384de722009-05-15 12:30:05 +02001592 break;
1593 }
1594
Joerg Roedel2a874422015-12-21 18:34:47 +01001595 if (next_bit > range->next_bit)
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001596 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001597 }
1598
1599 return address;
1600}
1601
Joerg Roedeld3086442008-06-26 21:27:57 +02001602static unsigned long dma_ops_alloc_addresses(struct device *dev,
1603 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001604 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001605 unsigned long align_mask,
1606 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001607{
Joerg Roedeld3086442008-06-26 21:27:57 +02001608 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001609
Joerg Roedelfe16f082009-05-22 12:27:53 +02001610#ifdef CONFIG_IOMMU_STRESS
Joerg Roedelebaecb42015-12-21 18:11:32 +01001611 dom->next_index = 0;
Joerg Roedelfe16f082009-05-22 12:27:53 +02001612 dom->need_flush = true;
1613#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001614
Joerg Roedel05ab49e2015-12-21 17:58:26 +01001615 address = dma_ops_area_alloc(dev, dom, pages, align_mask, dma_mask);
Joerg Roedeld3086442008-06-26 21:27:57 +02001616
Joerg Roedel384de722009-05-15 12:30:05 +02001617 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001618 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001619
1620 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1621
1622 return address;
1623}
1624
Joerg Roedel431b2a22008-07-11 17:14:22 +02001625/*
1626 * The address free function.
1627 *
1628 * called with domain->lock held
1629 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001630static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1631 unsigned long address,
1632 unsigned int pages)
1633{
Joerg Roedel384de722009-05-15 12:30:05 +02001634 unsigned i = address >> APERTURE_RANGE_SHIFT;
1635 struct aperture_range *range = dom->aperture[i];
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001636 unsigned long flags;
Joerg Roedel80be3082008-11-06 14:59:05 +01001637
Joerg Roedel384de722009-05-15 12:30:05 +02001638 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1639
Joerg Roedel47bccd62009-05-22 12:40:54 +02001640#ifdef CONFIG_IOMMU_STRESS
1641 if (i < 4)
1642 return;
1643#endif
1644
Joerg Roedeld41ab092015-12-21 18:20:03 +01001645 if (address + pages > range->next_bit) {
1646 domain_flush_tlb(&dom->domain);
1647 domain_flush_complete(&dom->domain);
1648 }
Joerg Roedel384de722009-05-15 12:30:05 +02001649
1650 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001651
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001652 spin_lock_irqsave(&range->bitmap_lock, flags);
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001653 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001654 spin_unlock_irqrestore(&range->bitmap_lock, flags);
Joerg Roedel384de722009-05-15 12:30:05 +02001655
Joerg Roedeld3086442008-06-26 21:27:57 +02001656}
1657
Joerg Roedel431b2a22008-07-11 17:14:22 +02001658/****************************************************************************
1659 *
1660 * The next functions belong to the domain allocation. A domain is
1661 * allocated for every IOMMU as the default domain. If device isolation
1662 * is enabled, every device get its own domain. The most important thing
1663 * about domains is the page table mapping the DMA address space they
1664 * contain.
1665 *
1666 ****************************************************************************/
1667
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001668/*
1669 * This function adds a protection domain to the global protection domain list
1670 */
1671static void add_domain_to_list(struct protection_domain *domain)
1672{
1673 unsigned long flags;
1674
1675 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1676 list_add(&domain->list, &amd_iommu_pd_list);
1677 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1678}
1679
1680/*
1681 * This function removes a protection domain to the global
1682 * protection domain list
1683 */
1684static void del_domain_from_list(struct protection_domain *domain)
1685{
1686 unsigned long flags;
1687
1688 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1689 list_del(&domain->list);
1690 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1691}
1692
Joerg Roedelec487d12008-06-26 21:27:58 +02001693static u16 domain_id_alloc(void)
1694{
1695 unsigned long flags;
1696 int id;
1697
1698 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1699 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1700 BUG_ON(id == 0);
1701 if (id > 0 && id < MAX_DOMAIN_ID)
1702 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1703 else
1704 id = 0;
1705 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1706
1707 return id;
1708}
1709
Joerg Roedela2acfb72008-12-02 18:28:53 +01001710static void domain_id_free(int id)
1711{
1712 unsigned long flags;
1713
1714 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1715 if (id > 0 && id < MAX_DOMAIN_ID)
1716 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1717 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1718}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001719
Joerg Roedel5c34c402013-06-20 20:22:58 +02001720#define DEFINE_FREE_PT_FN(LVL, FN) \
1721static void free_pt_##LVL (unsigned long __pt) \
1722{ \
1723 unsigned long p; \
1724 u64 *pt; \
1725 int i; \
1726 \
1727 pt = (u64 *)__pt; \
1728 \
1729 for (i = 0; i < 512; ++i) { \
Joerg Roedel0b3fff52015-06-18 10:48:34 +02001730 /* PTE present? */ \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001731 if (!IOMMU_PTE_PRESENT(pt[i])) \
1732 continue; \
1733 \
Joerg Roedel0b3fff52015-06-18 10:48:34 +02001734 /* Large PTE? */ \
1735 if (PM_PTE_LEVEL(pt[i]) == 0 || \
1736 PM_PTE_LEVEL(pt[i]) == 7) \
1737 continue; \
1738 \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001739 p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
1740 FN(p); \
1741 } \
1742 free_page((unsigned long)pt); \
1743}
1744
1745DEFINE_FREE_PT_FN(l2, free_page)
1746DEFINE_FREE_PT_FN(l3, free_pt_l2)
1747DEFINE_FREE_PT_FN(l4, free_pt_l3)
1748DEFINE_FREE_PT_FN(l5, free_pt_l4)
1749DEFINE_FREE_PT_FN(l6, free_pt_l5)
1750
Joerg Roedel86db2e52008-12-02 18:20:21 +01001751static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001752{
Joerg Roedel5c34c402013-06-20 20:22:58 +02001753 unsigned long root = (unsigned long)domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001754
Joerg Roedel5c34c402013-06-20 20:22:58 +02001755 switch (domain->mode) {
1756 case PAGE_MODE_NONE:
1757 break;
1758 case PAGE_MODE_1_LEVEL:
1759 free_page(root);
1760 break;
1761 case PAGE_MODE_2_LEVEL:
1762 free_pt_l2(root);
1763 break;
1764 case PAGE_MODE_3_LEVEL:
1765 free_pt_l3(root);
1766 break;
1767 case PAGE_MODE_4_LEVEL:
1768 free_pt_l4(root);
1769 break;
1770 case PAGE_MODE_5_LEVEL:
1771 free_pt_l5(root);
1772 break;
1773 case PAGE_MODE_6_LEVEL:
1774 free_pt_l6(root);
1775 break;
1776 default:
1777 BUG();
Joerg Roedelec487d12008-06-26 21:27:58 +02001778 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001779}
1780
Joerg Roedelb16137b2011-11-21 16:50:23 +01001781static void free_gcr3_tbl_level1(u64 *tbl)
1782{
1783 u64 *ptr;
1784 int i;
1785
1786 for (i = 0; i < 512; ++i) {
1787 if (!(tbl[i] & GCR3_VALID))
1788 continue;
1789
1790 ptr = __va(tbl[i] & PAGE_MASK);
1791
1792 free_page((unsigned long)ptr);
1793 }
1794}
1795
1796static void free_gcr3_tbl_level2(u64 *tbl)
1797{
1798 u64 *ptr;
1799 int i;
1800
1801 for (i = 0; i < 512; ++i) {
1802 if (!(tbl[i] & GCR3_VALID))
1803 continue;
1804
1805 ptr = __va(tbl[i] & PAGE_MASK);
1806
1807 free_gcr3_tbl_level1(ptr);
1808 }
1809}
1810
Joerg Roedel52815b72011-11-17 17:24:28 +01001811static void free_gcr3_table(struct protection_domain *domain)
1812{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001813 if (domain->glx == 2)
1814 free_gcr3_tbl_level2(domain->gcr3_tbl);
1815 else if (domain->glx == 1)
1816 free_gcr3_tbl_level1(domain->gcr3_tbl);
Joerg Roedel23d3a982015-08-13 11:15:13 +02001817 else
1818 BUG_ON(domain->glx != 0);
Joerg Roedelb16137b2011-11-21 16:50:23 +01001819
Joerg Roedel52815b72011-11-17 17:24:28 +01001820 free_page((unsigned long)domain->gcr3_tbl);
1821}
1822
Joerg Roedel431b2a22008-07-11 17:14:22 +02001823/*
1824 * Free a domain, only used if something went wrong in the
1825 * allocation path and we need to free an already allocated page table
1826 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001827static void dma_ops_domain_free(struct dma_ops_domain *dom)
1828{
Joerg Roedel384de722009-05-15 12:30:05 +02001829 int i;
1830
Joerg Roedelec487d12008-06-26 21:27:58 +02001831 if (!dom)
1832 return;
1833
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001834 del_domain_from_list(&dom->domain);
1835
Joerg Roedel86db2e52008-12-02 18:20:21 +01001836 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001837
Joerg Roedel384de722009-05-15 12:30:05 +02001838 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1839 if (!dom->aperture[i])
1840 continue;
1841 free_page((unsigned long)dom->aperture[i]->bitmap);
1842 kfree(dom->aperture[i]);
1843 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001844
1845 kfree(dom);
1846}
1847
Joerg Roedel431b2a22008-07-11 17:14:22 +02001848/*
1849 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001850 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001851 * structures required for the dma_ops interface
1852 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001853static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001854{
1855 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001856
1857 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1858 if (!dma_dom)
1859 return NULL;
1860
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001861 if (protection_domain_init(&dma_dom->domain))
Joerg Roedelec487d12008-06-26 21:27:58 +02001862 goto free_dma_dom;
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001863
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001864 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001865 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001866 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001867 dma_dom->domain.priv = dma_dom;
1868 if (!dma_dom->domain.pt_root)
1869 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001870
Joerg Roedel1c655772008-09-04 18:40:05 +02001871 dma_dom->need_flush = false;
1872
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001873 add_domain_to_list(&dma_dom->domain);
1874
Joerg Roedel576175c2009-11-23 19:08:46 +01001875 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001876 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001877
Joerg Roedel431b2a22008-07-11 17:14:22 +02001878 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001879 * mark the first page as allocated so we never return 0 as
1880 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001881 */
Joerg Roedel384de722009-05-15 12:30:05 +02001882 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedelebaecb42015-12-21 18:11:32 +01001883 dma_dom->next_index = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001884
Joerg Roedelec487d12008-06-26 21:27:58 +02001885
1886 return dma_dom;
1887
1888free_dma_dom:
1889 dma_ops_domain_free(dma_dom);
1890
1891 return NULL;
1892}
1893
Joerg Roedel431b2a22008-07-11 17:14:22 +02001894/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001895 * little helper function to check whether a given protection domain is a
1896 * dma_ops domain
1897 */
1898static bool dma_ops_domain(struct protection_domain *domain)
1899{
1900 return domain->flags & PD_DMA_OPS_MASK;
1901}
1902
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001903static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001904{
Joerg Roedel132bd682011-11-17 14:18:46 +01001905 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001906 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001907
Joerg Roedel132bd682011-11-17 14:18:46 +01001908 if (domain->mode != PAGE_MODE_NONE)
1909 pte_root = virt_to_phys(domain->pt_root);
1910
Joerg Roedel38ddf412008-09-11 10:38:32 +02001911 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1912 << DEV_ENTRY_MODE_SHIFT;
1913 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001914
Joerg Roedelee6c2862011-11-09 12:06:03 +01001915 flags = amd_iommu_dev_table[devid].data[1];
1916
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001917 if (ats)
1918 flags |= DTE_FLAG_IOTLB;
1919
Joerg Roedel52815b72011-11-17 17:24:28 +01001920 if (domain->flags & PD_IOMMUV2_MASK) {
1921 u64 gcr3 = __pa(domain->gcr3_tbl);
1922 u64 glx = domain->glx;
1923 u64 tmp;
1924
1925 pte_root |= DTE_FLAG_GV;
1926 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1927
1928 /* First mask out possible old values for GCR3 table */
1929 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1930 flags &= ~tmp;
1931
1932 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1933 flags &= ~tmp;
1934
1935 /* Encode GCR3 table into DTE */
1936 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1937 pte_root |= tmp;
1938
1939 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1940 flags |= tmp;
1941
1942 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1943 flags |= tmp;
1944 }
1945
Joerg Roedelee6c2862011-11-09 12:06:03 +01001946 flags &= ~(0xffffUL);
1947 flags |= domain->id;
1948
1949 amd_iommu_dev_table[devid].data[1] = flags;
1950 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001951}
1952
Joerg Roedel15898bb2009-11-24 15:39:42 +01001953static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001954{
Joerg Roedel355bf552008-12-08 12:02:41 +01001955 /* remove entry from the device table seen by the hardware */
Joerg Roedelcbf3ccd2015-10-20 14:59:36 +02001956 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1957 amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
Joerg Roedel355bf552008-12-08 12:02:41 +01001958
Joerg Roedelc5cca142009-10-09 18:31:20 +02001959 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001960}
1961
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001962static void do_attach(struct iommu_dev_data *dev_data,
1963 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001964{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001965 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001966 u16 alias;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001967 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001968
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001969 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele25bfb52015-10-20 17:33:38 +02001970 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001971 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001972
1973 /* Update data structures */
1974 dev_data->domain = domain;
1975 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001976
1977 /* Do reference counting */
1978 domain->dev_iommu[iommu->index] += 1;
1979 domain->dev_cnt += 1;
1980
Joerg Roedele25bfb52015-10-20 17:33:38 +02001981 /* Update device table */
1982 set_dte_entry(dev_data->devid, domain, ats);
1983 if (alias != dev_data->devid)
1984 set_dte_entry(dev_data->devid, domain, ats);
1985
Joerg Roedel6c542042011-06-09 17:07:31 +02001986 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001987}
1988
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001989static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001990{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001991 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001992 u16 alias;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001993
Joerg Roedel5adad992015-10-09 16:23:33 +02001994 /*
1995 * First check if the device is still attached. It might already
1996 * be detached from its domain because the generic
1997 * iommu_detach_group code detached it and we try again here in
1998 * our alias handling.
1999 */
2000 if (!dev_data->domain)
2001 return;
2002
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002003 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele25bfb52015-10-20 17:33:38 +02002004 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02002005
Joerg Roedelc4596112009-11-20 14:57:32 +01002006 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002007 dev_data->domain->dev_iommu[iommu->index] -= 1;
2008 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01002009
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002010 /* Update data structures */
2011 dev_data->domain = NULL;
2012 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002013 clear_dte_entry(dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02002014 if (alias != dev_data->devid)
2015 clear_dte_entry(alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002016
2017 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002018 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002019}
2020
2021/*
2022 * If a device is not yet associated with a domain, this function does
2023 * assigns it visible for the hardware
2024 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002025static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002026 struct protection_domain *domain)
2027{
Julia Lawall84fe6c12010-05-27 12:31:51 +02002028 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002029
Joerg Roedel272e4f92015-10-20 17:33:37 +02002030 /*
2031 * Must be called with IRQs disabled. Warn here to detect early
2032 * when its not.
2033 */
2034 WARN_ON(!irqs_disabled());
2035
Joerg Roedel15898bb2009-11-24 15:39:42 +01002036 /* lock domain */
2037 spin_lock(&domain->lock);
2038
Joerg Roedel397111a2014-08-05 17:31:51 +02002039 ret = -EBUSY;
Joerg Roedel150952f2015-10-20 17:33:35 +02002040 if (dev_data->domain != NULL)
Joerg Roedel397111a2014-08-05 17:31:51 +02002041 goto out_unlock;
Joerg Roedel24100052009-11-25 15:59:57 +01002042
Joerg Roedel397111a2014-08-05 17:31:51 +02002043 /* Attach alias group root */
Joerg Roedel150952f2015-10-20 17:33:35 +02002044 do_attach(dev_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002045
Julia Lawall84fe6c12010-05-27 12:31:51 +02002046 ret = 0;
2047
2048out_unlock:
2049
Joerg Roedel355bf552008-12-08 12:02:41 +01002050 /* ready */
2051 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002052
Julia Lawall84fe6c12010-05-27 12:31:51 +02002053 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002054}
2055
Joerg Roedel52815b72011-11-17 17:24:28 +01002056
2057static void pdev_iommuv2_disable(struct pci_dev *pdev)
2058{
2059 pci_disable_ats(pdev);
2060 pci_disable_pri(pdev);
2061 pci_disable_pasid(pdev);
2062}
2063
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002064/* FIXME: Change generic reset-function to do the same */
2065static int pri_reset_while_enabled(struct pci_dev *pdev)
2066{
2067 u16 control;
2068 int pos;
2069
Joerg Roedel46277b72011-12-07 14:34:02 +01002070 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002071 if (!pos)
2072 return -EINVAL;
2073
Joerg Roedel46277b72011-12-07 14:34:02 +01002074 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2075 control |= PCI_PRI_CTRL_RESET;
2076 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002077
2078 return 0;
2079}
2080
Joerg Roedel52815b72011-11-17 17:24:28 +01002081static int pdev_iommuv2_enable(struct pci_dev *pdev)
2082{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002083 bool reset_enable;
2084 int reqs, ret;
2085
2086 /* FIXME: Hardcode number of outstanding requests for now */
2087 reqs = 32;
2088 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2089 reqs = 1;
2090 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002091
2092 /* Only allow access to user-accessible pages */
2093 ret = pci_enable_pasid(pdev, 0);
2094 if (ret)
2095 goto out_err;
2096
2097 /* First reset the PRI state of the device */
2098 ret = pci_reset_pri(pdev);
2099 if (ret)
2100 goto out_err;
2101
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002102 /* Enable PRI */
2103 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002104 if (ret)
2105 goto out_err;
2106
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002107 if (reset_enable) {
2108 ret = pri_reset_while_enabled(pdev);
2109 if (ret)
2110 goto out_err;
2111 }
2112
Joerg Roedel52815b72011-11-17 17:24:28 +01002113 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2114 if (ret)
2115 goto out_err;
2116
2117 return 0;
2118
2119out_err:
2120 pci_disable_pri(pdev);
2121 pci_disable_pasid(pdev);
2122
2123 return ret;
2124}
2125
Joerg Roedelc99afa22011-11-21 18:19:25 +01002126/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002127#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002128
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002129static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002130{
Joerg Roedela3b93122012-04-12 12:49:26 +02002131 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002132 int pos;
2133
Joerg Roedel46277b72011-12-07 14:34:02 +01002134 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002135 if (!pos)
2136 return false;
2137
Joerg Roedela3b93122012-04-12 12:49:26 +02002138 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002139
Joerg Roedela3b93122012-04-12 12:49:26 +02002140 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002141}
2142
Joerg Roedel15898bb2009-11-24 15:39:42 +01002143/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02002144 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01002145 * assigns it visible for the hardware
2146 */
2147static int attach_device(struct device *dev,
2148 struct protection_domain *domain)
2149{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002150 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002151 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002152 unsigned long flags;
2153 int ret;
2154
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002155 dev_data = get_dev_data(dev);
2156
Joerg Roedel52815b72011-11-17 17:24:28 +01002157 if (domain->flags & PD_IOMMUV2_MASK) {
Joerg Roedel02ca2022015-07-28 16:58:49 +02002158 if (!dev_data->passthrough)
Joerg Roedel52815b72011-11-17 17:24:28 +01002159 return -EINVAL;
2160
Joerg Roedel02ca2022015-07-28 16:58:49 +02002161 if (dev_data->iommu_v2) {
2162 if (pdev_iommuv2_enable(pdev) != 0)
2163 return -EINVAL;
Joerg Roedel52815b72011-11-17 17:24:28 +01002164
Joerg Roedel02ca2022015-07-28 16:58:49 +02002165 dev_data->ats.enabled = true;
2166 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2167 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
2168 }
Joerg Roedel52815b72011-11-17 17:24:28 +01002169 } else if (amd_iommu_iotlb_sup &&
2170 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002171 dev_data->ats.enabled = true;
2172 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2173 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002174
Joerg Roedel15898bb2009-11-24 15:39:42 +01002175 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002176 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002177 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2178
2179 /*
2180 * We might boot into a crash-kernel here. The crashed kernel
2181 * left the caches in the IOMMU dirty. So we have to flush
2182 * here to evict all dirty stuff.
2183 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002184 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002185
2186 return ret;
2187}
2188
2189/*
2190 * Removes a device from a protection domain (unlocked)
2191 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002192static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002193{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002194 struct protection_domain *domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002195
Joerg Roedel272e4f92015-10-20 17:33:37 +02002196 /*
2197 * Must be called with IRQs disabled. Warn here to detect early
2198 * when its not.
2199 */
2200 WARN_ON(!irqs_disabled());
2201
Joerg Roedelf34c73f2015-10-20 17:33:34 +02002202 if (WARN_ON(!dev_data->domain))
2203 return;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002204
Joerg Roedel2ca76272010-01-22 16:45:31 +01002205 domain = dev_data->domain;
2206
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002207 spin_lock(&domain->lock);
Joerg Roedel24100052009-11-25 15:59:57 +01002208
Joerg Roedel150952f2015-10-20 17:33:35 +02002209 do_detach(dev_data);
Joerg Roedel71f77582011-06-09 19:03:15 +02002210
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002211 spin_unlock(&domain->lock);
Joerg Roedel355bf552008-12-08 12:02:41 +01002212}
2213
2214/*
2215 * Removes a device from a protection domain (with devtable_lock held)
2216 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002217static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002218{
Joerg Roedel52815b72011-11-17 17:24:28 +01002219 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002220 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002221 unsigned long flags;
2222
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002223 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002224 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002225
Joerg Roedel355bf552008-12-08 12:02:41 +01002226 /* lock device table */
2227 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002228 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002229 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002230
Joerg Roedel02ca2022015-07-28 16:58:49 +02002231 if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
Joerg Roedel52815b72011-11-17 17:24:28 +01002232 pdev_iommuv2_disable(to_pci_dev(dev));
2233 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002234 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002235
2236 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002237}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002238
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002239static int amd_iommu_add_device(struct device *dev)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002240{
Joerg Roedel71f77582011-06-09 19:03:15 +02002241 struct iommu_dev_data *dev_data;
Joerg Roedel07ee8692015-05-28 18:41:42 +02002242 struct iommu_domain *domain;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002243 struct amd_iommu *iommu;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002244 u16 devid;
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002245 int ret;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002246
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002247 if (!check_device(dev) || get_dev_data(dev))
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002248 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002249
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002250 devid = get_device_id(dev);
2251 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01002252
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002253 ret = iommu_init_device(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002254 if (ret) {
2255 if (ret != -ENOTSUPP)
2256 pr_err("Failed to initialize device %s - trying to proceed anyway\n",
2257 dev_name(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +01002258
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002259 iommu_ignore_device(dev);
Joerg Roedel343e9ca2015-05-28 18:41:43 +02002260 dev->archdata.dma_ops = &nommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002261 goto out;
2262 }
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002263 init_iommu_group(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002264
Joerg Roedel07ee8692015-05-28 18:41:42 +02002265 dev_data = get_dev_data(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002266
2267 BUG_ON(!dev_data);
2268
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002269 if (iommu_pass_through || dev_data->iommu_v2)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002270 iommu_request_dm_for_dev(dev);
2271
2272 /* Domains are initialized for this device - have a look what we ended up with */
2273 domain = iommu_get_domain_for_dev(dev);
Joerg Roedel32302322015-07-28 16:58:50 +02002274 if (domain->type == IOMMU_DOMAIN_IDENTITY)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002275 dev_data->passthrough = true;
Joerg Roedel32302322015-07-28 16:58:50 +02002276 else
Joerg Roedel07ee8692015-05-28 18:41:42 +02002277 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002278
2279out:
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002280 iommu_completion_wait(iommu);
2281
Joerg Roedele275a2a2008-12-10 18:27:25 +01002282 return 0;
2283}
2284
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002285static void amd_iommu_remove_device(struct device *dev)
Joerg Roedel8638c492009-12-10 11:12:25 +01002286{
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002287 struct amd_iommu *iommu;
2288 u16 devid;
2289
2290 if (!check_device(dev))
2291 return;
2292
2293 devid = get_device_id(dev);
2294 iommu = amd_iommu_rlookup_table[devid];
2295
2296 iommu_uninit_device(dev);
2297 iommu_completion_wait(iommu);
Joerg Roedel8638c492009-12-10 11:12:25 +01002298}
2299
Joerg Roedel431b2a22008-07-11 17:14:22 +02002300/*****************************************************************************
2301 *
2302 * The next functions belong to the dma_ops mapping/unmapping code.
2303 *
2304 *****************************************************************************/
2305
2306/*
2307 * In the dma_ops path we only have the struct device. This function
2308 * finds the corresponding IOMMU, the protection domain and the
2309 * requestor id for a given device.
2310 * If the device is not yet associated with a domain this is also done
2311 * in this function.
2312 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002313static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002314{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002315 struct protection_domain *domain;
Joerg Roedel063071d2015-05-28 18:41:38 +02002316 struct iommu_domain *io_domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002317
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002318 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002319 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002320
Joerg Roedel063071d2015-05-28 18:41:38 +02002321 io_domain = iommu_get_domain_for_dev(dev);
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002322 if (!io_domain)
2323 return NULL;
Joerg Roedel063071d2015-05-28 18:41:38 +02002324
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002325 domain = to_pdomain(io_domain);
2326 if (!dma_ops_domain(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002327 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002328
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002329 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002330}
2331
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002332static void update_device_table(struct protection_domain *domain)
2333{
Joerg Roedel492667d2009-11-27 13:25:47 +01002334 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002335
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002336 list_for_each_entry(dev_data, &domain->dev_list, list)
2337 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002338}
2339
2340static void update_domain(struct protection_domain *domain)
2341{
2342 if (!domain->updated)
2343 return;
2344
2345 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002346
2347 domain_flush_devices(domain);
2348 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002349
2350 domain->updated = false;
2351}
2352
Joerg Roedel431b2a22008-07-11 17:14:22 +02002353/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002354 * This function fetches the PTE for a given address in the aperture
2355 */
2356static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2357 unsigned long address)
2358{
Joerg Roedel384de722009-05-15 12:30:05 +02002359 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002360 u64 *pte, *pte_page;
2361
Joerg Roedel384de722009-05-15 12:30:05 +02002362 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2363 if (!aperture)
2364 return NULL;
2365
2366 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002367 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002368 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002369 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002370 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2371 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002372 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002373
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002374 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002375
2376 return pte;
2377}
2378
2379/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002380 * This is the generic map function. It maps one 4kb page at paddr to
2381 * the given address in the DMA address space for the domain.
2382 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002383static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002384 unsigned long address,
2385 phys_addr_t paddr,
2386 int direction)
2387{
2388 u64 *pte, __pte;
2389
2390 WARN_ON(address > dom->aperture_size);
2391
2392 paddr &= PAGE_MASK;
2393
Joerg Roedel8bda3092009-05-12 12:02:46 +02002394 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002395 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002396 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002397
2398 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2399
2400 if (direction == DMA_TO_DEVICE)
2401 __pte |= IOMMU_PTE_IR;
2402 else if (direction == DMA_FROM_DEVICE)
2403 __pte |= IOMMU_PTE_IW;
2404 else if (direction == DMA_BIDIRECTIONAL)
2405 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2406
Joerg Roedela7fb6682015-12-21 12:50:54 +01002407 WARN_ON_ONCE(*pte);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002408
2409 *pte = __pte;
2410
2411 return (dma_addr_t)address;
2412}
2413
Joerg Roedel431b2a22008-07-11 17:14:22 +02002414/*
2415 * The generic unmapping function for on page in the DMA address space.
2416 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002417static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002418 unsigned long address)
2419{
Joerg Roedel384de722009-05-15 12:30:05 +02002420 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002421 u64 *pte;
2422
2423 if (address >= dom->aperture_size)
2424 return;
2425
Joerg Roedel384de722009-05-15 12:30:05 +02002426 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2427 if (!aperture)
2428 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002429
Joerg Roedel384de722009-05-15 12:30:05 +02002430 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2431 if (!pte)
2432 return;
2433
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002434 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002435
Joerg Roedela7fb6682015-12-21 12:50:54 +01002436 WARN_ON_ONCE(!*pte);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002437
2438 *pte = 0ULL;
2439}
2440
Joerg Roedel431b2a22008-07-11 17:14:22 +02002441/*
2442 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002443 * contiguous memory region into DMA address space. It is used by all
2444 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002445 * Must be called with the domain lock held.
2446 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002447static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002448 struct dma_ops_domain *dma_dom,
2449 phys_addr_t paddr,
2450 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002451 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002452 bool align,
2453 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002454{
2455 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002456 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002457 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002458 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002459 int i;
2460
Joerg Roedele3c449f2008-10-15 22:02:11 -07002461 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002462 paddr &= PAGE_MASK;
2463
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002464 INC_STATS_COUNTER(total_map_requests);
2465
Joerg Roedelc1858972008-12-12 15:42:39 +01002466 if (pages > 1)
2467 INC_STATS_COUNTER(cross_page);
2468
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002469 if (align)
2470 align_mask = (1UL << get_order(size)) - 1;
2471
Joerg Roedel11b83882009-05-19 10:23:15 +02002472retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002473 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2474 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002475 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedelebaecb42015-12-21 18:11:32 +01002476 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
2477 goto out;
2478
Joerg Roedel11b83882009-05-19 10:23:15 +02002479 /*
Joerg Roedelebaecb42015-12-21 18:11:32 +01002480 * setting next_index here will let the address
Joerg Roedel11b83882009-05-19 10:23:15 +02002481 * allocator only scan the new allocated range in the
2482 * first run. This is a small optimization.
2483 */
Joerg Roedelebaecb42015-12-21 18:11:32 +01002484 dma_dom->next_index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel11b83882009-05-19 10:23:15 +02002485
2486 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002487 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002488 * allocation again
2489 */
2490 goto retry;
2491 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002492
2493 start = address;
2494 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002495 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002496 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002497 goto out_unmap;
2498
Joerg Roedelcb76c322008-06-26 21:28:00 +02002499 paddr += PAGE_SIZE;
2500 start += PAGE_SIZE;
2501 }
2502 address += offset;
2503
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002504 ADD_STATS_COUNTER(alloced_io_mem, size);
2505
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002506 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002507 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002508 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002509 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002510 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002511
Joerg Roedelcb76c322008-06-26 21:28:00 +02002512out:
2513 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002514
2515out_unmap:
2516
2517 for (--i; i >= 0; --i) {
2518 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002519 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002520 }
2521
Joerg Roedel53b3b652015-12-21 13:14:52 +01002522 domain_flush_pages(&dma_dom->domain, address, size);
2523
Joerg Roedel53812c12009-05-12 12:17:38 +02002524 dma_ops_free_addresses(dma_dom, address, pages);
2525
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002526 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002527}
2528
Joerg Roedel431b2a22008-07-11 17:14:22 +02002529/*
2530 * Does the reverse of the __map_single function. Must be called with
2531 * the domain lock held too
2532 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002533static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002534 dma_addr_t dma_addr,
2535 size_t size,
2536 int dir)
2537{
Joerg Roedel04e04632010-09-23 16:12:48 +02002538 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002539 dma_addr_t i, start;
2540 unsigned int pages;
2541
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002542 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002543 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002544 return;
2545
Joerg Roedel04e04632010-09-23 16:12:48 +02002546 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002547 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002548 dma_addr &= PAGE_MASK;
2549 start = dma_addr;
2550
2551 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002552 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002553 start += PAGE_SIZE;
2554 }
2555
Joerg Roedel80be3082008-11-06 14:59:05 +01002556 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002557 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002558 dma_dom->need_flush = false;
2559 }
Joerg Roedel84b3a0b2015-12-21 13:23:59 +01002560
2561 SUB_STATS_COUNTER(alloced_io_mem, size);
2562
2563 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002564}
2565
Joerg Roedel431b2a22008-07-11 17:14:22 +02002566/*
2567 * The exported map_single function for dma_ops.
2568 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002569static dma_addr_t map_page(struct device *dev, struct page *page,
2570 unsigned long offset, size_t size,
2571 enum dma_data_direction dir,
2572 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002573{
2574 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002575 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002576 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002577 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002578 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002579
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002580 INC_STATS_COUNTER(cnt_map_single);
2581
Joerg Roedel94f6d192009-11-24 16:40:02 +01002582 domain = get_domain(dev);
2583 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002584 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002585 else if (IS_ERR(domain))
2586 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002587
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002588 dma_mask = *dev->dma_mask;
2589
Joerg Roedel4da70b92008-06-26 21:28:01 +02002590 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002591
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002592 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002593 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002594 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002595 goto out;
2596
Joerg Roedel17b124b2011-04-06 18:01:35 +02002597 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002598
2599out:
2600 spin_unlock_irqrestore(&domain->lock, flags);
2601
2602 return addr;
2603}
2604
Joerg Roedel431b2a22008-07-11 17:14:22 +02002605/*
2606 * The exported unmap_single function for dma_ops.
2607 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002608static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2609 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002610{
2611 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002612 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002613
Joerg Roedel146a6912008-12-12 15:07:12 +01002614 INC_STATS_COUNTER(cnt_unmap_single);
2615
Joerg Roedel94f6d192009-11-24 16:40:02 +01002616 domain = get_domain(dev);
2617 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002618 return;
2619
Joerg Roedel4da70b92008-06-26 21:28:01 +02002620 spin_lock_irqsave(&domain->lock, flags);
2621
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002622 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002623
Joerg Roedel17b124b2011-04-06 18:01:35 +02002624 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002625
2626 spin_unlock_irqrestore(&domain->lock, flags);
2627}
2628
Joerg Roedel431b2a22008-07-11 17:14:22 +02002629/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002630 * The exported map_sg function for dma_ops (handles scatter-gather
2631 * lists).
2632 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002633static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002634 int nelems, enum dma_data_direction dir,
2635 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002636{
2637 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002638 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002639 int i;
2640 struct scatterlist *s;
2641 phys_addr_t paddr;
2642 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002643 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002644
Joerg Roedeld03f0672008-12-12 15:09:48 +01002645 INC_STATS_COUNTER(cnt_map_sg);
2646
Joerg Roedel94f6d192009-11-24 16:40:02 +01002647 domain = get_domain(dev);
Joerg Roedela0e191b2013-04-09 15:04:36 +02002648 if (IS_ERR(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002649 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002650
Joerg Roedel832a90c2008-09-18 15:54:23 +02002651 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002652
Joerg Roedel65b050a2008-06-26 21:28:02 +02002653 spin_lock_irqsave(&domain->lock, flags);
2654
2655 for_each_sg(sglist, s, nelems, i) {
2656 paddr = sg_phys(s);
2657
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002658 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002659 paddr, s->length, dir, false,
2660 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002661
2662 if (s->dma_address) {
2663 s->dma_length = s->length;
2664 mapped_elems++;
2665 } else
2666 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002667 }
2668
Joerg Roedel17b124b2011-04-06 18:01:35 +02002669 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002670
2671out:
2672 spin_unlock_irqrestore(&domain->lock, flags);
2673
2674 return mapped_elems;
2675unmap:
2676 for_each_sg(sglist, s, mapped_elems, i) {
2677 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002678 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002679 s->dma_length, dir);
2680 s->dma_address = s->dma_length = 0;
2681 }
2682
2683 mapped_elems = 0;
2684
2685 goto out;
2686}
2687
Joerg Roedel431b2a22008-07-11 17:14:22 +02002688/*
2689 * The exported map_sg function for dma_ops (handles scatter-gather
2690 * lists).
2691 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002692static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002693 int nelems, enum dma_data_direction dir,
2694 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002695{
2696 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002697 struct protection_domain *domain;
2698 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002699 int i;
2700
Joerg Roedel55877a62008-12-12 15:12:14 +01002701 INC_STATS_COUNTER(cnt_unmap_sg);
2702
Joerg Roedel94f6d192009-11-24 16:40:02 +01002703 domain = get_domain(dev);
2704 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002705 return;
2706
Joerg Roedel65b050a2008-06-26 21:28:02 +02002707 spin_lock_irqsave(&domain->lock, flags);
2708
2709 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002710 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002711 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002712 s->dma_address = s->dma_length = 0;
2713 }
2714
Joerg Roedel17b124b2011-04-06 18:01:35 +02002715 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002716
2717 spin_unlock_irqrestore(&domain->lock, flags);
2718}
2719
Joerg Roedel431b2a22008-07-11 17:14:22 +02002720/*
2721 * The exported alloc_coherent function for dma_ops.
2722 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002723static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002724 dma_addr_t *dma_addr, gfp_t flag,
2725 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002726{
Joerg Roedel832a90c2008-09-18 15:54:23 +02002727 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002728 struct protection_domain *domain;
2729 unsigned long flags;
2730 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002731
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002732 INC_STATS_COUNTER(cnt_alloc_coherent);
2733
Joerg Roedel94f6d192009-11-24 16:40:02 +01002734 domain = get_domain(dev);
2735 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedel3b839a52015-04-01 14:58:47 +02002736 page = alloc_pages(flag, get_order(size));
2737 *dma_addr = page_to_phys(page);
2738 return page_address(page);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002739 } else if (IS_ERR(domain))
2740 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002741
Joerg Roedel3b839a52015-04-01 14:58:47 +02002742 size = PAGE_ALIGN(size);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002743 dma_mask = dev->coherent_dma_mask;
2744 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
Joerg Roedel2d0ec7a2015-06-01 17:30:57 +02002745 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002746
Joerg Roedel3b839a52015-04-01 14:58:47 +02002747 page = alloc_pages(flag | __GFP_NOWARN, get_order(size));
2748 if (!page) {
Mel Gormand0164ad2015-11-06 16:28:21 -08002749 if (!gfpflags_allow_blocking(flag))
Joerg Roedel3b839a52015-04-01 14:58:47 +02002750 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002751
Joerg Roedel3b839a52015-04-01 14:58:47 +02002752 page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
2753 get_order(size));
2754 if (!page)
2755 return NULL;
2756 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002757
Joerg Roedel832a90c2008-09-18 15:54:23 +02002758 if (!dma_mask)
2759 dma_mask = *dev->dma_mask;
2760
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002761 spin_lock_irqsave(&domain->lock, flags);
2762
Joerg Roedel3b839a52015-04-01 14:58:47 +02002763 *dma_addr = __map_single(dev, domain->priv, page_to_phys(page),
Joerg Roedel832a90c2008-09-18 15:54:23 +02002764 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002765
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002766 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002767 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002768 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002769 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002770
Joerg Roedel17b124b2011-04-06 18:01:35 +02002771 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002772
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002773 spin_unlock_irqrestore(&domain->lock, flags);
2774
Joerg Roedel3b839a52015-04-01 14:58:47 +02002775 return page_address(page);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002776
2777out_free:
2778
Joerg Roedel3b839a52015-04-01 14:58:47 +02002779 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2780 __free_pages(page, get_order(size));
Joerg Roedel5b28df62008-12-02 17:49:42 +01002781
2782 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002783}
2784
Joerg Roedel431b2a22008-07-11 17:14:22 +02002785/*
2786 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002787 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002788static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002789 void *virt_addr, dma_addr_t dma_addr,
2790 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002791{
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002792 struct protection_domain *domain;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002793 unsigned long flags;
2794 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002795
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002796 INC_STATS_COUNTER(cnt_free_coherent);
2797
Joerg Roedel3b839a52015-04-01 14:58:47 +02002798 page = virt_to_page(virt_addr);
2799 size = PAGE_ALIGN(size);
2800
Joerg Roedel94f6d192009-11-24 16:40:02 +01002801 domain = get_domain(dev);
2802 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002803 goto free_mem;
2804
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002805 spin_lock_irqsave(&domain->lock, flags);
2806
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002807 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002808
Joerg Roedel17b124b2011-04-06 18:01:35 +02002809 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002810
2811 spin_unlock_irqrestore(&domain->lock, flags);
2812
2813free_mem:
Joerg Roedel3b839a52015-04-01 14:58:47 +02002814 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2815 __free_pages(page, get_order(size));
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002816}
2817
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002818/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002819 * This function is called by the DMA layer to find out if we can handle a
2820 * particular device. It is part of the dma_ops.
2821 */
2822static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2823{
Joerg Roedel420aef82009-11-23 16:14:57 +01002824 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002825}
2826
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002827static struct dma_map_ops amd_iommu_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002828 .alloc = alloc_coherent,
2829 .free = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002830 .map_page = map_page,
2831 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002832 .map_sg = map_sg,
2833 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002834 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002835};
2836
Joerg Roedel3a18404c2015-05-28 18:41:45 +02002837int __init amd_iommu_init_api(void)
Joerg Roedel27c21272011-05-30 15:56:24 +02002838{
Joerg Roedel3a18404c2015-05-28 18:41:45 +02002839 return bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01002840}
2841
Joerg Roedel6631ee92008-06-26 21:28:05 +02002842int __init amd_iommu_init_dma_ops(void)
2843{
Joerg Roedel32302322015-07-28 16:58:50 +02002844 swiotlb = iommu_pass_through ? 1 : 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002845 iommu_detected = 1;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002846
Joerg Roedel52717822015-07-28 16:58:51 +02002847 /*
2848 * In case we don't initialize SWIOTLB (actually the common case
2849 * when AMD IOMMU is enabled), make sure there are global
2850 * dma_ops set as a fall-back for devices not handled by this
2851 * driver (for example non-PCI devices).
2852 */
2853 if (!swiotlb)
2854 dma_ops = &nommu_dma_ops;
2855
Joerg Roedel7f265082008-12-12 13:50:21 +01002856 amd_iommu_stats_init();
2857
Joerg Roedel62410ee2012-06-12 16:42:43 +02002858 if (amd_iommu_unmap_flush)
2859 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
2860 else
2861 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
2862
Joerg Roedel6631ee92008-06-26 21:28:05 +02002863 return 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002864}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002865
2866/*****************************************************************************
2867 *
2868 * The following functions belong to the exported interface of AMD IOMMU
2869 *
2870 * This interface allows access to lower level functions of the IOMMU
2871 * like protection domain handling and assignement of devices to domains
2872 * which is not possible with the dma_ops interface.
2873 *
2874 *****************************************************************************/
2875
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002876static void cleanup_domain(struct protection_domain *domain)
2877{
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002878 struct iommu_dev_data *entry;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002879 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002880
2881 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2882
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002883 while (!list_empty(&domain->dev_list)) {
2884 entry = list_first_entry(&domain->dev_list,
2885 struct iommu_dev_data, list);
2886 __detach_device(entry);
Joerg Roedel492667d2009-11-27 13:25:47 +01002887 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002888
2889 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2890}
2891
Joerg Roedel26508152009-08-26 16:52:40 +02002892static void protection_domain_free(struct protection_domain *domain)
2893{
2894 if (!domain)
2895 return;
2896
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002897 del_domain_from_list(domain);
2898
Joerg Roedel26508152009-08-26 16:52:40 +02002899 if (domain->id)
2900 domain_id_free(domain->id);
2901
2902 kfree(domain);
2903}
2904
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002905static int protection_domain_init(struct protection_domain *domain)
2906{
2907 spin_lock_init(&domain->lock);
2908 mutex_init(&domain->api_lock);
2909 domain->id = domain_id_alloc();
2910 if (!domain->id)
2911 return -ENOMEM;
2912 INIT_LIST_HEAD(&domain->dev_list);
2913
2914 return 0;
2915}
2916
Joerg Roedel26508152009-08-26 16:52:40 +02002917static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002918{
2919 struct protection_domain *domain;
2920
2921 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2922 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002923 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002924
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002925 if (protection_domain_init(domain))
Joerg Roedel26508152009-08-26 16:52:40 +02002926 goto out_err;
2927
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002928 add_domain_to_list(domain);
2929
Joerg Roedel26508152009-08-26 16:52:40 +02002930 return domain;
2931
2932out_err:
2933 kfree(domain);
2934
2935 return NULL;
2936}
2937
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002938static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
2939{
2940 struct protection_domain *pdomain;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002941 struct dma_ops_domain *dma_domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002942
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002943 switch (type) {
2944 case IOMMU_DOMAIN_UNMANAGED:
2945 pdomain = protection_domain_alloc();
2946 if (!pdomain)
2947 return NULL;
2948
2949 pdomain->mode = PAGE_MODE_3_LEVEL;
2950 pdomain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2951 if (!pdomain->pt_root) {
2952 protection_domain_free(pdomain);
2953 return NULL;
2954 }
2955
2956 pdomain->domain.geometry.aperture_start = 0;
2957 pdomain->domain.geometry.aperture_end = ~0ULL;
2958 pdomain->domain.geometry.force_aperture = true;
2959
2960 break;
2961 case IOMMU_DOMAIN_DMA:
2962 dma_domain = dma_ops_domain_alloc();
2963 if (!dma_domain) {
2964 pr_err("AMD-Vi: Failed to allocate\n");
2965 return NULL;
2966 }
2967 pdomain = &dma_domain->domain;
2968 break;
Joerg Roedel07f643a2015-05-28 18:41:41 +02002969 case IOMMU_DOMAIN_IDENTITY:
2970 pdomain = protection_domain_alloc();
2971 if (!pdomain)
2972 return NULL;
2973
2974 pdomain->mode = PAGE_MODE_NONE;
2975 break;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002976 default:
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002977 return NULL;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002978 }
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002979
2980 return &pdomain->domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002981}
2982
2983static void amd_iommu_domain_free(struct iommu_domain *dom)
Joerg Roedel26508152009-08-26 16:52:40 +02002984{
2985 struct protection_domain *domain;
2986
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002987 if (!dom)
Joerg Roedel98383fc2008-12-02 18:34:12 +01002988 return;
2989
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002990 domain = to_pdomain(dom);
2991
Joerg Roedel98383fc2008-12-02 18:34:12 +01002992 if (domain->dev_cnt > 0)
2993 cleanup_domain(domain);
2994
2995 BUG_ON(domain->dev_cnt != 0);
2996
Joerg Roedel132bd682011-11-17 14:18:46 +01002997 if (domain->mode != PAGE_MODE_NONE)
2998 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002999
Joerg Roedel52815b72011-11-17 17:24:28 +01003000 if (domain->flags & PD_IOMMUV2_MASK)
3001 free_gcr3_table(domain);
3002
Joerg Roedel8b408fe2010-03-08 14:20:07 +01003003 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003004}
3005
Joerg Roedel684f2882008-12-08 12:07:44 +01003006static void amd_iommu_detach_device(struct iommu_domain *dom,
3007 struct device *dev)
3008{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003009 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003010 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003011 u16 devid;
3012
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003013 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003014 return;
3015
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003016 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003017
Joerg Roedel657cbb62009-11-23 15:26:46 +01003018 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003019 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003020
3021 iommu = amd_iommu_rlookup_table[devid];
3022 if (!iommu)
3023 return;
3024
Joerg Roedel684f2882008-12-08 12:07:44 +01003025 iommu_completion_wait(iommu);
3026}
3027
Joerg Roedel01106062008-12-02 19:34:11 +01003028static int amd_iommu_attach_device(struct iommu_domain *dom,
3029 struct device *dev)
3030{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003031 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel657cbb62009-11-23 15:26:46 +01003032 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003033 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003034 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003035
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003036 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003037 return -EINVAL;
3038
Joerg Roedel657cbb62009-11-23 15:26:46 +01003039 dev_data = dev->archdata.iommu;
3040
Joerg Roedelf62dda62011-06-09 12:55:35 +02003041 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003042 if (!iommu)
3043 return -EINVAL;
3044
Joerg Roedel657cbb62009-11-23 15:26:46 +01003045 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003046 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003047
Joerg Roedel15898bb2009-11-24 15:39:42 +01003048 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003049
3050 iommu_completion_wait(iommu);
3051
Joerg Roedel15898bb2009-11-24 15:39:42 +01003052 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003053}
3054
Joerg Roedel468e2362010-01-21 16:37:36 +01003055static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003056 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003057{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003058 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003059 int prot = 0;
3060 int ret;
3061
Joerg Roedel132bd682011-11-17 14:18:46 +01003062 if (domain->mode == PAGE_MODE_NONE)
3063 return -EINVAL;
3064
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003065 if (iommu_prot & IOMMU_READ)
3066 prot |= IOMMU_PROT_IR;
3067 if (iommu_prot & IOMMU_WRITE)
3068 prot |= IOMMU_PROT_IW;
3069
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003070 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003071 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003072 mutex_unlock(&domain->api_lock);
3073
Joerg Roedel795e74f72010-05-11 17:40:57 +02003074 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003075}
3076
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003077static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3078 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003079{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003080 struct protection_domain *domain = to_pdomain(dom);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003081 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003082
Joerg Roedel132bd682011-11-17 14:18:46 +01003083 if (domain->mode == PAGE_MODE_NONE)
3084 return -EINVAL;
3085
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003086 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003087 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003088 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003089
Joerg Roedel17b124b2011-04-06 18:01:35 +02003090 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003091
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003092 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003093}
3094
Joerg Roedel645c4c82008-12-02 20:05:50 +01003095static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
Varun Sethibb5547a2013-03-29 01:23:58 +05303096 dma_addr_t iova)
Joerg Roedel645c4c82008-12-02 20:05:50 +01003097{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003098 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel3039ca12015-04-01 14:58:48 +02003099 unsigned long offset_mask, pte_pgsize;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003100 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003101
Joerg Roedel132bd682011-11-17 14:18:46 +01003102 if (domain->mode == PAGE_MODE_NONE)
3103 return iova;
3104
Joerg Roedel3039ca12015-04-01 14:58:48 +02003105 pte = fetch_pte(domain, iova, &pte_pgsize);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003106
Joerg Roedela6d41a42009-09-02 17:08:55 +02003107 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003108 return 0;
3109
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003110 offset_mask = pte_pgsize - 1;
3111 __pte = *pte & PM_ADDR_MASK;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003112
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003113 return (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003114}
3115
Joerg Roedelab636482014-09-05 10:48:21 +02003116static bool amd_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003117{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003118 switch (cap) {
3119 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedelab636482014-09-05 10:48:21 +02003120 return true;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003121 case IOMMU_CAP_INTR_REMAP:
Joerg Roedelab636482014-09-05 10:48:21 +02003122 return (irq_remapping_enabled == 1);
Will Deaconcfdeec22014-10-27 11:24:48 +00003123 case IOMMU_CAP_NOEXEC:
3124 return false;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003125 }
3126
Joerg Roedelab636482014-09-05 10:48:21 +02003127 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003128}
3129
Joerg Roedel35cf2482015-05-28 18:41:37 +02003130static void amd_iommu_get_dm_regions(struct device *dev,
3131 struct list_head *head)
3132{
3133 struct unity_map_entry *entry;
3134 u16 devid;
3135
3136 devid = get_device_id(dev);
3137
3138 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
3139 struct iommu_dm_region *region;
3140
3141 if (devid < entry->devid_start || devid > entry->devid_end)
3142 continue;
3143
3144 region = kzalloc(sizeof(*region), GFP_KERNEL);
3145 if (!region) {
3146 pr_err("Out of memory allocating dm-regions for %s\n",
3147 dev_name(dev));
3148 return;
3149 }
3150
3151 region->start = entry->address_start;
3152 region->length = entry->address_end - entry->address_start;
3153 if (entry->prot & IOMMU_PROT_IR)
3154 region->prot |= IOMMU_READ;
3155 if (entry->prot & IOMMU_PROT_IW)
3156 region->prot |= IOMMU_WRITE;
3157
3158 list_add_tail(&region->list, head);
3159 }
3160}
3161
3162static void amd_iommu_put_dm_regions(struct device *dev,
3163 struct list_head *head)
3164{
3165 struct iommu_dm_region *entry, *next;
3166
3167 list_for_each_entry_safe(entry, next, head, list)
3168 kfree(entry);
3169}
3170
Thierry Redingb22f6432014-06-27 09:03:12 +02003171static const struct iommu_ops amd_iommu_ops = {
Joerg Roedelab636482014-09-05 10:48:21 +02003172 .capable = amd_iommu_capable,
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003173 .domain_alloc = amd_iommu_domain_alloc,
3174 .domain_free = amd_iommu_domain_free,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003175 .attach_dev = amd_iommu_attach_device,
3176 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003177 .map = amd_iommu_map,
3178 .unmap = amd_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07003179 .map_sg = default_iommu_map_sg,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003180 .iova_to_phys = amd_iommu_iova_to_phys,
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02003181 .add_device = amd_iommu_add_device,
3182 .remove_device = amd_iommu_remove_device,
Joerg Roedela960fad2015-10-21 23:51:39 +02003183 .device_group = pci_device_group,
Joerg Roedel35cf2482015-05-28 18:41:37 +02003184 .get_dm_regions = amd_iommu_get_dm_regions,
3185 .put_dm_regions = amd_iommu_put_dm_regions,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003186 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003187};
3188
Joerg Roedel0feae532009-08-26 15:26:30 +02003189/*****************************************************************************
3190 *
3191 * The next functions do a basic initialization of IOMMU for pass through
3192 * mode
3193 *
3194 * In passthrough mode the IOMMU is initialized and enabled but not used for
3195 * DMA-API translation.
3196 *
3197 *****************************************************************************/
3198
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003199/* IOMMUv2 specific functions */
3200int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3201{
3202 return atomic_notifier_chain_register(&ppr_notifier, nb);
3203}
3204EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3205
3206int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3207{
3208 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3209}
3210EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003211
3212void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3213{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003214 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel132bd682011-11-17 14:18:46 +01003215 unsigned long flags;
3216
3217 spin_lock_irqsave(&domain->lock, flags);
3218
3219 /* Update data structure */
3220 domain->mode = PAGE_MODE_NONE;
3221 domain->updated = true;
3222
3223 /* Make changes visible to IOMMUs */
3224 update_domain(domain);
3225
3226 /* Page-table is not visible to IOMMU anymore, so free it */
3227 free_pagetable(domain);
3228
3229 spin_unlock_irqrestore(&domain->lock, flags);
3230}
3231EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003232
3233int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3234{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003235 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel52815b72011-11-17 17:24:28 +01003236 unsigned long flags;
3237 int levels, ret;
3238
3239 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3240 return -EINVAL;
3241
3242 /* Number of GCR3 table levels required */
3243 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3244 levels += 1;
3245
3246 if (levels > amd_iommu_max_glx_val)
3247 return -EINVAL;
3248
3249 spin_lock_irqsave(&domain->lock, flags);
3250
3251 /*
3252 * Save us all sanity checks whether devices already in the
3253 * domain support IOMMUv2. Just force that the domain has no
3254 * devices attached when it is switched into IOMMUv2 mode.
3255 */
3256 ret = -EBUSY;
3257 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3258 goto out;
3259
3260 ret = -ENOMEM;
3261 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3262 if (domain->gcr3_tbl == NULL)
3263 goto out;
3264
3265 domain->glx = levels;
3266 domain->flags |= PD_IOMMUV2_MASK;
3267 domain->updated = true;
3268
3269 update_domain(domain);
3270
3271 ret = 0;
3272
3273out:
3274 spin_unlock_irqrestore(&domain->lock, flags);
3275
3276 return ret;
3277}
3278EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003279
3280static int __flush_pasid(struct protection_domain *domain, int pasid,
3281 u64 address, bool size)
3282{
3283 struct iommu_dev_data *dev_data;
3284 struct iommu_cmd cmd;
3285 int i, ret;
3286
3287 if (!(domain->flags & PD_IOMMUV2_MASK))
3288 return -EINVAL;
3289
3290 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3291
3292 /*
3293 * IOMMU TLB needs to be flushed before Device TLB to
3294 * prevent device TLB refill from IOMMU TLB
3295 */
3296 for (i = 0; i < amd_iommus_present; ++i) {
3297 if (domain->dev_iommu[i] == 0)
3298 continue;
3299
3300 ret = iommu_queue_command(amd_iommus[i], &cmd);
3301 if (ret != 0)
3302 goto out;
3303 }
3304
3305 /* Wait until IOMMU TLB flushes are complete */
3306 domain_flush_complete(domain);
3307
3308 /* Now flush device TLBs */
3309 list_for_each_entry(dev_data, &domain->dev_list, list) {
3310 struct amd_iommu *iommu;
3311 int qdep;
3312
Joerg Roedel1c1cc452015-07-30 11:24:45 +02003313 /*
3314 There might be non-IOMMUv2 capable devices in an IOMMUv2
3315 * domain.
3316 */
3317 if (!dev_data->ats.enabled)
3318 continue;
Joerg Roedel22e266c2011-11-21 15:59:08 +01003319
3320 qdep = dev_data->ats.qdep;
3321 iommu = amd_iommu_rlookup_table[dev_data->devid];
3322
3323 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3324 qdep, address, size);
3325
3326 ret = iommu_queue_command(iommu, &cmd);
3327 if (ret != 0)
3328 goto out;
3329 }
3330
3331 /* Wait until all device TLBs are flushed */
3332 domain_flush_complete(domain);
3333
3334 ret = 0;
3335
3336out:
3337
3338 return ret;
3339}
3340
3341static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3342 u64 address)
3343{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003344 INC_STATS_COUNTER(invalidate_iotlb);
3345
Joerg Roedel22e266c2011-11-21 15:59:08 +01003346 return __flush_pasid(domain, pasid, address, false);
3347}
3348
3349int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3350 u64 address)
3351{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003352 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003353 unsigned long flags;
3354 int ret;
3355
3356 spin_lock_irqsave(&domain->lock, flags);
3357 ret = __amd_iommu_flush_page(domain, pasid, address);
3358 spin_unlock_irqrestore(&domain->lock, flags);
3359
3360 return ret;
3361}
3362EXPORT_SYMBOL(amd_iommu_flush_page);
3363
3364static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3365{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003366 INC_STATS_COUNTER(invalidate_iotlb_all);
3367
Joerg Roedel22e266c2011-11-21 15:59:08 +01003368 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3369 true);
3370}
3371
3372int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3373{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003374 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003375 unsigned long flags;
3376 int ret;
3377
3378 spin_lock_irqsave(&domain->lock, flags);
3379 ret = __amd_iommu_flush_tlb(domain, pasid);
3380 spin_unlock_irqrestore(&domain->lock, flags);
3381
3382 return ret;
3383}
3384EXPORT_SYMBOL(amd_iommu_flush_tlb);
3385
Joerg Roedelb16137b2011-11-21 16:50:23 +01003386static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3387{
3388 int index;
3389 u64 *pte;
3390
3391 while (true) {
3392
3393 index = (pasid >> (9 * level)) & 0x1ff;
3394 pte = &root[index];
3395
3396 if (level == 0)
3397 break;
3398
3399 if (!(*pte & GCR3_VALID)) {
3400 if (!alloc)
3401 return NULL;
3402
3403 root = (void *)get_zeroed_page(GFP_ATOMIC);
3404 if (root == NULL)
3405 return NULL;
3406
3407 *pte = __pa(root) | GCR3_VALID;
3408 }
3409
3410 root = __va(*pte & PAGE_MASK);
3411
3412 level -= 1;
3413 }
3414
3415 return pte;
3416}
3417
3418static int __set_gcr3(struct protection_domain *domain, int pasid,
3419 unsigned long cr3)
3420{
3421 u64 *pte;
3422
3423 if (domain->mode != PAGE_MODE_NONE)
3424 return -EINVAL;
3425
3426 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3427 if (pte == NULL)
3428 return -ENOMEM;
3429
3430 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3431
3432 return __amd_iommu_flush_tlb(domain, pasid);
3433}
3434
3435static int __clear_gcr3(struct protection_domain *domain, int pasid)
3436{
3437 u64 *pte;
3438
3439 if (domain->mode != PAGE_MODE_NONE)
3440 return -EINVAL;
3441
3442 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3443 if (pte == NULL)
3444 return 0;
3445
3446 *pte = 0;
3447
3448 return __amd_iommu_flush_tlb(domain, pasid);
3449}
3450
3451int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3452 unsigned long cr3)
3453{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003454 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003455 unsigned long flags;
3456 int ret;
3457
3458 spin_lock_irqsave(&domain->lock, flags);
3459 ret = __set_gcr3(domain, pasid, cr3);
3460 spin_unlock_irqrestore(&domain->lock, flags);
3461
3462 return ret;
3463}
3464EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3465
3466int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3467{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003468 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003469 unsigned long flags;
3470 int ret;
3471
3472 spin_lock_irqsave(&domain->lock, flags);
3473 ret = __clear_gcr3(domain, pasid);
3474 spin_unlock_irqrestore(&domain->lock, flags);
3475
3476 return ret;
3477}
3478EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003479
3480int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3481 int status, int tag)
3482{
3483 struct iommu_dev_data *dev_data;
3484 struct amd_iommu *iommu;
3485 struct iommu_cmd cmd;
3486
Joerg Roedel399be2f2011-12-01 16:53:47 +01003487 INC_STATS_COUNTER(complete_ppr);
3488
Joerg Roedelc99afa22011-11-21 18:19:25 +01003489 dev_data = get_dev_data(&pdev->dev);
3490 iommu = amd_iommu_rlookup_table[dev_data->devid];
3491
3492 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3493 tag, dev_data->pri_tlp);
3494
3495 return iommu_queue_command(iommu, &cmd);
3496}
3497EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003498
3499struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3500{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003501 struct protection_domain *pdomain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003502
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003503 pdomain = get_domain(&pdev->dev);
3504 if (IS_ERR(pdomain))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003505 return NULL;
3506
3507 /* Only return IOMMUv2 domains */
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003508 if (!(pdomain->flags & PD_IOMMUV2_MASK))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003509 return NULL;
3510
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003511 return &pdomain->domain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003512}
3513EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003514
3515void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3516{
3517 struct iommu_dev_data *dev_data;
3518
3519 if (!amd_iommu_v2_supported())
3520 return;
3521
3522 dev_data = get_dev_data(&pdev->dev);
3523 dev_data->errata |= (1 << erratum);
3524}
3525EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003526
3527int amd_iommu_device_info(struct pci_dev *pdev,
3528 struct amd_iommu_device_info *info)
3529{
3530 int max_pasids;
3531 int pos;
3532
3533 if (pdev == NULL || info == NULL)
3534 return -EINVAL;
3535
3536 if (!amd_iommu_v2_supported())
3537 return -EINVAL;
3538
3539 memset(info, 0, sizeof(*info));
3540
3541 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3542 if (pos)
3543 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3544
3545 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3546 if (pos)
3547 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3548
3549 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3550 if (pos) {
3551 int features;
3552
3553 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3554 max_pasids = min(max_pasids, (1 << 20));
3555
3556 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3557 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3558
3559 features = pci_pasid_features(pdev);
3560 if (features & PCI_PASID_CAP_EXEC)
3561 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3562 if (features & PCI_PASID_CAP_PRIV)
3563 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3564 }
3565
3566 return 0;
3567}
3568EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003569
3570#ifdef CONFIG_IRQ_REMAP
3571
3572/*****************************************************************************
3573 *
3574 * Interrupt Remapping Implementation
3575 *
3576 *****************************************************************************/
3577
3578union irte {
3579 u32 val;
3580 struct {
3581 u32 valid : 1,
3582 no_fault : 1,
3583 int_type : 3,
3584 rq_eoi : 1,
3585 dm : 1,
3586 rsvd_1 : 1,
3587 destination : 8,
3588 vector : 8,
3589 rsvd_2 : 8;
3590 } fields;
3591};
3592
Jiang Liu9c724962015-04-14 10:29:52 +08003593struct irq_2_irte {
3594 u16 devid; /* Device ID for IRTE table */
3595 u16 index; /* Index into IRTE table*/
3596};
3597
Jiang Liu7c71d302015-04-13 14:11:33 +08003598struct amd_ir_data {
3599 struct irq_2_irte irq_2_irte;
3600 union irte irte_entry;
3601 union {
3602 struct msi_msg msi_entry;
3603 };
3604};
3605
3606static struct irq_chip amd_ir_chip;
3607
Joerg Roedel2b324502012-06-21 16:29:10 +02003608#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3609#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3610#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3611#define DTE_IRQ_REMAP_ENABLE 1ULL
3612
3613static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3614{
3615 u64 dte;
3616
3617 dte = amd_iommu_dev_table[devid].data[2];
3618 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3619 dte |= virt_to_phys(table->table);
3620 dte |= DTE_IRQ_REMAP_INTCTL;
3621 dte |= DTE_IRQ_TABLE_LEN;
3622 dte |= DTE_IRQ_REMAP_ENABLE;
3623
3624 amd_iommu_dev_table[devid].data[2] = dte;
3625}
3626
3627#define IRTE_ALLOCATED (~1U)
3628
3629static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3630{
3631 struct irq_remap_table *table = NULL;
3632 struct amd_iommu *iommu;
3633 unsigned long flags;
3634 u16 alias;
3635
3636 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3637
3638 iommu = amd_iommu_rlookup_table[devid];
3639 if (!iommu)
3640 goto out_unlock;
3641
3642 table = irq_lookup_table[devid];
3643 if (table)
3644 goto out;
3645
3646 alias = amd_iommu_alias_table[devid];
3647 table = irq_lookup_table[alias];
3648 if (table) {
3649 irq_lookup_table[devid] = table;
3650 set_dte_irq_entry(devid, table);
3651 iommu_flush_dte(iommu, devid);
3652 goto out;
3653 }
3654
3655 /* Nothing there yet, allocate new irq remapping table */
3656 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3657 if (!table)
3658 goto out;
3659
Joerg Roedel197887f2013-04-09 21:14:08 +02003660 /* Initialize table spin-lock */
3661 spin_lock_init(&table->lock);
3662
Joerg Roedel2b324502012-06-21 16:29:10 +02003663 if (ioapic)
3664 /* Keep the first 32 indexes free for IOAPIC interrupts */
3665 table->min_index = 32;
3666
3667 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3668 if (!table->table) {
3669 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003670 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003671 goto out;
3672 }
3673
3674 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3675
3676 if (ioapic) {
3677 int i;
3678
3679 for (i = 0; i < 32; ++i)
3680 table->table[i] = IRTE_ALLOCATED;
3681 }
3682
3683 irq_lookup_table[devid] = table;
3684 set_dte_irq_entry(devid, table);
3685 iommu_flush_dte(iommu, devid);
3686 if (devid != alias) {
3687 irq_lookup_table[alias] = table;
Alex Williamsone028a9e2014-04-22 10:08:40 -06003688 set_dte_irq_entry(alias, table);
Joerg Roedel2b324502012-06-21 16:29:10 +02003689 iommu_flush_dte(iommu, alias);
3690 }
3691
3692out:
3693 iommu_completion_wait(iommu);
3694
3695out_unlock:
3696 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3697
3698 return table;
3699}
3700
Jiang Liu3c3d4f92015-04-13 14:11:38 +08003701static int alloc_irq_index(u16 devid, int count)
Joerg Roedel2b324502012-06-21 16:29:10 +02003702{
3703 struct irq_remap_table *table;
3704 unsigned long flags;
3705 int index, c;
3706
3707 table = get_irq_table(devid, false);
3708 if (!table)
3709 return -ENODEV;
3710
3711 spin_lock_irqsave(&table->lock, flags);
3712
3713 /* Scan table for free entries */
3714 for (c = 0, index = table->min_index;
3715 index < MAX_IRQS_PER_TABLE;
3716 ++index) {
3717 if (table->table[index] == 0)
3718 c += 1;
3719 else
3720 c = 0;
3721
3722 if (c == count) {
Joerg Roedel2b324502012-06-21 16:29:10 +02003723 for (; c != 0; --c)
3724 table->table[index - c + 1] = IRTE_ALLOCATED;
3725
3726 index -= count - 1;
Joerg Roedel2b324502012-06-21 16:29:10 +02003727 goto out;
3728 }
3729 }
3730
3731 index = -ENOSPC;
3732
3733out:
3734 spin_unlock_irqrestore(&table->lock, flags);
3735
3736 return index;
3737}
3738
Joerg Roedel2b324502012-06-21 16:29:10 +02003739static int modify_irte(u16 devid, int index, union irte irte)
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 -EINVAL;
3748
3749 table = get_irq_table(devid, false);
3750 if (!table)
3751 return -ENOMEM;
3752
3753 spin_lock_irqsave(&table->lock, flags);
3754 table->table[index] = irte.val;
3755 spin_unlock_irqrestore(&table->lock, flags);
3756
3757 iommu_flush_irt(iommu, devid);
3758 iommu_completion_wait(iommu);
3759
3760 return 0;
3761}
3762
3763static void free_irte(u16 devid, int index)
3764{
3765 struct irq_remap_table *table;
3766 struct amd_iommu *iommu;
3767 unsigned long flags;
3768
3769 iommu = amd_iommu_rlookup_table[devid];
3770 if (iommu == NULL)
3771 return;
3772
3773 table = get_irq_table(devid, false);
3774 if (!table)
3775 return;
3776
3777 spin_lock_irqsave(&table->lock, flags);
3778 table->table[index] = 0;
3779 spin_unlock_irqrestore(&table->lock, flags);
3780
3781 iommu_flush_irt(iommu, devid);
3782 iommu_completion_wait(iommu);
3783}
3784
Jiang Liu7c71d302015-04-13 14:11:33 +08003785static int get_devid(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02003786{
Jiang Liu7c71d302015-04-13 14:11:33 +08003787 int devid = -1;
Joerg Roedel5527de72012-06-26 11:17:32 +02003788
Jiang Liu7c71d302015-04-13 14:11:33 +08003789 switch (info->type) {
3790 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3791 devid = get_ioapic_devid(info->ioapic_id);
3792 break;
3793 case X86_IRQ_ALLOC_TYPE_HPET:
3794 devid = get_hpet_devid(info->hpet_id);
3795 break;
3796 case X86_IRQ_ALLOC_TYPE_MSI:
3797 case X86_IRQ_ALLOC_TYPE_MSIX:
3798 devid = get_device_id(&info->msi_dev->dev);
3799 break;
3800 default:
3801 BUG_ON(1);
3802 break;
Joerg Roedel5527de72012-06-26 11:17:32 +02003803 }
3804
Jiang Liu7c71d302015-04-13 14:11:33 +08003805 return devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02003806}
3807
Jiang Liu7c71d302015-04-13 14:11:33 +08003808static struct irq_domain *get_ir_irq_domain(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02003809{
Jiang Liu7c71d302015-04-13 14:11:33 +08003810 struct amd_iommu *iommu;
3811 int devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02003812
Jiang Liu7c71d302015-04-13 14:11:33 +08003813 if (!info)
3814 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02003815
Jiang Liu7c71d302015-04-13 14:11:33 +08003816 devid = get_devid(info);
3817 if (devid >= 0) {
3818 iommu = amd_iommu_rlookup_table[devid];
3819 if (iommu)
3820 return iommu->ir_domain;
3821 }
Joerg Roedel5527de72012-06-26 11:17:32 +02003822
Jiang Liu7c71d302015-04-13 14:11:33 +08003823 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02003824}
3825
Jiang Liu7c71d302015-04-13 14:11:33 +08003826static struct irq_domain *get_irq_domain(struct irq_alloc_info *info)
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003827{
Jiang Liu7c71d302015-04-13 14:11:33 +08003828 struct amd_iommu *iommu;
3829 int devid;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003830
Jiang Liu7c71d302015-04-13 14:11:33 +08003831 if (!info)
3832 return NULL;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003833
Jiang Liu7c71d302015-04-13 14:11:33 +08003834 switch (info->type) {
3835 case X86_IRQ_ALLOC_TYPE_MSI:
3836 case X86_IRQ_ALLOC_TYPE_MSIX:
3837 devid = get_device_id(&info->msi_dev->dev);
3838 if (devid >= 0) {
3839 iommu = amd_iommu_rlookup_table[devid];
3840 if (iommu)
3841 return iommu->msi_domain;
3842 }
3843 break;
3844 default:
3845 break;
3846 }
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003847
Jiang Liu7c71d302015-04-13 14:11:33 +08003848 return NULL;
Joerg Roedeld9761952012-06-26 16:00:08 +02003849}
3850
Joerg Roedel6b474b82012-06-26 16:46:04 +02003851struct irq_remap_ops amd_iommu_irq_ops = {
Joerg Roedel6b474b82012-06-26 16:46:04 +02003852 .prepare = amd_iommu_prepare,
3853 .enable = amd_iommu_enable,
3854 .disable = amd_iommu_disable,
3855 .reenable = amd_iommu_reenable,
3856 .enable_faulting = amd_iommu_enable_faulting,
Jiang Liu7c71d302015-04-13 14:11:33 +08003857 .get_ir_irq_domain = get_ir_irq_domain,
3858 .get_irq_domain = get_irq_domain,
Joerg Roedel6b474b82012-06-26 16:46:04 +02003859};
Jiang Liu7c71d302015-04-13 14:11:33 +08003860
3861static void irq_remapping_prepare_irte(struct amd_ir_data *data,
3862 struct irq_cfg *irq_cfg,
3863 struct irq_alloc_info *info,
3864 int devid, int index, int sub_handle)
3865{
3866 struct irq_2_irte *irte_info = &data->irq_2_irte;
3867 struct msi_msg *msg = &data->msi_entry;
3868 union irte *irte = &data->irte_entry;
3869 struct IO_APIC_route_entry *entry;
3870
Jiang Liu7c71d302015-04-13 14:11:33 +08003871 data->irq_2_irte.devid = devid;
3872 data->irq_2_irte.index = index + sub_handle;
3873
3874 /* Setup IRTE for IOMMU */
3875 irte->val = 0;
3876 irte->fields.vector = irq_cfg->vector;
3877 irte->fields.int_type = apic->irq_delivery_mode;
3878 irte->fields.destination = irq_cfg->dest_apicid;
3879 irte->fields.dm = apic->irq_dest_mode;
3880 irte->fields.valid = 1;
3881
3882 switch (info->type) {
3883 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3884 /* Setup IOAPIC entry */
3885 entry = info->ioapic_entry;
3886 info->ioapic_entry = NULL;
3887 memset(entry, 0, sizeof(*entry));
3888 entry->vector = index;
3889 entry->mask = 0;
3890 entry->trigger = info->ioapic_trigger;
3891 entry->polarity = info->ioapic_polarity;
3892 /* Mask level triggered irqs. */
3893 if (info->ioapic_trigger)
3894 entry->mask = 1;
3895 break;
3896
3897 case X86_IRQ_ALLOC_TYPE_HPET:
3898 case X86_IRQ_ALLOC_TYPE_MSI:
3899 case X86_IRQ_ALLOC_TYPE_MSIX:
3900 msg->address_hi = MSI_ADDR_BASE_HI;
3901 msg->address_lo = MSI_ADDR_BASE_LO;
3902 msg->data = irte_info->index;
3903 break;
3904
3905 default:
3906 BUG_ON(1);
3907 break;
3908 }
3909}
3910
3911static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
3912 unsigned int nr_irqs, void *arg)
3913{
3914 struct irq_alloc_info *info = arg;
3915 struct irq_data *irq_data;
3916 struct amd_ir_data *data;
3917 struct irq_cfg *cfg;
3918 int i, ret, devid;
3919 int index = -1;
3920
3921 if (!info)
3922 return -EINVAL;
3923 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI &&
3924 info->type != X86_IRQ_ALLOC_TYPE_MSIX)
3925 return -EINVAL;
3926
3927 /*
3928 * With IRQ remapping enabled, don't need contiguous CPU vectors
3929 * to support multiple MSI interrupts.
3930 */
3931 if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
3932 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
3933
3934 devid = get_devid(info);
3935 if (devid < 0)
3936 return -EINVAL;
3937
3938 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
3939 if (ret < 0)
3940 return ret;
3941
Jiang Liu7c71d302015-04-13 14:11:33 +08003942 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
3943 if (get_irq_table(devid, true))
3944 index = info->ioapic_pin;
3945 else
3946 ret = -ENOMEM;
3947 } else {
Jiang Liu3c3d4f92015-04-13 14:11:38 +08003948 index = alloc_irq_index(devid, nr_irqs);
Jiang Liu7c71d302015-04-13 14:11:33 +08003949 }
3950 if (index < 0) {
3951 pr_warn("Failed to allocate IRTE\n");
Jiang Liu7c71d302015-04-13 14:11:33 +08003952 goto out_free_parent;
3953 }
3954
3955 for (i = 0; i < nr_irqs; i++) {
3956 irq_data = irq_domain_get_irq_data(domain, virq + i);
3957 cfg = irqd_cfg(irq_data);
3958 if (!irq_data || !cfg) {
3959 ret = -EINVAL;
3960 goto out_free_data;
3961 }
3962
Joerg Roedela130e692015-08-13 11:07:25 +02003963 ret = -ENOMEM;
3964 data = kzalloc(sizeof(*data), GFP_KERNEL);
3965 if (!data)
3966 goto out_free_data;
3967
Jiang Liu7c71d302015-04-13 14:11:33 +08003968 irq_data->hwirq = (devid << 16) + i;
3969 irq_data->chip_data = data;
3970 irq_data->chip = &amd_ir_chip;
3971 irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
3972 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
3973 }
Joerg Roedela130e692015-08-13 11:07:25 +02003974
Jiang Liu7c71d302015-04-13 14:11:33 +08003975 return 0;
3976
3977out_free_data:
3978 for (i--; i >= 0; i--) {
3979 irq_data = irq_domain_get_irq_data(domain, virq + i);
3980 if (irq_data)
3981 kfree(irq_data->chip_data);
3982 }
3983 for (i = 0; i < nr_irqs; i++)
3984 free_irte(devid, index + i);
3985out_free_parent:
3986 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3987 return ret;
3988}
3989
3990static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
3991 unsigned int nr_irqs)
3992{
3993 struct irq_2_irte *irte_info;
3994 struct irq_data *irq_data;
3995 struct amd_ir_data *data;
3996 int i;
3997
3998 for (i = 0; i < nr_irqs; i++) {
3999 irq_data = irq_domain_get_irq_data(domain, virq + i);
4000 if (irq_data && irq_data->chip_data) {
4001 data = irq_data->chip_data;
4002 irte_info = &data->irq_2_irte;
4003 free_irte(irte_info->devid, irte_info->index);
4004 kfree(data);
4005 }
4006 }
4007 irq_domain_free_irqs_common(domain, virq, nr_irqs);
4008}
4009
4010static void irq_remapping_activate(struct irq_domain *domain,
4011 struct irq_data *irq_data)
4012{
4013 struct amd_ir_data *data = irq_data->chip_data;
4014 struct irq_2_irte *irte_info = &data->irq_2_irte;
4015
4016 modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
4017}
4018
4019static void irq_remapping_deactivate(struct irq_domain *domain,
4020 struct irq_data *irq_data)
4021{
4022 struct amd_ir_data *data = irq_data->chip_data;
4023 struct irq_2_irte *irte_info = &data->irq_2_irte;
4024 union irte entry;
4025
4026 entry.val = 0;
4027 modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
4028}
4029
4030static struct irq_domain_ops amd_ir_domain_ops = {
4031 .alloc = irq_remapping_alloc,
4032 .free = irq_remapping_free,
4033 .activate = irq_remapping_activate,
4034 .deactivate = irq_remapping_deactivate,
4035};
4036
4037static int amd_ir_set_affinity(struct irq_data *data,
4038 const struct cpumask *mask, bool force)
4039{
4040 struct amd_ir_data *ir_data = data->chip_data;
4041 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
4042 struct irq_cfg *cfg = irqd_cfg(data);
4043 struct irq_data *parent = data->parent_data;
4044 int ret;
4045
4046 ret = parent->chip->irq_set_affinity(parent, mask, force);
4047 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
4048 return ret;
4049
4050 /*
4051 * Atomically updates the IRTE with the new destination, vector
4052 * and flushes the interrupt entry cache.
4053 */
4054 ir_data->irte_entry.fields.vector = cfg->vector;
4055 ir_data->irte_entry.fields.destination = cfg->dest_apicid;
4056 modify_irte(irte_info->devid, irte_info->index, ir_data->irte_entry);
4057
4058 /*
4059 * After this point, all the interrupts will start arriving
4060 * at the new destination. So, time to cleanup the previous
4061 * vector allocation.
4062 */
Jiang Liuc6c20022015-04-14 10:30:02 +08004063 send_cleanup_vector(cfg);
Jiang Liu7c71d302015-04-13 14:11:33 +08004064
4065 return IRQ_SET_MASK_OK_DONE;
4066}
4067
4068static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
4069{
4070 struct amd_ir_data *ir_data = irq_data->chip_data;
4071
4072 *msg = ir_data->msi_entry;
4073}
4074
4075static struct irq_chip amd_ir_chip = {
4076 .irq_ack = ir_ack_apic_edge,
4077 .irq_set_affinity = amd_ir_set_affinity,
4078 .irq_compose_msi_msg = ir_compose_msi_msg,
4079};
4080
4081int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
4082{
4083 iommu->ir_domain = irq_domain_add_tree(NULL, &amd_ir_domain_ops, iommu);
4084 if (!iommu->ir_domain)
4085 return -ENOMEM;
4086
4087 iommu->ir_domain->parent = arch_get_ir_parent_domain();
4088 iommu->msi_domain = arch_create_msi_irq_domain(iommu->ir_domain);
4089
4090 return 0;
4091}
Joerg Roedel2b324502012-06-21 16:29:10 +02004092#endif